-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdictionary.go
More file actions
156 lines (141 loc) · 4.26 KB
/
dictionary.go
File metadata and controls
156 lines (141 loc) · 4.26 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
package explain
import (
"fmt"
"strings"
"github.com/sqlc-dev/doubleclick/ast"
)
// explainDictionaryAttributeDeclaration outputs a dictionary attribute declaration.
func explainDictionaryAttributeDeclaration(sb *strings.Builder, n *ast.DictionaryAttributeDeclaration, indent string, depth int) {
children := 0
if n.Type != nil {
children++
}
if n.Default != nil {
children++
}
if n.Expression != nil {
children++
}
if children > 0 {
fmt.Fprintf(sb, "%sDictionaryAttributeDeclaration %s (children %d)\n", indent, n.Name, children)
} else {
fmt.Fprintf(sb, "%sDictionaryAttributeDeclaration %s\n", indent, n.Name)
}
if n.Type != nil {
Node(sb, n.Type, depth+1)
}
if n.Default != nil {
Node(sb, n.Default, depth+1)
}
if n.Expression != nil {
Node(sb, n.Expression, depth+1)
}
}
// explainDictionaryDefinition outputs a dictionary definition section.
func explainDictionaryDefinition(sb *strings.Builder, n *ast.DictionaryDefinition, indent string, depth int) {
children := 0
if len(n.PrimaryKey) > 0 {
children++
}
if n.Source != nil {
children++
}
if n.Lifetime != nil {
children++
}
if n.Layout != nil {
children++
}
if n.Range != nil {
children++
}
if len(n.Settings) > 0 {
children++
}
if children > 0 {
fmt.Fprintf(sb, "%sDictionary definition (children %d)\n", indent, children)
} else {
fmt.Fprintf(sb, "%sDictionary definition\n", indent)
}
// PRIMARY KEY
if len(n.PrimaryKey) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.PrimaryKey))
for _, pk := range n.PrimaryKey {
Node(sb, pk, depth+2)
}
}
// SOURCE
if n.Source != nil {
explainDictionarySource(sb, n.Source, indent+" ", depth+1)
}
// LIFETIME
if n.Lifetime != nil {
explainDictionaryLifetime(sb, n.Lifetime, indent+" ", depth+1)
}
// RANGE (if present, comes before LAYOUT)
if n.Range != nil {
explainDictionaryRange(sb, n.Range, indent+" ", depth+1)
}
// LAYOUT
if n.Layout != nil {
explainDictionaryLayout(sb, n.Layout, indent+" ", depth+1)
}
// SETTINGS
if len(n.Settings) > 0 {
fmt.Fprintf(sb, "%s Set\n", indent)
}
}
// explainDictionarySource outputs a dictionary SOURCE clause.
func explainDictionarySource(sb *strings.Builder, n *ast.DictionarySource, indent string, depth int) {
// FunctionWithKeyValueArguments has extra space before name
// Always has 1 child for ExpressionList (even when empty)
fmt.Fprintf(sb, "%sFunctionWithKeyValueArguments %s (children %d)\n", indent, strings.ToLower(n.Type), 1)
if len(n.Args) > 0 {
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Args))
for _, arg := range n.Args {
explainKeyValuePair(sb, arg, indent+" ", depth+2)
}
} else {
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
}
}
// explainKeyValuePair outputs a key-value pair (lowercase "pair").
func explainKeyValuePair(sb *strings.Builder, n *ast.KeyValuePair, indent string, depth int) {
children := 0
if n.Value != nil {
children = 1
}
if children > 0 {
fmt.Fprintf(sb, "%spair (children %d)\n", indent, children)
Node(sb, n.Value, depth+1)
} else {
fmt.Fprintf(sb, "%spair\n", indent)
}
}
// explainDictionaryLifetime outputs a dictionary LIFETIME clause.
func explainDictionaryLifetime(sb *strings.Builder, n *ast.DictionaryLifetime, indent string, depth int) {
// LIFETIME is output as "Dictionary lifetime" without children count typically
fmt.Fprintf(sb, "%sDictionary lifetime\n", indent)
}
// explainDictionaryLayout outputs a dictionary LAYOUT clause.
func explainDictionaryLayout(sb *strings.Builder, n *ast.DictionaryLayout, indent string, depth int) {
children := 0
if len(n.Args) > 0 {
children = 1
}
if children > 0 {
fmt.Fprintf(sb, "%sDictionary layout (children %d)\n", indent, children)
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Args))
for _, arg := range n.Args {
explainKeyValuePair(sb, arg, indent+" ", depth+2)
}
} else {
fmt.Fprintf(sb, "%sDictionary layout (children 1)\n", indent)
fmt.Fprintf(sb, "%s ExpressionList\n", indent)
}
}
// explainDictionaryRange outputs a dictionary RANGE clause.
// Note: ClickHouse's EXPLAIN does not output children for Dictionary range.
func explainDictionaryRange(sb *strings.Builder, n *ast.DictionaryRange, indent string, depth int) {
fmt.Fprintf(sb, "%sDictionary range\n", indent)
}