Repository navigation
Keep the empty IN set early stop of a filter when its AND is JIT-compiled - #131211
Conversation
…iled FilterTransform::prepare ends the read when the filter is false for every row once the sets are built. isAlwaysFalseByEmptySet (ClickHouse#116538) finds an `in`/`globalIn` over an empty set by following `and` nodes by name. With compile_expressions, ActionsDAG::compileFunctions replaces a compilable `and` with an LLVMFunction whose name is the CompileDAG dump and whose children are the INPUT nodes of the compiled DAG. The `in` (not compilable) becomes one of those children, but the walk stops at the compiled node because it is not named `and`. The updateHeader dry run does not fold it either: a 0-row `in` over a Nullable column is not constant, and LLVMExecutableFunction folds only when all arguments are constant. So the source is read to the end of the key range and every row is filtered out afterwards (ClickHouse#131133: 14 ms -> 203 ms on the synthetic repro, 29.7 s -> 214.6 s on the original query). ActionsDAG::getCompiledConjuncts, next to getCompiledFunctions, maps the i-th INPUT of the compiled DAG to the i-th child of the node (the order getCompilableDAG appends them in and compileFunctions swaps them in) and returns the children reached from the root through `and` FUNCTION nodes only. isAlwaysFalseByEmptySet recurses into them for a compiled node. Any other shape (non-LLVM function, a root that is not `and`, an input count mismatch) returns nothing, which is the previous behaviour. With query_plan_merge_filters (default), FilterStep splits AND atoms into separate filters, so a flat `a > 10 AND x IN (...)` leaves `and(<input>, in)` with no compilable child and is never compiled. The `and` is compiled when the remainder keeps a nested `and` (the issue's shape) or with query_plan_merge_filters = 0; the test covers both. Closes ClickHouse#131133 Co-Authored-By: Claude Opus 5.5 <[email protected]>
isAlwaysFalseByEmptySet ends the read at prepare time when a conjunct is an IN over an empty set. It did not check FutureSet::isMutableDuringQuery: the set of an ENGINE = Set table is the very Set object that StorageSet::insertBlock fills, so a concurrent INSERT can make it non-empty while the query runs. Index analysis already refuses such sets (05078); with the previous commit every JIT-compiled AND reaches this walk, so a compiled filter over an empty Set table would now stop reading. Refuse mutable sets, for the uncompiled walk as well, so both paths agree. With transform_null_in = 1 the analyzer rewrites IN to nullIn. Without the JIT the header dry run folds it (05045), but under a compiled AND it cannot fold (LLVMExecutableFunction folds only all-constant arguments) and the walk rejected the name, so the slowdown from ClickHouse#131133 remained for the same query under that setting. nullIn(x, empty set) is 0 for every x, NULL included, so accept nullIn and globalNullIn. The test gains a transform_null_in = 1 assert and an empty ENGINE = Set table that must still be read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reword the empty-set fold comment: the negative variants never make the filter always false (NULL NOT IN () is NULL, not true). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (click to expand)
|
|
cc @Manerone, could you review this? The empty IN set early stop of a filter matched and nodes by name, so a JIT-compiled AND hid the empty IN and the read went to the end; the walk now sees through the compiled node via ActionsDAG::getCompiledConjuncts. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261113-064700 |
|
Workflow [PR], commit [b843736] Summary: ✅
AI ReviewSummaryRestores early read termination for empty immutable Missing context / blind spots
Final Verdict✅ Approve — no code changes requested; normal CI gates still apply. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units1002 translation units recompiled, 9936 s compile time in total, 1002 of them have a recent master baseline. |
CI finish ledger - 82f9e95Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261113-133009 |
# Conflicts: # src/Interpreters/ExpressionJIT.cpp
|
Update on the
So the only fix I see is in the gate, and that is your call, @egor-click. Should the perf job:
|
|
🕵 Merged |
The walk returns only the INPUTs of the compiled `and` chain. That finds every `in` conjunct because `FunctionIn` does not override `isCompilableImpl` and a `Set` argument fails `canBeNativeType`, so an `in` always stays a child of the compiled node. Make that dependency visible where it lives. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Fixing PR for the |
Resolve the conflict in src/Interpreters/ExpressionJIT.cpp: the new ActionsDAG::getCompiledConjuncts sits right above compileFunctions, whose parameter type differs on 26.10 (std::unordered_set; master switched it to UnorderedSetWithMemoryTracking in #129617, which is not on 26.10). Keep the 26.10 signature and add only the lines of #131211. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #131211 to 26.10: Keep the empty `IN` set early stop of a filter when its `AND` is JIT-compiled
…lter when its `AND` is JIT-compiled
Backport #131211 to 26.10: Keep the empty `IN` set early stop of a filter when its `AND` is JIT-compiled
Closes: #131133
Related: #116538
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A query that combines
x IN (subquery)with other conditions byANDnow stops reading as soon as the subquery returns an empty set also whencompile_expressionshas compiled theAND(by default once the same expression has been seenmin_count_to_compile_expressiontimes). Before, such a query, for example withFINAL, read the whole key range and only then filtered every row out.Description
Reported by @ fm4v: 14 ms -> 203 ms on the synthetic repro, 29.7 s -> 214.6 s and twice the peak memory on the original query.
FilterTransform::prepareends the read when a conjunct of the filter isin/globalInover an empty set (isAlwaysFalseByEmptySet, #116538). That walk followsandnodes by name. When the JIT compiles theAND,ActionsDAG::compileFunctionsreplaces the node's function with anLLVMFunctionwhose children are the inputs of the compiled DAG, so the walk never reaches thein. The header dry run cannot fold it either: the compiled function folds only when all its arguments are constant, and a 0-rowinover aNullablecolumn is not constant.This PR adds
ActionsDAG::getCompiledConjuncts, next togetCompiledFunctions: for a compiled node whose root is anandchain, it returns the children that are conjuncts of that chain, and the walk recurses into them. TheANDstays compiled. The walk also acceptsnullIn/globalNullIn(INwithtransform_null_in = 1), and, like index analysis, it no longer trusts the emptiness of a set that can grow while the query runs (FutureSet::isMutableDuringQuery, anENGINE = Settable), so such a filter now reads with the JIT off too.Validation: the new test asserts
read_rows = 0for six filter shapes (non-NullableandNullableIN,FINAL, a nestedandinside the compiled function,query_plan_merge_filters = 0,transform_null_in = 1); master reads 90112 or 100000 rows for each. An emptyENGINE = Settable still reads. On the issue's schema with 4M rows and JIT on,SelectedRowsis 4,061,765 on master and 5,010 with this PR, the same as with JIT off. 50 randomized runs of the new test and05045_empty_in_set_short_circuit: no failures.collectConstantColumnsAfterFilteralso matchesandby name, but it does not change how much is read.Workflow [PR]
Sync PR [sync-upstream/pr/131211]
Version info
26.11.1.2498-master(included in26.11and later)26.10.8.37