Repository navigation
Fix inconsistent AST formatting for aliased lambda under access operator (STID 1941-1bfa) - #107092
Conversation
…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]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-1:20260610-212800 |
|
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: |
|
Workflow [PR], commit [4ffca9c] Summary: ✅
AI ReviewSummaryI did not find any correctness or coverage issues in the current diff. The Final VerdictStatus: ✅ Approve |
|
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. |
|
@groeneai merge master |
…-under-operator-list-index-1941-1bfa
|
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. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code |
CI finish ledger — 4ffca9cCI finished on the post-merge-master commit. No test failures. No PR-caused failures.
Session id: cron:our-pr-ci-monitor:20260716-000000 |
…a-under-operator-list-index-1941-1bfa Fix inconsistent AST formatting for aliased lambda under access operator (STID 1941-1bfa)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an
Inconsistent AST formattinglogical error that could abort the server in debug and sanitizer builds when an aliased lambda was used as the operand of an access operator (.Ntuple 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 STID1941-1bfa. The error message isInconsistent 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):
Root cause:
FormatStateStacked::list_element_indexrecords 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 thatf(x, y -> z)is not misformatted asf((x, y) -> z).ASTFunction::formatImplWithoutAliasderived itsnested_need_parens/nested_dont_need_parensframes from the parent frame without clearinglist_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 inexecuteQueryaborted.The fix resets
list_element_indexto 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 -> xstaysSELECT 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-1bfaerror-message hash; it is independent of the parser-side fixes in #105839 and #106691 (ExpressionListParsers.cpp) and theCODEC/STATISTICS/BACKUP_NAMEformatter fix already merged in #104991.Version info
26.7.1.1062(included in26.7and later)