Skip to content

Fix inconsistent AST formatting of an IN operand of a tuple element in a function argument - #124325

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-inconsistent-ast-in-under-tuple-element
Oct 7, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-inconsistent-ast-in-under-tuple-element

Conversation

@groeneai

@groeneai groeneai commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #114883

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Formatting of a query where an IN is the left operand of a tuple element ((x IN (1)).1) inside a multi-argument function call is now stable across a format-parse-format round trip.

Description

Requested in #114883 (comment). No related open issue found.

BuzzHouse (amd_debug) aborts with Inconsistent AST formatting between 'ExpressionList' and 'ExpressionList' (STID 1941-1bfa) when an IN is the left operand of a .N tuple element inside a multi-argument call. Failing jobs: #114883, #117187. Minimal repro: SELECT f((40 IN (1)).1[4], 38) is formatted as f((((40 IN (1))).1)[4], 38) and re-formatted as f(((40 IN (1)).1)[4], 38).

The tupleElement branch of ASTFunction::formatImplWithoutAlias wraps a compound left operand in its own parens but keeps current_function set, so a direct IN child adds one more pair as if it were a bare argument of f. After re-parsing, the outer pair becomes the tuple element's parenthesized flag, whose emission clears current_function, and the IN pair disappears.

The fix clears current_function for that wrapped operand, as the parenthesized flag path, the IN's own wrap and the alias wrap already do. The only other output change: a redundant pair around such an IN is no longer printed (f(((40 IN (1))).1, 38) becomes f((40 IN (1)).1, 38)). Both forms parse to the same AST, on old and new versions.

Not a Bug Fix: the check is debug-only, and a tuple element over an IN result is never type-valid, so a release build (measured on 26.10.1.448) rejects these queries with ILLEGAL_TYPE_OF_ARGUMENT before and after.

Validation: the new test aborts a base server and passes 50/50 randomized runs with the fix; the formatter and parser stateless tests and parser gtests pass.


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

Version info

  • Merged into: 26.10.1.1787-master (included in 26.10 and later)

…function argument

`SELECT f((40 IN (1)).1[4], 38)` formatted as `f((((40 IN (1))).1)[4], 38)`
and re-formatted as `f(((40 IN (1)).1)[4], 38)`, so the debug AST round-trip
check aborted with "Inconsistent AST formatting" (BuzzHouse, STID 1941-1bfa).

The `tupleElement` branch wraps a compound left operand in its own parens,
but left `current_function` set. A direct IN child then added one more pair
because it believed it was still a bare argument of `f`. After re-parsing,
the outer pair becomes the `parenthesized` flag of the tuple element, whose
emission clears `current_function`, so the IN pair disappears.

Clear `current_function` for that wrapped operand, as the flag path in
`decideParensEmission`, the IN's own wrap and the alias wrap already do.
The only other visible change is that a redundant pair around such an IN is
no longer printed, e.g. `f(((40 IN (1))).1, 38)` is now `f((40 IN (1)).1, 38)`;
both forms parse to the same AST.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 6, 2026
@groeneai

groeneai commented Oct 6, 2026

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 a separate cold review of the diff (0 blocker/major).

# Sev Finding Verdict Evidence / action
- - No findings - Checked: every formatter site that emits parens for a child (parenthesized flag path, alias wrap, the IN's own wrap, negate/NOT, IN right-hand side, tuple/array/index) either already clears current_function or cannot place an IN pair next to its own; the only reader of current_function is the IN branch.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-11:20261006-220700

@groeneai

groeneai commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT f((40 IN (1)).1[4], 38) on a debug binary aborts every time (rc 134, Inconsistent AST formatting); formatQuerySingleLine(q) = formatQuerySingleLine(formatQuerySingleLine(q)) returns 0 on any build.
b Root cause explained? Yes. The tupleElement left-operand wrap emitted its own parens but kept current_function, so a direct IN child added one more pair. After re-parsing, the outer pair becomes the tuple element's parenthesized flag, whose emission clears current_function, and the IN pair disappears.
c Fix matches root cause? Yes. The wrapped operand's frame now clears current_function, at the site that emits the parens, like the flag path, the IN's own wrap and the alias wrap. No guard in the round-trip check.
d Test intent preserved / new tests added? Yes. New 05331_inconsistent_ast_in_under_tuple_element_in_function_arg: 3 statements that abort an unfixed debug server, 11 idempotence rows, 2 canonical forms.
e Both directions demonstrated? Yes. Base server: FAIL, server died with the logical error; base formatter rows all 0. Fix: OK, and 50/50 randomized runs.
f Fix is general across code paths? Yes. Every site that suppresses a child's own parens was checked; only this one can reach an IN with a stale current_function. Ancestors [M], binary operator, nested .N, and an array index were measured: no divergence remains.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. IN, NOT IN, GLOBAL IN, != SOME (rewritten to notIn), aliased IN, first and second argument, VALUES arguments, .N.M[K]. Baselines kept for 1-argument, top-level and tuple/array literal shapes. Type wrappers do not apply (syntax only).
h Backward compatible? (maintainer-approved exception only) Yes. Formatting only. Old and new text give the same EXPLAIN AST on both versions; the only change is a dropped redundant pair.
i Invariants and contracts preserved? Yes. A node inside parens that its parent emitted sees the same frame as one with its own parenthesized parens (need_parens=false, current_function=nullptr). No other frame field changes.

Session id: cron:clickhouse-impl-slot-4:20261006-210400

@clickhouse-gh

clickhouse-gh Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [03e480a]

Summary: ✅


AI Review

Summary

This PR fixes the stale current_function context in the tupleElement dot-syntax formatter when the left operand is manually wrapped in parentheses, which is the path that made IN under .N format differently after a parse round trip inside multi-argument calls. The change is narrowly scoped to the broken formatter branch, matches the documented meaning of current_function, and the new stateless test covers the original reproducer plus nearby variants (NOT IN, GLOBAL IN, aliases, nested indexing, and VALUES).

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 03e480a.

Metric Baseline Current Δ
Lines 88.20% 88.30% +0.10%
Functions 92.10% 91.80% -0.30%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code

Full report · Diff report

@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 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 03e480a3f with master af72d84f3 (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

@alexey-milovidov alexey-milovidov self-assigned this Oct 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 7, 2026
Merged via the queue into ClickHouse:master with commit d2d406e Oct 7, 2026
149 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 7, 2026
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-sql-syntax SQL/grammar parsing, AST nodes, syntax-level features. groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-not-for-changelog This PR should not be mentioned in the changelog 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.

3 participants