-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpressions.go
More file actions
1016 lines (964 loc) · 33.8 KB
/
expressions.go
File metadata and controls
1016 lines (964 loc) · 33.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package explain
import (
"fmt"
"strings"
"github.com/sqlc-dev/doubleclick/ast"
)
// escapeAlias escapes backslashes in alias names for EXPLAIN output
func escapeAlias(alias string) string {
return strings.ReplaceAll(alias, "\\", "\\\\")
}
func explainIdentifier(sb *strings.Builder, n *ast.Identifier, indent string) {
name := formatIdentifierName(n)
if n.Alias != "" {
fmt.Fprintf(sb, "%sIdentifier %s (alias %s)\n", indent, name, escapeAlias(n.Alias))
} else {
fmt.Fprintf(sb, "%sIdentifier %s\n", indent, name)
}
}
// formatIdentifierName formats an identifier name, handling JSON path notation
func formatIdentifierName(n *ast.Identifier) string {
if len(n.Parts) == 0 {
return ""
}
if len(n.Parts) == 1 {
return n.Parts[0]
}
result := n.Parts[0]
for _, p := range n.Parts[1:] {
// JSON path notation: ^fieldname should be formatted as ^`fieldname`
if strings.HasPrefix(p, "^") {
result += ".^`" + p[1:] + "`"
} else {
result += "." + p
}
}
return result
}
func explainLiteral(sb *strings.Builder, n *ast.Literal, indent string, depth int) {
// Check if this is a tuple - either with expressions or empty
if n.Type == ast.LiteralTuple {
if exprs, ok := n.Value.([]ast.Expression); ok {
// Check if empty tuple or has complex expressions
if len(exprs) == 0 {
// Empty tuple renders as Function tuple with empty ExpressionList
fmt.Fprintf(sb, "%sFunction tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
return
}
// Single-element tuples (from trailing comma syntax like (1,)) always render as Function tuple
if len(exprs) == 1 {
fmt.Fprintf(sb, "%sFunction tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(exprs))
for _, e := range exprs {
Node(sb, e, depth+2)
}
return
}
hasComplexExpr := false
for _, e := range exprs {
// Simple literals (numbers, strings, etc.) are OK
if lit, isLit := e.(*ast.Literal); isLit {
// Nested tuples that contain only primitive literals are OK
if lit.Type == ast.LiteralTuple {
if !containsOnlyPrimitiveLiteralsWithUnary(lit) {
hasComplexExpr = true
break
}
continue
}
// Arrays are always complex in tuple context
if lit.Type == ast.LiteralArray {
hasComplexExpr = true
break
}
// Other literals are simple
continue
}
// Unary negation of numeric literals is also simple
if unary, isUnary := e.(*ast.UnaryExpr); isUnary && unary.Op == "-" {
if lit, isLit := unary.Operand.(*ast.Literal); isLit {
if lit.Type == ast.LiteralInteger || lit.Type == ast.LiteralFloat {
continue
}
}
}
// Everything else is complex
hasComplexExpr = true
break
}
if hasComplexExpr {
// Render as Function tuple instead of Literal
fmt.Fprintf(sb, "%sFunction tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(exprs))
for _, e := range exprs {
Node(sb, e, depth+2)
}
return
}
} else if n.Value == nil {
// nil value means empty tuple
fmt.Fprintf(sb, "%sFunction tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
return
}
}
// Check if this is an array with complex expressions or empty that should be rendered as Function array
if n.Type == ast.LiteralArray {
if exprs, ok := n.Value.([]ast.Expression); ok {
// Empty array renders as Function array with empty ExpressionList
if len(exprs) == 0 {
fmt.Fprintf(sb, "%sFunction array (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
return
}
// Check if we should render as Function array
// This happens when:
// 1. Contains non-literal, non-negation expressions OR
// 2. Contains tuples OR
// 3. Contains nested arrays with non-literal expressions OR
// 4. Contains nested arrays that are empty or contain tuples/non-literals
shouldUseFunctionArray := false
hasNestedArrays := false
nestedArraysNeedFunctionFormat := false
for _, e := range exprs {
if lit, ok := e.(*ast.Literal); ok {
if lit.Type == ast.LiteralArray {
hasNestedArrays = true
// Check if inner array needs Function array format:
// - Contains non-literal expressions OR
// - Contains tuples OR
// - Is empty OR
// - Contains empty arrays
if innerExprs, ok := lit.Value.([]ast.Expression); ok {
if containsNonLiteralExpressions(innerExprs) ||
len(innerExprs) == 0 ||
containsTuples(innerExprs) ||
containsEmptyArrays(innerExprs) {
nestedArraysNeedFunctionFormat = true
}
}
} else if lit.Type == ast.LiteralTuple {
// Tuples are complex
shouldUseFunctionArray = true
}
} else if !isSimpleLiteralOrNegation(e) {
shouldUseFunctionArray = true
}
}
// Use Function array when:
// - nested arrays that need Function format (contain non-literals, tuples, or empty arrays at any depth)
// Note: nested arrays that are ALL single-element should still be Literal format
if hasNestedArrays && nestedArraysNeedFunctionFormat {
shouldUseFunctionArray = true
}
// Also check for empty arrays at any depth within nested arrays
if hasNestedArrays && containsEmptyArraysRecursive(exprs) {
shouldUseFunctionArray = true
}
// Also check for tuples at any depth within nested arrays
if hasNestedArrays && containsTuplesRecursive(exprs) {
shouldUseFunctionArray = true
}
if shouldUseFunctionArray {
// Render as Function array instead of Literal
fmt.Fprintf(sb, "%sFunction array (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(exprs))
for _, e := range exprs {
Node(sb, e, depth+2)
}
return
}
} else if n.Value == nil {
// nil value means empty array
fmt.Fprintf(sb, "%sFunction array (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
return
}
}
fmt.Fprintf(sb, "%sLiteral %s\n", indent, FormatLiteral(n))
}
// isSimpleLiteralOrNegation checks if an expression is a simple literal
// or a unary negation of a numeric literal (for array elements)
func isSimpleLiteralOrNegation(e ast.Expression) bool {
// Direct literal check
if lit, ok := e.(*ast.Literal); ok {
// Nested arrays/tuples are complex
return lit.Type != ast.LiteralTuple && lit.Type != ast.LiteralArray
}
// Unary minus of a literal integer/float is also simple (negative number)
if unary, ok := e.(*ast.UnaryExpr); ok && unary.Op == "-" {
if lit, ok := unary.Operand.(*ast.Literal); ok {
return lit.Type == ast.LiteralInteger || lit.Type == ast.LiteralFloat
}
}
return false
}
// containsOnlyArraysOrTuples checks if a slice of expressions contains
// only array or tuple literals (including empty arrays).
// Returns true if the slice is empty or contains only arrays/tuples.
func containsOnlyArraysOrTuples(exprs []ast.Expression) bool {
if len(exprs) == 0 {
return true // empty is considered "only arrays"
}
for _, e := range exprs {
if lit, ok := e.(*ast.Literal); ok {
if lit.Type != ast.LiteralArray && lit.Type != ast.LiteralTuple {
return false
}
} else {
return false
}
}
return true
}
// containsNonLiteralExpressions checks if a slice of expressions contains
// any non-literal expressions (identifiers, function calls, etc.)
func containsNonLiteralExpressions(exprs []ast.Expression) bool {
for _, e := range exprs {
if _, ok := e.(*ast.Literal); ok {
continue
}
// Unary minus of a literal (negative number) is also acceptable
if unary, ok := e.(*ast.UnaryExpr); ok && unary.Op == "-" {
if _, ok := unary.Operand.(*ast.Literal); ok {
continue
}
}
return true
}
return false
}
// containsNonLiteralInNested checks if an array or tuple literal contains
// non-literal elements at any nesting level (identifiers, function calls, etc.)
func containsNonLiteralInNested(lit *ast.Literal) bool {
if lit.Type != ast.LiteralArray && lit.Type != ast.LiteralTuple {
return false
}
exprs, ok := lit.Value.([]ast.Expression)
if !ok {
return false
}
for _, e := range exprs {
// Check if this element is a non-literal (identifier, function call, etc.)
if _, isLit := e.(*ast.Literal); !isLit {
return true
}
// Recursively check nested arrays/tuples
if innerLit, ok := e.(*ast.Literal); ok {
if containsNonLiteralInNested(innerLit) {
return true
}
}
}
return false
}
// containsTuples checks if a slice of expressions contains any tuple literals
func containsTuples(exprs []ast.Expression) bool {
for _, e := range exprs {
if lit, ok := e.(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
return true
}
}
return false
}
// containsEmptyArrays checks if a slice of expressions contains any empty array literals
func containsEmptyArrays(exprs []ast.Expression) bool {
for _, e := range exprs {
if lit, ok := e.(*ast.Literal); ok && lit.Type == ast.LiteralArray {
if innerExprs, ok := lit.Value.([]ast.Expression); ok && len(innerExprs) == 0 {
return true
}
}
}
return false
}
// containsEmptyArraysRecursive checks if any nested array at any depth is empty
func containsEmptyArraysRecursive(exprs []ast.Expression) bool {
for _, e := range exprs {
if lit, ok := e.(*ast.Literal); ok && lit.Type == ast.LiteralArray {
if innerExprs, ok := lit.Value.([]ast.Expression); ok {
if len(innerExprs) == 0 {
return true
}
// Recursively check nested arrays
if containsEmptyArraysRecursive(innerExprs) {
return true
}
}
}
}
return false
}
// containsTuplesRecursive checks if any nested array contains tuples at any depth
func containsTuplesRecursive(exprs []ast.Expression) bool {
for _, e := range exprs {
if lit, ok := e.(*ast.Literal); ok {
if lit.Type == ast.LiteralTuple {
return true
}
if lit.Type == ast.LiteralArray {
if innerExprs, ok := lit.Value.([]ast.Expression); ok {
if containsTuplesRecursive(innerExprs) {
return true
}
}
}
}
}
return false
}
func explainBinaryExpr(sb *strings.Builder, n *ast.BinaryExpr, indent string, depth int) {
// Convert operator to function name
fnName := OperatorToFunction(n.Op)
// For || (concat) operator, flatten chained concatenations
if n.Op == "||" {
operands := collectConcatOperands(n)
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(operands))
for _, op := range operands {
Node(sb, op, depth+2)
}
return
}
// For OR and AND operators, flatten left-associative chains
// but preserve explicit parenthesization like "(a OR b) OR c"
if n.Op == "OR" || n.Op == "AND" {
operands := collectLogicalOperands(n)
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(operands))
for _, op := range operands {
Node(sb, op, depth+2)
}
return
}
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, n.Left, depth+2)
Node(sb, n.Right, depth+2)
}
// collectConcatOperands flattens chained || (concat) operations into a list of operands
func collectConcatOperands(n *ast.BinaryExpr) []ast.Expression {
var operands []ast.Expression
// Recursively collect from left side if it's also a concat
if left, ok := n.Left.(*ast.BinaryExpr); ok && left.Op == "||" {
operands = append(operands, collectConcatOperands(left)...)
} else {
operands = append(operands, n.Left)
}
// Recursively collect from right side if it's also a concat
if right, ok := n.Right.(*ast.BinaryExpr); ok && right.Op == "||" {
operands = append(operands, collectConcatOperands(right)...)
} else {
operands = append(operands, n.Right)
}
return operands
}
// collectLogicalOperands flattens chained OR/AND operations into a list of operands,
// but respects explicit parenthesization. For example:
// - "a OR b OR c" → [a, b, c] (flattened)
// - "(a OR b) OR c" → [(a OR b), c] (preserved due to explicit parens)
func collectLogicalOperands(n *ast.BinaryExpr) []ast.Expression {
var operands []ast.Expression
// Recursively collect from left side if it's the same operator AND not parenthesized
if left, ok := n.Left.(*ast.BinaryExpr); ok && left.Op == n.Op && !left.Parenthesized {
operands = append(operands, collectLogicalOperands(left)...)
} else {
operands = append(operands, n.Left)
}
// Don't flatten right side - explicit parentheses would be on the left in left-associative parsing
operands = append(operands, n.Right)
return operands
}
func explainUnaryExpr(sb *strings.Builder, n *ast.UnaryExpr, indent string, depth int) {
// Handle negate of literal numbers - output as negative literal instead of function
if n.Op == "-" {
if lit, ok := n.Operand.(*ast.Literal); ok {
switch lit.Type {
case ast.LiteralInteger:
// Convert positive integer to negative
switch val := lit.Value.(type) {
case int64:
negVal := -val
// ClickHouse normalizes -0 to UInt64_0
if negVal == 0 {
fmt.Fprintf(sb, "%sLiteral UInt64_0\n", indent)
} else if negVal > 0 {
fmt.Fprintf(sb, "%sLiteral UInt64_%d\n", indent, negVal)
} else {
fmt.Fprintf(sb, "%sLiteral Int64_%d\n", indent, negVal)
}
return
case uint64:
// ClickHouse normalizes -0 to UInt64_0
if val == 0 {
fmt.Fprintf(sb, "%sLiteral UInt64_0\n", indent)
} else {
fmt.Fprintf(sb, "%sLiteral Int64_-%d\n", indent, val)
}
return
}
case ast.LiteralFloat:
val := lit.Value.(float64)
s := FormatFloat(-val)
fmt.Fprintf(sb, "%sLiteral Float64_%s\n", indent, s)
return
}
}
}
fnName := UnaryOperatorToFunction(n.Op)
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
Node(sb, n.Operand, depth+2)
}
func explainSubquery(sb *strings.Builder, n *ast.Subquery, indent string, depth int) {
children := 1
if n.Alias != "" {
fmt.Fprintf(sb, "%sSubquery (alias %s) (children %d)\n", indent, escapeAlias(n.Alias), children)
} else {
fmt.Fprintf(sb, "%sSubquery (children %d)\n", indent, children)
}
// Set context flag before recursing into subquery content
// This affects how negated literals with aliases are formatted
prevContext := inSubqueryContext
inSubqueryContext = true
Node(sb, n.Query, depth+1)
inSubqueryContext = prevContext
}
func explainAliasedExpr(sb *strings.Builder, n *ast.AliasedExpr, depth int) {
// For aliased expressions, we need to show the underlying expression with the alias
indent := strings.Repeat(" ", depth)
switch e := n.Expr.(type) {
case *ast.Literal:
// Check if this is a tuple with complex expressions that should be rendered as Function tuple
if e.Type == ast.LiteralTuple {
if exprs, ok := e.Value.([]ast.Expression); ok {
needsFunctionFormat := false
// Empty tuples always use Function tuple format
if len(exprs) == 0 {
needsFunctionFormat = true
}
for _, expr := range exprs {
if _, isLit := expr.(*ast.Literal); !isLit {
needsFunctionFormat = true
break
}
// Also check if nested arrays/tuples contain non-literal elements
if lit, ok := expr.(*ast.Literal); ok {
if containsNonLiteralInNested(lit) {
needsFunctionFormat = true
break
}
}
}
if needsFunctionFormat {
// Render as Function tuple with alias
fmt.Fprintf(sb, "%sFunction tuple (alias %s) (children %d)\n", indent, escapeAlias(n.Alias), 1)
// For empty ExpressionList, don't include children count
if len(exprs) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(exprs))
} else {
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
}
for _, expr := range exprs {
Node(sb, expr, depth+2)
}
return
}
}
}
// Check if this is an array containing specific expressions that need Function array format
if e.Type == ast.LiteralArray {
if exprs, ok := e.Value.([]ast.Expression); ok {
needsFunctionFormat := false
hasNestedArrays := false
// Empty arrays always use Function array format
if len(exprs) == 0 {
needsFunctionFormat = true
}
for _, expr := range exprs {
// Check for tuples - use Function array
if lit, ok := expr.(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
needsFunctionFormat = true
break
}
// Check for nested arrays
if lit, ok := expr.(*ast.Literal); ok && lit.Type == ast.LiteralArray {
hasNestedArrays = true
// Check if inner array is empty or contains empty arrays
if innerExprs, ok := lit.Value.([]ast.Expression); ok {
if len(innerExprs) == 0 || containsEmptyArrays(innerExprs) {
needsFunctionFormat = true
break
}
}
}
// Check for identifiers - use Function array
if _, ok := expr.(*ast.Identifier); ok {
needsFunctionFormat = true
break
}
// Check for function calls - use Function array
if _, ok := expr.(*ast.FunctionCall); ok {
needsFunctionFormat = true
break
}
// Check for CAST expressions - use Function array
if _, ok := expr.(*ast.CastExpr); ok {
needsFunctionFormat = true
break
}
// Check for binary expressions - use Function array
if _, ok := expr.(*ast.BinaryExpr); ok {
needsFunctionFormat = true
break
}
// Check for other non-literal expressions (skip arrays/tuples which are handled separately)
if lit, ok := expr.(*ast.Literal); !ok {
// Not a literal - check if it's a unary negation of a number (which is OK)
if unary, ok := expr.(*ast.UnaryExpr); ok && unary.Op == "-" {
if innerLit, ok := unary.Operand.(*ast.Literal); ok {
if innerLit.Type == ast.LiteralInteger || innerLit.Type == ast.LiteralFloat {
continue // Negated number is OK
}
}
}
needsFunctionFormat = true
break
} else if lit.Type != ast.LiteralArray && lit.Type != ast.LiteralTuple {
// Simple literal (not array/tuple) - OK
continue
}
// Arrays and tuples are handled by the earlier checks for nested arrays
}
// Also check for empty arrays at any depth within nested arrays
if hasNestedArrays && containsEmptyArraysRecursive(exprs) {
needsFunctionFormat = true
}
// Also check for tuples at any depth within nested arrays
if hasNestedArrays && containsTuplesRecursive(exprs) {
needsFunctionFormat = true
}
if needsFunctionFormat {
// Render as Function array with alias
fmt.Fprintf(sb, "%sFunction array (alias %s) (children %d)\n", indent, escapeAlias(n.Alias), 1)
if len(exprs) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(exprs))
} else {
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
}
for _, expr := range exprs {
Node(sb, expr, depth+2)
}
return
}
}
}
fmt.Fprintf(sb, "%sLiteral %s (alias %s)\n", indent, FormatLiteral(e), escapeAlias(n.Alias))
case *ast.BinaryExpr:
// Binary expressions become functions with alias
fnName := OperatorToFunction(e.Op)
// For || (concat) operator, flatten chained concatenations
if e.Op == "||" {
operands := collectConcatOperands(e)
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, escapeAlias(n.Alias), 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(operands))
for _, op := range operands {
Node(sb, op, depth+2)
}
} else if e.Op == "OR" || e.Op == "AND" {
// For OR and AND operators, flatten but respect explicit parenthesization
operands := collectLogicalOperands(e)
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, escapeAlias(n.Alias), 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(operands))
for _, op := range operands {
Node(sb, op, depth+2)
}
} else {
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, escapeAlias(n.Alias), 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, e.Left, depth+2)
Node(sb, e.Right, depth+2)
}
case *ast.UnaryExpr:
// Handle negated numeric literals - output as Literal instead of Function negate
// When an aliased expression is a negated literal, output as negative Literal
if e.Op == "-" {
if lit, ok := e.Operand.(*ast.Literal); ok {
switch lit.Type {
case ast.LiteralInteger:
// Convert negated integer to negative literal
switch val := lit.Value.(type) {
case int64:
fmt.Fprintf(sb, "%sLiteral Int64_%d (alias %s)\n", indent, -val, escapeAlias(n.Alias))
return
case uint64:
fmt.Fprintf(sb, "%sLiteral Int64_-%d (alias %s)\n", indent, val, escapeAlias(n.Alias))
return
}
case ast.LiteralFloat:
// Always convert negated floats to literals (especially for -inf, -nan)
val := lit.Value.(float64)
s := FormatFloat(-val)
fmt.Fprintf(sb, "%sLiteral Float64_%s (alias %s)\n", indent, s, escapeAlias(n.Alias))
return
}
}
}
// Unary expressions become functions with alias
fnName := UnaryOperatorToFunction(e.Op)
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, escapeAlias(n.Alias), 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
Node(sb, e.Operand, depth+2)
case *ast.FunctionCall:
// Function calls already handle aliases
explainFunctionCallWithAlias(sb, e, n.Alias, indent, depth)
case *ast.Lambda:
// Lambda expressions with alias
explainLambdaWithAlias(sb, e, n.Alias, indent, depth)
case *ast.ExtractExpr:
// EXTRACT expressions with alias
explainExtractExprWithAlias(sb, e, n.Alias, indent, depth)
case *ast.Identifier:
// Identifiers with alias
fmt.Fprintf(sb, "%sIdentifier %s (alias %s)\n", indent, e.Name(), escapeAlias(n.Alias))
case *ast.IntervalExpr:
// Interval expressions with alias
explainIntervalExpr(sb, e, n.Alias, indent, depth)
case *ast.TernaryExpr:
// Ternary expressions become if functions with alias
fmt.Fprintf(sb, "%sFunction if (alias %s) (children %d)\n", indent, escapeAlias(n.Alias), 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 3)
Node(sb, e.Condition, depth+2)
Node(sb, e.Then, depth+2)
Node(sb, e.Else, depth+2)
case *ast.CastExpr:
// CAST expressions always show the alias from the AliasedExpr wrapper
explainCastExprWithAlias(sb, e, n.Alias, indent, depth)
case *ast.ArrayAccess:
// Array access with alias
explainArrayAccessWithAlias(sb, e, n.Alias, indent, depth)
case *ast.TupleAccess:
// Tuple access with alias
explainTupleAccessWithAlias(sb, e, n.Alias, indent, depth)
case *ast.InExpr:
// IN expressions with alias
explainInExprWithAlias(sb, e, n.Alias, indent, depth)
case *ast.CaseExpr:
// CASE expressions with alias
explainCaseExprWithAlias(sb, e, n.Alias, indent, depth)
case *ast.ExistsExpr:
// EXISTS expressions with alias
explainExistsExprWithAlias(sb, e, n.Alias, indent, depth)
default:
// For other types, recursively explain and add alias info
Node(sb, n.Expr, depth)
}
}
func explainAsterisk(sb *strings.Builder, n *ast.Asterisk, indent string, depth int) {
// Check if there are any column transformers (EXCEPT, REPLACE, APPLY)
hasTransformers := len(n.Transformers) > 0 || len(n.Except) > 0 || len(n.Replace) > 0 || len(n.Apply) > 0
if n.Table != "" {
if hasTransformers {
fmt.Fprintf(sb, "%sQualifiedAsterisk (children %d)\n", indent, 2)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
explainColumnsTransformers(sb, n, indent+" ", depth+1)
} else {
fmt.Fprintf(sb, "%sQualifiedAsterisk (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
}
} else {
if hasTransformers {
fmt.Fprintf(sb, "%sAsterisk (children %d)\n", indent, 1)
explainColumnsTransformers(sb, n, indent+" ", depth+1)
} else {
fmt.Fprintf(sb, "%sAsterisk\n", indent)
}
}
}
func explainColumnsTransformers(sb *strings.Builder, n *ast.Asterisk, indent string, depth int) {
// Use Transformers if available (preserves order), otherwise fall back to legacy arrays
if len(n.Transformers) > 0 {
fmt.Fprintf(sb, "%sColumnsTransformerList (children %d)\n", indent, len(n.Transformers))
for _, t := range n.Transformers {
explainSingleTransformer(sb, t, indent, depth)
}
return
}
// Legacy: use separate arrays (doesn't preserve order)
transformerCount := 0
if len(n.Except) > 0 {
transformerCount++
}
if len(n.Replace) > 0 {
transformerCount++
}
// Each APPLY adds one transformer
transformerCount += len(n.Apply)
fmt.Fprintf(sb, "%sColumnsTransformerList (children %d)\n", indent, transformerCount)
if len(n.Except) > 0 {
fmt.Fprintf(sb, "%s ColumnsExceptTransformer (children %d)\n", indent, len(n.Except))
for _, col := range n.Except {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, col)
}
}
if len(n.Replace) > 0 {
fmt.Fprintf(sb, "%s ColumnsReplaceTransformer (children %d)\n", indent, len(n.Replace))
for _, replace := range n.Replace {
children := 0
if replace.Expr != nil {
children = 1
}
fmt.Fprintf(sb, "%s ColumnsReplaceTransformer::Replacement (children %d)\n", indent, children)
if replace.Expr != nil {
// Output the expression without alias - the replacement name is implied
Node(sb, replace.Expr, depth+3)
}
}
}
// Each APPLY function gets its own ColumnsApplyTransformer
for range n.Apply {
fmt.Fprintf(sb, "%s ColumnsApplyTransformer\n", indent)
}
}
func explainSingleTransformer(sb *strings.Builder, t *ast.ColumnTransformer, indent string, depth int) {
switch t.Type {
case "apply":
fmt.Fprintf(sb, "%s ColumnsApplyTransformer\n", indent)
case "except":
fmt.Fprintf(sb, "%s ColumnsExceptTransformer (children %d)\n", indent, len(t.Except))
for _, col := range t.Except {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, col)
}
case "replace":
fmt.Fprintf(sb, "%s ColumnsReplaceTransformer (children %d)\n", indent, len(t.Replaces))
for _, replace := range t.Replaces {
children := 0
if replace.Expr != nil {
children = 1
}
fmt.Fprintf(sb, "%s ColumnsReplaceTransformer::Replacement (children %d)\n", indent, children)
if replace.Expr != nil {
Node(sb, replace.Expr, depth+3)
}
}
}
}
func explainColumnsMatcher(sb *strings.Builder, n *ast.ColumnsMatcher, indent string, depth int) {
// Check if there are any column transformers (EXCEPT, REPLACE, APPLY)
hasTransformers := len(n.Transformers) > 0 || len(n.Except) > 0 || len(n.Replace) > 0 || len(n.Apply) > 0
// Determine the matcher type based on whether it's a pattern or a list
if len(n.Columns) > 0 {
// ColumnsListMatcher for COLUMNS(col1, col2, ...)
typeName := "ColumnsListMatcher"
if n.Qualifier != "" {
typeName = "QualifiedColumnsListMatcher"
}
childCount := 1 // ExpressionList of columns
if n.Qualifier != "" {
childCount++
}
if hasTransformers {
childCount++ // for ColumnsTransformerList
}
fmt.Fprintf(sb, "%s%s (children %d)\n", indent, typeName, childCount)
if n.Qualifier != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Qualifier)
}
// Output the columns as ExpressionList
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Columns))
for _, col := range n.Columns {
Node(sb, col, depth+2)
}
if hasTransformers {
explainColumnsMatcherTransformers(sb, n, indent+" ", depth+1)
}
} else {
// ColumnsRegexpMatcher for COLUMNS('pattern')
typeName := "ColumnsRegexpMatcher"
if n.Qualifier != "" {
typeName = "QualifiedColumnsRegexpMatcher"
}
if n.Qualifier != "" {
childCount := 1 // Identifier
if hasTransformers {
childCount++
}
fmt.Fprintf(sb, "%s%s (children %d)\n", indent, typeName, childCount)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Qualifier)
if hasTransformers {
explainColumnsMatcherTransformers(sb, n, indent+" ", depth+1)
}
} else {
if hasTransformers {
fmt.Fprintf(sb, "%s%s (children %d)\n", indent, typeName, 1)
explainColumnsMatcherTransformers(sb, n, indent+" ", depth+1)
} else {
fmt.Fprintf(sb, "%s%s\n", indent, typeName)
}
}
}
}
func explainColumnsMatcherTransformers(sb *strings.Builder, n *ast.ColumnsMatcher, indent string, depth int) {
// Use Transformers if available (preserves order), otherwise fall back to legacy arrays
if len(n.Transformers) > 0 {
fmt.Fprintf(sb, "%sColumnsTransformerList (children %d)\n", indent, len(n.Transformers))
for _, t := range n.Transformers {
explainSingleTransformer(sb, t, indent, depth)
}
return
}
// Legacy: use separate arrays (doesn't preserve order)
transformerCount := 0
if len(n.Except) > 0 {
transformerCount++
}
if len(n.Replace) > 0 {
transformerCount++
}
// Each APPLY adds one transformer
transformerCount += len(n.Apply)
fmt.Fprintf(sb, "%sColumnsTransformerList (children %d)\n", indent, transformerCount)
if len(n.Except) > 0 {
fmt.Fprintf(sb, "%s ColumnsExceptTransformer (children %d)\n", indent, len(n.Except))
for _, col := range n.Except {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, col)
}
}
if len(n.Replace) > 0 {
fmt.Fprintf(sb, "%s ColumnsReplaceTransformer (children %d)\n", indent, len(n.Replace))
for _, replace := range n.Replace {
children := 0
if replace.Expr != nil {
children = 1
}
fmt.Fprintf(sb, "%s ColumnsReplaceTransformer::Replacement (children %d)\n", indent, children)
if replace.Expr != nil {
// Output the expression without alias - the replacement name is implied
Node(sb, replace.Expr, depth+3)
}
}
}
// Each APPLY function gets its own ColumnsApplyTransformer
for range n.Apply {
fmt.Fprintf(sb, "%s ColumnsApplyTransformer\n", indent)
}
}
func explainWithElement(sb *strings.Builder, n *ast.WithElement, indent string, depth int) {
// For WITH elements, we need to show the underlying expression with the name as alias
// When name is empty, don't show the alias part
switch e := n.Query.(type) {
case *ast.Literal:
// Empty tuples should be rendered as Function tuple, not Literal
if e.Type == ast.LiteralTuple {
if exprs, ok := e.Value.([]ast.Expression); ok && len(exprs) == 0 {
if n.Name != "" {
fmt.Fprintf(sb, "%sFunction tuple (alias %s) (children %d)\n", indent, n.Name, 1)
} else {
fmt.Fprintf(sb, "%sFunction tuple (children %d)\n", indent, 1)
}
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
return
}
}
// Arrays containing non-literal expressions should be rendered as Function array
if e.Type == ast.LiteralArray {
if exprs, ok := e.Value.([]ast.Expression); ok {
needsFunctionFormat := false
for _, elem := range exprs {
if !isSimpleLiteralOrNegation(elem) {
needsFunctionFormat = true
break
}
}
if needsFunctionFormat {
// Render as Function array with alias
if n.Name != "" {
fmt.Fprintf(sb, "%sFunction array (alias %s) (children %d)\n", indent, n.Name, 1)
} else {
fmt.Fprintf(sb, "%sFunction array (children %d)\n", indent, 1)
}
if len(exprs) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(exprs))
} else {
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
}
for _, elem := range exprs {
Node(sb, elem, depth+2)
}
return
}
}
}
if n.Name != "" {
fmt.Fprintf(sb, "%sLiteral %s (alias %s)\n", indent, FormatLiteral(e), n.Name)
} else {
fmt.Fprintf(sb, "%sLiteral %s\n", indent, FormatLiteral(e))
}
case *ast.Identifier:
if n.Name != "" {
fmt.Fprintf(sb, "%sIdentifier %s (alias %s)\n", indent, e.Name(), n.Name)
} else {
fmt.Fprintf(sb, "%sIdentifier %s\n", indent, e.Name())
}
case *ast.FunctionCall:
explainFunctionCallWithAlias(sb, e, n.Name, indent, depth)
case *ast.Lambda:
explainLambdaWithAlias(sb, e, n.Name, indent, depth)
case *ast.BinaryExpr:
// Binary expressions become functions
fnName := OperatorToFunction(e.Op)
// For || (concat) operator, flatten chained concatenations
if e.Op == "||" {
operands := collectConcatOperands(e)
if n.Name != "" {
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, n.Name, 1)
} else {
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(operands))
for _, op := range operands {
Node(sb, op, depth+2)
}
} else if e.Op == "OR" || e.Op == "AND" {
// For OR and AND operators, flatten but respect explicit parenthesization
operands := collectLogicalOperands(e)
if n.Name != "" {
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, n.Name, 1)
} else {
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(operands))
for _, op := range operands {
Node(sb, op, depth+2)
}
} else {
if n.Name != "" {
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, n.Name, 1)
} else {
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, e.Left, depth+2)
Node(sb, e.Right, depth+2)
}
case *ast.Subquery:
// Check if this is "(subquery) AS alias" syntax vs "name AS (subquery)" syntax
if e.Alias != "" {
// "(subquery) AS alias" syntax: output Subquery with alias directly