Skip to content

Fix view source substitution in subqueries inside view inner queries - #124875

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
adityaksolves:fix/view-inner-query-subquery-view-source
Oct 10, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
adityaksolves:fix/view-inner-query-subquery-view-source

Conversation

@adityaksolves

@adityaksolves adityaksolves commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

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

Fixes wrong results when a materialized view reads an ordinary view over a table with a row policy that contains a subquery (x IN (SELECT ...) or a scalar subquery). The policy's subquery now reads the real source table instead of the block being inserted into the materialized view.

Closes #124551

Details

When a materialized view query reads an ordinary view, only the top-level SELECT of the materialized view should read the inserted block. The inner query of the view must read real tables. PR #122345 added an !isViewInnerQuery() guard for this in several places, but two places that pass getViewSource() to InterpreterSelectQueryAnalyzer were missed:

  • src/Interpreters/interpretSubquery.cpp (interpretSubqueryWithAnalyzer)
  • src/Interpreters/ExecuteScalarSubqueriesVisitor.cpp (getQueryInterpreter)

Both now pass nullptr when isViewInnerQuery() is true. The check-only usages in ExecuteScalarSubqueriesVisitor.cpp do not substitute anything and are unchanged.

Test: 05345_in_subquery_row_policy_under_view_inner_query_in_materialized_view. The IN section covers a view over a Merge table and a direct view. The scalar section covers a view over a Merge table.

Verification (local build on master 08480e808f7, branch rebased onto 48f09dce392 afterwards without rebuild):

  • Reproducer before the fix: 2 1 0 / 3 1 0 / 4 0 0. After the fix: 2 0 0 / 3 0 0 / 4 0 0.
  • With only the interpretSubquery.cpp fix, the scalar section of the new test fails (1 1). With both fixes it passes.
  • 05321_in_subquery_of_standalone_expression_under_materialized_view still passes.
  • Checked manually, not in the test: scalar policy through a direct view and a nested view returns correct results, and a scalar subquery in the top-level SELECT of a non-view materialized view still reads the inserted block.
  • The test was run locally with --no-random-settings.

Not changed: JoinedTables.cpp (old planner) also uses getViewSource() without the guard, but the bug did not reproduce with enable_analyzer = 0.


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

Version info

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

Subqueries (IN and scalar) of a row policy inside an ordinary view read the inserted block of the materialized view instead of the real source table. Do not pass the view source when the context is a view inner query, as already done in resolveFunction and evaluateScalarSubqueryIfNeeded. Fixes ClickHouse#124551.
@clickhouse-gh clickhouse-gh Bot added the comp-materialized-view Materialized views: insert-triggered MV pipeline, insert dependencies, deduplication. label Oct 9, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 9, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bef3a20]

Summary: ✅


AI Review

Summary

This PR closes the two remaining analyzer entry points that still passed getViewSource() into subquery planning inside ordinary-view inner queries. The new isViewInnerQuery() guards in src/Interpreters/interpretSubquery.cpp and src/Interpreters/ExecuteScalarSubqueriesVisitor.cpp line up with the earlier analyzer-side fixes, and the new stateless regression test exercises both the IN-subquery and scalar-subquery row-policy repros. I did not find any new correctness, safety, or compatibility issues in the current diff.

Final Verdict

✅ No findings from me on the current version of this PR.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default external Author is not a member of the ClickHouse organization labels Oct 9, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124875&sha=64ff1cd5ae444d858972bb8edba99394fd1daf7e&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29 (05210_lightweight_update_materialized_cte_in_ignore: Unknown table expression identifier '_materialized_cte_c_...', failing in 55 PRs since 2026-10-08) and provide a fix in a separate PR. If the fix is already in progress, link it here.

The other failures (05316_secret_arguments_declared_by_engines, 05055_string_filters_move_to_prewhere) are already fixed on master (351f549, #124911); master was merged into this branch.

@groeneai

Copy link
Copy Markdown
Collaborator

Not caused by this PR. The fix is #123121 (mine, open).

It is the parallel replicas race from #112642: the initiator's local plan waits for the materialized CTE only in its own branch, so the query sent to the other replicas can start before _materialized_cte_c_* exists, and they throw UNKNOWN_TABLE (query 26 of the test, as in this run). With a slow CTE body that query fails 10 of 10 runs on master and passes 10 of 10 with #123121. Since 2026-10-08 the test failed on 55 PRs and twice on master. #123121 has been waiting for review since 2026-09-30 (requested from @ novikd and @ devcrafter).

@clickhouse-gh

clickhouse-gh Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bef3a20b6 with master d741c9860 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

5 object files changed (+41.09 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/interpretSubquery.cpp.o 77.16 KiB 98.07 KiB +20.92 KiB (+27.11%)
src/CMakeFiles/dbms.dir/Interpreters/ExecuteScalarSubqueriesVisitor.cpp.o 188.63 KiB 208.80 KiB +20.17 KiB (+10.69%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

8 translation units recompiled, 15 s compile time in total, 8 of them have a recent master baseline.

Job report

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. external Author is not a member of the ClickHouse organization 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.

Standalone IN subquery inside an ordinary view reads the MV inserted block

4 participants