Repository navigation
Expand file tree
/
Copy pathtypeexpr.go
More file actions
324 lines (305 loc) · 8.65 KB
/
Copy pathtypeexpr.go
File metadata and controls
324 lines (305 loc) · 8.65 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package core
import (
"strconv"
"strings"
)
// TypeExpr is a type written as a call expression: a lowercased name applied
// to arguments that are other types, integers, booleans or strings, each
// with an optional label, and a nullable flag at whatever depth it applies.
// Nothing about a nested type is special-cased, so an array of nullable
// strings is array(string nullable), a map is map(string, uint32) and a
// named tuple is tuple(lat: float64, lon: float64). The catalog resolves the
// names; the expression only records what was declared or inferred.
type TypeExpr struct {
Name string `json:"name"`
Nullable bool `json:"nullable,omitempty"`
Args []TypeArg `json:"args,omitempty"`
}
// TypeArg is one argument of a TypeExpr: exactly one of Type, Int, Bool,
// String or Ident is set. Ident is a bare word that is not a type — the max
// of nvarchar(max), the function of SimpleAggregateFunction(sum, UInt64),
// the fields of interval day to second.
type TypeArg struct {
Label string `json:"label,omitempty"`
Type *TypeExpr `json:"type,omitempty"`
Int *int64 `json:"int,omitempty"`
Bool *bool `json:"bool,omitempty"`
String *string `json:"string,omitempty"`
Ident *string `json:"ident,omitempty"`
}
// ArrayTypeName is the family every dialect's array is an instance of: an
// array of integers is array(integer), whatever the dialect spells it.
const ArrayTypeName = "array"
// Array wraps element in one array dimension.
func Array(element *TypeExpr) *TypeExpr {
return &TypeExpr{Name: ArrayTypeName, Args: []TypeArg{{Type: element}}}
}
// IsArray reports whether the expression is an array.
func (t *TypeExpr) IsArray() bool {
return t != nil && t.Name == ArrayTypeName && len(t.Args) > 0 && t.Args[0].Type != nil
}
// Element is an array's element type, or nil for anything else.
func (t *TypeExpr) Element() *TypeExpr {
if !t.IsArray() {
return nil
}
return t.Args[0].Type
}
// ArrayDims counts the array dimensions wrapped around the expression's
// innermost type, and Innermost is that type: the integer of an array of
// arrays of integers.
func (t *TypeExpr) ArrayDims() int {
dims := 0
for t.IsArray() {
dims++
t = t.Element()
}
return dims
}
func (t *TypeExpr) Innermost() *TypeExpr {
for t.IsArray() {
t = t.Element()
}
return t
}
// Clone copies the expression, arguments and all, so that a caller can set
// nullability on the copy without touching a cached one.
func (t *TypeExpr) Clone() *TypeExpr {
if t == nil {
return nil
}
out := &TypeExpr{Name: t.Name, Nullable: t.Nullable}
if len(t.Args) > 0 {
out.Args = make([]TypeArg, len(t.Args))
for i, a := range t.Args {
out.Args[i] = a
out.Args[i].Type = a.Type.Clone()
}
}
return out
}
// WithNullable returns a copy of the expression with its own nullability set
// as given; the nullability of a nested type is left alone.
func (t *TypeExpr) WithNullable(nullable bool) *TypeExpr {
out := t.Clone()
if out != nil {
out.Nullable = nullable
}
return out
}
// Key is the expression's canonical spelling, which identifies its row in
// the catalog: the expression's own nullability is not part of it, since a
// row is never nullable, while the nullability of a nested type is.
func (t *TypeExpr) Key() string {
if t == nil {
return ""
}
if !t.Nullable {
return t.String()
}
return t.WithNullable(false).String()
}
// ParseTypeExpr reads a type spelled the way every dialect spells one, as a
// name optionally applied to a parenthesised, comma-separated argument list
// whose entries may be labelled (`lat Float64` in a tuple, `'a' = 1` in an
// enum). A Nullable(T) wrapper becomes T with Nullable set, and a trailing
// [] becomes an array of the element.
func ParseTypeExpr(s string) *TypeExpr {
s = strings.TrimSpace(s)
if element, ok := strings.CutSuffix(s, ArraySuffix); ok {
return Array(ParseTypeExpr(element))
}
name, args := splitTypeArgs(s)
name = strings.ToLower(name)
if name == "nullable" && len(args) == 1 {
t := ParseTypeExpr(args[0])
t.Nullable = true
return t
}
t := &TypeExpr{Name: name}
for _, a := range args {
t.Args = append(t.Args, parseTypeArg(a))
}
return t
}
func parseTypeArg(a string) TypeArg {
a = strings.TrimSpace(a)
if strings.HasPrefix(a, "'") {
end := quotedEnd(a)
lit := strings.ReplaceAll(strings.ReplaceAll(a[1:end-1], `\'`, `'`), `''`, `'`)
if rest := strings.TrimSpace(a[end:]); strings.HasPrefix(rest, "=") {
arg := parseTypeArg(rest[1:])
arg.Label = lit
return arg
}
return TypeArg{String: &lit}
}
if n, err := strconv.ParseInt(a, 10, 64); err == nil {
return TypeArg{Int: &n}
}
switch strings.ToLower(a) {
case "true", "false":
b := strings.EqualFold(a, "true")
return TypeArg{Bool: &b}
}
// A label is a word before a colon, as the canonical form writes it:
// `lat: Float64`. A word before a space is a label too, as ClickHouse
// writes `lat Float64`, but only when what follows is a single word,
// since `timestamp with time zone` is a name and not a label.
head := a
if p := strings.IndexByte(a, '('); p >= 0 {
head = a[:p]
}
if i := strings.IndexByte(head, ':'); i > 0 && !strings.ContainsAny(head[:i], " '\"") {
return TypeArg{Label: strings.TrimSpace(a[:i]), Type: ParseTypeExpr(a[i+1:])}
}
if i := strings.IndexByte(head, ' '); i > 0 && !strings.Contains(strings.TrimSpace(head[i+1:]), " ") {
return TypeArg{Label: a[:i], Type: ParseTypeExpr(a[i+1:])}
}
return TypeArg{Type: ParseTypeExpr(a)}
}
// quotedEnd returns the index just past the single-quoted literal starting
// at the beginning of s, honouring backslash escapes and doubled quotes.
func quotedEnd(s string) int {
for i := 1; i < len(s); i++ {
switch {
case s[i] == '\\' && i+1 < len(s):
i++
case s[i] == '\'' && i+1 < len(s) && s[i+1] == '\'':
i++
case s[i] == '\'':
return i + 1
}
}
return len(s)
}
// splitTypeArgs splits `Base(arg, arg)` into its base name and top-level
// arguments, leaving nested parentheses and quoted strings intact. Words
// after the closing parenthesis belong to the name, since MySQL writes
// decimal(10,2) unsigned and PostgreSQL timestamp(3) with time zone.
func splitTypeArgs(t string) (string, []string) {
open := strings.IndexByte(t, '(')
if open < 0 {
return t, nil
}
close := matchingParen(t, open)
if close < 0 {
return t, nil
}
base := strings.TrimSpace(t[:open])
if rest := strings.TrimSpace(t[close+1:]); rest != "" {
base += " " + rest
}
inner := t[open+1 : close]
var (
args []string
depth int
quote byte
start int
)
for i := 0; i < len(inner); i++ {
c := inner[i]
switch {
case quote != 0:
if c == '\\' {
i++
} else if c == quote {
quote = 0
}
case c == '\'' || c == '"' || c == '`':
quote = c
case c == '(':
depth++
case c == ')':
depth--
case c == ',' && depth == 0:
args = append(args, strings.TrimSpace(inner[start:i]))
start = i + 1
}
}
if last := strings.TrimSpace(inner[start:]); last != "" || len(args) > 0 {
args = append(args, last)
}
return base, args
}
// matchingParen finds the parenthesis closing the one at open, skipping
// nested parentheses and quoted strings, or -1 when it is not closed.
func matchingParen(t string, open int) int {
depth := 0
var quote byte
for i := open; i < len(t); i++ {
c := t[i]
switch {
case quote != 0:
if c == '\\' {
i++
} else if c == quote {
quote = 0
}
case c == '\'' || c == '"' || c == '`':
quote = c
case c == '(':
depth++
case c == ')':
depth--
if depth == 0 {
return i
}
}
}
return -1
}
// HasNullable reports whether the expression marks nullability anywhere,
// which tells whether the spelling it came from said so itself.
func (t *TypeExpr) HasNullable() bool {
if t == nil {
return false
}
if t.Nullable {
return true
}
for _, a := range t.Args {
if a.Type.HasNullable() {
return true
}
}
return false
}
// String renders the expression in its canonical text form, with a trailing
// "nullable" marking a nullable type.
func (t *TypeExpr) String() string {
if t == nil {
return ""
}
var b strings.Builder
b.WriteString(t.Name)
if len(t.Args) > 0 {
b.WriteByte('(')
for i, a := range t.Args {
if i > 0 {
b.WriteString(", ")
}
if a.Label != "" {
b.WriteString(a.Label)
b.WriteString(": ")
}
switch {
case a.Type != nil:
b.WriteString(a.Type.String())
case a.Int != nil:
b.WriteString(strconv.FormatInt(*a.Int, 10))
case a.Bool != nil:
b.WriteString(strconv.FormatBool(*a.Bool))
case a.String != nil:
b.WriteString("'" + strings.ReplaceAll(*a.String, "'", `\'`) + "'")
case a.Ident != nil:
b.WriteString(*a.Ident)
}
}
b.WriteByte(')')
}
if t.Nullable {
b.WriteString(" nullable")
}
return b.String()
}