-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselect.go
More file actions
223 lines (216 loc) · 5.45 KB
/
select.go
File metadata and controls
223 lines (216 loc) · 5.45 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
package explain
import (
"fmt"
"strings"
"github.com/sqlc-dev/doubleclick/ast"
)
func explainSelectWithUnionQuery(sb *strings.Builder, n *ast.SelectWithUnionQuery, indent string, depth int) {
children := countSelectUnionChildren(n)
fmt.Fprintf(sb, "%sSelectWithUnionQuery (children %d)\n", indent, children)
// Wrap selects in ExpressionList
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Selects))
for _, sel := range n.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
var hasFormat bool
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && sq.Format != nil {
Node(sb, sq.Format, depth+1)
hasFormat = true
break
}
}
// When FORMAT is present, SETTINGS is output at SelectWithUnionQuery level
if hasFormat {
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && 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)
}
// SETTINGS - output here if there's no FORMAT, otherwise it's at SelectWithUnionQuery level
if len(n.Settings) > 0 && n.Format == nil {
fmt.Fprintf(sb, "%s Set\n", indent)
}
}
func explainOrderByElement(sb *strings.Builder, n *ast.OrderByElement, indent string, depth int) {
children := 1 // expression
if n.WithFill {
children++ // FillModifier
}
fmt.Fprintf(sb, "%sOrderByElement (children %d)\n", indent, children)
Node(sb, n.Expression, depth+1)
if n.WithFill {
fillChildren := 0
if n.FillFrom != nil {
fillChildren++
}
if n.FillTo != nil {
fillChildren++
}
if n.FillStep != nil {
fillChildren++
}
if fillChildren > 0 {
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 {
fmt.Fprintf(sb, "%s FillModifier\n", indent)
}
}
}
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
var hasFormat bool
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && sq.Format != nil {
count++
hasFormat = true
break
}
}
// When FORMAT is present, SETTINGS is counted at this level
if hasFormat {
for _, sel := range n.Selects {
if sq, ok := sel.(*ast.SelectQuery); ok && len(sq.Settings) > 0 {
count++
break
}
}
}
return count
}
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 n.Offset != nil {
count++
}
// SETTINGS is counted here only if there's no FORMAT
// If FORMAT is present, SETTINGS is at SelectWithUnionQuery level
if len(n.Settings) > 0 && n.Format == nil {
count++
}
return count
}