Skip to content

Commit d95f3da

Browse files
committed
goldeneye: check the SQLite analyze cases against sqlite3 itself
The analyze cases under internal/endtoend/testdata were only checked against a real database for ClickHouse. Now the sqlite ones are checked too, against one more sqlite3 shell that `install sqlite` builds, with SQLITE_ENABLE_COLUMN_METADATA and every extension option: `.stats stmt` says which table column each result column is read from, the query's own rows over the fixture and over no rows type the expressions and find the NULLs, and the parameters are followed through the bytecode EXPLAIN prints to the column each is compared with, stored into or seeks by. The query-file parsing, placeholder rewriting and the JSON shape of an engine's answer move from the clickhouse package into endtoend, where both engines share them. An analyze_expressions case with a fixture covers aggregates, IN lists, LIMIT, an outer join and RETURNING for SQLite; the five sqlite cases match what SQLite reports. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdKaVjSmdkmXPeUkxetyDY
1 parent 3c2546a commit d95f3da

21 files changed

Lines changed: 1946 additions & 217 deletions

File tree

‎CLAUDE.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ from a live database by `/internal/goldeneye`, a nested module, and its tests
150150
verify the committed files against one byte for byte. The same module checks
151151
the `analyze_*` cases under `/internal/endtoend/testdata/` against what the
152152
database itself reports for them, so a `fixture.sql` next to a case's schema
153-
gives the queries rows to run against. Engines whose database is not
154-
available skip.
153+
gives the queries rows to run against. ClickHouse and SQLite have the check
154+
today; engines whose database is not available skip.
155155

156156
```bash
157157
cd internal/goldeneye
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "analyze",
3+
"args": ["--dialect", "sqlite", "--schema", "schema.sql", "query.sql"],
4+
"contexts": ["base"]
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
INSERT INTO users (id, name, bio, score) VALUES (1, 'ann', NULL, 1.5);
2+
INSERT INTO users (id, name, bio, score) VALUES (2, 'bob', 'hello', 2.5);
3+
INSERT INTO posts (id, user_id, title, created) VALUES (1, 1, 'first', '2024-01-01');
4+
INSERT INTO posts (id, user_id, title, created) VALUES (2, 1, NULL, '2024-01-02');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- name: PostStats :one
2+
SELECT count(*) AS total, max(id) AS latest, min(created) AS first
3+
FROM posts WHERE user_id = ?;
4+
5+
-- name: UserScores :one
6+
SELECT avg(score) AS mean, sum(score) AS sum, group_concat(name) AS names
7+
FROM users;
8+
9+
-- name: ListUsers :many
10+
SELECT id, lower(name) AS lname, id + 1 AS next
11+
FROM users WHERE id IN (?, ?);
12+
13+
-- name: ListPosts :many
14+
SELECT id, title FROM posts ORDER BY created LIMIT ? OFFSET ?;
15+
16+
-- name: UserPosts :many
17+
SELECT u.name, p.title, p.created
18+
FROM users u LEFT JOIN posts p ON p.user_id = u.id
19+
WHERE u.name LIKE ? || '%';
20+
21+
-- name: CreatePost :one
22+
INSERT INTO posts (user_id, title, created) VALUES (?, ?, datetime('now'))
23+
RETURNING id, created;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE TABLE users (
2+
id INTEGER PRIMARY KEY,
3+
name TEXT NOT NULL,
4+
bio TEXT,
5+
score REAL NOT NULL DEFAULT 0
6+
);
7+
8+
CREATE TABLE posts (
9+
id INTEGER PRIMARY KEY,
10+
user_id INTEGER NOT NULL REFERENCES users(id),
11+
title TEXT,
12+
created TEXT NOT NULL
13+
);
14+
15+
CREATE INDEX posts_user ON posts(user_id);
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
[
2+
{
3+
"name": "PostStats",
4+
"cmd": ":one",
5+
"columns": [
6+
{
7+
"name": "total",
8+
"type": {
9+
"name": "integer"
10+
}
11+
},
12+
{
13+
"name": "latest",
14+
"type": {
15+
"name": "integer",
16+
"nullable": true
17+
}
18+
},
19+
{
20+
"name": "first",
21+
"type": {
22+
"name": "text",
23+
"nullable": true
24+
}
25+
}
26+
],
27+
"params": [
28+
{
29+
"number": 1,
30+
"column": {
31+
"name": "user_id",
32+
"type": {
33+
"name": "integer"
34+
},
35+
"table": "posts"
36+
}
37+
}
38+
]
39+
},
40+
{
41+
"name": "UserScores",
42+
"cmd": ":one",
43+
"columns": [
44+
{
45+
"name": "mean",
46+
"type": {
47+
"name": "real",
48+
"nullable": true
49+
}
50+
},
51+
{
52+
"name": "sum",
53+
"type": {
54+
"name": "real",
55+
"nullable": true
56+
}
57+
},
58+
{
59+
"name": "names",
60+
"type": {
61+
"name": "text",
62+
"nullable": true
63+
}
64+
}
65+
],
66+
"params": []
67+
},
68+
{
69+
"name": "ListUsers",
70+
"cmd": ":many",
71+
"columns": [
72+
{
73+
"name": "id",
74+
"type": {
75+
"name": "integer"
76+
},
77+
"table": "users"
78+
},
79+
{
80+
"name": "lname",
81+
"type": {
82+
"name": "text"
83+
}
84+
},
85+
{
86+
"name": "next",
87+
"type": {
88+
"name": "integer"
89+
}
90+
}
91+
],
92+
"params": [
93+
{
94+
"number": 1,
95+
"column": {
96+
"name": "id",
97+
"type": {
98+
"name": "integer"
99+
},
100+
"table": "users"
101+
}
102+
},
103+
{
104+
"number": 2,
105+
"column": {
106+
"name": "id",
107+
"type": {
108+
"name": "integer"
109+
},
110+
"table": "users"
111+
}
112+
}
113+
]
114+
},
115+
{
116+
"name": "ListPosts",
117+
"cmd": ":many",
118+
"columns": [
119+
{
120+
"name": "id",
121+
"type": {
122+
"name": "integer"
123+
},
124+
"table": "posts"
125+
},
126+
{
127+
"name": "title",
128+
"type": {
129+
"name": "text",
130+
"nullable": true
131+
},
132+
"table": "posts"
133+
}
134+
],
135+
"params": [
136+
{
137+
"number": 1,
138+
"column": {
139+
"name": "",
140+
"type": {
141+
"name": "integer"
142+
}
143+
}
144+
},
145+
{
146+
"number": 2,
147+
"column": {
148+
"name": "",
149+
"type": {
150+
"name": "integer"
151+
}
152+
}
153+
}
154+
]
155+
},
156+
{
157+
"name": "UserPosts",
158+
"cmd": ":many",
159+
"columns": [
160+
{
161+
"name": "name",
162+
"type": {
163+
"name": "text"
164+
},
165+
"table": "users"
166+
},
167+
{
168+
"name": "title",
169+
"type": {
170+
"name": "text",
171+
"nullable": true
172+
},
173+
"table": "posts"
174+
},
175+
{
176+
"name": "created",
177+
"type": {
178+
"name": "text"
179+
},
180+
"table": "posts"
181+
}
182+
],
183+
"params": [
184+
{
185+
"number": 1,
186+
"column": {
187+
"name": "",
188+
"type": {
189+
"name": "text"
190+
}
191+
}
192+
}
193+
]
194+
},
195+
{
196+
"name": "CreatePost",
197+
"cmd": ":one",
198+
"columns": [
199+
{
200+
"name": "id",
201+
"type": {
202+
"name": "integer"
203+
},
204+
"table": "posts"
205+
},
206+
{
207+
"name": "created",
208+
"type": {
209+
"name": "text"
210+
},
211+
"table": "posts"
212+
}
213+
],
214+
"params": [
215+
{
216+
"number": 1,
217+
"column": {
218+
"name": "user_id",
219+
"type": {
220+
"name": "integer"
221+
},
222+
"table": "posts"
223+
}
224+
},
225+
{
226+
"number": 2,
227+
"column": {
228+
"name": "title",
229+
"type": {
230+
"name": "text",
231+
"nullable": true
232+
},
233+
"table": "posts"
234+
}
235+
}
236+
]
237+
}
238+
]

‎internal/goldeneye/README.md‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ the hand-written files alone, and the checks do not look at them.
8080
`sqlite/signatures.go`, since a SQLite function returns NULL as often by
8181
setting no result as by saying so. The pinned release is the one the main module's
8282
driver embeds. SQLite has no catalog of types or operators, so
83-
`types.jsonl` and `operators.jsonl` are hand-written.
83+
`types.jsonl` and `operators.jsonl` are hand-written. `install` builds one
84+
more shell, for the analysis check below; nothing is generated from it.
8485

8586
## Layout
8687

8788
- `dialect/` — the record types the files are made of, mirrored from
8889
`internal/core/seed`, and the helpers that write a generated set of files
8990
into an engine directory or diff it against what is committed.
90-
- `endtoend/` — finds the analyze cases and compares an engine's answer with
91-
a case's committed output.
91+
- `endtoend/` — finds the analyze cases, splits their query files, and
92+
holds the shape of an engine's answer, which it compares with a case's
93+
committed output.
9294
- `postgresql/`, `duckdb/`, `clickhouse/`, `sqlite/` — one package per
9395
engine, each exposing `Locate`, `Version` and `Generate`, `Analyze` where
9496
the engine has an analysis check, and tests that run the checks.
@@ -111,5 +113,32 @@ asks for `--ast` is skipped, since only sqlc can print that.
111113
`EXPLAIN QUERY TREE`, and parameters from sentinel constants substituted for
112114
`?`, `sqlc.arg()` and `sqlc.narg()`, since ClickHouse itself never sees a
113115
placeholder; `INSERT ... VALUES` parameters map onto `DESCRIBE TABLE`.
116+
- **`sqlite`** runs each case through one more shell `install` builds, under
117+
`analysis/`: every extension option at once, so that any case's schema
118+
loads, and `SQLITE_ENABLE_COLUMN_METADATA`, which lets the shell's `.stats
119+
stmt` say which table column each result column of a statement is read
120+
from. That column's declared type is the result column's, and its NOT
121+
NULL decides nullability, the rowid counting as NOT NULL. SQLite types
122+
values rather than expressions, so a column the library has no origin for
123+
— an aggregate, an arithmetic result — is typed by the storage class of
124+
the value it returns, which is why a case wants a `fixture.sql`: the query
125+
is run over the fixture, with each parameter bound to a value of the
126+
column it stands in for, and again over no rows, and a column is nullable
127+
when either run returns a NULL for it — an aggregate over nothing, the far
128+
side of an outer join. The library reports nothing about a parameter but
129+
its number, so parameters are found in the bytecode `EXPLAIN` prints, the
130+
way ClickHouse's are found in its query tree: each is followed from the
131+
register its `Variable` loads, through copies and the expressions it is an
132+
argument of, to the first opcode that uses it against something the
133+
catalog can name — the other operand of a comparison, the row a seek lands
134+
on, the position in the record an `Insert` writes, the column of an IN
135+
list's ephemeral table it comes back out of. One that reaches nothing
136+
nameable is described by what the program requires of it, when it
137+
requires anything: `MustBeInt` makes LIMIT's an integer. `sqlc.arg(name)`
138+
becomes `?N`, numbered as sqlc numbers them, so a repeated name is one
139+
parameter. Two things the check reports that sqlc does not: a bare column
140+
selected alongside an aggregate is NULL over no rows, and so nullable, and
141+
a comparison such as `x IS NULL` is an integer, since that is what SQLite
142+
returns.
114143

115144
The other engines have no analysis check yet.

0 commit comments

Comments
 (0)