-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpressions.go
More file actions
512 lines (485 loc) · 16.9 KB
/
Copy pathexpressions.go
File metadata and controls
512 lines (485 loc) · 16.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
package explain
import (
"fmt"
"strings"
"github.com/sqlc-dev/doubleclick/ast"
)
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, 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
}
hasComplexExpr := false
for _, e := range exprs {
// Simple literals (numbers, strings, etc.) are OK
if lit, isLit := e.(*ast.Literal); isLit {
// Nested tuples/arrays are complex
if lit.Type == ast.LiteralTuple || 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 that all have exactly 1 element (homogeneous single-element arrays) OR
// 4. Contains nested arrays with non-literal expressions OR
// 5. Contains nested arrays that are empty or contain tuples/non-literals
shouldUseFunctionArray := false
allAreSingleElementArrays := true
hasNestedArrays := false
nestedArraysNeedFunctionFormat := false
for _, e := range exprs {
if lit, ok := e.(*ast.Literal); ok {
if lit.Type == ast.LiteralArray {
hasNestedArrays = true
// Check if this inner array has exactly 1 element
if innerExprs, ok := lit.Value.([]ast.Expression); ok {
if len(innerExprs) != 1 {
allAreSingleElementArrays = false
}
// Check if inner array needs Function array format:
// - Contains non-literal expressions OR
// - Contains tuples OR
// - Is empty OR
// - Contains empty arrays
if containsNonLiteralExpressions(innerExprs) ||
len(innerExprs) == 0 ||
containsTuples(innerExprs) ||
containsEmptyArrays(innerExprs) {
nestedArraysNeedFunctionFormat = true
}
} else {
allAreSingleElementArrays = false
}
} else if lit.Type == ast.LiteralTuple {
// Tuples are complex
shouldUseFunctionArray = true
}
} else if !isSimpleLiteralOrNegation(e) {
shouldUseFunctionArray = true
}
}
// Use Function array when:
// - nested arrays that are ALL single-element
// - nested arrays that need Function format (contain non-literals, tuples, or empty arrays)
if hasNestedArrays && (allAreSingleElementArrays || nestedArraysNeedFunctionFormat) {
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 {
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
}
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
}
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
}
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:
fmt.Fprintf(sb, "%sLiteral Int64_%d\n", indent, -val)
return
case uint64:
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, n.Alias, children)
} else {
fmt.Fprintf(sb, "%sSubquery (children %d)\n", indent, children)
}
Node(sb, n.Query, depth+1)
}
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 {
hasComplexExpr := false
for _, expr := range exprs {
if _, isLit := expr.(*ast.Literal); !isLit {
hasComplexExpr = true
break
}
}
if hasComplexExpr {
// Render as Function tuple with alias
fmt.Fprintf(sb, "%sFunction tuple (alias %s) (children %d)\n", indent, n.Alias, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(exprs))
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
// 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 identifiers - use Function array
if _, ok := expr.(*ast.Identifier); ok {
needsFunctionFormat = true
break
}
}
if needsFunctionFormat {
// Render as Function array with alias
fmt.Fprintf(sb, "%sFunction array (alias %s) (children %d)\n", indent, 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), 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, 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, 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:
// Unary expressions become functions with alias
fnName := UnaryOperatorToFunction(e.Op)
fmt.Fprintf(sb, "%sFunction %s (alias %s) (children %d)\n", indent, fnName, 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.Identifier:
// Identifiers with alias
fmt.Fprintf(sb, "%sIdentifier %s (alias %s)\n", indent, e.Name(), 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, 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 - show alias only for CAST(x AS Type) syntax, not CAST(x, 'Type')
if e.UsedASSyntax {
explainCastExprWithAlias(sb, e, n.Alias, indent, depth)
} else {
explainCastExpr(sb, e, indent, depth)
}
case *ast.ArrayAccess:
// Array access - ClickHouse doesn't show aliases on arrayElement in EXPLAIN AST
explainArrayAccess(sb, e, indent, depth)
case *ast.TupleAccess:
// Tuple access - ClickHouse doesn't show aliases on tupleElement in EXPLAIN AST
explainTupleAccess(sb, e, 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)
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) {
if n.Table != "" {
fmt.Fprintf(sb, "%sQualifiedAsterisk (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
} else {
fmt.Fprintf(sb, "%sAsterisk\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:
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.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 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:
if n.Name != "" {
fmt.Fprintf(sb, "%sSubquery (alias %s) (children %d)\n", indent, n.Name, 1)
} else {
fmt.Fprintf(sb, "%sSubquery (children %d)\n", indent, 1)
}
Node(sb, e.Query, depth+1)
case *ast.CastExpr:
explainCastExprWithAlias(sb, e, n.Name, indent, depth)
default:
// For other types, just output the expression (alias may be lost)
Node(sb, n.Query, depth)
}
}