Skip to content

Keep ASTViewTargets children in the formatted order, whatever the order of the TimeSeries target clauses - #128897

Open
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-view-targets-children-order
Open

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-view-targets-children-order

Conversation

@groeneai

@groeneai groeneai commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The AST of a TimeSeries table definition no longer depends on the order in which the INNER COLUMNS and INNER ENGINE clauses of its targets are written, so debug builds no longer report Inconsistent AST formatting for such a definition.

Description

A debug server aborts with Logical error: 'Inconsistent AST formatting' (STID 1941-1bfa) when a TimeSeries target's INNER ENGINE clause comes before its INNER COLUMNS clause, or when the clauses of one target are separated by a clause of another target. Both are valid syntax. It killed Stateless tests (amd_debug, parallel) on #123144, whose new test writes RECENT SAMPLES INNER ENGINE = ... RECENT SAMPLES INNER COLUMNS (...): job report.

Root cause: the ASTViewTargets setters append each AST to children in the order the clauses are parsed, while the formatter writes the clauses of each target together, INNER COLUMNS before INNER ENGINE. The formatted query parses back to a different child order, so its tree hash differs. clone() and readJSON() used a third order.

Change: ASTViewTargets::normalizeChildrenOrder() rebuilds children from targets in the order the formatter writes them (per target: table, inner columns, inner engine). The setters, clone() and readJSON() use it, so the parsed AST, its formatted and reparsed copy, its clone and its JSON round trip have the same children. A definition already written in the formatted order (stored metadata, SHOW CREATE) keeps the same AST. Release builds do not run this check and created the table correctly.

Validation: the new test 05493_time_series_target_clause_order aborts an unfixed debug server and passes 50/50 with randomized settings on the fixed one. The new gtest case TreeHashCompleteness.ViewTargetsCloneAndJSONRoundTripHashEqual fails without the change. TimeSeries, AST JSON and materialized view tests give the same results before and after.


Workflow [PR]
Sync PR [sync-upstream/pr/128897]

`ParserViewTargets` accepts the clauses of the targets of a TimeSeries
table in any order, and one target's clauses may be separated by a clause
of another target. The `ASTViewTargets` setters appended each AST to
`children` in that clause order, while the formatter writes the clauses of
each target together, `INNER COLUMNS` before `INNER ENGINE`. A query
written `RECENT SAMPLES INNER ENGINE = ... RECENT SAMPLES INNER COLUMNS (...)`
therefore parsed to children `[engine, columns]` and its formatted text to
`[columns, engine]`, and the debug check in `executeQueryImpl` aborted with
`Inconsistent AST formatting` (STID 1941-1bfa). It killed
`Stateless tests (amd_debug, parallel)` on ClickHouse#123144, whose new test uses
that order. `clone()` and `readJSON()` rebuilt `children` in a third order
(`inner_engine` first).

`normalizeChildrenOrder()` rebuilds `children` from `targets` in the order
`formatTarget` writes the clauses: per target the table AST, the inner
columns, the inner engine. The three AST setters, `clone()` and
`readJSON()` use it, so the parsed AST, its formatted and reparsed copy,
its clone and its JSON round trip have the same children. Definitions
already written in the formatted order keep exactly the children they had.
This follows `ASTQueryWithOutput::output_option_members` and
`ASTStorage::normalizeChildrenOrder`.

The new stateless test creates the CI shape and compares `EXPLAIN AST` of
ENGINE-first and interleaved definitions with the formatted order; the new
gtest case checks clone and JSON round trip hashes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate and internal review (click to expand)
# Question Answer
a Deterministic repro? Yes. An ENGINE-first TimeSeries target (e.g. RECENT SAMPLES INNER ENGINE = MergeTree RECENT SAMPLES INNER COLUMNS (extra UInt8) under EXPLAIN AST) aborts every unfixed debug build; so do the CI statement of #123144, TAGS, and interleaved targets.
b Root cause explained? The setters append to children in clause order, the formatter writes each target's INNER COLUMNS before INNER ENGINE, so the reparsed tree hash differs. clone() and readJSON() used a third order.
c Fix matches root cause? Yes. One private helper of ASTViewTargets rebuilds children in the formatter's order. No guard, no parser restriction, no hash change.
d Test intent preserved / new tests added? New SQL test 05493_time_series_target_clause_order, plus gtest TreeHashCompleteness.ViewTargetsCloneAndJSONRoundTripHashEqual for clone and JSON, which SQL cannot observe.
e Both directions demonstrated? Yes. Unfixed debug: 5 of 8 statements abort and the server dies on the new test. Fixed: all 8 succeed, EXPLAIN pairs identical, 50/50 with randomized settings. The gtest fails without the source change.
f Fix is general across code paths? Yes. The three AST setters, clone() and readJSON() all use the helper; removeTarget() only erases.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. RECENT SAMPLES, TAGS and METRIC FAMILIES measured, ENGINE-first and interleaved, with engine SETTINGS and column defaults. Materialized view tests unchanged.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; a definition in the formatted order (stored metadata, SHOW CREATE) keeps the same AST.
i Invariants and contracts preserved? children matches targets after every setter, clone and readJSON; the target fields own their ASTs, and a clone shares no subtree with the original.

Internal second-model review, 1 finding: ⚠️ QueryFuzzer shuffles targets without reordering children (QueryFuzzer.cpp:2158). DISAGREE: no consumer of a fuzzed AST compares its tree hash (the fuzzers format it and send the text, FuzzLoop.cpp:300, executeQuery.cpp:3780); pre-existing, not in this diff.

@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

cc @nikitamikhaylov @george-larionov, could you review this and add the can be tested label so CI runs? An ENGINE-first TimeSeries target definition aborted debug builds with Inconsistent AST formatting, because the ASTViewTargets setters kept children in clause order while the formatter writes INNER COLUMNS first; children is now rebuilt in the formatter order.

@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [71deca6]

Summary: ✅


AI Review

Summary

Reviewed #128897 at 71deca61629233f8c39642e06d312cf26102d52b, including all prior discussion. The change keeps ASTViewTargets::children in formatter order across setters, cloning, and JSON restoration, fixing Inconsistent AST formatting for reordered TimeSeries target clauses in debug builds. No actionable findings. The new 05493_time_series_target_clause_order regression passed in Fast test; broader debug and unit CI results were still pending. No local build or tests were run.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-ci can be tested Allows running workflows for external contributors comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. labels Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 71deca616 with master e6c0a8e34 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

190 translation units recompiled, 1503 s compile time in total, 190 of them have a recent master baseline.

Job report

@vitlibar vitlibar added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label Nov 3, 2026
@vitlibar vitlibar self-assigned this Nov 3, 2026
@clickhouse-gh clickhouse-gh Bot removed the comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. label Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... pr-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants