Repository navigation
Respect short_circuit_function_evaluation when pruning files and parts by virtual columns - #125033
Conversation
…umns
The filters that prune files (file, url, object storage globs, brace
expansions and explicit keys, data lakes, S3Queue) and MergeTree parts
or system-table rows by virtual columns were ExpressionActions built
with a default ExpressionActionsSettings, whose
short_circuit_function_evaluation is DISABLE. So and/or/if/multiIf over
_file, _path or _part evaluated every argument for every file or part
name, and a branch that the guard excludes threw:
WHERE _file LIKE 'events_%' AND toDate(substring(_file, 8, 10)) >= '2024-01-01'
-> CANNOT_PARSE_DATE on 'readme.csv'
WHERE _part = 'all_1_1_0' OR intDiv(10, length(_part) - 9) > 0
-> ILLEGAL_DIVISION
even though the main filter (query settings, `enable` by default) would
not throw, and even with short_circuit_function_evaluation = 'force_enable'.
Build these filters with ExpressionActionsSettings(<query context>), as
StorageMerge already does for its filter. JIT stays off
(CompileExpressions::no). Covers VirtualColumnUtils::buildFilterExpression,
which every buildFilterExpression / filterBlockWithPredicate caller goes
through, and the ten direct constructions in StorageFile, StorageURL,
StorageObjectStorageSource, ObjectStorageQueueSource and IObjectIterator.
A guard on a non-virtual column (`b = 'a' AND toDate(substring(_file, ...))`)
still throws: the pushed-down part of the filter drops that conjunct.
That is the broader question of ClickHouse#100827.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261010-040100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261010-020600 |
|
Workflow [PR], commit [ff1fc6e] Summary: ✅
AI ReviewSummaryThis PR fixes virtual-column pruning so the Final Verdict✅ No blocking findings. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units33 translation units recompiled, 295 s compile time in total, 33 of them have a recent master baseline. |
The targeted AST fuzzers already run on small PRs and caught the `columns were captured` exception from #125033 at a higher rate per run than the untargeted ones (about 1.4% vs 0.8%), so an extra untargeted AST fuzzer adds little. The stress test checks data races under load, which nothing else in a small PR does.
…ct code The small-PR rule from ClickHouse#120996 skipped every untargeted stress test and fuzzer on a PR with fewer than 100 lines of product code. ClickHouse#125033 (60 lines in `src/`) passed its targeted fuzzers and introduced the exception `Cannot call function A because is has B arguments but C columns were captured` (STID 2458-4cb4), which `AST fuzzer (amd_msan)` and `Stress test (amd_tsan)` found on master a few hours after the merge. Keep `AST fuzzer (amd_debug)` and `Stress test (amd_tsan)` for small PRs that change at least one line of product code. PRs that change only tests, docs or CI scripts keep skipping all of them.
`cloneDAGWithInversionPushDown` gives the copy of an `indexHint` node the hint's own arguments as children, but added it with the planner's `function_base`, which `PlannerActionsVisitor` builds for no arguments. `ReadFromMergeTree::buildIndexes` passes that copy to `filterPartsByVirtualColumns`, which executes it. Eager execution ignored the mismatch (`FunctionIndexHint::executeImpl` ignores its arguments), but since ClickHouse#125033 the part-pruning filter honours `short_circuit_function_evaluation`, so a hint at a lazily executed position becomes a `ColumnFunction` over a 0-argument base with N captured columns, and `ColumnFunction::appendArguments` throws: Logical error: 'Cannot capture 1 column(s) because function indexHint has 0 arguments.' Found by the AST fuzzer (STID 3408-3735). Build the copy's base for its children through a `FunctionToOverloadResolverAdaptor` over the same `FunctionIndexHint`, as `ActionsDAG::buildFilterActionsDAGImpl` already does for hint copies. The children are kept because `StatisticsPartPruner`, `optimizeDirectReadFromTextIndex` and `WhatIfFilterAnalysis` read them; the generated name, the `UInt8` result type and the object behind `getFunction()` are unchanged, so those readers see the same DAG. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`splitFilterNodeForAllowedInputs` keeps only the conjuncts of an `and` that can be evaluated on the allowed (virtual) inputs. When two or more of them survive, it returned the copied node as is, so the node had fewer children than the `function_base` it carries was built for. Executing it eagerly happens to work, because `and` accepts any arity. Since ClickHouse#125033 these filters honour `short_circuit_function_evaluation`. A reduced `and` that is a lazily evaluated argument of another short-circuit function (for example the right side of an `or`) and has a lazy-suitable descendant is wrapped by `ExpressionActions` as a `ColumnFunction` with the stale 3-argument function and 2 captured columns, and `ColumnFunction::reduce` throws `LOGICAL_ERROR: Cannot call function and because is has 3 arguments but 2 columns were captured`. Found by the AST fuzzer in `Stress test (amd_tsan)` on master. Build a fresh `and` for the kept children instead, and convert its result back to the original type with `addBooleanCondition`, as the single-child path above already does. `ActionsDAG::removeUnusedConjunctions` rebuilds a reduced conjunction the same way. This keeps the invariant the `or` branch and the all-children-kept `and` branch rely on: every returned node has a function built for exactly its children and the input node's result type. The new test fails on master (the server aborts) and passes with the fix; a build from before ClickHouse#125033 also passes it, so the defect is master-only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #100827
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an exception when a
WHEREcondition on the_file,_pathor_partvirtual column guards a branch withif,and,orormultiIf, for exampleWHERE _file LIKE 'events_%' AND toDate(substring(_file, 8, 10)) >= '2024-01-01'. Pruning files and parts by virtual columns now respectsshort_circuit_function_evaluationinstead of evaluating the excluded branch for every file or part name.Description
On 26.8.18.2,
SELECT count() FROM file('dir/*.csv', CSV, 'a Int32, b String') WHERE if(_file LIKE 'events_%', toDate(substring(_file, 8, 10)) >= '2024-01-01', 0)overdir/events_2024-01-01.csvanddir/readme.csvthrowsCANNOT_PARSE_DATEinstead of returning 1. The same happens for_parton MergeTree (_part = 'all_1_1_0' OR intDiv(10, length(_part) - 9) > 0throwsILLEGAL_DIVISION), forurl/s3paths, and even withshort_circuit_function_evaluation = 'force_enable'. The same predicate over an ordinary column returns the right answer.Root cause: every filter that prunes files, parts or system-table rows by virtual columns was built with a default
ExpressionActionsSettings, whoseshort_circuit_function_evaluationisdisable, so all arguments ofand/or/ifwere evaluated for every file or part name.Change: build these filters with
ExpressionActionsSettings(<query context>)(asStorageMergealready does):VirtualColumnUtils::buildFilterExpression(MergeTree parts, Merge, system tables) and the file-list filters offile,url, object storage, data lakes and S3Queue. JIT stays off.Test
05353_virtual_column_filter_short_circuitcoversfile, MergeTree,s3(glob, brace expansion, single key) andurl, plus twodisablecontrols that still throw. It fails on master and passes with the fix; reverting any single covered site turns exactly its arms red.Not changed: a guard on a non-virtual column (
b = 'a' AND toDate(substring(_file, 8, 10)) >= ...) still throws, because the pushed-down part of the filter drops that conjunct; that is the broader question of #100827.Workflow [PR]
Sync PR [sync-upstream/pr/125033]
Version info
26.10.1.2036-master(included in26.10and later)