Skip to content

Fix inconsistent AST formatting for aliased lambda under access operator (STID 1941-1bfa) - #107092

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-lambda-under-operator-list-index-1941-1bfa
Jul 17, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-lambda-under-operator-list-index-1941-1bfa

Conversation

@groeneai

@groeneai groeneai commented Jun 10, 2026 •

Copy link
Copy Markdown
Collaborator

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 an Inconsistent AST formatting logical error that could abort the server in debug and sanitizer builds when an aliased lambda was used as the operand of an access operator (.N tuple element or [] array element) at a non-first position of an expression list, e.g. SELECT 1, ((p0, p1) -> p0 AS a7).4[3] FROM t.

Description

Found by the BuzzHouse / AST fuzzer (AST fuzzer (amd_debug, targeted), BuzzHouse (amd_debug)) under STID 1941-1bfa. The error message is Inconsistent AST formatting between 'ExpressionList' and 'ExpressionList' in the query: ....

CI provenance (one of many sightings; this exact shape from PR #106686 BuzzHouse (amd_debug)):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106686&sha=77905b46be20b0fdc784218bfb395556b00f316a&name_0=PR&name_1=BuzzHouse%20%28amd_debug%29

Minimal reproducer (aborts on master debug build, returns a normal type error after this fix):

SELECT 1, ((p0, p1) -> p0 AS a7).4[3] FROM t

Root cause: FormatStateStacked::list_element_index records a node's index among the direct elements of the enclosing expression list and is only meaningful one level deep. The lambda formatter self-parenthesizes when the index is greater than zero so that f(x, y -> z) is not misformatted as f((x, y) -> z). ASTFunction::formatImplWithoutAlias derived its nested_need_parens / nested_dont_need_parens frames from the parent frame without clearing list_element_index, so the index of the outermost SELECT-list element leaked down through the access operators into the nested lambda. The lambda then emitted an extra pair of parens, and because the access operator also wrapped it, the format-parse-format round-trip diverged by one paren level and the internal consistency check in executeQuery aborted.

The fix resets list_element_index to 0 on the two nested frames where they are created. The argument-list loops that legitimately rely on the index (array, tuple, map, and the generic argument loop) re-set it explicitly per argument afterwards, so the lambda-as-direct-list-element case (SELECT 1, x -> x stays SELECT 1, (x -> x)) and the lambda-as-non-first-function-argument case (f(x, y -> z)) are unaffected.

This is one of several distinct root causes that share the generic 1941-1bfa error-message hash; it is independent of the parser-side fixes in #105839 and #106691 (ExpressionListParsers.cpp) and the CODEC/STATISTICS/BACKUP_NAME formatter fix already merged in #104991.

Version info

  • Merged into: 26.7.1.1062 (included in 26.7 and later)

…tor (STID 1941-1bfa)

A parenthesized and aliased lambda used as the operand of an access operator
(tupleElement ".N" or arrayElement "[]") aborted the server with
'Inconsistent AST formatting' LOGICAL_ERROR in debug and sanitizer builds when
the expression was placed at a non-first position of an expression list. For
example:

  SELECT 1, ((p0, p1) -> p0 AS a7).4[3] FROM t

formatted as "((((p0, p1) -> p0) AS a7).4)[3]" on the first pass but as
"(((p0, p1) -> p0 AS a7).4)[3]" after parsing the formatted text back and
reformatting, so the format-parse-format round-trip diverged by one paren
level and the internal consistency check in executeQuery aborted.

Root cause: FormatStateStacked::list_element_index records a node's index among
the direct elements of the enclosing expression list and is only meaningful one
level deep. The lambda formatter self-parenthesizes when the index is greater
than zero so that f(x, y -> z) is not misformatted as f((x, y) -> z).
ASTFunction::formatImplWithoutAlias derived its nested_need_parens and
nested_dont_need_parens frames from the parent frame without clearing
list_element_index, so the index of the outermost SELECT-list element leaked
down through the access operators into the nested lambda. The lambda then
emitted an extra pair of parens, and because the access operator also wrapped
it, the round-trip became unstable.

The fix resets list_element_index to 0 on the two nested frames at the point
they are created. The argument-list loops that legitimately rely on the index
(array, tuple, map, and the generic argument loop) re-set it explicitly per
argument afterwards, so the lambda-as-direct-list-element case and the
lambda-as-non-first-function-argument case are unaffected.

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. SELECT 1, ((p0, p1) -> p0 AS a7).4[3] FROM t aborts the master debug build 100% of the time (SIGABRT, Inconsistent AST formatting between 'ExpressionList' and 'ExpressionList'). A 432-combination differential round-trip sweep over lambda/operator/list-position shapes found 0 divergences before-vs-after only on the fixed binary.
b Root cause explained? Yes. FormatStateStacked::list_element_index is the node's index among the direct elements of its enclosing list and is only valid one level deep (the lambda formatter self-parenthesizes when it is > 0 so f(x, y -> z) is not printed as f((x, y) -> z)). ASTFunction::formatImplWithoutAlias built nested_need_parens/nested_dont_need_parens from the parent frame without clearing the index, so the SELECT-list element index leaked down through the access operators (tupleElement, arrayElement) into the nested lambda. The lambda then emitted an extra paren, the access operator wrapped it again, and the format-parse-format round-trip diverged by one paren level (((expr) AS a) vs (expr AS a)).
c Fix matches root cause? Yes. The fix resets list_element_index = 0 on the two nested frames at the exact point of leakage. It is not a band-aid: it restores the field's documented one-level-deep semantics. The legitimate consumers (array/tuple/map/generic-arg loops) re-set the index per-argument afterwards, so they keep working.
d Test intent preserved / new tests added? New regression test 04327_inconsistent_ast_aliased_lambda_under_operator_1941_1bfa.{sql,reference}. It asserts the repro no longer aborts (executes to a normal ILLEGAL_TYPE_OF_ARGUMENT/UNKNOWN_IDENTIFIER), locks the canonical formatting via formatQuerySingleLine, and explicitly protects the two behaviors that MUST be preserved: lambda as a direct non-first list element (SELECT 1, x -> x stays (x -> x)) and lambda as a non-first function argument (f(a, (x, y) -> x)). The prior #104991 regression test (04241) produces byte-identical output before and after this change.
e Both directions demonstrated? Yes. Old binary: SIGABRT (exit 134, LOGICAL_ERROR). New binary: returns a clean ILLEGAL_TYPE_OF_ARGUMENT (the round-trip check passes), and the reference matches exactly.
f Fix is general, not a narrow patch? Yes. The reset is applied once at the common source of both nested frames in ASTFunction::formatImplWithoutAlias, so every operator operand path benefits, not just tupleElement/arrayElement. The 432-shape differential sweep (.N, [], chained, unary/binary operators, nested lambdas, varying list positions) shows no remaining divergence and no regression. This is a distinct root cause from the sibling 1941-1bfa parser-side fixes (#105839, #106691 in ExpressionListParsers.cpp) and the merged formatter fix #104991 (allow_operators=false tuple wrapping); no overlap.

Session id: cron:clickhouse-worker-slot-1:20260610-212800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @evillique @george-larionov — could you review? This is a new root cause under the chronic STID 1941-1bfa umbrella @alexey-milovidov asked us to track: ASTFunction::formatImplWithoutAlias leaked the SELECT-list list_element_index into nested operator operands, so an aliased lambda under .N/[] self-parenthesized inconsistently and broke the format-parse-format round-trip. Fix resets the index on the nested frames; distinct from the parser-side siblings #105839/#106691.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4ffca9c]

Summary: ✅


AI Review

Summary

I did not find any correctness or coverage issues in the current diff. The list_element_index reset in ASTFunction::formatImplWithoutAlias matches the leak described in the PR, and the new stateless test covers the reproducer plus the key non-regression shapes. Residual gap: I did not run a local debug binary in this checkout because no build artifacts were present, so this verdict is based on code inspection and the green CI report.

Final Verdict

Status: ✅ Approve

@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Dear @evillique, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@evillique evillique self-assigned this Jul 15, 2026
@evillique

Copy link
Copy Markdown
Member

@groeneai merge master

@groeneai

Copy link
Copy Markdown
Collaborator Author

Merged current master into the branch (guarded: no submodule/contrib drift, net diff is still just the ASTFunction.cpp fix + regression test). CI re-running on the merged head.

@clickhouse-gh

clickhouse-gh Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 28.60% 85.80% +57.20%
Functions 39.40% 92.70% +53.30%
Branches 20.60% 78.00% +57.40%

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

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 4ffca9c

CI finished on the post-merge-master commit. No test failures. No PR-caused failures.

Check / test Reason Owner / fixing PR
(no failing checks) — —
CH Inc sync — CH Inc sync (private, not actionable)

Session id: cron:our-pr-ci-monitor:20260716-000000

@alexey-milovidov
alexey-milovidov merged commit f73263b into ClickHouse:master Jul 17, 2026
175 of 176 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 Jul 17, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…a-under-operator-list-index-1941-1bfa

Fix inconsistent AST formatting for aliased lambda under access operator (STID 1941-1bfa)
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 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