Skip to content

Format the composition operator f | g as an operator inside CODEC and other function-call-form contexts - #125567

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-compose-operator-in-call-form
Nov 11, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-compose-operator-in-call-form

Conversation

@groeneai

@groeneai groeneai commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #116029

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed formatting of the function composition operator f | g inside CODEC, STATISTICS, backup engine arguments and the EXPLAIN SYNTAX output of non-SELECT queries: it was written as a call to __compose, which could not be run back.

Description

On 26.10.2.103, EXPLAIN SYNTAX CREATE TABLE t (x Array(UInt64) DEFAULT arrayMap(plus(_, 1) | multiply(_, 2), [1])) ENGINE = Memory prints arrayMap(__compose(plus(_, 1), multiply(_, 2)), [1]), which fails with Code: 36 when run.

CI: Stress test (amd_debug) on #117704 aborted with Logical error: 'Inconsistent AST formatting: the original AST: ... differs from the result of parsing back formatted AST' (report). The fuzzer put arrayMap(plus(_, 1048577) | multiply(-2147483648), [2]) inside a column codec. The shortest form, on a debug build:

CREATE TABLE t (x UInt64 CODEC(ZSTD(a | b))) ENGINE = Memory; -- abort instead of Code: 433

Root cause. Inside CODEC, STATISTICS, backup engine arguments, viewIfPermitted ... ELSE and EXPLAIN SYNTAX, ASTFunction writes operators as calls. The __compose branch sat inside that operators-allowed block, so f | g came out as __compose(f, g). That text re-parses as an ordinary call, and the tree hash tells the two apart on purpose (c445e7b), so the round-trip check fails.

Change. Write f | g as the operator in every context. The __compose branch moves in front of the allow_operators check, with the same conditions and body. Contexts that allow operators produce exactly the same output, and an ordinary __compose(a, b) call is still written as a call. | came in #116029 and is released in 26.10.

Validation. A new test covers EXPLAIN SYNTAX of a codec and a default, a STATISTICS clause, and nested composition in ALTER ... CODEC. On master its first line aborts a debug server with the CI message; with the fix it passes, 50/50 with random settings. The existing function composition tests pass.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.1985-master (included in 26.11 and later)

…ll-form contexts

In `CODEC`, `STATISTICS`, backup engine arguments, the `ELSE` argument of
`viewIfPermitted` and `EXPLAIN SYNTAX`, `ASTFunction` writes operators as
function calls. The `__compose` branch was inside the block that runs only
when operators are allowed, so there `f | g` was written as `__compose(f, g)`.
That text parses back as an ordinary call, and the tree hash of `__compose`
includes `isOperator()` on purpose (c445e7b), so debug builds failed the
round-trip check in `executeQuery` with "Inconsistent AST formatting", and
`EXPLAIN SYNTAX` of a non-SELECT query printed text that does not run.

Found by the AST fuzzer in Stress test (amd_debug), which put
`arrayMap(plus(_, 1048577) | multiply(-2147483648), [2])` into a column codec.

Move the branch in front of the `allow_operators` check with the same
conditions and body. Output where operators are allowed does not change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 13, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (2 nits, no blocker or major).

# Sev Finding Verdict Evidence / action
1 💡 viewIfPermitted(SELECT ... ELSE <x>) where <x> is itself an f | g node now prints ELSE a | b, which cannot be parsed back (before: ELSE __compose(a, b), a tree-hash mismatch) AGREE, not blocking Only an AST mutation can build this tree: ELSE f | g is a syntax error before and after the fix, and no parseable spelling of it exists. The reachable shape ELSE null(a | b) is fixed.
2 💡 The test has no viewIfPermitted ... ELSE null(a | b) line AGREE, not blocking The moved branch does not depend on which context disabled operators; that shape was checked both ways locally (abort before, normal Code: 36 after).

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-8:20261013-071500

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CREATE TABLE t (x UInt64 CODEC(ZSTD(a | b))) ENGINE = Memory aborts a debug build of master every time, at the tree-hash check in executeQuery.cpp. The fuzzed query from the CI log does the same.
b Root cause explained? In call-form contexts (CODEC, STATISTICS, backup engine arguments, viewIfPermitted ... ELSE, EXPLAIN SYNTAX) the __compose branch was unreachable, so f | g was written as __compose(f, g). That re-parses with is_operator = 0, and the tree hash of __compose includes isOperator(), so the round trip fails.
c Fix matches root cause? Yes. The formatter no longer writes a call spelling that means something else. The hash distinction and the parser stay unchanged.
d Test intent preserved / new tests added? New .sql test with 4 call-form contexts (codec, default, STATISTICS, nested composition in ALTER ... CODEC). Its first line aborts a debug server on master.
e Both directions demonstrated? Yes. On unfixed HEAD the CODEC, STATISTICS, ALTER, TTL RECOMPRESS, BACKUP, viewIfPermitted ... ELSE and EXPLAIN SYNTAX shapes plus the fuzzed query all abort. On the fix each one gives the normal result or error. The new test kills a base server with the CI message and passes 50/50 with random settings on the fix.
f Fix is general across code paths? All call-form contexts go through the same ASTFunction branch, and each one was measured. Other operators' call forms already re-parse to the same tree (checked with plus, negate, lambda).
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked nested left and right composition, an alias, an operand of plus or negate, a lambda body and arrayMap. An ordinary __compose(a, b) call still formats as a call. Data types do not apply to AST formatting.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or stored-metadata change. Only EXPLAIN SYNTAX of non-SELECT queries changes, and it now prints runnable text. | is master-only (#116029).
i Invariants and contracts preserved? parse(format(parse(T))) keeps the tree hash of parse(T) in every context. The moved branch writes the same text and goes through the same alias and window tail. Output in contexts that allow operators is byte-identical.

Session id: cron:clickhouse-impl-slot-4:20261013-063600

@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f095628]

Summary: ✅


AI Review

Summary

Preserves the f | g composition operator in formatting contexts that disable other operators, fixing AST round trips and non-SELECT EXPLAIN SYNTAX output. Reviewed PR head f095628c2d3547a31cef6244a36a3631e5ff5739; no actionable findings. The focused regression test passes in the PR-head Fast test report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. labels Oct 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f095628c2 with master 3b3f22da1 (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

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

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - a9fa845

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (amd_tsan, s3 storage, targeted) not from this PR: the job errored before running any test. Its test-selection step could not reach the CI database (play.clickhouse.com refused connections, [Errno 111] Connection refused, from about 09:09 UTC), so Failed to get previously failed tests required for test selection a fix task is moved to pending (investigating at full effort, fixing-PR link to follow here)
Integration tests (amd_msan, targeted) same CI database outage in the test-selection step, no test ran a fix task is moved to pending (same task as above)
Integration tests (amd_tsan, targeted) same CI database outage in the test-selection step, no test ran a fix task is moved to pending (same task as above)
Integration tests (arm_binary, distributed plan, targeted) same CI database outage in the test-selection step, no test ran a fix task is moved to pending (same task as above)

Mergeable Check and PR are the praktika rollups of the rows above. The report has no other failed check on this head; the new test 05385_function_composition_operator_in_function_call_form passed in all five flaky checks and in the other two targeted shards.

Session id: cron:our-pr-ci-monitor:20261013-113003

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the targeted-job test-selection Connection refused errors in the CI ledger above: #125613. A refused connection to the CI database is now retried for up to 3 minutes, then the targeted job skips instead of erroring.

…r-in-call-form

# Conflicts:
#	src/Parsers/ASTFunction.cpp
@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Nov 3, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 11, 2026
@alexey-milovidov alexey-milovidov added the blocker This issue / pr blocks a new release label Nov 11, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 11, 2026
Merged via the queue into ClickHouse:master with commit e05cbde Nov 11, 2026
186 checks passed
@clickgapai

Copy link
Copy Markdown
Contributor

@groeneai @alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Nov 15, 2026
… an operand of `|`

In function-call form (`CODEC`, `STATISTICS`, backup engine arguments,
`viewIfPermitted ... ELSE`, `EXPLAIN SYNTAX`) ClickHouse#125567 prints `f | g` as an
operator and groups each operand by formatting it with `need_parens`. Every
call-form function is printed as an atomic `name(args)`, except a one-argument
`not`, which is written bare (`is_plain_not_call`) because a standalone
`not(x)` parses back to the same AST. But `not(` is operator syntax for the
parser (prefix `NOT`, priority 5, below `|` at priority 8), and unlike the
operator-form `NOT` that writer ignored `need_parens`; the user's own `(NOT a)`
parentheses are dropped too, since call form suppresses the `parenthesized`
parentheses of every function. So `(NOT a) | b` was printed as `not(a) | b`,
which parses back as `not(__compose(a, b))`: debug and sanitizer builds hit the
`Inconsistent AST formatting` LOGICAL_ERROR, and `EXPLAIN SYNTAX` output for
such a query failed with `Code: 43` when run.

The bare `not(x)` writer now honours `need_parens` exactly like the operator
form, so it prints `(not(a)) | b`, which parses back to the same AST (the tree
hash ignores `parenthesized`) and formats to itself. `__compose` stays unaware
of its operands' syntax. The only other call-form caller that sets
`need_parens` is a window frame offset, which now prints `(not(0)) PRECEDING`
in `EXPLAIN SYNTAX`, matching the operator form; both texts run identically.
Declaration formatters (DEFAULT, ORDER BY, TTL, ...) set it only for an
aliased expression, and the alias formatter consumes it, so they are
unchanged.

Closes ClickHouse#130934

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release can be tested Allows running workflows for external contributors comp-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants