@@ -49,12 +49,27 @@ func explainLiteral(sb *strings.Builder, n *ast.Literal, indent string, depth in
4949 }
5050 hasComplexExpr := false
5151 for _ , e := range exprs {
52- lit , isLit := e .(* ast.Literal )
53- // Non-literals or tuple/array literals count as complex
54- if ! isLit || (isLit && (lit .Type == ast .LiteralTuple || lit .Type == ast .LiteralArray )) {
55- hasComplexExpr = true
56- break
52+ // Simple literals (numbers, strings, etc.) are OK
53+ if lit , isLit := e .(* ast.Literal ); isLit {
54+ // Nested tuples/arrays are complex
55+ if lit .Type == ast .LiteralTuple || lit .Type == ast .LiteralArray {
56+ hasComplexExpr = true
57+ break
58+ }
59+ // Other literals are simple
60+ continue
61+ }
62+ // Unary negation of numeric literals is also simple
63+ if unary , isUnary := e .(* ast.UnaryExpr ); isUnary && unary .Op == "-" {
64+ if lit , isLit := unary .Operand .(* ast.Literal ); isLit {
65+ if lit .Type == ast .LiteralInteger || lit .Type == ast .LiteralFloat {
66+ continue
67+ }
68+ }
5769 }
70+ // Everything else is complex
71+ hasComplexExpr = true
72+ break
5873 }
5974 if hasComplexExpr {
6075 // Render as Function tuple instead of Literal
@@ -329,6 +344,33 @@ func explainAliasedExpr(sb *strings.Builder, n *ast.AliasedExpr, depth int) {
329344 }
330345 }
331346 }
347+ // Check if this is an array containing specific expressions that need Function array format
348+ if e .Type == ast .LiteralArray {
349+ if exprs , ok := e .Value .([]ast.Expression ); ok {
350+ needsFunctionFormat := false
351+ for _ , expr := range exprs {
352+ // Check for tuples - use Function array
353+ if lit , ok := expr .(* ast.Literal ); ok && lit .Type == ast .LiteralTuple {
354+ needsFunctionFormat = true
355+ break
356+ }
357+ // Check for identifiers - use Function array
358+ if _ , ok := expr .(* ast.Identifier ); ok {
359+ needsFunctionFormat = true
360+ break
361+ }
362+ }
363+ if needsFunctionFormat {
364+ // Render as Function array with alias
365+ fmt .Fprintf (sb , "%sFunction array (alias %s) (children %d)\n " , indent , n .Alias , 1 )
366+ fmt .Fprintf (sb , "%s ExpressionList (children %d)\n " , indent , len (exprs ))
367+ for _ , expr := range exprs {
368+ Node (sb , expr , depth + 2 )
369+ }
370+ return
371+ }
372+ }
373+ }
332374 fmt .Fprintf (sb , "%sLiteral %s (alias %s)\n " , indent , FormatLiteral (e ), n .Alias )
333375 case * ast.BinaryExpr :
334376 // Binary expressions become functions with alias
@@ -450,6 +492,8 @@ func explainWithElement(sb *strings.Builder, n *ast.WithElement, indent string,
450492 fmt .Fprintf (sb , "%sSubquery (children %d)\n " , indent , 1 )
451493 }
452494 Node (sb , e .Query , depth + 1 )
495+ case * ast.CastExpr :
496+ explainCastExprWithAlias (sb , e , n .Name , indent , depth )
453497 default :
454498 // For other types, just output the expression (alias may be lost)
455499 Node (sb , n .Query , depth )
0 commit comments