The clickhouse-reference-*.test.ts suites run the parser and formatters against (most of) the SELECT statements in ClickHouse's test suite.
For each input file in tests/clickhouse-reference, these tests
- (
clickhouse-reference-ast.test.ts) Runparse()on the input and compare the output AST to the expected AST — ClickHouse's ownEXPLAIN AST json = 1output, generated by theclickhousebinary. The comparison usesformatExplainJson(seesrc/json-explain.ts), which removes node metadata (location/parent/comments) and the library-only fields absent from the reference AST, leaving exactly the ClickHouse-native view.<AST Error>and<Query Parameters>entries in the expected file are skipped. - (
clickhouse-reference-explain.test.ts) RunformatExplain()on the input and compare it to the canonical explain output. This ensures that the parser is parsing all of the information that ClickHouse does. - (
clickhouse-reference-format.test.ts) Runformat()on the AST and compare the output to the expected format. - (
clickhouse-reference-round-trip.test.ts) Runparse()on the formatted SQL and compare the output AST to the AST from parsing the original input. The comparison usesstripVolatile(only location/parent and the non-semantic explain-only flags removed), so comments and the semantic library-only fields must survive the round trip too.
scripts/filter-reference-queries.tsis used to clean up unsupported files and statements from the original test input set.scripts/generate-expected-outputs.tsis used to generate the expected outputs for each input file. Run this when changing formatting or AST output.- Use
npm run generate:expected-outputsto run this script - Updates to expected outputs should be persisted in git, so that reviewers can be aware of changes to parser and formatter outputs.
- By default, this script will not generate any EXPLAIN outputs for inputs that already have EXPLAIN outputs (since these are reference outputs generated by ClickHouse). Pass
--regenerate-all-references(e.g.npm run generate:expected-outputs -- --regenerate-all-references) to overwrite every existing AST and explain file as well, or--regenerate-changed-referencesto overwrite only those whose input.sqlfile has uncommitted git changes (staged, working-tree, or untracked). - When EXPLAIN or AST outputs need to be generated, the script will automatically start a ClickHouse container via
docker compose up -d, wait for it to be ready, and stop it (docker compose down) on exit. If a ClickHouse server is already running at the expected URL, it is reused and left running.
- Use
npm run diff:ast/diff:format/diff:explainshow this library's output, the expected output, and a diff between them for one or more reference cases (a.sqlfilename, comma-separated list, or glob) — the fastest way to debug a failing reference case.npm run parse/format/explainprint just the output (for a reference case or for raw SQL via--sql). Pass-hfor usage.
The query-params.test.ts tests parsing and formatting of query parameters. ClickHouse EXPLAIN AST requires substitutions to be set for each query parameter, so the reference tests don't cover these cases.