Skip to content

Fix a crash and lost rows on INSERT when a row policy of a table read by a materialized view uses IN <source table> - #125893

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:mv-source-table-in-hidden-filters
Oct 19, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:mv-source-table-in-hidden-filters

Conversation

@groeneai

@groeneai groeneai commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #123514
Related: #124740
Related: #125610

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 server crash (or missing rows in the view, with a Null source table) on INSERT into the source table of a materialized view when a row policy or additional_table_filters of a table read by the view references the source table, e.g. USING x IN src.

Description

On 26.9.16.11, when a row policy or additional_table_filters of a table read by a materialized view uses IN <source table>, an INSERT into the source crashes the server (MergeTree source) or the view writes no rows (Null source). 26.9.9.28 and 26.3.42.3 return the expected rows. No related open issue found.

CREATE TABLE src (x UInt64) ENGINE = MergeTree ORDER BY x;
CREATE TABLE child (x UInt64) ENGINE = MergeTree ORDER BY x;
INSERT INTO child VALUES (1), (2), (3);
CREATE ROW POLICY p ON child USING x IN src TO ALL;
CREATE TABLE dst (x UInt64) ENGINE = Memory;
CREATE MATERIALIZED VIEW mv TO dst AS SELECT x FROM src WHERE x IN (SELECT x FROM child);
INSERT INTO src VALUES (2), (3), (4); -- SIGSEGV in KeyCondition

Since #123514 the resolver gives a reference to the view's source table the catalog storage with the view source's metadata (no primary key), and relies on replaceStorageInQueryTree to swap the inserted block in after the passes. buildFilterQueryTree, which builds the trees of row policies, additional_table_filters, parallel_replicas_custom_key and additional_result_filter, never did the swap, so the IN set was built by reading the real MergeTree with key-less metadata.

The fix does the swap in buildFilterQueryTree, with the guard the scalar-subquery path uses: the inner query of an ordinary view reads the table (05345). The filter reads the inserted block again, as before #123514 and as IN (SELECT x FROM src) does today.

The new test crashes the unfixed server, passes 50/50 with randomized settings and 20/20 with --replicated-database and with parallel replicas; 26.9.9.28 and 26.3.42.3 produce its reference unchanged.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.195-master (included in 26.11 and later)
  • Backported to: 26.10.2.20, 26.9.23.5, 26.8.32.7, 26.7.36.11, 26.3.57.7

…w's source table

Since ClickHouse#123514 `tryResolveTableIdentifier` resolves the source table of a
materialized view (outside view inner queries) to the catalog storage with the
view source's metadata, and relies on `replaceStorageInQueryTree` to swap the
inserted block in after the passes. `buildFilterQueryTree`, which builds the
query tree of every row policy, `additional_table_filters`,
`parallel_replicas_custom_key` and `additional_result_filter`, never did the
swap. A filter like `x IN src` (or an `IN` subquery reading `src` as a
non-leftmost table) under an MV push therefore planned a read of the real
`MergeTree` with key-less metadata, and `KeyCondition` dereferenced the missing
primary key expression (SIGSEGV). With a source engine without index analysis
(`Null`) the filter read the empty table instead of the block, and the view
wrote no rows.

Do the swap in `buildFilterQueryTree`, with the same guard as the analysis-time
scalar subquery path: the inner query of an ordinary view reads the table, not
the block (05345).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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; 1 finding) plus my own cold review of the diff (no code findings).

# Sev Finding Verdict Evidence / action
1 💡 Changelog category should be Critical Bug Fix because the fix is for a SIGSEGV DISAGREE That category is set only on a maintainer's request; Bug Fix holds: on 26.9.16.11 the INSERT crashes the server or the view writes no rows, through documented row policies and additional_table_filters.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261014-214100

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The SQL in the description (or the new test) kills the unfixed server with SIGSEGV on every run (KeyCondition.cpp:1622 <- ReadFromMergeTree::buildIndexes <- FutureSetFromSubquery::buildOrderedSetInplace). Also on released 26.9.16.11 (rc 139).
b Root cause explained? Since #123514 a reference to the MV's source table resolves to the catalog storage with the view source's key-less metadata, and replaceStorageInQueryTree must swap the inserted block in. buildFilterQueryTree (row policies, additional_table_filters, parallel_replicas_custom_key, additional_result_filter) never swapped, so the IN set read the real MergeTree through a KeyCondition with no key expression. With a Null source it read the empty table instead of the block.
c Fix matches root cause? Yes. The missing swap is added in the one producer that lacked it, with the guard the scalar-subquery path already uses. Nothing is guarded at the crash site.
d Test intent preserved / new tests added? New 05402_mv_source_table_in_hidden_filters: policy IN table, policy IN (... JOIN src) with src non-leftmost, additional_table_filters, Null source (a wrong-result arm independent of the crash), and an ordinary-view arm pinning that a view's inner query still reads the table.
e Both directions demonstrated? Unfixed server: the test dies (server died, SIGSEGV). Fixed: passes, 50/50 randomized, 20/20 with --replicated-database, 20/20 + 20/20 with parallel replicas (parallel_replicas_local_plan 1 and 0). Mutations: removing the swap brings the crash back; dropping the view-inner guard drops the view_inner line.
f Fix is general across code paths? Every filter tree is built by buildFilterQueryTree (row policy, custom key, additional_table_filters, additional_result_filter, projection row policy, Distributed view inlining). The view query, scalar subqueries and the leftmost table of an IN subquery already swap; the nested merge/loop/executable interpreters are #125610.
g Fix generalizes across inputs (params/datatypes/wrappers)? IN, NOT IN, globalIn, IN (subquery) with the source table non-leftmost, MergeTree and Null sources, single- and multi-block inserts, row policy, additional_table_filters and additional_result_filter were all checked. The substitution is by table identity, so type wrappers do not apply.
h Backward compatible? (maintainer-approved exception only) Yes. It restores the pre-#123514 result: 26.9.9.28, 26.8.18.2 and 26.3.42.3 produce the test's reference unchanged. No setting or format change; outside an MV push there is no view source, so nothing changes.
i Invariants and contracts preserved? replaceStorageInQueryTree clones the tree, and column nodes of the filtered table keep their source, so the planner still finds its table expression. Access checks run before the swap, against the catalog table, as #123514 intends. An ordinary view's inner query keeps reading the table (05345 and the view_inner arm). The row-policy, additional_*_filter and MV-source tests give identical results to the unfixed build.

Session id: cron:clickhouse-impl-self-slot-57:20261014-201400

@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [057ccb9]

Summary: ✅


AI Review

Summary

This PR restores inserted-block substitution in materialized-view filter subqueries, preventing a server crash with MergeTree sources and missing rows with Null sources while preserving ordinary-view semantics. No actionable findings. The new regression test passed in Fast test.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @novikd @al13n321, could you review this and add can be tested? buildFilterQueryTree never swapped the materialized view source table for the inserted block, so a row policy or additional_table_filters with IN <source table> read the real MergeTree with the key-less view-source metadata; the fix does the same swap the scalar-subquery path does.

@clickhouse-gh clickhouse-gh Bot added the comp-materialized-view Materialized views: insert-triggered MV pipeline, insert dependencies, deduplication. label Oct 14, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 15, 2026
@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 19, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 19, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 19, 2026
Merged via the queue into ClickHouse:master with commit 6ce833a Oct 19, 2026
521 of 525 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 19, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 19, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 19, 2026
Backport #125893 to 26.10: Fix a crash and lost rows on INSERT when a row policy of a table read by a materialized view uses `IN <source table>`
PedroTadim added a commit that referenced this pull request Oct 19, 2026
Cherry-pick of #125893 onto 26.3: only the include block of Planner/Utils.cpp conflicted; add the
InterpreterSelectQueryAnalyzer.h include.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PedroTadim added a commit that referenced this pull request Oct 19, 2026
Cherry-pick of #125893 onto 26.7: only the include block of Planner/Utils.cpp conflicted; add the
InterpreterSelectQueryAnalyzer.h include.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PedroTadim added a commit that referenced this pull request Oct 19, 2026
Cherry-pick of #125893 onto 26.8: only the include block of Planner/Utils.cpp conflicted; add the
InterpreterSelectQueryAnalyzer.h include.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PedroTadim added a commit that referenced this pull request Oct 19, 2026
Cherry-pick of #125893 onto 26.9: only the include block of Planner/Utils.cpp conflicted; add the
InterpreterSelectQueryAnalyzer.h include.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse added a commit that referenced this pull request Oct 19, 2026
…row policy of a table read by a materialized view uses `IN <source table>`
robot-clickhouse added a commit that referenced this pull request Oct 19, 2026
…row policy of a table read by a materialized view uses `IN <source table>`
robot-clickhouse added a commit that referenced this pull request Oct 19, 2026
…row policy of a table read by a materialized view uses `IN <source table>`
robot-clickhouse added a commit that referenced this pull request Oct 19, 2026
…row policy of a table read by a materialized view uses `IN <source table>`
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 19, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 19, 2026
Backport #125893 to 26.9: Fix a crash and lost rows on INSERT when a row policy of a table read by a materialized view uses `IN <source table>`
clickhouse-gh Bot added a commit that referenced this pull request Oct 19, 2026
Backport #125893 to 26.8: Fix a crash and lost rows on INSERT when a row policy of a table read by a materialized view uses `IN <source table>`
alexey-milovidov added a commit that referenced this pull request Oct 19, 2026
Backport #125893 to 26.3: Fix a crash and lost rows on INSERT when a row policy of a table read by a materialized view uses `IN <source table>`
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 20, 2026
…ilters

`buildFilterQueryTree` swapped the inserted block into the tree of a row
policy or of `additional_table_filters` (ClickHouse#125893) whenever the context had a
view source, so inside a materialized view the input query of `executable`
or a `loop` read the table while the policy of the table it read used the
inserted block (`x NOT IN (SELECT x FROM src)` kept rows the stored table
excludes). It now uses `getViewSourceForSubqueries`, like the IN and scalar
subquery paths: the view query, and its replan for a `Merge` child or a
`Buffer`, still read the block.

Two arms in 05389 read a table with such a policy directly through
`executable` and `loop`, and the `merge_in` arm becomes `merge_not_in`, so a
`Merge` branch that silently returns nothing cannot pass it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #125893 to 26.7: Fix a crash and lost rows on INSERT when a row policy of a table read by a materialized view uses `IN <source table>`
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-materialized-view Materialized views: insert-triggered MV pipeline, insert dependencies, deduplication. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants