Skip to content

Fix NOT_FOUND_COLUMN_IN_BLOCK when a set skip index is used with a row policy - #107971

Merged
tiandiwonder merged 3 commits into
masterfrom
fix-set-index-row-policy-not-found-column
Jun 29, 2026
Merged

tiandiwonder merged 3 commits into
masterfrom
fix-set-index-row-policy-not-found-column

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A SELECT on a MergeTree table that has a set data-skipping index throws NOT_FOUND_COLUMN_IN_BLOCK when the querying user is subject to a row policy whose USING expression has a constant-true term expressed as a function (e.g. arrayExists(x -> 1 = 1, [...])) OR-ed with a term that references a column the index does not cover.

CREATE TABLE t (a UInt32, b UInt32, INDEX ia a TYPE set(0) GRANULARITY 4)
ENGINE = MergeTree ORDER BY a;

CREATE ROW POLICY rp ON t FOR SELECT
USING arrayExists(x -> (1 = 1), ['y']) OR has([], b) TO ALL;

SELECT * FROM t WHERE a = 123;
-- Code: 10. NOT_FOUND_COLUMN_IN_BLOCK: Not found column b in block ...
--   While executing MergeTreeSelect(pool: ReadPoolInOrder, algorithm: InOrder).

The same predicate written directly in WHERE is constant-folded into a bare literal before reaching the index condition, so it reproduces only via the row-policy filter. Switching the index to bloom_filter avoids it. Reproduced on 26.2, 26.5 and current master.

Root cause

MergeTreeIndexConditionSet builds an ExpressionActions (actions) from the filter that is executed per index, over a granule block that contains only that index's own column(s). In MergeTreeIndexConditionSet::atomFromDAG a node that carries a constant column was returned verbatim:

if (node_to_check->column)
    return &node;

When constant-folding reduces or(arrayExists(x -> 1 = 1, …), <term over a non-indexed column>) to a constant value, it keeps the call tree underneath (the arrayExists/has functions and their INPUT for the non-indexed column). Returning that folded FUNCTION node verbatim lets ActionsDAG::cloneSubDAG pull the foreign INPUT into actions. At read time — lazy per-granule skip-index filtering in MergeTreeSelectProcessor (InOrder) → getPossibleGranules → actions->execute(block) — that column is absent from the index granule block, hence NOT_FOUND_COLUMN_IN_BLOCK. A plain literal const term (e.g. 1) is folded away entirely and does not leak; only the set index builds this per-granule ExpressionActions, so bloom_filter is unaffected.

This is adjacent to the earlier or(x, 0) fix (#98776, in checkDAGUseless); this one is in the actions-building path and is triggered by a constant-true function term.

Fix

In atomFromDAG, when a constant-folded FUNCTION node is encountered, re-add it as a fresh leaf constant (same value/type/name) instead of returning the original node. This preserves the folded value while dropping the now-irrelevant argument subtree, so actions only ever references this index's own columns. Plain literal/COLUMN constants are still returned verbatim.

Regression test 04340_set_index_row_policy_const_folded_function covers the bare-has and higher-order (arrayExists lambda capture) shapes; it fails on the unfixed binary with the error above and passes after the fix.

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

Fixed a SELECT failing with NOT_FOUND_COLUMN_IN_BLOCK on a table that has a set data-skipping index when a row policy filters it using an always-true condition combined with a check on a non-indexed column.

Version info

  • Merged into: 26.7.1.218 (included in 26.7 and later)

A SELECT on a table with a `set` data-skipping index could throw
`NOT_FOUND_COLUMN_IN_BLOCK` when a row policy `USING` expression contained a
constant-true term expressed as a function (e.g. `arrayExists(x -> 1 = 1, [...])`)
OR-ed with a term that references a non-indexed column.

`MergeTreeIndexConditionSet::atomFromDAG` returned a constant-folded `FUNCTION`
node verbatim. Such a node carries a constant value but still keeps its argument
subtree, which may reference columns that are not part of the index.
`ActionsDAG::cloneSubDAG` then pulled those foreign `INPUT`s into the per-index
`actions`, which failed at read time (lazy per-granule skip-index filtering in
`MergeTreeSelectProcessor`) because the set granule block only carries the index's
own column(s). The same predicate written directly in `WHERE` is constant-folded
into a bare literal before reaching the index condition, so it only reproduced via
the row-policy filter.

Re-add a constant-folded function node as a fresh leaf constant so its now
irrelevant argument subtree is dropped from `actions`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2d97ada]

Summary: ❌

job_name test_name status info comment
Stress test (amd_debug) FAIL
Hung check failed, possible deadlock found FAIL cidb, issue ISSUE EXISTS
Stress test (amd_tsan) FAIL
ThreadSanitizer: potential deadlock (STID: 5654-6326) FAIL cidb, issue ISSUE EXISTS

AI Review

Summary

This PR fixes the set skip-index row-policy failure by converting constant-folded function atoms into leaf constants before cloning the per-index action DAG. The change matches the promised bug fix, the regression test covers the failing row-policy shapes, and I found no unresolved review findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 19, 2026
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Jun 19, 2026

@KochetovNicolai KochetovNicolai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Per review: the folded-FUNCTION leaf-constant fix in atomFromDAG created a new
ColumnConst from the node's value. ActionsDAG::Node::column is already a
ColumnConstPtr (COW) and addColumn normalizes the row count, so pass the
existing column directly - avoids the Field extraction and column rebuild.
@clickhouse-gh

clickhouse-gh Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.40% +0.10%
Functions 92.50% 92.60% +0.10%
Branches 77.60% 77.50% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 6/7 (85.71%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 78111aa7-5e5c-4cf7-9289-8cb22fded3b6
  • MIRAI run: 126a9b21-f376-4b36-910b-34c27214c67d
  • PR check IDs:
    • clickbench_129408_1782466071
    • clickbench_129422_1782466071
    • clickbench_129428_1782466071
    • tpch_adapted_1_official_129435_1782466071
    • tpch_adapted_1_official_129447_1782466071
    • tpch_adapted_1_official_129508_1782466072

alexey-milovidov pushed a commit to m-selmi/ClickHouse that referenced this pull request Jun 28, 2026
Building a secondary (skip) index during an INSERT runs granule by granule in
`MergeTreeDataPartWriterOnDisk::calculateAndSerializeSkipIndices` with no
cancellation point. The INSERT pipeline only enforces query limits between
blocks (`MergeTreeSink::consume`), so once execution is inside `writeTempPart`
-> writer -> skip-index build, a `KILL QUERY` cannot interrupt it. An unbounded
`set(0)` index on a high-cardinality column with a small `GRANULARITY` makes that
build run for minutes, which tripped the 60s hung-check in `Stress test
(arm_tsan)` even though the query was already cancelled (`is_cancelled = 1`).

Check the query status once per granule via `checkTimeLimit`, mirroring the
existing precedent in `MergeTreeIndexVectorSimilarity`. This covers every
skip-index type in one shared place and honors both cancellation and
`max_execution_time` (throw mode). `set(0)` semantics are unchanged.

Related: ClickHouse#107971

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@tiandiwonder
tiandiwonder added this pull request to the merge queue Jun 29, 2026
Merged via the queue into master with commit fb43827 Jun 29, 2026
164 of 167 checks passed
@tiandiwonder
tiandiwonder deleted the fix-set-index-row-policy-not-found-column branch June 29, 2026 03:03
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 29, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Building a secondary (skip) index during an INSERT runs granule by granule in
`MergeTreeDataPartWriterOnDisk::calculateAndSerializeSkipIndices` with no
cancellation point. The INSERT pipeline only enforces query limits between
blocks (`MergeTreeSink::consume`), so once execution is inside `writeTempPart`
-> writer -> skip-index build, a `KILL QUERY` cannot interrupt it. An unbounded
`set(0)` index on a high-cardinality column with a small `GRANULARITY` makes that
build run for minutes, which tripped the 60s hung-check in `Stress test
(arm_tsan)` even though the query was already cancelled (`is_cancelled = 1`).

Check the query status once per granule via `checkTimeLimit`, mirroring the
existing precedent in `MergeTreeIndexVectorSimilarity`. This covers every
skip-index type in one shared place and honors both cancellation and
`max_execution_time` (throw mode). `set(0)` semantics are unchanged.

Related: ClickHouse#107971

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…w-policy-not-found-column

Fix `NOT_FOUND_COLUMN_IN_BLOCK` when a `set` skip index is used with a row policy
pull Bot pushed a commit to Mu-L/ClickHouse that referenced this pull request Sep 25, 2026
…ition rewrite

On 26.6, a filter that combines a condition on a column carrying a `set` skip
index with `coalesce(nullIf(c2, ''), nullIf(c3, ''), 'x') = 'v1'` over columns
that index does not cover fails index analysis with

  Code: 10. Not found column c2 in block: while executing
  'INPUT : 1 -> c2 String : 5' ... (NOT_FOUND_COLUMN_IN_BLOCK)

and `EXPLAIN indexes = 1` fails identically, so the fault is in analysis rather
than execution. Reported in ClickHouse#121912 against a Cloud build, reproduced here on
the official OSS binaries 26.6.1.1193 and 26.6.8.7 with plain MergeTree.

The mechanism. ActionsDAGWithInversionPushDown is built once per filter and is
the shared input to KeyCondition and to every skip-index condition, which is how
a setting read only in KeyCondition.cpp changes what a set index sees. With
allow_key_condition_coalesce_rewrite enabled, tryRewriteCoalesceComparison turns
the comparison into a disjunction whose last disjunct carries 'x' = 'v1'; that is
false, so the disjunct folds to a constant FUNCTION node which still retains its
argument subtree over c2 and c3. Before ClickHouse#107971, atomFromDAG returned that folded
node verbatim, so cloneSubDAG pulled INPUT c2 into the index's actions while the
per-granule block holds only the indexed column c1.

ClickHouse#107971 fixed it by re-adding a constant-carrying FUNCTION as a leaf constant,
which is why 26.7 and later are green. It was never backported, so every 26.6
release including 26.6.8.7 still throws; whether it should be is a maintainer's
call, not something to hand-author here.

The coalesce rewrite is a second, independent entry point into that defect.
ClickHouse#107971's own test pins only the row-policy entry point, and of the five existing
coalesce/skip-index tests in 0_stateless none uses TYPE set, so this shape was
uncovered. The test pins both spellings that throw (coalesce with a trailing
constant, and the ifNull spelling over a single uncovered column), each against
its setting-off control, and asserts that i1 is still analysed so the count
assertions cannot pass vacuously.

allow_key_condition_coalesce_rewrite and use_skip_indexes are pinned per query
rather than with a blanket no-random-settings tag, since use_skip_indexes = 0 is
the one setting that makes the shape green even unpatched. index_granularity is
pinned in CREATE TABLE because the runner randomizes it over 1..65536.

Reverting the ClickHouse#107971 guard on master makes this test fail 10/10 with the
original error; restoring it passes 20/20. 150 randomized runs are green across
three randomizer arms.

Co-Authored-By: Claude Opus 5 <[email protected]>
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.

NOT_FOUND_COLUMN_IN_BLOCK (Code 10) with allow_key_condition_coalesce_rewrite=1 on 26.6.1.2207

4 participants