Skip to content

Fix NOT_FOUND_COLUMN_IN_BLOCK with row policy and additional_table_filters on a shared column - #111099

Merged
PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-111077-row-policy-additional-table-filter-shared-column
Jul 31, 2026
Merged

PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-111077-row-policy-additional-table-filter-shared-column

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

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 into CHANGELOG.md):

Fix NOT_FOUND_COLUMN_IN_BLOCK when a row policy and additional_table_filters reference the same column that is not otherwise selected by the query.

Description

Closes: #111077

When a table has a row policy and the query uses additional_table_filters referencing the same column as the policy (and that column is not otherwise projected), the query failed with NOT_FOUND_COLUMN_IN_BLOCK.

For a storage that supports PREWHERE (e.g. MergeTree) the row policy is applied inside the reading step as a row_level_filter. That filter is built by buildFilterInfo, which only keeps a filter input in the filter's outputs if the input is among the columns needed so far by the query. When the row-level filter is built, the shared column is not yet in that set (only the SELECT/WHERE columns are), so it is dropped from the reading step's output block. The additional_table_filters step, applied on top of the reading step, then cannot find the column.

The explicit-PREWHERE path already pre-collects columns_needed_by_other_filters to avoid this. This change extends the same idea to the row_level_filter path: the columns the additional_table_filters step needs are pre-collected and preserved in the row-level filter's outputs, so the reading step keeps them. The change only adds columns to a filter's output set, so results are unaffected; when additional_table_filters is not set the behavior is unchanged.

Added a stateless regression test covering the shared-column case, a mix of the shared column with another unprojected column, optimize_move_to_prewhere = 0, and LowCardinality(Nullable(...)) on the shared column.

…lters on a shared column

When a table has a row policy and the query uses additional_table_filters
referencing the same column as the policy (and that column is not otherwise
selected), the query failed with NOT_FOUND_COLUMN_IN_BLOCK.

For a storage supporting PREWHERE the row policy is applied inside the reading
step as a row_level_filter, built by buildFilterInfo which only keeps a filter
input in its outputs if the input is among the columns needed so far. The shared
column is not yet required when the row-level filter is built, so it is dropped
from the reading step output, and the additional_table_filters step applied on
top can no longer find it.

Pre-collect the columns the additional_table_filters step needs and preserve
them in the row-level filter's outputs, mirroring the columns_needed_by_other_filters
handling already done for explicit PREWHERE. Only adds columns to a filter's
output set, so results are unaffected.

Closes: ClickHouse#111077

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT count() FROM t WHERE k > 0 SETTINGS additional_table_filters = {'default.t': 'tenant != ''y'''} with a row policy tenant != 'x' fails every time (Code:10 NOT_FOUND_COLUMN_IN_BLOCK) on master (Build ca9b22b9), passes every time with the fix (Build f4420989).
b Root cause explained? The row policy is applied inside the reading step as row_level_filter (MergeTree supportsPrewhere()). buildFilterInfo keeps a filter input in the filter's outputs only if it is among the columns needed so far. When the row-level filter is built the shared column is not yet required (only SELECT/WHERE cols are), so it is dropped from the reading-step output; the additional_table_filters step applied on top then cannot find it.
c Fix matches root cause? Yes. Pre-collect the columns the additional_table_filters step needs and preserve them in the row-level filter's outputs, so the reading step keeps them. Reuses the existing columns_needed_by_other_filters pre-collect already done for explicit PREWHERE. Not a symptom guard.
d Test intent preserved / new tests added? New stateless test 04613_row_policy_additional_table_filter_shared_column (shared-column case, shared+extra column, optimize_move_to_prewhere = 0, LowCardinality(Nullable(...)) on the shared column). No existing test weakened.
e Both directions demonstrated? Yes. Without fix: repro + all 4 type-wrapper variants error NOT_FOUND_COLUMN_IN_BLOCK. With fix: all return correct results (both filters verified applied, surviving rows checked).
f Fix is general across code paths? Yes. Preserves all input columns of the additional filter (single, multi-column, mixed shared+extra), not one hardcoded name. The symmetric subsystem (explicit PREWHERE) already had this mechanism; this extends it to the row_level_filter path.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Verified String, Nullable(String), LowCardinality(String), LowCardinality(Nullable(String)) all fail without and pass with the fix; both filter orderings; explicit-PREWHERE and parallel-replicas paths.
h Backward compatible? Yes. Pure planner change; only adds columns to a filter's output set (never removes). No setting default / on-disk / wire / format change. When additional_table_filters is unset the behavior is byte-identical. No SettingsChangesHistory entry needed.
i Invariants and contracts preserved? Yes. buildFilterInfo's contract honored (empty required-names means "keep all", so the set is seeded with the current columns before adding). The row_level_filter is built once, so the setRowLevelFilterActions clone stays consistent. tryRestoreColumn is idempotent, so the temp additional-filter build is side-effect-safe.

Session id: cron:clickhouse-author-slot-2:20260720-145000

@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review — adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings; bounded: 1 full pass + 1 fix round + 1 delta recheck).

No blockers, majors, or nits were raised. The reviewer saw the exact change set (3 files changed, +86/-4) and returned no findings.

An earlier approach-stage review (Gate A) raised two design/minimality points, both of which were accepted and folded into the final approach before any code was written (reuse the existing PREWHERE columns_needed_by_other_filters pre-collect idiom rather than a post-hoc DAG repair, which also removes the row-level-filter clone-consistency concern).

Session id: cron:clickhouse-author-slot-2:20260720-145000

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @yakov-olkhovskiy @KochetovNicolai — could you review this? It fixes NOT_FOUND_COLUMN_IN_BLOCK (#111077) when a row policy and additional_table_filters reference the same unprojected column: the row-level filter (applied inside the reading step) dropped the shared column, so the additional filter couldn't find it. The fix pre-collects the additional filter's columns and preserves them in the row-level filter's outputs, mirroring the existing columns_needed_by_other_filters handling for explicit PREWHERE.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jul 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [51b1217]

Summary: ❌

job_name test_name status info comment
Stress test (amd_asan_ubsan) FAIL
Hung check failed, possible deadlock found FAIL cidb, issue ISSUE EXISTS
AST fuzzer (arm_asan_ubsan) FAIL
AddressSanitizer: heap-use-after-free (STID: 2837-40ba) FAIL cidb IGNORED

AI Review

Summary

This PR preserves the columns that a later additional_table_filters step needs when the row policy filter is built before that step, which matches the reported NOT_FOUND_COLUMN_IN_BLOCK failure mode. The planner change is localized, the new regression test exercises the shared-column scenario and its relevant wrappers, and I did not find a remaining correctness or compatibility issue in the current diff.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 20, 2026
Comment thread src/Planner/PlannerJoinTree.cpp Resolved
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 1c5f272

Every failure below has an owner: a fixing PR (ours 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
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) / 04507_mvt_encode_state_serialize Trunk-wide breakage, not PR-caused: a folded Variant constant loses its member-type name under parallel-replica query-tree->AST serialization, so the secondary re-casts Tuple(Float64,Float64) -> Geometry and throws Code 70. Hit ~8 master runs + ~18 unrelated PRs on this one config today. #111136 (external, open)

Session id: cron:our-pr-ci-monitor:20260720-233000

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 8383908

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens.

Check / test Reason Owner / fixing PR
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) / Server died + !part.empty() (STID 5217-5b3d) parallel-replicas unknown-stream abort, trunk bug #111689 (ours, open)
Stress test (amd_debug) / !part.empty() (STID 5182-2f27) KeyCondition monotonicity canary, trunk bug #109023 (ours, open)
Stateless tests (amd_asan_ubsan, distributed plan, parallel) / 03216_arrayWithConstant_limits shard-level OvercommitTracker memory pressure, test is a victim #111705 (external, merged 2026-07-25T17:26:25Z) - NOT on this branch, merging master will pick it up
Stateless tests (amd_asan_ubsan, distributed plan, parallel) / 02477_single_value_data_string_regression same OvercommitTracker shard pressure #110630 (external, open)
Upgrade check (amd_release) / Error message in clickhouse-server.log (system.metric_log readonly, Code 229) trunk error-log leak #111703 (external, merged 2026-07-24T17:24:53Z) - NOT on this branch, merging master will pick it up

None of these are caused by this PR's diff: Fast test, Style check and all five Bugfix-validation jobs are green on this head.

Session id: cron:our-pr-ci-monitor:20260726-163000

@PedroTadim PedroTadim self-assigned this Jul 30, 2026
…-row-policy-additional-table-filter-shared-column
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.60% 78.60% +0.00%

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

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 51b1217

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 (arm_asan_ubsan) / heap-use-after-free in ExecutingGraph::updateNode (STID 2837-40ba) trunk bug, heap-use-after-free on runtime node removal #111017 (mine, open)
Stress test (amd_asan_ubsan) / Hung check failed, possible deadlock found cancelled SELECT json.a ran 594s in SerializationObject::deserializeObjectStructureStatePrefix; cancellation not honored on that read path a fix task is created (investigating at full effort, fixing-PR link to follow on this PR)

All 174 check-runs are completed and the praktika Finish Workflow aggregator has run;
these are the only two failures.

Neither is caused by this diff, which is 3 files: src/Planner/PlannerJoinTree.cpp plus the
new stateless test 04613_row_policy_additional_table_filter_shared_column.{sql,reference}.
Nothing here touches the pipeline executor graph, JSON serialization, the MergeTree reader, or
any cancellation path.

Detail on the hung check, read from hung_check.log (exactly one hung row): elapsed 594.326199, is_cancelled 1, read_rows 0, memory_usage 22.4 MB, query select json.a from test format Null. The stuck thread is in CachedCompressedReadBuffer::nextImpl under
SerializationObjectDynamicPath::deserializeBinaryBulkStatePrefix ->
MergeTreeReaderWide::deserializePrefix (:504), while the client thread waits in
PullingAsyncPipelineExecutor::pull. It is a distinct sub-signature from the other open
hung-check sub-causes (async-insert JSON parse, text-index marks cache, RemoteQueryExecutor
was_cancelled_mutex), so it gets its own owner rather than being folded into any of them.

Session id: cron:our-pr-ci-monitor:20260730-180000

@PedroTadim
PedroTadim added this pull request to the merge queue Jul 31, 2026
Merged via the queue into ClickHouse:master with commit 8531d9c Jul 31, 2026
176 of 179 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 31, 2026
@groeneai

groeneai commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator Author

Following up on the ledger above: the fixing PR for the Stress test (amd_asan_ubsan) hung check on this commit is #112918.

That row was the only one owned by a fix task rather than a PR. The hung select json.a from test format Null was a missing interruption point, not a deadlock: SerializationObject::deserializeObjectStructureStatePrefix consumes the whole path list before the first row is produced, and ExecutionThreadContext::executeJob only observes cancellation between work calls, so a cancelled query kept reading. #112918 adds a checkpoint inside those prefix loops.

#111017 (the other row) has since merged.

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 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

Development

Successfully merging this pull request may close these issues.

Row policy and additional_table_filters NOT_FOUND_COLUMN_IN_BLOCK

4 participants