Repository navigation
Fix inconsistent AST formatting for CODEC with tuple-wrapped multi-arg unary functions (STID 1941-1bfa) - #104991
Conversation
…_operators=false` contexts
Inside `CODEC` / `STATISTICS` / `BACKUP_NAME` argument lists, `ASTFunction::formatImpl`
forces `frame.allow_operators = false`, so a multi-argument `Function_tuple` falls back
to its function-call form `tuple(arg, arg, ...)` instead of the operator form
`(arg, arg, ...)`.
`RoundBracketsLayer::getResultImpl` in `src/Parsers/ExpressionListParsers.cpp` sets
`parenthesized = true` on any node it unwraps from a single-element `(expr)`. So when a
query like
CREATE TABLE t (c0 Int CODEC(not((not(materialize(1), materialize(2)))), ZSTD))
ENGINE = Memory
first formats, the parser-synthesized inner `Function_tuple` (lifted from the multi-arg
`not(a, b)`) emits as `tuple(materialize(1), materialize(2))`. Re-parsing the output then
sees `tuple(...)` wrapped in `(...)` from the outer `not(...)` argument list and sets
`parenthesized = true` on the re-parsed `Function_tuple`. The next format pass adds an
extra `(...)` around `tuple(...)` via the `parenthesized` flag's parens emission in
`IAST::decideParensEmission`, breaking the format-parse-format round-trip with
`Logical error: 'Inconsistent AST formatting' (STID 1941-1bfa)` in debug / sanitizer
builds.
Both `tuple(a, b)` and `(tuple(a, b))` denote the same value, so dropping the redundant
outer paren in `allow_operators = false` contexts is semantics-preserving and keeps the
round-trip stable. The new clause mirrors the existing literal-tuple suppression a few
lines above.
The fix is gated on `args.size() > 1` so the single-argument `tuple(x)` form keeps its
existing behaviour, and on `!frame.allow_operators` so plain expression contexts where
the operator form `(a, b)` is canonical are unaffected.
This is a partial fix for chronic STID 1941-1bfa: it addresses the `CODEC` / multi-arg
`not(a, b)` subvariant and the `ALTER TABLE ... MODIFY STATISTICS ... TYPE
concatAssumeInjective((...))` subvariant. The other reported subvariants — `DESCRIBE
TABLE (SELECT ... COLUMNS(multiIf(...)) ...)` and qualifier-chained `db.t.COLUMNS(*)` —
involve different parser/formatter paths (`COLUMNS` matcher / `DESCRIBE` wrapper) and
need separate fixes.
Related: PR ClickHouse#102547 (zoomxi, INSERT-with-SAMPLE-OFFSET), PR ClickHouse#104520 (data-type
collection literals), PR ClickHouse#104626 (lambda-after-comma).
Affected PRs (90-day window per @alexey-milovidov 2026-05-14 directive on PR ClickHouse#100412):
93037, 104440, 104812, 104591, 100177, 100651, 104523, 94517, 103428, 99495, 97540,
103866, 103349, 101447, 102646, 100453, 100386, 99179, 94849, 98677, 99131, 99277,
99275, 99227, 99229, 99246, 98884, 99138, 99021, 99105, 98906, 99055, 99011, 99066,
99033, 97821, 70661, 103389, 103347, 100700, master.
CI report (PR ClickHouse#100412 sighting):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100412&sha=3284612ceea8078d500d582ce82e5ace86b239cf&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
|
Pre-PR validation gate (self-assessment per (a) Deterministic repro? Yes. (b) Root cause explained? Yes. The first format emits (c) Fix matches root cause? Yes. Suppresses the (d) Test intent preserved? / New tests added? Yes, new test (e) Both directions demonstrated? Yes. Before the fix the original (f) Fix is general, not a narrow patch? Yes within its scope, with documented limitations:
Session: |
|
cc @evillique @alexey-milovidov @george-larionov — could you review this? It fixes the chronic STID 1941-1bfa |
|
Workflow [PR], commit [551bf44] Summary: ✅
AI ReviewSummaryThis PR stabilizes Final VerdictStatus: ✅ Approve |
Issue ClickHouse#105396 reported the same `Inconsistent AST formatting` (STID 1941-1bfa) firing on `RESTORE TABLE src AS dst FROM Memory(...)` queries. The `Memory(...)` backup name is parsed by `parseBackupName` in `src/Parsers/ParserBackupQuery.cpp` and tagged as `ASTFunction::Kind::BACKUP_NAME`, which is the third kind that forces `frame.allow_operators = false` at `ASTFunction.cpp:298`. Descendants of the backup name inherit this frame state, so a multi-arg `Function_tuple` nested inside the engine argument list hits the exact same round-trip path as the existing `CODEC` / `STATISTICS` reproducers. The fix in `IAST::decideParensEmission` is gated on `!frame.allow_operators` and therefore already covers the BACKUP_NAME case. This commit adds explicit regression coverage for it so the issue ClickHouse#105396 failure shape stays caught.
|
@groeneai merge with master |
|
@PedroTadim merged master into the PR branch as requested. New HEAD SHA: Merge was conflict-free. PR diff is unchanged (3 files, 113 insertions):
src/Parsers/IAST.cpp blob was identical between the PR's merge-base and current master, so no behavioural changes from the rebase. CI should retrigger automatically. |
LLVM Coverage Report
Changed lines: 100.00% (6/6) · Uncovered code |
Queries like the following abort the server with
Logical error: 'Inconsistent AST formatting between 'ExpressionList' and 'ExpressionList'(STID 1941-1bfa) in debug / sanitiser builds, because the format-parse-format round-trip check inDB::executeQueryImpldiverges by one paren level around the innertuple(...):The first format pass emits
CODEC(not((not(tuple(materialize(1), materialize(2))))), ZSTD). The re-parse seestuple(...)wrapped in(...)(from the outernot(...)parenthesised argument);RoundBracketsLayer::getResultImplinsrc/Parsers/ExpressionListParsers.cppsetsparenthesized = trueon the unwrapped innerFunction_tuple. On the second format pass theparenthesizedflag adds an extra(...)aroundtuple(...)—not((not((tuple(materialize(1), materialize(2))))))— and the round-trip check fires.Inside
CODEC/STATISTICS/BACKUP_NAMEargument lists,ASTFunction::formatImplforcesFormatStateStacked::allow_operators = false, so a multi-argumentFunction_tuplefalls back to its function-call formtuple(arg, arg, ...)(instead of the operator form(arg, arg, ...)). In that context the outerparenthesizedparen is always redundant —(tuple(a, b))andtuple(a, b)denote the same value — so suppressing it inIAST::decideParensEmissionkeeps the round-trip stable.The new clause mirrors the existing literal-tuple suppression a few lines above. The fix is gated on:
func->name == "tuple"(other operator-form functions likeplus/equalsneed their parens for precedence)func->arguments->children.size() > 1(single-argumenttuple(x)keeps its existing behaviour and round-trips cleanly)!frame.allow_operators(plain expression contexts where the operator form(a, b)is canonical are unaffected — multi-element tuple literals there are already handled by the existingField::Types::Tupleclause above)This is a partial fix for chronic STID 1941-1bfa. It addresses two subvariants reported by
@alexey-milovidovon PRs #100412 / #94517 / #104591:CREATE TABLE ... CODEC(not(a, b))(variadic-2 call on unary function inCODEC)ALTER TABLE ... MODIFY STATISTICS ... TYPE concatAssumeInjective((...))Other reported subvariants —
DESCRIBE TABLE (SELECT ... COLUMNS(multiIf(...)) ...)and qualifier-chainedSELECT db.t.COLUMNS(*) FROM ...— involve different parser/formatter paths (COLUMNSmatcher /DESCRIBEwrapper) and need separate fixes; they are still tracked in the canonical task.Related: PR #102547 (
@zoomxi, INSERT-with-SAMPLE-OFFSET subvariant), PR #104520 (data-type collection literals subvariant), PR #104626 (lambda-after-comma subvariant, issue #104605).CI sighting on PR #100412 (one of 40+ affected PRs in the last 90 days plus master hits): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100412&sha=3284612ceea8078d500d582ce82e5ace86b239cf&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
Logical error: 'Inconsistent AST formatting'server abort (STID 1941-1bfa) on queries likeCREATE TABLE t (c0 Int CODEC(not((not(materialize(1), materialize(2)))), ZSTD)) ENGINE = Memoryin debug / sanitiser builds. The formatter no longer emits redundant outer parens around a multi-argumenttuple(...)function call that appears insideCODEC/STATISTICS/BACKUP_NAMEargument lists (where the operator form(a, b)is disabled), keeping the format-parse-format round-trip stable.Documentation entry for user-facing changes
Version info
26.6.1.284