Repository navigation
Conversation
…ilt yet ExecutableFunctionCapture::executeImpl decided that a lambda may be advertised as constant using a bare isConst() over its captured arguments. A captured IN set arrives as ColumnConst(ColumnSet), and isConst() is true for it whether or not the set has been built, so during header evaluation the lambda was wrapped in ColumnConst while no set existed yet. The higher-order function above it then had all-constant arguments, so defaultImplementationForConstantArguments evaluated it once with dry_run = true and wrapped the result in ColumnConst, and the value it wrapped was the dummy FunctionIn reserves for a not-ready set. Two consumers read that dummy as an answer: FilterTransform::prepare trusts ConstantFilterDescription(*column).always_false and closes its input, dropping every row, and a derived table's projection returns the frozen 0. The reported shapes are in issue ClickHouse#114581, on official builds 26.8.1.1068 and 26.8.1.1194. FunctionIn already declines that promotion itself (useDefaultImplementationForConstants() == false), which is why WHERE 1 IN (SELECT 1) is correct today; the capture defeated the opt-out one level up, by making the higher-order function eligible for exactly the promotion in had refused. This is a readiness gate rather than the removal of an optimization: once CreatingSets has built the set the same fold is correct and still happens, so projection results and isConstant do not change. The rule already existed in two carriers, getFunctionArguments for a function's direct arguments and isLambdaBodySuitableForConstantFolding for a lambda body's COLUMN nodes, whose comment already points at the first one; the execution-time captured-argument path was the carrier missing it. Their duplicated copy of the rule is now one shared predicate. typeid_cast<const ColumnConst *>(...) != nullptr is exactly equivalent to isConst(): isConst() is overridden only by ColumnConst, against an IColumn default of false, so the only behavioural delta is the new gate. Alternatives rejected. Gating on isLambdaBodySuitableForConstantFolding does not work: the set is a captured argument, an INPUT inside the lambda's own DAG that the body walk skips, not a body node. Stopping FilterTransform from trusting a dry-run constant is the symptom site, would have to be repeated at every filter, and the always_false early exit is a wanted optimization that WHERE 1 IN (SELECT 2) relies on. Stopping defaultImplementationForConstantArguments from const-wrapping under dry_run is far broader, since updateHeader depends on const propagation. Making in's not-ready dummy self-identifying would break columnMatchesType, and in has already done its part correctly. Closes: ClickHouse#114581
The changelog entry claims the defect reached the array* and map* higher-order families, in WHERE, HAVING, projections and derived tables, with IN, NOT IN and GLOBAL IN, but every set-bearing arm of the test was arrayExists with a local IN, so a partial regression that kept that one shape correct while re-breaking any of the other four dimensions would have left the suite green. Each new arm is shaped so the correct answer is a returned row, because the dummy a not-ready set answers with is false: an arm expecting an empty result would be satisfied by the defect and could never redden. Measured through a server on both builds. On the base build 6ce5a90c the three IN-variant arms return no rows where they must return one, and the map* arm kills the server outright with 'Not-ready Set is passed as the second argument for function in' because FunctionMapToArrayAdapter does not forward the dry-run flag; on 65fdd40d all four are correct. The pre-existing arms keep their values on both. The map* arm's own spelling differs from the map* cases in ClickHouse#119907, which fixes that missing dry-run forwarding at its source; if it merges first this arm stays as a user-visible contract pin for the same query.
Read literally the first clause was false. ColumnSet derives from IColumnDummy,
which does not override isConst(), so ColumnSet::isConst() is false; isConst()
has exactly two definitions in the tree, ColumnConst's returning true and
IColumn's returning false. A set is const because it arrives wrapped in
ColumnConst, which is how ActionsDAG states it at the site that wraps it
("ColumnSet is now always wrapped in ColumnConst"), and that wrapper is what the
predicate looks through.
Comment only. Verified by stripping comments from both revisions and comparing
the remaining code, with a deliberately perturbed copy confirming the comparison
detects a real change.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds, 2 findings; my own cold
Severity: ❌ blocker / Two things the review checked itself rather than taking from the implementation notes, in case they Session id: cron:clickhouse-review-self-slot-58:20260914-110900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20260914-104700 |
|
Workflow [PR], commit [06d0633] Summary: ❌
AI ReviewSummaryThis PR prevents constant promotion of lambdas that capture an unbuilt Missing context / blind spots
Final VerdictStatus: ✅ Approve. No new inline findings; the previously resolved nested-lambda concern does not hold in the current code. |
A code review asked whether a lambda nested inside another one still reaches the readiness gate. It does, by two routes, and which one applies depends on the inner lambda: when it references the outer parameter the analyzer flattens both into a single capture whose list holds the set directly (Capture[Set, UInt8, Array(UInt8)]), and when it does not, the inner lambda is hoisted out of the outer body, so what the outer lambda captures is the inner capture's result, which the gate has already refused to fold. Either way no ColumnConst reaches FilterTransform::prepare. The arm is live rather than one the defect would satisfy: on a build of this branch's base it returns no rows, against one row with the fix, measured through a server one statement at a time.
The comment claimed that spelling builds its set by another route, which is not true of the local query it annotates: src/Planner/CollectSets.cpp does not mention `global` at all, so set collection does not distinguish `in` from `globalIn`, and the GLOBAL-specific handling needs a distributed join tree. The arm itself stays, since it pins a distinct spelling and reddens on the base build.
The description claims this gate also fixes the all-constant map* and nested arrayFold spellings that abort on master with `Not-ready Set`. The map* half was already pinned; this is the arrayFold half, which is worth its own arm because arrayFold is a separate implementation rather than a FunctionArrayMapped specialization, so a regression could reach it while every other arm stays green. Exit code asserted on both builds: the arm aborts the base build (134, `Not-ready Set` logical error) and returns [1] with the fix.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units24 translation units recompiled, 178 s compile time in total, 24 of them have a recent master baseline. |
CI finish ledger - ca4b398Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260914-190625 |
|
Fixing PR for It stops |
|
@groeneai Please continue working on this PR. Merge latest master, check failing tests. Prepare this PR for merging. |
|
Merged master (
CI is running on the new head. What is left for merging is a review: none has been submitted yet. |
…unbuilt-in-set Resolved the conflict in src/Functions/FunctionsMiscellaneous.h: master's isLambdaBodyWithObservableSideEffects (85bfa64) is kept byte-identical, and columnHoldsUnbuiltSet follows it, unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Merged master again at |
CI finish ledger - 06d0633Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The build failure dropped 166 dependent jobs, so no functional test has run on this head yet. Session id: cron:our-pr-ci-monitor:20261016-130004 |
Closes: #114581
Related: #119907
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results when a lambda given to a higher-order function contains an
INsubquery and the function's other arguments are constant:SELECT number FROM numbers(1) WHERE arrayExists(x -> x IN (SELECT 1), [1, 2])silently returned no rows. It affected thearray*andmap*higher-order families, inWHERE,HAVING, projections and derived tables, withIN,NOT INandGLOBAL IN. Closes #114581.Description
ExecutableFunctionCapture::executeImpldecided that a lambda may be advertised as constant with a bareisConst()over its captured arguments. A captured set arrives asColumnConst(ColumnSet), which is const whether or not the set is built, so during header evaluation the lambda became a constant, the higher-order function above it became eligible for constant promotion, and the promoted value wasFunctionIn's not-ready-set dummy.FunctionIndeclines that promotion itself (useDefaultImplementationForConstants() == false), which is whyWHERE 1 IN (SELECT 1)is correct; the capture defeated the opt-out one level up.FilterTransform::preparethen readalways_falseoff that constant and closed its input; a derived table returned the frozen0.The fix is a readiness gate, not the removal of an optimization: a captured argument counts as foldable only if it is a
ColumnConstthat does not hold an unbuilt set. That rule already exists for a function's direct arguments (getFunctionArguments) and for a lambda body'sCOLUMNnodes; the execution-time captured-argument path was the carrier missing it. Once the set is built the same expression folds as before, so projection results andisConstantdo not change.Not addressed here: the execution-time half of
43e6af36863c1, where a non-deterministic lambda over a constant array is still drawn once per block (it needs that walk precomputed at capture construction), and theenable_analyzer = 0Code: 47note in #114581. I kept both out so this stays one rule, but I will fold the first into this PR if its author would rather have it here than separate.Side effect: the all-constant
map*and nestedarrayFoldspellings that abort on master withNot-ready Setnow return correct results, since the promotion that carried the dry-run flag into those callees no longer happens. #119907 fixes that forwarding at its source and covers routes this gate does not reach.Every shape reported in #114581, measured on two builds of this branch's base (click to expand)
Base
6ce5a90c1e195d52054ee8f93939be30bc283674, with the fix65fdd40de186dc5f15895218db4c56705dd550d3. Exit code asserted on every probe.SELECT arrayExists(x -> x IN (SELECT 2), [2])111SELECT * FROM (SELECT arrayExists(x -> x IN (SELECT 2), [2]))011SELECT arrayFilter(x -> x IN (SELECT 2), [1, 2, 3])[2][2][2]SELECT * FROM (SELECT arrayFilter(x -> x IN (SELECT 2), [1, 2, 3]))[][2][2]SELECT arrayMap(x -> x IN (SELECT '2'), [2, 3])[1,0][1,0][1,0]SELECT * FROM (SELECT arrayMap(x -> x IN (SELECT '2'), [2, 3]))[0,0][1,0][1,0]SELECT count() FROM (SELECT 1 AS k) WHERE arrayExists(x -> x IN (SELECT 1), [k])011WITH tm1 AS (SELECT arrayExists(x -> x IN (SELECT 2), [2])) SELECT * FROM tm1011New test
05215_lambda_capturing_unbuilt_in_setfails on the base build and passes with the fix; 50 randomized runs green; the 49 in-tree tests naming a higher-order function overIN (,isConstant(ormap*overIN (pass on both builds.Workflow [PR]
Sync PR [sync-upstream/pr/119938]