Skip to content

Analyzer: fix qualified CTE column references in views - #111386

Merged
novikd merged 6 commits into
masterfrom
fix-analyzer-view-cte-qualified-column
Jul 31, 2026
Merged

novikd merged 6 commits into
masterfrom
fix-analyzer-view-cte-qualified-column

Conversation

@novikd

@novikd novikd commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

Related: https://github.com/ClickHouse/clickhouse-private/issues/55715

Queries stored in views lost the ability to qualify columns by CTE name under the analyzer (enable_analyzer = 1):

CREATE VIEW v AS WITH c AS (SELECT 1 AS x) SELECT c.x AS f FROM c AS s;
SELECT * FROM v SETTINGS enable_analyzer = 1;
-- Code: 47. DB::Exception: Unknown expression identifier `c.x` in scope
--   WITH c AS (SELECT 1 AS x) SELECT c.x AS f FROM c AS s. Maybe you meant: ['s.x'].

The same query works inline under both analyzers. CREATE VIEW expands CTE references in FROM into subqueries with cte_name set (ApplyWithSubqueryVisitor, needed so AddDefaultDatabaseVisitor does not qualify the CTE name with the default database), and QueryTreeBuilder dropped that name when building the query tree, so the table expression was addressable only by its alias. Inline, the analyzer resolves the CTE reference from the WITH section, and the resulting table expression keeps both the CTE name and the alias.

The fix propagates cte_name of the expanded subquery into the query tree node, making views (and everything else going through ApplyWithSubqueryVisitor, e.g. materialized views and ALTER TABLE ... MODIFY QUERY) behave identically to inline queries. The cte_name of ASTSubquery in IN-clause subqueries is intentionally left untouched — nothing binds identifiers against the name of an IN subquery.

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 UNKNOWN_IDENTIFIER error for columns qualified by CTE name (cte_name.column) in queries stored in views when the analyzer is enabled.

Version info

  • Merged into: 26.8.1.503 (included in 26.8 and later)
  • Backported to: 26.7.2.49, 26.6.2.149, 26.5.6.108

novikd and others added 3 commits July 22, 2026 14:05
CREATE VIEW expands CTE references in FROM into subqueries with
cte_name set (ApplyWithSubqueryVisitor). QueryTreeBuilder dropped
that name, so qualified identifiers like `c.x` in
`WITH c AS (...) SELECT c.x FROM c AS s` stopped resolving when the
query was stored in a view, while working inline.

ClickHouse/clickhouse-private#55715 (comment)
@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [805786a]

Summary: ✅


AI Review

Summary

This PR propagates cte_name from ApplyWithSubqueryVisitor-expanded FROM subqueries into the query tree so stored queries resolve cte_name.column the same way inline analyzer queries already do. I reviewed the current diff, the existing review thread, and the current code paths in QueryTreeBuilder and QueryAnalyzer; I did not find a remaining correctness issue in the patched behavior, and the current PR CI is all green.

Final Verdict

✅ No blocking or major issues found in the current patch.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 22, 2026
Comment thread src/Analyzer/QueryTreeBuilder.cpp Resolved
@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: 10/10 (100.00%) · Uncovered code

Full report · Diff report

@KochetovNicolai KochetovNicolai self-assigned this Jul 31, 2026
@novikd
novikd added this pull request to the merge queue Jul 31, 2026
Merged via the queue into master with commit c89e9b6 Jul 31, 2026
179 checks passed
@novikd
novikd deleted the fix-analyzer-view-cte-qualified-column branch July 31, 2026 11:11
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 31, 2026
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 31, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 31, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Jul 31, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 31, 2026
Backport #111386 to 26.7: Analyzer: fix qualified CTE column references in views
novikd added a commit that referenced this pull request Aug 3, 2026
Backport #111386 to 26.5: Analyzer: fix qualified CTE column references in views
novikd added a commit that referenced this pull request Aug 3, 2026
Backport #111386 to 26.6: Analyzer: fix qualified CTE column references in views
UnamedRus pushed a commit to UnamedRus/ClickHouse that referenced this pull request Aug 12, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants