@@ -219,6 +219,23 @@ func (f *postgreSQLFormatter) formatRangeSubselect(subselect *pg_query.RangeSubs
219219 }
220220}
221221
222+ // formatExpressionWithTextCast formats an expression and adds ::text cast to string literals
223+ // This is used in CASE THEN/ELSE clauses to match PostgreSQL's canonical representation
224+ func (f * postgreSQLFormatter ) formatExpressionWithTextCast (expr * pg_query.Node ) {
225+ // Check if this is a string constant that needs a ::text cast
226+ if aConst := expr .GetAConst (); aConst != nil {
227+ if aConst .GetSval () != nil {
228+ // Format the string literal with ::text cast
229+ f .formatAConst (aConst )
230+ f .buffer .WriteString ("::text" )
231+ return
232+ }
233+ }
234+
235+ // For other expressions, format normally
236+ f .formatExpression (expr )
237+ }
238+
222239// formatExpression formats a general expression
223240//
224241// NOTE: Two important expression types for array operations:
@@ -558,14 +575,15 @@ func (f *postgreSQLFormatter) formatCaseExpr(caseExpr *pg_query.CaseExpr) {
558575 f .buffer .WriteString (" WHEN " )
559576 f .formatExpression (when .Expr )
560577 f .buffer .WriteString (" THEN " )
561- f .formatExpressionStripCast (when .Result )
578+ // Add ::text cast to string literals in THEN clause
579+ f .formatExpressionWithTextCast (when .Result )
562580 }
563581 }
564582
565- // Format ELSE clause, stripping unnecessary type casts from constants/NULL
583+ // Format ELSE clause - add ::text cast to string literals
566584 if caseExpr .Defresult != nil {
567585 f .buffer .WriteString (" ELSE " )
568- f .formatExpressionStripCast (caseExpr .Defresult )
586+ f .formatExpressionWithTextCast (caseExpr .Defresult )
569587 }
570588
571589 f .buffer .WriteString (" END" )
@@ -647,26 +665,6 @@ func (f *postgreSQLFormatter) formatNullTest(nullTest *pg_query.NullTest) {
647665 }
648666}
649667
650- // formatExpressionStripCast formats an expression, stripping unnecessary type casts from constants and NULL
651- func (f * postgreSQLFormatter ) formatExpressionStripCast (expr * pg_query.Node ) {
652- // If this is a TypeCast of a constant or NULL, format just the value without the cast
653- if typeCast := expr .GetTypeCast (); typeCast != nil {
654- if typeCast .Arg != nil {
655- if aConst := typeCast .Arg .GetAConst (); aConst != nil {
656- // This is a typed constant, format just the constant value
657- f .formatAConst (aConst )
658- return
659- }
660- // For non-constant args, recursively strip casts
661- f .formatExpressionStripCast (typeCast .Arg )
662- return
663- }
664- }
665-
666- // Otherwise, format normally
667- f .formatExpression (expr )
668- }
669-
670668// formatAArrayExpr formats array expressions (ARRAY[...])
671669func (f * postgreSQLFormatter ) formatAArrayExpr (arrayExpr * pg_query.A_ArrayExpr ) {
672670 f .buffer .WriteString ("ARRAY[" )
@@ -682,17 +680,17 @@ func (f *postgreSQLFormatter) formatAArrayExpr(arrayExpr *pg_query.A_ArrayExpr)
682680// formatArrayAsIN is a helper to format "column IN (values)" syntax
683681// Used by both formatAExpr and formatScalarArrayOpExpr to convert "= ANY(ARRAY[...])" to "IN (...)"
684682func (f * postgreSQLFormatter ) formatArrayAsIN (leftExpr * pg_query.Node , arrayElements []* pg_query.Node ) {
685- // Format left side (the column/expression)
686- f .formatExpressionStripCast (leftExpr )
683+ // Format left side (the column/expression) - preserves type casts for canonical representation
684+ f .formatExpression (leftExpr )
687685
688686 f .buffer .WriteString (" IN (" )
689687
690- // Format array elements as comma-separated list, stripping unnecessary type casts
688+ // Format array elements as comma-separated list - preserves type casts for canonical representation
691689 for i , elem := range arrayElements {
692690 if i > 0 {
693691 f .buffer .WriteString (", " )
694692 }
695- f .formatExpressionStripCast (elem )
693+ f .formatExpression (elem )
696694 }
697695
698696 f .buffer .WriteString (")" )
0 commit comments