-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.go
More file actions
951 lines (906 loc) · 30.9 KB
/
functions.go
File metadata and controls
951 lines (906 loc) · 30.9 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
package explain
import (
"fmt"
"strings"
"github.com/sqlc-dev/doubleclick/ast"
)
func explainFunctionCall(sb *strings.Builder, n *ast.FunctionCall, indent string, depth int) {
explainFunctionCallWithAlias(sb, n, n.Alias, indent, depth)
}
func explainFunctionCallWithAlias(sb *strings.Builder, n *ast.FunctionCall, alias string, indent string, depth int) {
children := 1 // arguments ExpressionList
if len(n.Parameters) > 0 {
children++ // parameters ExpressionList
}
// Only count WindowDefinition as a child for inline window specs that have content
// Empty OVER () doesn't produce a WindowDefinition in ClickHouse EXPLAIN AST
// Named refs like "OVER w" are shown in the SELECT's WINDOW clause instead
hasNonEmptyWindowSpec := n.Over != nil && n.Over.Name == "" && windowSpecHasContent(n.Over)
if hasNonEmptyWindowSpec {
children++ // WindowDefinition for OVER clause
}
// Normalize function name
fnName := NormalizeFunctionName(n.Name)
// Append "Distinct" if the function has DISTINCT modifier
if n.Distinct {
fnName = fnName + "Distinct"
}
if alias != "" {
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, alias, children)
} else {
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, children)
}
// Arguments (Settings are included as part of argument count)
argCount := len(n.Arguments)
if len(n.Settings) > 0 {
argCount++ // Set is counted as one argument
}
fmt.Fprintf(sb, "%s ExpressionList", indent)
if argCount > 0 {
fmt.Fprintf(sb, " (children %d)", argCount)
}
fmt.Fprintln(sb)
for _, arg := range n.Arguments {
// For view() table function, unwrap Subquery wrapper
// Also reset the subquery context since view() SELECT is not in a Subquery node
if strings.ToLower(n.Name) == "view" {
if sq, ok := arg.(*ast.Subquery); ok {
prevContext := inSubqueryContext
inSubqueryContext = false
Node(sb, sq.Query, depth+2)
inSubqueryContext = prevContext
continue
}
}
Node(sb, arg, depth+2)
}
// Settings appear as Set node inside ExpressionList
if len(n.Settings) > 0 {
fmt.Fprintf(sb, "%s Set\n", indent)
}
// Parameters (for parametric functions)
if len(n.Parameters) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Parameters))
for _, p := range n.Parameters {
Node(sb, p, depth+2)
}
}
// Window definition (for window functions with inline OVER clause)
// WindowDefinition is a sibling to ExpressionList, so use the same indent
// Only output for non-empty inline specs, not named references like "OVER w"
if hasNonEmptyWindowSpec {
explainWindowSpec(sb, n.Over, indent+" ", depth+1)
}
}
// windowSpecHasContent returns true if the window spec has any content.
// ClickHouse EXPLAIN AST never includes WindowDefinition nodes for window
// functions, even when OVER clause has PARTITION BY, ORDER BY, or frame specs.
func windowSpecHasContent(w *ast.WindowSpec) bool {
return false
}
func explainLambda(sb *strings.Builder, n *ast.Lambda, indent string, depth int) {
// Lambda is represented as Function lambda with tuple of params and body
fmt.Fprintf(sb, "%sFunction lambda (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
// Parameters as tuple
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Parameters))
for _, p := range n.Parameters {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, p)
}
// Body
Node(sb, n.Body, depth+2)
}
func explainCastExpr(sb *strings.Builder, n *ast.CastExpr, indent string, depth int) {
explainCastExprWithAlias(sb, n, n.Alias, indent, depth)
}
func explainCastExprWithAlias(sb *strings.Builder, n *ast.CastExpr, alias string, indent string, depth int) {
// For :: operator syntax, ClickHouse hides alias only when expression is
// an array/tuple with complex content that gets formatted as string
hideAlias := false
useArrayFormat := false
if n.OperatorSyntax {
if lit, ok := n.Expr.(*ast.Literal); ok {
if lit.Type == ast.LiteralArray || lit.Type == ast.LiteralTuple {
// Determine format based on both content and target type
useArrayFormat = shouldUseArrayFormat(lit, n.Type)
hideAlias = !useArrayFormat
}
}
}
// CAST is represented as Function CAST with expr and type as arguments
if alias != "" && !hideAlias {
fmt.Fprintf(sb, "%sFunction CAST (alias %s) (children %d)\n", indent, alias, 1)
} else {
fmt.Fprintf(sb, "%sFunction CAST (children %d)\n", indent, 1)
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
// For :: operator syntax with simple literals, format as string literal
// For function syntax or complex expressions, use normal AST node
if n.OperatorSyntax {
if lit, ok := n.Expr.(*ast.Literal); ok {
// For arrays/tuples of simple primitives, use FormatLiteral (Array_[...] format)
// For strings and other types, use string format
if lit.Type == ast.LiteralArray || lit.Type == ast.LiteralTuple {
if useArrayFormat {
fmt.Fprintf(sb, "%s Literal %s\n", indent, FormatLiteral(lit))
} else {
// Complex content - format as string
exprStr := formatExprAsString(lit)
fmt.Fprintf(sb, "%s Literal \\'%s\\'\n", indent, exprStr)
}
} else {
// Simple literal - format as string
exprStr := formatExprAsString(lit)
fmt.Fprintf(sb, "%s Literal \\'%s\\'\n", indent, exprStr)
}
} else if negatedLit := extractNegatedLiteral(n.Expr); negatedLit != "" {
// Handle negated literal like -0::Int16 -> CAST('-0', 'Int16')
fmt.Fprintf(sb, "%s Literal \\'%s\\'\n", indent, negatedLit)
} else {
// Complex expression - use normal AST node
Node(sb, n.Expr, depth+2)
}
} else {
Node(sb, n.Expr, depth+2)
}
// Type is formatted as a literal string, or as a node if it's a dynamic type expression
if n.TypeExpr != nil {
Node(sb, n.TypeExpr, depth+2)
} else {
typeStr := FormatDataType(n.Type)
// Only escape if the DataType doesn't have parameters - this means the entire
// type was parsed from a string literal and may contain unescaped quotes.
// If it has parameters, FormatDataType already handles escaping.
if n.Type == nil || len(n.Type.Parameters) == 0 {
typeStr = escapeStringLiteral(typeStr)
}
fmt.Fprintf(sb, "%s Literal \\'%s\\'\n", indent, typeStr)
}
}
// shouldUseArrayFormat determines whether to use Array_[...] format or string format
// for array/tuple literals in :: cast expressions.
// This depends on both the literal content and the target type.
func shouldUseArrayFormat(lit *ast.Literal, targetType *ast.DataType) bool {
// First check if the literal contains only primitive literals (not expressions)
if !containsOnlyLiterals(lit) {
return false
}
// For arrays of strings, check the target type to determine format
if lit.Type == ast.LiteralArray && hasStringElements(lit) {
// Only use Array_ format when casting to Array(String) specifically
// For other types like Array(JSON), Array(LowCardinality(String)), etc., use string format
if targetType != nil && strings.ToLower(targetType.Name) == "array" && len(targetType.Parameters) > 0 {
if innerType, ok := targetType.Parameters[0].(*ast.DataType); ok {
// Only use Array_ format if inner type is exactly "String" with no parameters
if strings.ToLower(innerType.Name) == "string" && len(innerType.Parameters) == 0 {
return true
}
}
}
// For any other type (JSON, LowCardinality, etc.), use string format
return false
}
// For non-string primitives, always use Array_ format
return true
}
// containsOnlyLiterals checks if a literal array/tuple contains only literal values (no expressions)
func containsOnlyLiterals(lit *ast.Literal) bool {
var exprs []ast.Expression
switch lit.Type {
case ast.LiteralArray, ast.LiteralTuple:
var ok bool
exprs, ok = lit.Value.([]ast.Expression)
if !ok {
return false
}
default:
return true
}
for _, e := range exprs {
innerLit, ok := e.(*ast.Literal)
if !ok {
return false
}
// Nested arrays/tuples need recursive check
if innerLit.Type == ast.LiteralArray || innerLit.Type == ast.LiteralTuple {
if !containsOnlyLiterals(innerLit) {
return false
}
}
}
return true
}
// hasStringElements checks if an array literal contains any string elements
func hasStringElements(lit *ast.Literal) bool {
if lit.Type != ast.LiteralArray {
return false
}
exprs, ok := lit.Value.([]ast.Expression)
if !ok {
return false
}
for _, e := range exprs {
if innerLit, ok := e.(*ast.Literal); ok {
if innerLit.Type == ast.LiteralString {
return true
}
}
}
return false
}
// containsOnlyPrimitives checks if a literal array/tuple contains only primitive literals
// Deprecated: Use shouldUseArrayFormat instead for :: cast expressions
func containsOnlyPrimitives(lit *ast.Literal) bool {
var exprs []ast.Expression
switch lit.Type {
case ast.LiteralArray, ast.LiteralTuple:
var ok bool
exprs, ok = lit.Value.([]ast.Expression)
if !ok {
return false
}
default:
return true
}
for _, e := range exprs {
innerLit, ok := e.(*ast.Literal)
if !ok {
return false
}
// Nested arrays/tuples need recursive check
if innerLit.Type == ast.LiteralArray || innerLit.Type == ast.LiteralTuple {
if !containsOnlyPrimitives(innerLit) {
return false
}
}
}
return true
}
// isNumericExpr checks if an expression is a numeric value (literal or unary minus of numeric)
func isNumericExpr(expr ast.Expression) bool {
if lit, ok := expr.(*ast.Literal); ok {
return lit.Type == ast.LiteralInteger || lit.Type == ast.LiteralFloat
}
if unary, ok := expr.(*ast.UnaryExpr); ok && unary.Op == "-" {
if lit, ok := unary.Operand.(*ast.Literal); ok {
return lit.Type == ast.LiteralInteger || lit.Type == ast.LiteralFloat
}
}
return false
}
// containsOnlyPrimitiveLiterals checks if a tuple literal contains only primitive literals (recursively)
func containsOnlyPrimitiveLiterals(lit *ast.Literal) bool {
if lit.Type != ast.LiteralTuple {
// Non-tuple literals are primitive
return true
}
exprs, ok := lit.Value.([]ast.Expression)
if !ok {
return false
}
for _, e := range exprs {
innerLit, ok := e.(*ast.Literal)
if !ok {
// Non-literal expression in tuple
return false
}
// Recursively check nested tuples
if innerLit.Type == ast.LiteralTuple {
if !containsOnlyPrimitiveLiterals(innerLit) {
return false
}
}
}
return true
}
// exprToLiteral converts a numeric expression to a literal (handles unary minus)
func exprToLiteral(expr ast.Expression) *ast.Literal {
if lit, ok := expr.(*ast.Literal); ok {
return lit
}
if unary, ok := expr.(*ast.UnaryExpr); ok && unary.Op == "-" {
if lit, ok := unary.Operand.(*ast.Literal); ok {
// Create a new literal with negated value
switch val := lit.Value.(type) {
case int64:
return &ast.Literal{Type: ast.LiteralInteger, Value: -val}
case uint64:
// Convert to int64 and negate
return &ast.Literal{Type: ast.LiteralInteger, Value: -int64(val)}
case float64:
return &ast.Literal{Type: ast.LiteralFloat, Value: -val}
}
}
}
return nil
}
// extractNegatedLiteral checks if expr is a negated literal (like -0, -12)
// and returns its string representation (like "-0", "-12") for :: cast expressions.
// Returns empty string if not a negated literal.
func extractNegatedLiteral(expr ast.Expression) string {
unary, ok := expr.(*ast.UnaryExpr)
if !ok || unary.Op != "-" {
return ""
}
lit, ok := unary.Operand.(*ast.Literal)
if !ok {
return ""
}
switch lit.Type {
case ast.LiteralInteger:
return "-" + formatExprAsString(lit)
case ast.LiteralFloat:
return "-" + formatExprAsString(lit)
}
return ""
}
func explainInExpr(sb *strings.Builder, n *ast.InExpr, indent string, depth int) {
// IN is represented as Function in
fnName := "in"
if n.Not {
fnName = "notIn"
}
if n.Global {
fnName = "global" + strings.Title(fnName)
}
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
// Determine if the IN list should be combined into a single tuple literal
// This happens when we have multiple literals of compatible types:
// - All numeric literals/expressions (integers/floats, including unary minus)
// - All string literals (only for small lists, max 10 items)
// - All tuple literals that contain only primitive literals (recursively)
canBeTupleLiteral := false
// Only combine strings into tuple for small lists (up to 10 items)
// Large string lists are kept as separate children in ClickHouse EXPLAIN AST
const maxStringTupleSize = 10
if n.Query == nil && len(n.List) > 1 {
allNumeric := true
allStrings := true
allTuples := true
allTuplesArePrimitive := true
for _, item := range n.List {
if lit, ok := item.(*ast.Literal); ok {
if lit.Type != ast.LiteralInteger && lit.Type != ast.LiteralFloat {
allNumeric = false
}
if lit.Type != ast.LiteralString {
allStrings = false
}
if lit.Type != ast.LiteralTuple {
allTuples = false
} else {
// Check if this tuple contains only primitive literals
if !containsOnlyPrimitiveLiterals(lit) {
allTuplesArePrimitive = false
}
}
} else if isNumericExpr(item) {
// Unary minus of numeric is still numeric
allStrings = false
allTuples = false
} else {
allNumeric = false
allStrings = false
allTuples = false
break
}
}
// For strings, only combine if list is small enough
// For tuples, only combine if all contain primitive literals
canBeTupleLiteral = allNumeric || (allStrings && len(n.List) <= maxStringTupleSize) || (allTuples && allTuplesArePrimitive)
}
// Count arguments: expr + list items or subquery
argCount := 1
if n.Query != nil {
argCount++
} else if canBeTupleLiteral {
// Multiple literals will be combined into a single tuple
argCount++
} else {
// Check if we have a single tuple literal that should be wrapped in Function tuple
if len(n.List) == 1 {
if lit, ok := n.List[0].(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
// Single tuple literal gets wrapped in Function tuple, so count as 1
argCount++
} else {
argCount += len(n.List)
}
} else {
// Check if all items are string literals (large list case - no wrapper)
allStringLiterals := true
for _, item := range n.List {
if lit, ok := item.(*ast.Literal); !ok || lit.Type != ast.LiteralString {
allStringLiterals = false
break
}
}
if allStringLiterals {
// Large string list - separate children
argCount += len(n.List)
} else {
// Non-string items get wrapped in a single Function tuple
argCount++
}
}
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, argCount)
Node(sb, n.Expr, depth+2)
if n.Query != nil {
// Subqueries in IN should be wrapped in Subquery node
fmt.Fprintf(sb, "%s Subquery (children %d)\n", indent, 1)
Node(sb, n.Query, depth+3)
} else if canBeTupleLiteral {
// Combine multiple literals into a single Tuple literal
tupleLit := &ast.Literal{
Type: ast.LiteralTuple,
Value: n.List,
}
fmt.Fprintf(sb, "%s Literal %s\n", indent, FormatLiteral(tupleLit))
} else if len(n.List) == 1 {
// Single element in the list
// If it's a tuple literal, wrap it in Function tuple
// Otherwise, output the element directly
if lit, ok := n.List[0].(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
// Wrap tuple literal in Function tuple
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
Node(sb, n.List[0], depth+4)
} else {
// Single non-tuple element - output directly
Node(sb, n.List[0], depth+2)
}
} else {
// Check if all items are tuple literals (some may have expressions)
allTuples := true
for _, item := range n.List {
if lit, ok := item.(*ast.Literal); !ok || lit.Type != ast.LiteralTuple {
allTuples = false
break
}
}
if allTuples {
// Wrap all tuples in Function tuple
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.List))
for _, item := range n.List {
explainTupleInInList(sb, item.(*ast.Literal), indent+" ", depth+4)
}
} else {
// Check if all items are string literals (large list case)
allStringLiterals := true
for _, item := range n.List {
if lit, ok := item.(*ast.Literal); !ok || lit.Type != ast.LiteralString {
allStringLiterals = false
break
}
}
if allStringLiterals {
// Large string list - output as separate children (no tuple wrapper)
for _, item := range n.List {
Node(sb, item, depth+2)
}
} else {
// Wrap non-literal/non-tuple list items in Function tuple
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.List))
for _, item := range n.List {
Node(sb, item, depth+4)
}
}
}
}
}
// explainTupleInInList renders a tuple in an IN list - either as Literal or Function tuple
func explainTupleInInList(sb *strings.Builder, lit *ast.Literal, indent string, depth int) {
if containsOnlyPrimitiveLiterals(lit) {
// All primitives - render as Literal Tuple_
fmt.Fprintf(sb, "%s Literal %s\n", indent, FormatLiteral(lit))
} else {
// Contains expressions - render as Function tuple
exprs, ok := lit.Value.([]ast.Expression)
if !ok {
fmt.Fprintf(sb, "%s Literal %s\n", indent, FormatLiteral(lit))
return
}
fmt.Fprintf(sb, "%s Function 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)
}
}
}
func explainInExprWithAlias(sb *strings.Builder, n *ast.InExpr, alias string, indent string, depth int) {
// IN is represented as Function in with alias
fnName := "in"
if n.Not {
fnName = "notIn"
}
if n.Global {
fnName = "global" + strings.Title(fnName)
}
if alias != "" {
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, alias, 1)
} else {
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
}
// Determine if the IN list should be combined into a single tuple literal
// Only combine strings into tuple for small lists (up to 10 items)
const maxStringTupleSizeWithAlias = 10
canBeTupleLiteral := false
if n.Query == nil && len(n.List) > 1 {
allNumeric := true
allStrings := true
allTuples := true
allTuplesArePrimitive := true
for _, item := range n.List {
if lit, ok := item.(*ast.Literal); ok {
if lit.Type != ast.LiteralInteger && lit.Type != ast.LiteralFloat {
allNumeric = false
}
if lit.Type != ast.LiteralString {
allStrings = false
}
if lit.Type != ast.LiteralTuple {
allTuples = false
} else {
if !containsOnlyPrimitiveLiterals(lit) {
allTuplesArePrimitive = false
}
}
} else if isNumericExpr(item) {
allStrings = false
allTuples = false
} else {
allNumeric = false
allStrings = false
allTuples = false
break
}
}
canBeTupleLiteral = allNumeric || (allStrings && len(n.List) <= maxStringTupleSizeWithAlias) || (allTuples && allTuplesArePrimitive)
}
// Count arguments
argCount := 1
if n.Query != nil {
argCount++
} else if canBeTupleLiteral {
argCount++
} else {
if len(n.List) == 1 {
if lit, ok := n.List[0].(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
argCount++
} else {
argCount += len(n.List)
}
} else {
// Check if all items are string literals (large list case - no wrapper)
allStringLiterals := true
for _, item := range n.List {
if lit, ok := item.(*ast.Literal); !ok || lit.Type != ast.LiteralString {
allStringLiterals = false
break
}
}
if allStringLiterals {
// Large string list - separate children
argCount += len(n.List)
} else {
// Non-string items get wrapped in a single Function tuple
argCount++
}
}
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, argCount)
Node(sb, n.Expr, depth+2)
if n.Query != nil {
fmt.Fprintf(sb, "%s Subquery (children %d)\n", indent, 1)
Node(sb, n.Query, depth+3)
} else if canBeTupleLiteral {
tupleLit := &ast.Literal{
Type: ast.LiteralTuple,
Value: n.List,
}
fmt.Fprintf(sb, "%s Literal %s\n", indent, FormatLiteral(tupleLit))
} else if len(n.List) == 1 {
if lit, ok := n.List[0].(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
Node(sb, n.List[0], depth+4)
} else {
Node(sb, n.List[0], depth+2)
}
} else {
// Check if all items are tuple literals (some may have expressions)
allTuples := true
for _, item := range n.List {
if lit, ok := item.(*ast.Literal); !ok || lit.Type != ast.LiteralTuple {
allTuples = false
break
}
}
if allTuples {
// Wrap all tuples in Function tuple
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.List))
for _, item := range n.List {
explainTupleInInList(sb, item.(*ast.Literal), indent+" ", depth+4)
}
} else {
// Check if all items are string literals (large list case)
allStringLiterals := true
for _, item := range n.List {
if lit, ok := item.(*ast.Literal); !ok || lit.Type != ast.LiteralString {
allStringLiterals = false
break
}
}
if allStringLiterals {
// Large string list - output as separate children (no tuple wrapper)
for _, item := range n.List {
Node(sb, item, depth+2)
}
} else {
// Wrap non-literal/non-tuple list items in Function tuple
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.List))
for _, item := range n.List {
Node(sb, item, depth+4)
}
}
}
}
}
func explainTernaryExpr(sb *strings.Builder, n *ast.TernaryExpr, indent string, depth int) {
// Ternary is represented as Function if with 3 arguments
fmt.Fprintf(sb, "%sFunction if (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 3)
Node(sb, n.Condition, depth+2)
Node(sb, n.Then, depth+2)
Node(sb, n.Else, depth+2)
}
func explainArrayAccess(sb *strings.Builder, n *ast.ArrayAccess, indent string, depth int) {
// Array access is represented as Function arrayElement
fmt.Fprintf(sb, "%sFunction arrayElement (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, n.Array, depth+2)
Node(sb, n.Index, depth+2)
}
func explainArrayAccessWithAlias(sb *strings.Builder, n *ast.ArrayAccess, alias string, indent string, depth int) {
// Array access is represented as Function arrayElement
if alias != "" {
fmt.Fprintf(sb, "%sFunction arrayElement (alias %s) (children %d)\n", indent, alias, 1)
} else {
fmt.Fprintf(sb, "%sFunction arrayElement (children %d)\n", indent, 1)
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, n.Array, depth+2)
Node(sb, n.Index, depth+2)
}
func explainTupleAccess(sb *strings.Builder, n *ast.TupleAccess, indent string, depth int) {
// Tuple access is represented as Function tupleElement
fmt.Fprintf(sb, "%sFunction tupleElement (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, n.Tuple, depth+2)
Node(sb, n.Index, depth+2)
}
func explainTupleAccessWithAlias(sb *strings.Builder, n *ast.TupleAccess, alias string, indent string, depth int) {
// Tuple access is represented as Function tupleElement
if alias != "" {
fmt.Fprintf(sb, "%sFunction tupleElement (alias %s) (children %d)\n", indent, alias, 1)
} else {
fmt.Fprintf(sb, "%sFunction tupleElement (children %d)\n", indent, 1)
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, n.Tuple, depth+2)
Node(sb, n.Index, depth+2)
}
func explainLikeExpr(sb *strings.Builder, n *ast.LikeExpr, indent string, depth int) {
// LIKE is represented as Function like
fnName := "like"
if n.CaseInsensitive {
fnName = "ilike"
}
if n.Not {
fnName = "not" + strings.Title(fnName)
}
if n.Alias != "" {
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, n.Alias, 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, n.Expr, depth+2)
Node(sb, n.Pattern, depth+2)
}
func explainBetweenExpr(sb *strings.Builder, n *ast.BetweenExpr, indent string, depth int) {
if n.Not {
// NOT BETWEEN is transformed to: expr < low OR expr > high
// Represented as: Function or with two comparisons: less and greater
fmt.Fprintf(sb, "%sFunction or (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
// less(expr, low)
fmt.Fprintf(sb, "%s Function less (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, n.Expr, depth+4)
Node(sb, n.Low, depth+4)
// greater(expr, high)
fmt.Fprintf(sb, "%s Function greater (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, n.Expr, depth+4)
Node(sb, n.High, depth+4)
} else {
// BETWEEN is represented as Function and with two comparisons
// expr >= low AND expr <= high
fmt.Fprintf(sb, "%sFunction and (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
// greaterOrEquals(expr, low)
fmt.Fprintf(sb, "%s Function greaterOrEquals (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, n.Expr, depth+4)
Node(sb, n.Low, depth+4)
// lessOrEquals(expr, high)
fmt.Fprintf(sb, "%s Function lessOrEquals (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 2)
Node(sb, n.Expr, depth+4)
Node(sb, n.High, depth+4)
}
}
func explainIsNullExpr(sb *strings.Builder, n *ast.IsNullExpr, indent string, depth int) {
// IS NULL is represented as Function isNull
fnName := "isNull"
if n.Not {
fnName = "isNotNull"
}
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
Node(sb, n.Expr, depth+2)
}
func explainCaseExpr(sb *strings.Builder, n *ast.CaseExpr, indent string, depth int) {
// Only output alias if it's unquoted (ClickHouse doesn't show quoted aliases)
alias := ""
if n.Alias != "" && !n.QuotedAlias {
alias = n.Alias
}
explainCaseExprWithAlias(sb, n, alias, indent, depth)
}
func explainCaseExprWithAlias(sb *strings.Builder, n *ast.CaseExpr, alias string, indent string, depth int) {
// CASE is represented as Function multiIf or caseWithExpression
if n.Operand != nil {
// CASE x WHEN ... form
argCount := 1 + len(n.Whens)*2 // operand + (condition, result) pairs
if n.Else != nil {
argCount++
}
if alias != "" {
fmt.Fprintf(sb, "%sFunction caseWithExpression (alias %s) (children %d)\n", indent, alias, 1)
} else {
fmt.Fprintf(sb, "%sFunction caseWithExpression (children %d)\n", indent, 1)
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, argCount)
Node(sb, n.Operand, depth+2)
for _, w := range n.Whens {
Node(sb, w.Condition, depth+2)
Node(sb, w.Result, depth+2)
}
if n.Else != nil {
Node(sb, n.Else, depth+2)
}
} else {
// CASE WHEN ... form
argCount := len(n.Whens) * 2
if n.Else != nil {
argCount++
}
if alias != "" {
fmt.Fprintf(sb, "%sFunction multiIf (alias %s) (children %d)\n", indent, alias, 1)
} else {
fmt.Fprintf(sb, "%sFunction multiIf (children %d)\n", indent, 1)
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, argCount)
for _, w := range n.Whens {
Node(sb, w.Condition, depth+2)
Node(sb, w.Result, depth+2)
}
if n.Else != nil {
Node(sb, n.Else, depth+2)
}
}
}
func explainIntervalExpr(sb *strings.Builder, n *ast.IntervalExpr, alias string, indent string, depth int) {
// INTERVAL is represented as Function toInterval<Unit>
// Unit needs to be title-cased (e.g., YEAR -> Year)
unit := n.Unit
if len(unit) > 0 {
unit = strings.ToUpper(unit[:1]) + strings.ToLower(unit[1:])
}
fnName := "toInterval" + unit
if alias != "" {
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, alias, 1)
} else {
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
}
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
Node(sb, n.Value, depth+2)
}
func explainExistsExpr(sb *strings.Builder, n *ast.ExistsExpr, indent string, depth int) {
// EXISTS is represented as Function exists
fmt.Fprintf(sb, "%sFunction exists (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s Subquery (children %d)\n", indent, 1)
Node(sb, n.Query, depth+3)
}
func explainExtractExpr(sb *strings.Builder, n *ast.ExtractExpr, indent string, depth int) {
// EXTRACT is represented as Function toYear, toMonth, etc.
// ClickHouse uses specific function names for date/time extraction
fnName := extractFieldToFunction(n.Field)
fmt.Fprintf(sb, "%sFunction %s (children %d)\n", indent, fnName, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
Node(sb, n.From, depth+2)
}
// extractFieldToFunction maps EXTRACT field names to ClickHouse function names
func extractFieldToFunction(field string) string {
switch strings.ToUpper(field) {
case "DAY":
return "toDayOfMonth"
case "MONTH":
return "toMonth"
case "YEAR":
return "toYear"
case "SECOND":
return "toSecond"
case "MINUTE":
return "toMinute"
case "HOUR":
return "toHour"
case "QUARTER":
return "toQuarter"
case "WEEK":
return "toWeek"
default:
// Fallback to generic "to" + TitleCase(field)
return "to" + strings.Title(strings.ToLower(field))
}
}
func explainWindowSpec(sb *strings.Builder, n *ast.WindowSpec, indent string, depth int) {
// Window spec is represented as WindowDefinition
// For simple cases like OVER (), just output WindowDefinition without children
children := 0
if n.Name != "" {
children++
}
if len(n.PartitionBy) > 0 {
children++
}
if len(n.OrderBy) > 0 {
children++
}
// Count frame offset as child if present
if n.Frame != nil && n.Frame.StartBound != nil && n.Frame.StartBound.Offset != nil {
children++
}
if children > 0 {
fmt.Fprintf(sb, "%sWindowDefinition (children %d)\n", indent, children)
if n.Name != "" {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Name)
}
if len(n.PartitionBy) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.PartitionBy))
for _, e := range n.PartitionBy {
Node(sb, e, depth+2)
}
}
if len(n.OrderBy) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.OrderBy))
for _, o := range n.OrderBy {
explainOrderByElement(sb, o, strings.Repeat(" ", depth+2), depth+2)
}
}
// Frame start offset
if n.Frame != nil && n.Frame.StartBound != nil && n.Frame.StartBound.Offset != nil {
Node(sb, n.Frame.StartBound.Offset, depth+1)
}
} else {
fmt.Fprintf(sb, "%sWindowDefinition\n", indent)
}
}