Skip to content

Commit b76757f

Browse files
committed
Make nullability an attribute of a testgen type
Nullable(T) is now T with nullable set, at whatever depth ClickHouse wrote it, instead of a call named nullable. Every engine has nullability and every consumer needs it, so an attribute on the type node spares readers from treating one name as special. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y6XkyWnx7iJFEb8q3AnYps
1 parent b6cd45d commit b76757f

6 files changed

Lines changed: 58 additions & 163 deletions

File tree

‎internal/engine/clickhouse/testgen/README.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@ query is explained and then executed, and the process exits.
4242
The output has the shape of `sqlc analyze`, except that each column's type is
4343
one expression rather than a name and flags. A type is a call: a lowercased
4444
`name` applied to `args`, each of which carries an optional `label` and
45-
exactly one of `type`, `int`, `bool` or `string`. `Nullable`, `Array` and
45+
exactly one of `type`, `int`, `bool` or `string`. `Array`, `Map` and
4646
`LowCardinality` are ordinary names in that grammar, so nothing about nesting
47-
is lost, and there is no separate nullability flag: a nullable column is one
48-
whose type is `nullable(...)`.
47+
is lost. Nullability is an attribute of a type rather than a wrapper:
48+
`Nullable(T)` becomes `T` with `nullable` set, at whatever depth ClickHouse
49+
wrote it.
4950

5051
```json
5152
{"name": "map", "args": [
5253
{"type": {"name": "string"}},
53-
{"type": {"name": "nullable", "args": [{"type": {"name": "uint8"}}]}}]}
54+
{"type": {"name": "uint8", "nullable": true}}]}
5455

5556
{"name": "tuple", "args": [
5657
{"label": "lat", "type": {"name": "float64"}},

‎internal/engine/clickhouse/testgen/testdata/exec/stdout.txt‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,8 @@
5656
"column": {
5757
"name": "tag",
5858
"type": {
59-
"name": "nullable",
60-
"args": [
61-
{
62-
"type": {
63-
"name": "string"
64-
}
65-
}
66-
]
59+
"name": "string",
60+
"nullable": true
6761
},
6862
"table": "events"
6963
}

‎internal/engine/clickhouse/testgen/testdata/expressions/stdout.txt‎

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@
2424
{
2525
"name": "first_tag",
2626
"type": {
27-
"name": "nullable",
28-
"args": [
29-
{
30-
"type": {
31-
"name": "string"
32-
}
33-
}
34-
]
27+
"name": "string",
28+
"nullable": true
3529
}
3630
},
3731
{
@@ -56,14 +50,8 @@
5650
{
5751
"name": "maybe",
5852
"type": {
59-
"name": "nullable",
60-
"args": [
61-
{
62-
"type": {
63-
"name": "string"
64-
}
65-
}
66-
]
53+
"name": "string",
54+
"nullable": true
6755
}
6856
},
6957
{
@@ -93,14 +81,8 @@
9381
{
9482
"name": "word",
9583
"type": {
96-
"name": "nullable",
97-
"args": [
98-
{
99-
"type": {
100-
"name": "string"
101-
}
102-
}
103-
]
84+
"name": "string",
85+
"nullable": true
10486
}
10587
}
10688
],
@@ -179,14 +161,8 @@
179161
"column": {
180162
"name": "tag",
181163
"type": {
182-
"name": "nullable",
183-
"args": [
184-
{
185-
"type": {
186-
"name": "string"
187-
}
188-
}
189-
]
164+
"name": "string",
165+
"nullable": true
190166
},
191167
"table": "events"
192168
}
@@ -228,14 +204,8 @@
228204
"column": {
229205
"name": "tag",
230206
"type": {
231-
"name": "nullable",
232-
"args": [
233-
{
234-
"type": {
235-
"name": "string"
236-
}
237-
}
238-
]
207+
"name": "string",
208+
"nullable": true
239209
},
240210
"table": "events"
241211
}
@@ -345,14 +315,8 @@
345315
{
346316
"name": "echo",
347317
"type": {
348-
"name": "nullable",
349-
"args": [
350-
{
351-
"type": {
352-
"name": "nothing"
353-
}
354-
}
355-
]
318+
"name": "nothing",
319+
"nullable": true
356320
}
357321
},
358322
{
@@ -368,14 +332,8 @@
368332
"column": {
369333
"name": "echo",
370334
"type": {
371-
"name": "nullable",
372-
"args": [
373-
{
374-
"type": {
375-
"name": "nothing"
376-
}
377-
}
378-
]
335+
"name": "nothing",
336+
"nullable": true
379337
}
380338
}
381339
}

‎internal/engine/clickhouse/testgen/testdata/subqueries/stdout.txt‎

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313
{
1414
"name": "tag",
1515
"type": {
16-
"name": "nullable",
17-
"args": [
18-
{
19-
"type": {
20-
"name": "string"
21-
}
22-
}
23-
]
16+
"name": "string",
17+
"nullable": true
2418
},
2519
"table": "events"
2620
},
@@ -94,14 +88,8 @@
9488
{
9589
"name": "tag",
9690
"type": {
97-
"name": "nullable",
98-
"args": [
99-
{
100-
"type": {
101-
"name": "string"
102-
}
103-
}
104-
]
91+
"name": "string",
92+
"nullable": true
10593
},
10694
"table": "events"
10795
},
@@ -150,14 +138,8 @@
150138
{
151139
"name": "user_count",
152140
"type": {
153-
"name": "nullable",
154-
"args": [
155-
{
156-
"type": {
157-
"name": "uint64"
158-
}
159-
}
160-
]
141+
"name": "uint64",
142+
"nullable": true
161143
}
162144
}
163145
],

‎internal/engine/clickhouse/testgen/testdata/types/stdout.txt‎

Lines changed: 16 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,8 @@
2020
{
2121
"name": "tag",
2222
"type": {
23-
"name": "nullable",
24-
"args": [
25-
{
26-
"type": {
27-
"name": "string"
28-
}
29-
}
30-
]
23+
"name": "string",
24+
"nullable": true
3125
},
3226
"table": "things"
3327
},
@@ -59,14 +53,8 @@
5953
"args": [
6054
{
6155
"type": {
62-
"name": "nullable",
63-
"args": [
64-
{
65-
"type": {
66-
"name": "string"
67-
}
68-
}
69-
]
56+
"name": "string",
57+
"nullable": true
7058
}
7159
}
7260
]
@@ -101,14 +89,8 @@
10189
"args": [
10290
{
10391
"type": {
104-
"name": "nullable",
105-
"args": [
106-
{
107-
"type": {
108-
"name": "string"
109-
}
110-
}
111-
]
92+
"name": "string",
93+
"nullable": true
11294
}
11395
}
11496
]
@@ -240,14 +222,8 @@
240222
},
241223
{
242224
"type": {
243-
"name": "nullable",
244-
"args": [
245-
{
246-
"type": {
247-
"name": "uint8"
248-
}
249-
}
250-
]
225+
"name": "uint8",
226+
"nullable": true
251227
}
252228
}
253229
]
@@ -311,14 +287,8 @@
311287
{
312288
"name": "tag",
313289
"type": {
314-
"name": "nullable",
315-
"args": [
316-
{
317-
"type": {
318-
"name": "string"
319-
}
320-
}
321-
]
290+
"name": "string",
291+
"nullable": true
322292
},
323293
"table": "things"
324294
},
@@ -350,14 +320,8 @@
350320
"args": [
351321
{
352322
"type": {
353-
"name": "nullable",
354-
"args": [
355-
{
356-
"type": {
357-
"name": "string"
358-
}
359-
}
360-
]
323+
"name": "string",
324+
"nullable": true
361325
}
362326
}
363327
]
@@ -392,14 +356,8 @@
392356
"args": [
393357
{
394358
"type": {
395-
"name": "nullable",
396-
"args": [
397-
{
398-
"type": {
399-
"name": "string"
400-
}
401-
}
402-
]
359+
"name": "string",
360+
"nullable": true
403361
}
404362
}
405363
]
@@ -531,14 +489,8 @@
531489
},
532490
{
533491
"type": {
534-
"name": "nullable",
535-
"args": [
536-
{
537-
"type": {
538-
"name": "uint8"
539-
}
540-
}
541-
]
492+
"name": "uint8",
493+
"nullable": true
542494
}
543495
}
544496
]

‎internal/engine/clickhouse/testgen/types.go‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import (
88
// A type is a call expression, the way ClickHouse itself models one: a
99
// lowercased name applied to an ordered argument list. Each argument is
1010
// another type, an integer, a boolean or a quoted string, optionally
11-
// labelled, so
12-
// Nullable, Array and LowCardinality are ordinary names and nothing about a
13-
// nested type is lost. The shape maps one to one onto a protobuf message
14-
// with a oneof for the argument value:
11+
// labelled, so Array, Map and LowCardinality are ordinary names and nothing
12+
// about a nested type is lost. Nullability is an attribute of a type rather
13+
// than a wrapper, since every engine has it and only ClickHouse spells it as
14+
// a type: Nullable(T) becomes T with nullable set, at whatever depth it
15+
// appears. The shape maps one to one onto a protobuf message with a oneof
16+
// for the argument value:
1517
//
1618
// Map(String, Nullable(UInt32))
1719
// {"name": "map", "args": [
1820
// {"type": {"name": "string"}},
19-
// {"type": {"name": "nullable", "args": [{"type": {"name": "uint32"}}]}}]}
21+
// {"type": {"name": "uint32", "nullable": true}}]}
2022
//
2123
// Tuple(lat Float64, lon Float64)
2224
// {"name": "tuple", "args": [
@@ -34,8 +36,9 @@ import (
3436
// is the reader's job; the output only records what was said.
3537

3638
type typeExpr struct {
37-
Name string `json:"name"`
38-
Args []typeArg `json:"args,omitempty"`
39+
Name string `json:"name"`
40+
Nullable bool `json:"nullable,omitempty"`
41+
Args []typeArg `json:"args,omitempty"`
3942
}
4043

4144
type typeArg struct {
@@ -50,6 +53,11 @@ type typeArg struct {
5053
func parseType(t string) *typeExpr {
5154
name, args := splitType(t)
5255
name = strings.ToLower(strings.TrimSpace(name))
56+
if name == "nullable" && len(args) == 1 {
57+
expr := parseType(args[0])
58+
expr.Nullable = true
59+
return expr
60+
}
5361
if name == "" {
5462
name = "nothing"
5563
}

0 commit comments

Comments
 (0)