Skip to content

Inline VIEW subquery during query analysis - #100830

Merged
novikd merged 9 commits into
masterfrom
analyzer-inline-view
Apr 4, 2026
Merged

novikd merged 9 commits into
masterfrom
analyzer-inline-view

Conversation

@novikd

@novikd novikd commented Mar 26, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Inline VIEW subquery in the query tree to allow more optimisations to be applied to the VIEW.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.566

@clickhouse-gh

clickhouse-gh Bot commented Mar 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [731eb3f]

Summary: ❌

job_name test_name status info comment
Stress test (arm_msan) failure
Server died FAIL cidb IGNORED
MemorySanitizer: use-of-uninitialized-value (STID: 2410-47e0) FAIL cidb IGNORED

AI Review

Summary

This PR adds experimental setting analyzer_inline_views and inlines ordinary VIEW definitions into analyzer query trees to unlock optimizations. The direction is good, but there are correctness gaps around preserving VIEW schema/name semantics after inlining. I cannot approve as-is.

Missing context
  • ⚠️ No CI run/report links or failed-job logs were provided in this review context.
Findings

❌ Blockers

  • [src/Analyzer/Resolve/QueryAnalyzer.cpp:5079] Inlined nodes preserve only table_node->getAlias(). For FROM view_name without explicit alias, qualified references through the original view name can stop resolving because TableNode name binding is dropped when replaced by QueryNode/UnionNode.
    Suggested fix: preserve original view binding when no user alias is provided (fallback alias to table name; keep compatibility for qualified references like view.col).

  • [src/Analyzer/Resolve/QueryAnalyzer.cpp:4945-5025] Inlining still derives output from inner_query instead of fully enforcing declared VIEW header semantics (name/type projection contract). Current logic primarily handles type differences, but header/name mapping cases remain incomplete and can change query behavior.
    Suggested fix: always normalize inlined projection to metadata columns (names + types) exactly as StorageView::read contract does, or skip inlining whenever header/output mismatch exists.

Tests
  • ⚠️ Add a dedicated stateless test for qualified column references through inlined views (SELECT v.col FROM v) with and without explicit alias.
  • ⚠️ Add a test where CREATE VIEW v (x, y) AS SELECT a, b ... keeps same types but different names; verify inlined and non-inlined behavior are identical.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ✅
No magic constants ✅
Backward compatibility ⚠️ Inlining can change existing VIEW name/header resolution semantics in some queries.
SettingsChangesHistory.cpp ✅
PR metadata quality ✅
Safe rollout ⚠️ Feature is gated, but current semantic mismatches block safe enablement.
Compilation time ✅
Performance & Safety

Inlining can improve plans, but semantic drift for VIEW output/name contract is a correctness risk (query result/identifier resolution differences), so safety must be fixed before relying on performance gains.

Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Preserve original view-qualified identifier binding after inlining.
    • Enforce full VIEW header contract (names and types) in the inlined path, or skip inlining when not guaranteed.
    • Add regression tests for qualified references and header rename-only cases.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 26, 2026
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
{
for (size_t i = 0; i < view_columns.size(); ++i)
{
if (!view_columns[i].type->equals(*subquery_columns[i].type))

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.

Will it work for subcolumns as well?
It's probably possible to get a subcolumn of the same type, e.g., for JSON.

Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
}

auto wrapper_context = Context::createCopy(scope.context);
auto wrapper_query = std::make_shared<QueryNode>(std::move(wrapper_context));

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.

I also think we can only have one query node for renames, type conversions and row policy filter

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

Overall looks good.

@clickhouse-gh

clickhouse-gh Bot commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% (739,288/879,534) 84.10% (739,566/879,668) +0.00% (+278)
Functions 90.80% (798,085/878,572) 90.90% (798,409/878,605) +0.10% (+324)
Branches 76.60% (239,674/312,888) 76.60% (239,733/312,936) +0.00% (+59)

Changed lines: 96.15% (150/156) · Uncovered code

Full report · Diff report

@novikd
novikd added this pull request to the merge queue Apr 4, 2026
@novikd

novikd commented Apr 4, 2026

Copy link
Copy Markdown
Member Author

Merged via the queue into master with commit 42d5d12 Apr 4, 2026
158 of 159 checks passed
@novikd
novikd deleted the analyzer-inline-view branch April 4, 2026 21:49
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 4, 2026
Mip182 pushed a commit to Mip182/ClickHouse that referenced this pull request May 9, 2026
…view

Inline VIEW subquery during query analysis
alexey-milovidov added a commit that referenced this pull request Oct 3, 2026
…to 26.3

Resolve the conflict in `StorageView.cpp` and reconcile the change with 26.3:

- 26.3 does not have `Context::isViewInnerQuery` (added on master by
  #94359, not backported).
  Add the `is_view_inner_query` flag to `Context` and set it in
  `getViewContext` of `StorageView`, so the inner query of an ordinary view
  reads the table itself, not the inserted block of a materialized view.
- 26.3 does not have view inlining in the analyzer
  (`StorageView::getViewSubqueryContext`, `analyzer_inline_views`, from
  #100830). Drop that part
  and the test `05293_mv_ordinary_view_over_source_inlined`.
- Use `QueryNode::getJoinTree` instead of `getJoinTreeNodeTyped`, which
  does not exist in 26.3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

3 participants