-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtables.go
More file actions
127 lines (117 loc) · 4.16 KB
/
tables.go
File metadata and controls
127 lines (117 loc) · 4.16 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
package explain
import (
"fmt"
"strings"
"github.com/sqlc-dev/doubleclick/ast"
)
func explainTablesInSelectQuery(sb *strings.Builder, n *ast.TablesInSelectQuery, indent string, depth int) {
fmt.Fprintf(sb, "%sTablesInSelectQuery (children %d)\n", indent, len(n.Tables))
for _, t := range n.Tables {
Node(sb, t, depth+1)
}
}
func explainTablesInSelectQueryElement(sb *strings.Builder, n *ast.TablesInSelectQueryElement, indent string, depth int) {
children := 1 // table
if n.Join != nil {
children++
}
fmt.Fprintf(sb, "%sTablesInSelectQueryElement (children %d)\n", indent, children)
if n.Table != nil {
Node(sb, n.Table, depth+1)
}
if n.Join != nil {
Node(sb, n.Join, depth+1)
}
}
func explainTableExpression(sb *strings.Builder, n *ast.TableExpression, indent string, depth int) {
children := 1 // table
fmt.Fprintf(sb, "%sTableExpression (children %d)\n", indent, children)
// If there's a subquery with an alias, pass the alias to the subquery output
if subq, ok := n.Table.(*ast.Subquery); ok {
// Check if subquery contains an EXPLAIN query - convert to viewExplain function
if explainQ, ok := subq.Query.(*ast.ExplainQuery); ok {
explainViewExplain(sb, explainQ, n.Alias, indent+" ", depth+1)
} else if n.Alias != "" {
fmt.Fprintf(sb, "%s Subquery (alias %s) (children %d)\n", indent, n.Alias, 1)
Node(sb, subq.Query, depth+2)
} else {
Node(sb, n.Table, depth+1)
}
} else if fn, ok := n.Table.(*ast.FunctionCall); ok && n.Alias != "" {
// Table function with alias
explainFunctionCallWithAlias(sb, fn, n.Alias, indent+" ", depth+1)
} else if ti, ok := n.Table.(*ast.TableIdentifier); ok && n.Alias != "" {
// Table identifier with alias
explainTableIdentifierWithAlias(sb, ti, n.Alias, indent+" ")
} else {
Node(sb, n.Table, depth+1)
}
}
// explainViewExplain handles EXPLAIN queries used as table sources, converting to viewExplain function
func explainViewExplain(sb *strings.Builder, n *ast.ExplainQuery, alias string, indent string, depth int) {
// When EXPLAIN is used as a table source, it becomes viewExplain function
// Arguments: 'EXPLAIN', 'options', subquery
fmt.Fprintf(sb, "%sFunction viewExplain (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 3)
// First argument: 'EXPLAIN' literal
fmt.Fprintf(sb, "%s Literal \\'EXPLAIN\\'\n", indent)
// Second argument: options string (empty for now since we don't track detailed options)
options := string(n.ExplainType)
if options == "PLAN" {
options = ""
}
fmt.Fprintf(sb, "%s Literal \\'%s\\'\n", indent, options)
// Third argument: the subquery being explained
fmt.Fprintf(sb, "%s Subquery (children %d)\n", indent, 1)
Node(sb, n.Statement, depth+3)
}
func explainTableIdentifierWithAlias(sb *strings.Builder, n *ast.TableIdentifier, alias string, indent string) {
name := n.Table
if n.Database != "" {
name = n.Database + "." + n.Table
}
fmt.Fprintf(sb, "%sTableIdentifier %s (alias %s)\n", indent, name, alias)
}
func explainTableIdentifier(sb *strings.Builder, n *ast.TableIdentifier, indent string) {
name := n.Table
if n.Database != "" {
name = n.Database + "." + n.Table
}
fmt.Fprintf(sb, "%sTableIdentifier %s\n", indent, name)
}
func explainArrayJoinClause(sb *strings.Builder, n *ast.ArrayJoinClause, indent string, depth int) {
fmt.Fprintf(sb, "%sArrayJoin (children %d)\n", indent, 1)
fmt.Fprintf(sb, "%s ExpressionList", indent)
if len(n.Columns) > 0 {
fmt.Fprintf(sb, " (children %d)", len(n.Columns))
}
fmt.Fprintln(sb)
for _, col := range n.Columns {
Node(sb, col, depth+2)
}
}
func explainTableJoin(sb *strings.Builder, n *ast.TableJoin, indent string, depth int) {
// TableJoin is part of TablesInSelectQueryElement
// ClickHouse EXPLAIN AST doesn't show join type in the output
children := 0
if n.On != nil {
children++
}
if len(n.Using) > 0 {
children++
}
if children > 0 {
fmt.Fprintf(sb, "%sTableJoin (children %d)\n", indent, children)
} else {
fmt.Fprintf(sb, "%sTableJoin\n", indent)
}
if n.On != nil {
Node(sb, n.On, depth+1)
}
if len(n.Using) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Using))
for _, u := range n.Using {
Node(sb, u, depth+2)
}
}
}