Skip to content

Rebuild a reduced and when pruning parts and files by virtual columns - #125126

Merged
antaljanosbenjamin merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/virtual-column-filter-partial-and
Oct 13, 2026
Merged

antaljanosbenjamin merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/virtual-column-filter-partial-and

Conversation

@groeneai

@groeneai groeneai commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #125033
Related: #125150

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Description

On master since #125033, pruning parts and files by virtual columns can fail with Cannot call function and because is has 3 arguments but 2 columns were captured. (STID 2458-4cb4, Stress test (amd_tsan) on master), a logical error that aborts debug and sanitizer builds:

SELECT count() FROM t WHERE _partition_id = '3' OR (id < 1000 AND _partition_id != '0' AND intDiv(10, length(_partition_id)) > 0)

splitFilterNodeForAllowedInputs keeps only the conjuncts of an and computable from the virtual columns, but returned the copy with the original function_base, built for all the arguments. Eager execution tolerates that; since #125033 these filters honour short_circuit_function_evaluation, so the reduced and in a lazily evaluated position becomes a ColumnFunction that rejects its captured columns. The reduced and is now rebuilt for the kept children and its type converted back (as ActionsDAG::removeUnusedConjunctions does).

The sibling indexHint defect on the same path (STID 3408-3735) was fixed by #125150, which is now in master, so this PR carries only the and fix.

The new test 05359 fails on master and passes with this change (50 randomized runs), and its max_rows_to_read limit checks that pruning still happens. The defect is master-only (#125033 is in no release), hence Not for changelog.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/125126]

Version info

  • Merged into: 26.10.1.2290-master (included in 26.10 and later)

groeneai and others added 2 commits October 11, 2026 00:02
`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]>
`max_rows_to_read` fails the test if the virtual-column filter stops pruning partition 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 11, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)
Finding Verdict
⚠️ The test's counts are 300 with or without part pruning, so it did not check that partition 0 is still pruned AGREE, fixed: the test sets max_rows_to_read = 300 (fails with TOO_MANY_ROWS when pruning is disabled)
💡 Only the MergeTree surface is tested; file/url/object storage path filters use the same function Noted: one surface exercises the changed lines

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. clickhouse local running the new test file (or the exact fuzzer query from the CI fatal.log) on a debug build of master 2b7371437b95e aborts every run with Cannot call function and because is has 3 arguments but 2 columns were captured, same stack as CI (ColumnFunction::reduce <- FunctionAnyArityLogical<AndImpl>::executeShortCircuit <- VirtualColumnUtils::filterBlockWithExpression <- filterPartsByVirtualColumns).
b Root cause explained? splitFilterNodeForAllowedInputs drops the conjuncts of an and that cannot be computed from virtual columns, but returned the copy with the original function_base (3 argument types) over 2 children. Since #125033 the filter runs with short-circuit evaluation, so that and in a lazy position is wrapped as ColumnFunction with 2 captured columns, and reduce rejects the arity mismatch.
c Fix matches root cause? Yes. The malformed node is fixed where it is built: a fresh and over the kept children plus a result-type conversion. The arity check in ColumnFunction::reduce is correct and unchanged; #125033 is kept.
d Test intent preserved / new tests added? New stateless test 05359_virtual_column_filter_partial_and (three queries: default settings, force_enable, a dropped Nullable conjunct); its max_rows_to_read = 300 limit fails the test if the filter stops pruning partition 0.
e Both directions demonstrated? Yes, on Build-ID-verified binaries: master aborts on each query, the fix returns 300 300 300 (the reference); a build from before #125033 also returns the reference; a build whose reduced and is dropped instead of rebuilt (no pruning) fails each query with Code: 158 (400 rows read). 50/50 randomized runs pass.
f Fix is general across code paths? Yes. Every branch of splitFilterNodeForAllowedInputs was checked: the single-child and and indexHint paths already build fresh nodes, or and the all-kept and keep their arity and child types. All consumers (part pruning, _part_offset, Merge, system tables, file/url/object-storage path filters) get their DAG from this function.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Plain, Nullable (the rebuilt and changes type and is converted back), FixedString and Nullable(Nothing) hint atoms (the CI query), constant children (folded by addFunction), default and force_enable short-circuit modes. Part pruning measured unchanged (300 of 400 rows read).
h Backward compatible? (maintainer-approved exception only) Yes. Planning-time DAG shape only; no setting, format or default change.
i Invariants and contracts preserved? Every function node returned by splitFilterNodeForAllowedInputs now has a function built for exactly its children and the input node's result type, on all paths; the allow_partial_result = false path returns before the new block. No locking or shared state involved.

Session id: cron:clickhouse-impl-slot-4:20261011-002400

@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f01040d]

Summary: ✅


AI Review

Summary

This PR fixes the virtual-column predicate splitter so that when it drops some conjuncts from an and, it rebuilds a fresh function node whose arity matches the surviving children instead of reusing the stale function_base. That matches the existing ActionsDAG::removeUnusedConjunctions pattern, preserves the original boolean result type via addBooleanCondition, and the new stateless regression test covers both the lazy short-circuit path and continued part pruning. I did not find a remaining correctness, safety, or performance issue in the current diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 11, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit f01040df9258d8b097feeeef78ccade72341dde1 was not compared: the CI logs cluster did not answer (exception 241, code 500: { "meta": [ { "name": "file", "type": "LowCardinality(String)" …).

See the job log for details.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 8209507

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
AST fuzzer (amd_debug, targeted) / Cannot capture 1 column(s) because function indexHint has 0 arguments (STID 3408-3735) not from this PR: a different defect on the same part-pruning path. The inversion-pushed-down copy of an indexHint node in KeyCondition gets the hint's arguments as children but keeps a 0-argument function base; it was first seen on an unrelated PR and also fails with this PR's fix applied #125150 (mine, open)
AST fuzzer (amd_debug, targeted, old_compatibility) / same STID 3408-3735 same as the row above #125150 (mine, open)
PR praktika rollup of the rows above #125150 (mine, open)
Sync - CH Inc sync (private, not actionable by me)

Session id: cron:our-pr-ci-monitor:20261011-053007

…indexHint arity bug it fixes

ClickHouse#125150 fixes `Cannot capture 1 column(s) because function indexHint has 0 arguments` (STID 3408-3735) on the same virtual-column pruning path, while its own targeted AST fuzzer fails on the reduced `and` fixed here. Carry both fixes together so each one's fuzzer run is clean.

Report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125126&sha=820950765c6c5aeb285ba1b91ae04ef5bc1c1de6&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
@fm4v

fm4v commented Oct 12, 2026

Copy link
Copy Markdown
Member

Merged #125150 into this branch. The targeted AST fuzzer here failed only on Cannot capture 1 column(s) because function indexHint has 0 arguments (STID 3408-3735), which 125150 fixes, while 125150's own targeted fuzzer fails on the reduced and fixed here (STID 2458-4cb4). Both bugs are on the same virtual-column pruning path exposed by #125033, so this PR now carries both fixes and 125150 can be closed once this merges.

@groeneai groeneai changed the title Rebuild a reduced and when pruning parts and files by virtual columns Rebuild copied and and indexHint nodes for their actual arguments when pruning by virtual columns Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks. I updated the title and description to cover both fixes.

@fm4v

fm4v commented Oct 12, 2026

Copy link
Copy Markdown
Member

@groeneai pull master to this PR

Comment thread src/Storages/VirtualColumnUtils.cpp Resolved
@groeneai groeneai changed the title Rebuild copied and and indexHint nodes for their actual arguments when pruning by virtual columns Rebuild a reduced and when pruning parts and files by virtual columns Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Merged master (8b7d3c8) into the branch, head is now f01040d. #125150 is already in master, so the diff is back to the and fix only (VirtualColumnUtils.cpp + 05359); I narrowed the title and description to match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants