-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexplain.go
More file actions
234 lines (214 loc) · 6.81 KB
/
explain.go
File metadata and controls
234 lines (214 loc) · 6.81 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
// Package explain provides EXPLAIN AST output functionality for ClickHouse SQL.
package explain
import (
"fmt"
"strings"
"github.com/sqlc-dev/doubleclick/ast"
)
// inSubqueryContext is a package-level flag to track when we're inside a Subquery
// This affects how negated literals with aliases are formatted
var inSubqueryContext bool
// Explain returns the EXPLAIN AST output for a statement, matching ClickHouse's format.
func Explain(stmt ast.Statement) string {
var sb strings.Builder
Node(&sb, stmt, 0)
return sb.String()
}
// Node writes the EXPLAIN AST output for an AST node.
func Node(sb *strings.Builder, node interface{}, depth int) {
if node == nil {
// nil can represent an empty tuple in function arguments
indent := strings.Repeat(" ", depth)
fmt.Fprintf(sb, "%sFunction tuple (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
return
}
indent := strings.Repeat(" ", depth)
switch n := node.(type) {
// Select statements
case *ast.SelectWithUnionQuery:
explainSelectWithUnionQuery(sb, n, indent, depth)
case *ast.SelectIntersectExceptQuery:
explainSelectIntersectExceptQuery(sb, n, indent, depth)
case *ast.SelectQuery:
explainSelectQuery(sb, n, indent, depth)
// Tables
case *ast.TablesInSelectQuery:
explainTablesInSelectQuery(sb, n, indent, depth)
case *ast.TablesInSelectQueryElement:
explainTablesInSelectQueryElement(sb, n, indent, depth)
case *ast.TableExpression:
explainTableExpression(sb, n, indent, depth)
case *ast.TableIdentifier:
explainTableIdentifier(sb, n, indent)
case *ast.ArrayJoinClause:
explainArrayJoinClause(sb, n, indent, depth)
case *ast.TableJoin:
explainTableJoin(sb, n, indent, depth)
// Expressions
case *ast.OrderByElement:
explainOrderByElement(sb, n, indent, depth)
case *ast.Identifier:
explainIdentifier(sb, n, indent)
case *ast.Literal:
explainLiteral(sb, n, indent, depth)
case *ast.BinaryExpr:
explainBinaryExpr(sb, n, indent, depth)
case *ast.UnaryExpr:
explainUnaryExpr(sb, n, indent, depth)
case *ast.Subquery:
explainSubquery(sb, n, indent, depth)
case *ast.AliasedExpr:
explainAliasedExpr(sb, n, depth)
case *ast.WithElement:
explainWithElement(sb, n, indent, depth)
case *ast.Asterisk:
explainAsterisk(sb, n, indent, depth)
case *ast.ColumnsMatcher:
fmt.Fprintf(sb, "%sColumnsRegexpMatcher\n", indent)
// Functions
case *ast.FunctionCall:
explainFunctionCall(sb, n, indent, depth)
case *ast.Lambda:
explainLambda(sb, n, indent, depth)
case *ast.CastExpr:
explainCastExpr(sb, n, indent, depth)
case *ast.InExpr:
explainInExpr(sb, n, indent, depth)
case *ast.TernaryExpr:
explainTernaryExpr(sb, n, indent, depth)
case *ast.ArrayAccess:
explainArrayAccess(sb, n, indent, depth)
case *ast.TupleAccess:
explainTupleAccess(sb, n, indent, depth)
case *ast.LikeExpr:
explainLikeExpr(sb, n, indent, depth)
case *ast.BetweenExpr:
explainBetweenExpr(sb, n, indent, depth)
case *ast.IsNullExpr:
explainIsNullExpr(sb, n, indent, depth)
case *ast.CaseExpr:
explainCaseExpr(sb, n, indent, depth)
case *ast.IntervalExpr:
explainIntervalExpr(sb, n, "", indent, depth)
case *ast.ExistsExpr:
explainExistsExpr(sb, n, indent, depth)
case *ast.ExtractExpr:
explainExtractExpr(sb, n, indent, depth)
// DDL statements
case *ast.InsertQuery:
explainInsertQuery(sb, n, indent, depth)
case *ast.CreateQuery:
explainCreateQuery(sb, n, indent, depth)
case *ast.DropQuery:
explainDropQuery(sb, n, indent, depth)
case *ast.RenameQuery:
explainRenameQuery(sb, n, indent, depth)
case *ast.SetQuery:
explainSetQuery(sb, indent)
case *ast.SystemQuery:
explainSystemQuery(sb, indent)
case *ast.ExplainQuery:
explainExplainQuery(sb, n, indent, depth)
case *ast.ShowQuery:
explainShowQuery(sb, n, indent)
case *ast.ShowPrivilegesQuery:
fmt.Fprintf(sb, "%sShowPrivilegesQuery\n", indent)
case *ast.ShowCreateQuotaQuery:
fmt.Fprintf(sb, "%sSHOW CREATE QUOTA query\n", indent)
case *ast.UseQuery:
explainUseQuery(sb, n, indent)
case *ast.DescribeQuery:
explainDescribeQuery(sb, n, indent)
case *ast.ExistsQuery:
explainExistsTableQuery(sb, n, indent)
case *ast.DetachQuery:
explainDetachQuery(sb, n, indent)
case *ast.AttachQuery:
explainAttachQuery(sb, n, indent)
case *ast.AlterQuery:
explainAlterQuery(sb, n, indent, depth)
case *ast.OptimizeQuery:
explainOptimizeQuery(sb, n, indent)
case *ast.TruncateQuery:
explainTruncateQuery(sb, n, indent)
// Types
case *ast.DataType:
explainDataType(sb, n, indent, depth)
case *ast.NameTypePair:
explainNameTypePair(sb, n, indent, depth)
case *ast.Parameter:
explainParameter(sb, n, indent)
default:
// For unhandled types, just print the type name
fmt.Fprintf(sb, "%s%T\n", indent, node)
}
}
// TablesWithArrayJoin handles FROM and ARRAY JOIN together as TablesInSelectQuery
func TablesWithArrayJoin(sb *strings.Builder, from *ast.TablesInSelectQuery, arrayJoin *ast.ArrayJoinClause, depth int) {
indent := strings.Repeat(" ", depth)
tableCount := 0
if from != nil {
tableCount = len(from.Tables)
}
if arrayJoin != nil {
tableCount++
}
fmt.Fprintf(sb, "%sTablesInSelectQuery (children %d)\n", indent, tableCount)
if from != nil {
for _, t := range from.Tables {
Node(sb, t, depth+1)
}
}
if arrayJoin != nil {
// ARRAY JOIN is wrapped in TablesInSelectQueryElement
fmt.Fprintf(sb, "%s TablesInSelectQueryElement (children %d)\n", indent, 1)
Node(sb, arrayJoin, depth+2)
}
}
// Column handles column declarations
func Column(sb *strings.Builder, col *ast.ColumnDeclaration, depth int) {
indent := strings.Repeat(" ", depth)
children := 0
if col.Type != nil {
children++
}
// EPHEMERAL columns without explicit default get defaultValueOfTypeName
hasEphemeralDefault := col.DefaultKind == "EPHEMERAL" && col.Default == nil
if col.Default != nil || hasEphemeralDefault {
children++
}
fmt.Fprintf(sb, "%sColumnDeclaration %s (children %d)\n", indent, col.Name, children)
if col.Type != nil {
Node(sb, col.Type, depth+1)
}
if col.Default != nil {
Node(sb, col.Default, depth+1)
} else if hasEphemeralDefault {
// EPHEMERAL columns without explicit default value show defaultValueOfTypeName function
fmt.Fprintf(sb, "%s Function defaultValueOfTypeName\n", indent)
}
}
func Index(sb *strings.Builder, idx *ast.IndexDefinition, depth int) {
indent := strings.Repeat(" ", depth)
children := 0
if idx.Expression != nil {
children++
}
if idx.Type != nil {
children++
}
fmt.Fprintf(sb, "%sIndex (children %d)\n", indent, children)
if idx.Expression != nil {
// Expression is typically an identifier
if ident, ok := idx.Expression.(*ast.Identifier); ok {
fmt.Fprintf(sb, "%s Identifier %s\n", indent, ident.Name())
} else {
Node(sb, idx.Expression, depth+1)
}
}
if idx.Type != nil {
// Type is a function like minmax, bloom_filter, etc.
explainFunctionCall(sb, idx.Type, indent+" ", depth+1)
}
}