Repository navigation
Fix a server abort for a lambda in a query with LIMIT AFTER or LIMIT UNTIL - #122045
PedroTadim merged 10 commits into
Conversation
ExpressionActions::linearizeActions gives a node one argument per child plus one extra argument only when the node is an INPUT, and its final loop then assumes every entry of ActionsDAG::inputs has that extra argument. Constant folding in removeUnusedActions retyped a childless Function-typed node to COLUMN in place without removing it from inputs, so such an entry had no arguments at all and arguments.front() ran on an empty vector: hardened libcxx turns that into __libcpp_verbose_abort, i.e. the server dies with SIGABRT. The folded value reaches that state through a header: a lambda that captures only an IN subquery set folds to a ColumnConst holding a ColumnFunction, that constant lands in the shard header, and ReadFromRemote::addConvertingActions builds the converting DAG from the header, where addInput keeps the ColumnConst on the input node. The disjunct that let a childless node be folded was added by ClickHouse#119186 to fold a capture-free lambda; before it, !children.empty() excluded every childless node, so an input could never be retyped. Restricting the disjunct to FUNCTION nodes keeps that fold (addFunctionImpl sets type = FUNCTION and tryFoldFunctionToConstant only sets node.column, so a folded lambda is still a FUNCTION node here) while restoring the invariant, which ActionsDAG::deserialize already states and enforces with INCORRECT_DATA. Guarding the crash site was rejected: it would leave a COLUMN node in inputs, so required_columns and input_positions would silently disagree with the pipeline header. Dropping the node from inputs was rejected too, because the pipeline still delivers that column and arity would break in executeOnColumns. Reachable from plain SQL with default settings, on a stable release: SELECT arrayExists(x -> (x IN (SELECT 2)), [2]) FROM remote('127.0.0.{2,3}', system.one) LIMIT UNTIL 1; The residual NOT_IMPLEMENTED that this query then returns (a Function(...) column cannot be serialized to a shard) is pre-existing: a binary with the pre-ClickHouse#119186 guard returns exactly the same error, with the server alive. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 gate finding, plus 3 from my own
What I checked independently of the author's own evidence, by reading the code: Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20260924-160600 |
Pre-PR validation
50 runs with randomization off: 50/50 OK. 50 runs with |
|
Workflow [PR], commit [e4c6a1c] Summary: ✅
AI ReviewSummaryThe change preserves constant Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 27 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - c72f9ccEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Why #122073 owns the performance rows rather than the diff in this pull request: it records that the measured CI runs never received Nothing else failed at this head: 129 checks green, 39 skipped, Session id: cron:our-pr-ci-monitor:20260924-220033 |
`analyzeLimitRange` adds an identity actions-chain step over every column the previous step makes available, so that columns read only by the LIMIT AFTER/UNTIL boundary survive chain finalization. That list also holds the `Function` column of every lambda and the `Set` placeholder of every `IN` of the projection step, so chain finalization made the projection output them, and they crossed into the `LimitRange` step header and, on a shard, into the header sent to the initiator. Three failures follow: - When the boundary repeats a lambda of the SELECT list, the boundary DAG takes that lambda from the header as an INPUT holding the folded constant. `removeUnusedActions` retyped that INPUT to a COLUMN in place (fixed in the previous commit) and `ExpressionActions::linearizeActions` aborted the server on an empty argument vector (STID 3843-5ec7, plain local SQL; also aborts the official 26.9.9.28 build). - `LimitRangeTransform` cuts the chunk, and cutting a constant `ColumnFunction` throws NOT_IMPLEMENTED "compareAt is not implemented for Function" inside a debug-build assertion. - A shard sends the lambda column, which has no serialization: NOT_IMPLEMENTED on the initiator or, before the previous commit, the same abort in `addConvertingActions` (STID 3843-4f7e, 3843-4f98). Build the identity step over value columns only, skipping the `Set` and `Function` types. `before_interpolate_actions` already applies this rule for the same reason (ClickHouse#118420). A boundary expression that needs a lambda or a set builds its own, as it already did when its lambda text differs from the SELECT list's. Neither type can be a boundary (boundaries are boolean) or a SELECT result, so nothing above the step loses a column it reads. The test is renamed to 05260_limit_after_until_lambda and covers the local LIMIT AFTER, LIMIT UNTIL and AFTER + UNTIL shapes, the debug-build shape, the rebuilt-set path, and the two remote shapes, which now return their rows instead of NOT_IMPLEMENTED. Co-Authored-By: Claude Opus 5.5 <[email protected]>
After the planner change no SQL query delivers a `Function`-typed constant column to an `ActionsDAG` input any more, so reverting the `removeUnusedActions` change alone leaves every statement of `05260_limit_after_until_lambda` passing. The unit test builds that shape directly: an INPUT holding a folded capture-free lambda, as `buildLimitRangeConditions` adds header columns, then `removeUnusedActions()` and `ExpressionActions`. With the old condition the INPUT is retyped to COLUMN while it stays in `inputs`, and the test fails on the `ActionType::INPUT` assertion before `ExpressionActions` is built. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the INPUT-folding invariant in ActionsDAG.cpp and the placeholder note in analyzeLimitRange to one line each. Co-Authored-By: Claude Opus 5.5 <[email protected]>
LIMIT AFTER or LIMIT UNTIL
Internal second-model review: adjudication log (click to expand)Pre-publication review of this extension by an independent model (engine: codex; 2 rounds) and my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261004-163500 |
Once JIT compiles the projection, ExpressionStep converts a pipeline header that still carries the constant lambda, and the converting actions retyped that input, aborting the server. Reported as ClickHouse#123971. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t test A capture-free lambda shared by the SELECT list and WHERE of a STREAM BOUNDED read with GROUP BY ALL reaches the removeUnusedActions change without LIMIT AFTER/UNTIL (STID 3952-41f1), so a SQL test replaces the unit test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The shared lambda reaches the read-round converting actions only through PREWHERE, which CI turns off in about 5% of runs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review of this extension (#123971) by an independent model (codex, claude; 3 rounds) and my own review.
Severity: ❌ blocker / |
|
Fixing PR for the |
|
@nihalzp @alexey-milovidov you should help reviewing this |
Backport #122045 to 26.10: Fix a server abort for a lambda in a query with `LIMIT AFTER` or `LIMIT UNTIL`
… with `LIMIT AFTER` or `LIMIT UNTIL`
Closes: #123971
Closes: #124366
Closes: #126569
Related: #119186
Related: #118420
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a server abort when a query with
LIMIT AFTERorLIMIT UNTILhas a lambda in itsSELECTlist that the boundary expression repeats, or once its expressions are JIT-compiled (by default on its fourth run). Such a query over remote shards also no longer fails withSerialization is not implemented for data type Function.Description
On 26.9 (official 26.9.9.28),
SELECT number, arraySum(x -> x, range(number)) FROM numbers(6) LIMIT AFTER arraySum(x -> x, range(number + 1)) > 3aborts the server, and so does the fourth run ofSELECT (number > 3) AND arrayExists(x -> (x >= NULL), [number]), number FROM numbers(10) LIMIT UNTIL number > 5.Failing CI: local (AST fuzzer, STID 3843-5ec7), remote (AST fuzzer, STID 3843-4f98), remote (Stress test, STID 3843-4f7e), streaming (AST fuzzer, STID 3952-41f1).
Root cause.
analyzeLimitRangekeeps every projection column, including each lambda'sFunctioncolumn and eachIN'sSetplaceholder, so they reach the range step and, on a shard, the initiator:SELECT-list lambda reads it as an input holding the folded constant.removeUnusedActionsretyped that input to a column in place (Keep a lambda on its side of ARRAY JOIN #119186), andlinearizeActionsaborted.ExpressionStepadds read the lambda as such an input: the same abort.NOT_IMPLEMENTEDin debug builds.NOT_IMPLEMENTED, or the same abort on the initiator.Fix:
analyzeLimitRangeskipsSetandFunctioncolumns, asbefore_interpolate_actionsdoes (Do not keep anINset placeholder alive forINTERPOLATE#118420). A boundary that needs one builds its own.removeUnusedActionsnever retypes anINPUT.Tests:
05260_limit_after_until_lambda(local, JIT-compiled, debug-build and remote shapes; all but the labelledINstatement fail on master) and05260_streaming_queries_shared_lambda(a lambda in bothSELECTandWHEREof aSTREAM BOUNDEDread withGROUP BY ALL; with noLIMIT, only theremoveUnusedActionschange fixes it).Sibling sites
Chain steps that forward every column of the previous step:
analyzeLimitRange(fixed here) andbefore_interpolate_actions(already skips both types).analyzeWindowandanalyzeSortstart from all columns but keep only their own outputs. The same lambda inWHERE,ORDER BY,GROUP BY/HAVING,LIMIT BY,QUALIFYor a window function is fine (measured); only the range step leaked.FunctionandSettypes cannot be nested inNullable,LowCardinality,Array,MaporTuple, so the top-level type check is complete. TheSetskip changes no measured result (the labelledINcase of the test and aSELECT-listINover remote shards pass on master); it only makes the boundary build its own set, asbefore_interpolate_actionsdoes.Sites that can put a non-input node into
ActionsDAG::inputs:removeUnusedActions(fixed here).substitutebreaks the same invariant but cannot reachlinearizeActions: theExpressionActionsbuilt in that subsystem (MergeTreeIndexSet.cpp,KeyCondition.cpp) come from a rebuild whose inputs are repopulated with a type check (cloneSubDAG,foldActionsByProjection). Every other write adds anINPUTor copies a valid list, anddeserializealready rejects a non-input entry.Workflow [PR]
Sync PR [sync-upstream/pr/122045]
Version info
26.11.1.292-master(included in26.11and later)