-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselect.go
More file actions
302 lines (289 loc) · 8.19 KB
/
select.go
File metadata and controls
302 lines (289 loc) · 8.19 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
package explain
import (
"fmt"
"strings"
"github.com/sqlc-dev/doubleclick/ast"
)
func explainSelectIntersectExceptQuery(sb *strings.Builder, n *ast.SelectIntersectExceptQuery, indent string, depth int) {
fmt.Fprintf(sb, "%sSelectIntersectExceptQuery (children %d)\n", indent, len(n.Selects))
for _, sel := range n.Selects {
Node(sb, sel, depth+1)
}
}
func explainSelectWithUnionQuery(sb *strings.Builder, n *ast.SelectWithUnionQuery, indent string, depth int) {
if n == nil {
return
}
children := countSelectUnionChildren(n)
fmt.Fprintf(sb, "%sSelectWithUnionQuery (children %d)\n", indent, children)
// ClickHouse optimizes UNION ALL when selects have identical expressions but different aliases.
// In that case, only the first SELECT is shown since column names come from the first SELECT anyway.
selects := simplifyUnionSelects(n.Selects)
// Wrap selects in ExpressionList
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(selects))
for _, sel := range selects {
Node(sb, sel, depth+2)
}
// INTO OUTFILE clause - check if any SelectQuery has IntoOutfile set
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && sq.IntoOutfile != nil {
fmt.Fprintf(sb, "%s Literal \\'%s\\'\n", indent, sq.IntoOutfile.Filename)
break
}
}
// FORMAT clause - check if any SelectQuery has Format set
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && sq.Format != nil {
Node(sb, sq.Format, depth+1)
break
}
}
// When SETTINGS comes AFTER FORMAT, it's output at SelectWithUnionQuery level
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && sq.SettingsAfterFormat && len(sq.Settings) > 0 {
fmt.Fprintf(sb, "%s Set\n", indent)
break
}
}
}
func explainSelectQuery(sb *strings.Builder, n *ast.SelectQuery, indent string, depth int) {
children := countSelectQueryChildren(n)
fmt.Fprintf(sb, "%sSelectQuery (children %d)\n", indent, children)
// WITH clause (ExpressionList) - output before columns
if len(n.With) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.With))
for _, w := range n.With {
Node(sb, w, depth+2)
}
}
// Columns (ExpressionList)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Columns))
for _, col := range n.Columns {
Node(sb, col, depth+2)
}
// FROM (including ARRAY JOIN as part of TablesInSelectQuery)
if n.From != nil || n.ArrayJoin != nil {
TablesWithArrayJoin(sb, n.From, n.ArrayJoin, depth+1)
}
// PREWHERE
if n.PreWhere != nil {
Node(sb, n.PreWhere, depth+1)
}
// WHERE
if n.Where != nil {
Node(sb, n.Where, depth+1)
}
// GROUP BY
if len(n.GroupBy) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.GroupBy))
for _, g := range n.GroupBy {
Node(sb, g, depth+2)
}
}
// HAVING
if n.Having != nil {
Node(sb, n.Having, depth+1)
}
// QUALIFY
if n.Qualify != nil {
Node(sb, n.Qualify, depth+1)
}
// WINDOW clause (named window definitions)
if len(n.Window) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Window))
for range n.Window {
fmt.Fprintf(sb, "%s WindowListElement\n", indent)
}
}
// ORDER BY
if len(n.OrderBy) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.OrderBy))
for _, o := range n.OrderBy {
Node(sb, o, depth+2)
}
}
// OFFSET (ClickHouse outputs offset before limit in EXPLAIN AST)
if n.Offset != nil {
Node(sb, n.Offset, depth+1)
}
// LIMIT
if n.Limit != nil {
Node(sb, n.Limit, depth+1)
}
// LIMIT BY - only output when there's no ORDER BY and no second LIMIT (matches ClickHouse behavior)
if len(n.LimitBy) > 0 && len(n.OrderBy) == 0 && !n.LimitByHasLimit {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.LimitBy))
for _, expr := range n.LimitBy {
Node(sb, expr, depth+2)
}
}
// SETTINGS is output at SelectQuery level only when NOT after FORMAT
// When SettingsAfterFormat is true, it's output at SelectWithUnionQuery level instead
if len(n.Settings) > 0 && !n.SettingsAfterFormat {
fmt.Fprintf(sb, "%s Set\n", indent)
}
}
func explainOrderByElement(sb *strings.Builder, n *ast.OrderByElement, indent string, depth int) {
// ClickHouse uses different formats for WITH FILL:
// - When FROM/TO are simple literals: direct children on OrderByElement
// - When FROM/TO are complex expressions or only STEP present: uses FillModifier wrapper
hasFromOrTo := n.FillFrom != nil || n.FillTo != nil
hasComplexFillExpr := hasFromOrTo && (isComplexExpr(n.FillFrom) || isComplexExpr(n.FillTo))
// Use FillModifier when:
// 1. Only STEP is present (no FROM/TO), or
// 2. FROM/TO contain complex expressions (not simple literals)
useFillModifier := n.WithFill && ((n.FillStep != nil && !hasFromOrTo) || hasComplexFillExpr)
if useFillModifier {
// Use FillModifier wrapper
fillChildren := 0
if n.FillFrom != nil {
fillChildren++
}
if n.FillTo != nil {
fillChildren++
}
if n.FillStep != nil {
fillChildren++
}
children := 2 // expression + FillModifier
fmt.Fprintf(sb, "%sOrderByElement (children %d)\n", indent, children)
Node(sb, n.Expression, depth+1)
fmt.Fprintf(sb, "%s FillModifier (children %d)\n", indent, fillChildren)
if n.FillFrom != nil {
Node(sb, n.FillFrom, depth+2)
}
if n.FillTo != nil {
Node(sb, n.FillTo, depth+2)
}
if n.FillStep != nil {
Node(sb, n.FillStep, depth+2)
}
} else {
// Use direct children for simple literal FROM/TO cases
children := 1 // expression
if n.FillFrom != nil {
children++
}
if n.FillTo != nil {
children++
}
if n.FillStep != nil {
children++
}
if n.Collate != "" {
children++
}
fmt.Fprintf(sb, "%sOrderByElement (children %d)\n", indent, children)
Node(sb, n.Expression, depth+1)
if n.FillFrom != nil {
Node(sb, n.FillFrom, depth+1)
}
if n.FillTo != nil {
Node(sb, n.FillTo, depth+1)
}
if n.FillStep != nil {
Node(sb, n.FillStep, depth+1)
}
if n.Collate != "" {
// COLLATE is output as a string literal
fmt.Fprintf(sb, "%s Literal \\'%s\\'\n", indent, n.Collate)
}
}
}
// isComplexExpr checks if an expression is complex (not a simple literal)
func isComplexExpr(expr ast.Expression) bool {
if expr == nil {
return false
}
switch expr.(type) {
case *ast.Literal:
return false
default:
return true
}
}
// hasOnlyLiterals checks if all expressions in a slice are literals
func hasOnlyLiterals(exprs []ast.Expression) bool {
for _, expr := range exprs {
if _, ok := expr.(*ast.Literal); !ok {
return false
}
}
return true
}
func countSelectUnionChildren(n *ast.SelectWithUnionQuery) int {
count := 1 // ExpressionList of selects
// Check if any SelectQuery has IntoOutfile set
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && sq.IntoOutfile != nil {
count++
break
}
}
// Check if any SelectQuery has Format set
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && sq.Format != nil {
count++
break
}
}
// When SETTINGS comes AFTER FORMAT, it's counted at SelectWithUnionQuery level
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && sq.SettingsAfterFormat && len(sq.Settings) > 0 {
count++
break
}
}
return count
}
// simplifyUnionSelects returns all SELECT statements in a UNION.
// ClickHouse does not simplify UNION ALL queries in EXPLAIN AST output.
func simplifyUnionSelects(selects []ast.Statement) []ast.Statement {
return selects
}
func countSelectQueryChildren(n *ast.SelectQuery) int {
count := 1 // columns ExpressionList
// WITH clause
if len(n.With) > 0 {
count++
}
// FROM and ARRAY JOIN together count as one child (TablesInSelectQuery)
if n.From != nil || n.ArrayJoin != nil {
count++
}
if n.PreWhere != nil {
count++
}
if n.Where != nil {
count++
}
if len(n.GroupBy) > 0 {
count++
}
if n.Having != nil {
count++
}
if n.Qualify != nil {
count++
}
if len(n.Window) > 0 {
count++
}
if len(n.OrderBy) > 0 {
count++
}
if n.Limit != nil {
count++
}
if len(n.LimitBy) > 0 && len(n.OrderBy) == 0 && !n.LimitByHasLimit {
count++
}
if n.Offset != nil {
count++
}
// SETTINGS is counted at SelectQuery level only when NOT after FORMAT
if len(n.Settings) > 0 && !n.SettingsAfterFormat {
count++
}
return count
}