Add formatExplainJSON exporter for ClickHouse EXPLAIN AST json = 1 with vendored reference suite - #26
Add formatExplainJSON exporter for ClickHouse EXPLAIN AST json = 1 with vendored reference suite#26peter-leonov-ch with Copilot wants to merge 2 commits into
Conversation
…x in AST; vendor AST JSON corpus
|
|
|
This is a result of the core agent having a little back and forth with this repo agent on the best strategy for AST sync. Most of the changes on the core agent made sense to me. Not sure (yet) about the changes to this repo. This is the partner PR to my fork of core: peter-leonov-ch/ClickHouse#1 |
|
JFYI. Here is the extracted tiny type sub-parser that is used for RowBinary responses parsing: https://www.npmjs.com/package/@clickhouse/datatype-parser |
|
Closing this, since the functionality has been implemented in another way:
Thanks again for the reference JSON AST implementation! |
Implements Phases 1–3 of the JSON-AST plan: vendor the
EXPLAIN AST json = 1fixture corpus from the upstream ClickHouse PR (peter-leonov-ch/ClickHouse#1), implement an exporter converting this library's AST to that v1 JSON contract, and add a reference test suite plus debug tooling.Vendored reference corpus
tests/clickhouse-reference-ast-json/cases/— the 41 SQL → JSON pairs from the upstream PR, pinned at commitad33aa4ceba52f42afb17246cc2778d3733cfb7e. README records the source PR, SHA, format version, and snapshot caveats (fixtures at that SHA are bare AST nodes with native 64-bit numbers).npm run generate:ast-json-fixtures,explain:ch --json, and aCLICKHOUSE_IMAGEoverride indocker-compose.yml. Until then the vendored copies are the source of truth.Exporter
src/explain-json.ts—formatExplainJSON(statements)(one{version: 1, ast}document per statement) andexplainJSON(statement), exported fromsrc/index.ts. Pure formatting over AST data, reusingOP_TO_FUNCTIONand helpers extracted fromsrc/explain.ts. Covers the SELECT family (SELECT/UNION/INTERSECT/EXCEPT); other statement kinds throw.Grammar/AST fidelity fixes surfaced by the contract
Per repo convention, data missing from the AST became grammar changes rather than parsing inside the formatter. These were silently discarded by the parser before (and dropped by
format()):NULLS FIRST/LASTon ORDER BY (nullsFirst) and bareWITH FILL(withFill)RESPECT/IGNORE NULLSon functions (nullsAction)OVER wnamed-window references (windowName) and the parent window name inOVER (w …)— previously the entireOVER wclause was lostANY/ALL/ASOF/SEMI/ANTI),GLOBALlocality, and comma-join vsCROSS JOININTERSECT/EXCEPT DISTINCT(distincton the intersect node)isOperatormarker on operator-syntax desugar sites (arr[1],tup.2,BETWEEN,-x,LIKE,||,?:,IS NULL, …) so explicit calls likearrayElement(arr, 1)are distinguishable from operator syntaxformat.tsnow prints the new semantic fields;isOperatoris treated as surface-syntax metadata (stripped in round-trip/golden comparisons, likelocation). Self-goldens intests/clickhouse-referenceregenerated accordingly (~890 files).Test suite and tooling
tests/clickhouse-reference-ast-json.test.ts— mirrors the existing reference suites: parse each fixture, run the exporter, deep-compare structurally. All 41 cases pass; the documented skip/known-divergence list is empty.npm run diff:ast-jsonfollowing the existingdiff:*pattern, with key-order-insensitive diffs;diff-libgeneralized to support alternate corpus layouts.Known follow-ups
no-control-regexlint nit insrc/explain-json.ts(unescapeLabel).