Skip to content

Fix logical error on filter push-down with a differently-typed same-name column - #113984

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix/virtual-column-filter-input-type-mismatch
Aug 12, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix/virtual-column-filter-input-type-mismatch

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 8, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix a logical error (Unexpected return type) when a filter over a view whose column type differs from the underlying storage (e.g. information_schema.tables, where engine is Nullable(String), over system.tables, where it is String) is pushed down to the storage.

Minimal reproducer:

SELECT 1 FROM (SELECT 1 FROM information_schema.tables WHERE indexHint(toString(engine)));
-- Code: 49. DB::Exception: Unexpected return type from toString. Expected Nullable(String). Got String. (LOGICAL_ERROR)

The predicate is typed against the view header, where engine is Nullable(String). When SourceStepWithFilter::applyFilters rebuilds the filter with ActionsDAG::buildFilterActionsDAG, the name-based input replacement substituted the storage column engine String for the input while the parent FUNCTION nodes were rebuilt with their existing function_base — leaving the rebuilt DAG (including the DAG captured inside indexHint) internally inconsistent: toString still declared Nullable(String) while returning String. Evaluating it over the candidate-tables block in getFilteredTables then failed the return-type assertion in ExpressionActions.

Two fixes:

  • ActionsDAG::buildFilterActionsDAG: skip a name-based input replacement when it would change the input type. Keeping the original input is safe — the subtree is then simply not evaluated over the storage columns, and the filter is still applied upstream.
  • canEvaluateSubtree in VirtualColumnUtils: match the allowed inputs by type as well as by name, as a fail-close guard at the evaluation site, so a mismatched predicate subtree is not pushed down.

The exact-type check exposed a lying push-down sample in StorageSystemTables: both detail::getFilteredTables and ReadFromSystemTables::applyFilters declared uuid as String while the real column of system.tables / system.detached_tables is UUID, which would have silently disabled the uuid prefilter fast path. The samples now declare UUID, and a test pins the fast path via the SelectedRows profile event.

Found by the AST fuzzer on two unrelated PRs (pre-existing on master; the same error also appears in master stress tests going back months): AST fuzzer (amd_debug) report.

Closes: #113982
Related: #113636

Version info

  • Merged into: 26.8.1.1195 (included in 26.8 and later)

…ame column

A predicate typed against a view header (e.g. `information_schema.tables`,
where `engine` is `Nullable(String)`) could be pushed down to the underlying
storage (`system.tables`, where `engine` is `String`) and evaluated over a
block with the mismatched column type, failing with a logical error
`Unexpected return type from toString. Expected Nullable(String). Got String`.

Two fixes:
- `buildFilterActionsDAG`: skip a name-based input replacement when it would
  change the input type. Parent `FUNCTION` nodes are rebuilt with their
  existing `function_base`, so a differently-typed input made the rebuilt DAG
  (including the DAG captured inside `indexHint`) internally inconsistent.
- `canEvaluateSubtree` in `VirtualColumnUtils`: match the allowed inputs by
  type as well as by name, so a mismatched predicate subtree is not pushed
  down (the filter is still applied upstream).

Found by the AST fuzzer:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113636&sha=bea222ed980b6db96146b4fe56dc968fa2280dd1&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29
on #113636

Closes: #113982

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0d91aad]

Summary: ✅


AI Review

Summary

This PR makes filter push-down fail closed when a pushed predicate would bind a same-name column with a different type, and it fixes the uuid sample types used by system.tables prefiltering. I did not find any remaining correctness or compatibility issues in the current head; the earlier bot concern about the uuid fast path is fixed and covered by the new stateless test.

Missing context / blind spots
  • ⚠️ I reviewed the current code and prior discussion, but I did not rerun 04828_filter_pushdown_input_type_mismatch or 04831_filter_pushdown_uuid_system_tables locally in this environment. A green stateless run or full PR CI would close that gap.
Final Verdict

No blocking issues found in the current head.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.60% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.90% 78.90% +0.00%

Changed lines: Changed C/C++ lines covered: 25/25 (100.00%) · Uncovered code

Full report · Diff report

The test scanned the whole `information_schema.tables`, so `system.tables`
computed `total_rows` for every table on the server. In a parallel stateless
run this hits other tests' half-dropped tables (e.g. a DeltaLake table whose
path was already removed), and their errors are streamed to the client as
server logs, failing the test with non-empty stderr:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113984&sha=ea086a58d6d2f255bbbde02f94e35ab84c4a64c7&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29

Add a `table_schema = currentDatabase()` conjunct to each query (verified via
the `SelectedRows` profile event that it is pushed down into `system.tables`,
so only the current database is enumerated) and create a table so the block
the predicate is evaluated over is non-empty. Verified that the modified test
still reproduces the logical error on a pre-fix binary and passes with the fix.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Storages/VirtualColumnUtils.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 CI adjudication for head ea086a58d6d2 and the fix pushed on top (8c81ade1485b):

@clickhouse-gh

clickhouse-gh Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

No arm_release build profile data for commit 0d91aad - the build was skipped, reused from cache, or predates profile upload.

The push-down sample blocks in `detail::getFilteredTables` and
`ReadFromSystemTables::applyFilters` declared `uuid` as `String`,
while the real column of `system.tables` / `system.detached_tables`
(and the block the split-off predicate is evaluated over) is `UUID`.
The name-only input matching used to hide this; with the exact-type
check in `splitFilterDagForAllowedInputs`, the lying sample would
silently disable the `uuid` prefilter fast path, forcing the source
to materialize every table row before the outer filter.

Declare the sample truthfully and add a test that pins the fast path
via the `SelectedRows` profile event (1 with the prefilter, one row
per table in the database without it). Verified: the test fails
without this change and passes with it.

Addresses the AI review finding on the pull request.

Co-Authored-By: Claude Fable 5 <[email protected]>
pull Bot pushed a commit to AnotherGenZ/ClickHouse that referenced this pull request Aug 9, 2026
Since `max_insert_threads` started to apply to plain `INSERT`s (ClickHouse#109000),
an insert fans out to multiple parallel `MergeTreeSink`s (the default
`max_insert_threads = 0` resolves to the number of cores). Each sink ran
the "too many parts" throw check in `onStart`, and nothing guarantees all
`onStart`s complete before a sibling sink commits its part: a sink whose
`onStart` ran late (e.g. its worker thread was preempted on a loaded
machine) counted the parts committed by its sibling sinks and spuriously
rejected with `TOO_MANY_PARTS` the very insert that wrote them. This made
`02458_relax_too_many_parts` flaky fleet-wide (~5-11 failures/day since
2026-08-01), e.g.:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113984&sha=ea086a58d6d2f255bbbde02f94e35ab84c4a64c7&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
on ClickHouse#113984

Move the throw check to `StorageMergeTree::write` / `StorageReplicatedMergeTree::write`,
which run on the query thread while the insert pipeline is being built, so
the check never sees the query's own parts. The sinks' `onStart` keeps the
delay-under-pressure behavior (`allow_throw = false`), where the parallel
streams sleep concurrently; the new `allow_delay = false` mode of
`delayInsertOrThrowIfNeeded` avoids sleeping once per sink stream on the
query thread.

Add the `merge_tree_sink_on_start_random_sleep` failpoint, which skews the
sinks' start times, making the race deterministic: with the failpoint, the
new test `04826_parallel_insert_sinks_too_many_parts_self_race` fails 5/5
with the pre-fix behavior and passes 5/5 with the fix.

Closes: ClickHouse#114015

Co-Authored-By: Claude Fable 5 <[email protected]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@alexey-milovidov alexey-milovidov self-assigned this Aug 11, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 11, 2026
Merged via the queue into master with commit 48b9107 Aug 12, 2026
181 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/virtual-column-filter-input-type-mismatch branch August 12, 2026 00:37
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 12, 2026
@clickgapai

Copy link
Copy Markdown
Contributor
Reproducer
CREATE VIEW v AS SELECT toNullable(engine) AS engine FROM system.tables;
SELECT 1 FROM (SELECT 1 FROM v WHERE indexHint(toString(engine))) FORMAT Null;
-- Code: 49. DB::Exception: Unexpected return type from toString. Expected Nullable(String). Got String. (LOGICAL_ERROR)
-- Reproduces on 26.7.3.19 down to 24.5.8.10; clean on 24.4.4.113 and on master after this PR.

Results:

  • Reproduces on: 26.7, 26.6, 26.5, 26.3, 25.8
  • Does not reproduce on: master
  • Tested versions: master, 26.7, 26.6, 26.5, 26.3, 25.8
  • ✓ Fix is on master — this PR's change fixes the bug on master. The releases listed above still have the bug and need the backport.

Backport the fix to 26.7, 26.6, 26.5, 26.3, 25.8.

CC component owner: @alexey-milovidov @KochetovNicolai

Analysis metadata

comp-query-execution · Severity P2 · Finding phase_d_pr113984

  • Verified against master build 0eb5eba20b92 (aarch64)
  • Also tested (unsupported, context only): 26.2 reproduces
Introducing change: not identified
  • The one boundary found was rejected — PR Parallelize copyFile in DiskObjectStorageTransaction #105089 only modifies disk object storage transaction logic and file copying parallelization, which is completely unrelated to filter push-down, type handling, or view column resolution that the bug involves. — a coincidental boundary, not the cause.
  • The source-build bisect converged to Parallelize copyFile in DiskObjectStorageTransaction #105089, but it is not the introducer (see above) — not attributing it.
  • Walked 8 release(s) below the supported window: still reproduces down to 24.5; clean at 24.4.
  • Secondary components: comp-system-table, comp-testing

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

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants