Repository navigation
Conversation
Groundwork for the query rewrite and formatting API: the AST nodes and serialization for `EXPLAIN TEXT`, without the parser or interpreter yet. Adds `ASTExplainTextAction`, a typed node for a single rewrite or format action (`ONELINE`, `MULTILINE`, `MODIFY LIMIT`, `MODIFY OFFSET`, `PAGE`, `MODIFY FORMAT`), with per-kind shape validation and AST JSON support. Adds the `FormattedQuery` kind to `ASTExplainQuery` with an `actions` child, wired through `clone`, `formatQueryImpl`, `writeJSON` and `readJSON`. Deserialization rejects actions on other kinds, and leading kind-specific settings on `EXPLAIN TEXT`. `EXPLAIN TEXT` is not reachable yet: the parser does not accept it, the interpreter throws, and it is rejected in subqueries and by the `viewExplain` table function. `QueryFuzzer` skips the kind so it cannot generate invalid kind/action combinations.
|
Workflow [PR], commit [ffbc49c] Summary: ✅
AI ReviewSummary
Missing context / blind spots
Findings
Final Verdict
LLVM Coverage ReportMeasured on commit ffbc49c.
Changed lines: Changed C/C++ lines covered: 1107/1168 (94.78%) · Uncovered code |
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/Parsers/CMakeFiles/clickhouse_parsers.dir/ASTExplainTextAction.cpp.o |
new | 317.38 KiB | +317.38 KiB |
src/Parsers/CMakeFiles/clickhouse_parsers.dir/ParserExplainTextActions.cpp.o |
new | 80.61 KiB | +80.61 KiB |
src/CMakeFiles/dbms.dir/Interpreters/ExplainTextRewrite.cpp.o |
new | 71.81 KiB | +71.81 KiB |
src/Parsers/CMakeFiles/clickhouse_parsers.dir/ParserExplainQuery.cpp.o |
148.10 KiB | 191.82 KiB | +43.72 KiB (+29.52%) |
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
1897 translation units recompiled, 10559 s compile time in total, 1894 of them have a recent master baseline.
# Conflicts: # src/Interpreters/InterpreterExplainQuery.cpp
fd80610 to
b9d6f8c
Compare
b9d6f8c to
88104d2
Compare
2e82513 to
e308c5a
Compare
|
cc @alexey-milovidov - I think I've covered everything I had in mind in this PR and responded to all of the AI review. Could you take a look whenever you get a chance and let me know if there's anything that I've missed? Thanks! All CI checks are green except |
…ken, and reject 'PAGE' with a negative 'LIMIT'
…d reject actions after a bare nested EXPLAIN TEXT
…options of the enclosing EXPLAIN TEXT
Out of scope here. Tracked separately in #121679 |
|
This is a good implementation, and thanks for contributing! However, I'd like to cancel the original task. The reason is that the JSON AST looks more interoperable, while inventing a new class of language inside SQL might be way too much. |
|
What was the reason for coming back to this task and reconsidering it? In one context, I needed a way to programmatically wrap a subquery in GROUP BY. And I thought, maybe I can use an API for query rewrite that we've proposed? But it does not cover this use case. Then I found that the proposed API for query rewrite is very limited and will not cover many other use cases. This sort of problem appears again and again in various programming languages. Some solve it with Lisp, some solve it with AST matching macros. When we just exposed AST as JSON, we put the problem aside, and now we can avoid solving it. |
Adds
EXPLAIN TEXTso clients can format SQL and change pagination or output format using ClickHouse's own parser, without executing the source query. Returns exactly one row containing aStringcolumn namedtext.The motivation follows the query-rewrite RFC: UI tools need to modify SQL without maintaining partial parsers that can corrupt its structure. Issue #75033 selects an
EXPLAINinterface so the operation is available through existing ClickHouse interfaces and request handling. The actions edit the source query's AST; they do not apply a limit or sort to a wrapped result set.Supports
ONELINE,MULTILINE,MODIFY LIMIT,MODIFY OFFSET,PAGE, andMODIFY FORMAT, applied from left to right. For example:{"text":"SELECT * FROM t LIMIT 5"}Several of the implementation choices were based on the following:
formatQueryand the earlier scalar-function proposal provides some background on formatting. The additional capability here is ordered clause rewriting using typed actions on a cloned AST.EXPLAINsupport informed the round-trip requirements. Parentheses preserve nested action boundaries when formatting a query for reparse.PAGEuses the current limit, removes the offset for page one, and checks multiplication overflow forUInt64literal limits.Pagination rewrites require a single plain
SELECT.MODIFY FORMATalso supports unions because their output format has a clear top-level owner. Inline-dataINSERTsources, subquery use, and directviewExplainuse are rejected in this initial scope.KEEP SPACE AND COMMENTS, projection/filter/order rewrites, and multi-branch union pagination remain outside this change. Existing whitespace-preservation helpers informatQueryFromJSONprovide a starting point for later work. The linked feature issue therefore remains open.Related: #75033
Related: #29922
Related: #48757
Related: #99545
Related: #100136
Related: #100412
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added
EXPLAIN TEXTto return formatted SQL without executing it, with optional changes to its limit, offset, page, and output format. Supports single-line and multiline output while preserving query parameters.Workflow [PR]
Sync PR [sync-upstream/pr/119167]