Skip to content

Fix WITH function-expression alias in CREATE VIEW with IN clause - #100042

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
fastio:bugfix-fix-with-func-alias-in-view
Mar 23, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
fastio:bugfix-fix-with-func-alias-in-view

Conversation

@fastio

@fastio fastio commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

ApplyWithSubqueryVisitor only collected ASTWithElement (subqueries) and ASTLiteral into its data maps, but missed ASTFunction and other expression types. Without replacement, AddDefaultDatabaseVisitor then qualified the identifier as a table name (e.g. default.ev), causing UNKNOWN_IDENTIFIER.

Replace the ASTLiteral-only check with a generic tryGetAlias check, so any aliased expression (including ASTFunction) is properly collected.

Fixes #99308

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 CREATE VIEW failing with UNKNOWN_IDENTIFIER when using a WITH function-expression alias (e.g. tuple(...)) as the right-hand side of IN.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.162

`ApplyWithSubqueryVisitor` only collected `ASTWithElement` (subqueries)
and `ASTLiteral` into its data maps, but missed `ASTFunction` and other
expression types. Without replacement, `AddDefaultDatabaseVisitor` then
qualified the identifier as a table name, causing `UNKNOWN_IDENTIFIER`.

Replace the `ASTLiteral`-only check with a generic `tryGetAlias` check,
so any aliased expression (including `ASTFunction`) is properly collected.

Fixes ClickHouse#99308
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Mar 21, 2026
@alexey-milovidov alexey-milovidov self-assigned this Mar 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8ce6726]

Summary: ✅


AI Review

Summary

This PR fixes ApplyWithSubqueryVisitor so WITH aliases are collected for any aliased expression (not only ASTLiteral), which resolves CREATE VIEW failures where IN uses a function-expression alias (e.g. tuple(...)). The change is small, targeted, and backed by a new stateless test reproducing the reported issue. I did not find high-confidence correctness, safety, or performance regressions in the touched code.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 100.00% (15/15, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov
alexey-milovidov merged commit e99d593 into ClickHouse:master Mar 23, 2026
159 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 23, 2026
alexey-milovidov pushed a commit that referenced this pull request Oct 4, 2026
…ryVisitor

`ApplyWithSubqueryVisitor` registered as a substitutable literal only the
alias of a direct `WITH` child, so in `WITH tuple([7] AS nested) AS wrapper`
it registered `wrapper` but not `nested`. A nested `SELECT` using `7 IN nested`
then kept the identifier, and the full `AddDefaultDatabaseVisitor` traversal,
which treats every name left in place as a table, qualified it as
`<db>.nested`. A view, a materialized view, `MODIFY QUERY` and
`ALTER TABLE ... UPDATE` with that shape read a same-named table instead of
the alias, or failed with `UNKNOWN_IDENTIFIER` (reproduced on 26.8.2.7 and
26.3.33.24).

The narrow pass that repairs a definition after SQL UDF expansion already
takes the aliases at any depth of a `WITH` expression, not inside a lambda or
a subquery, like the analyzer. A `Replicated` database writes that repaired
text into the DDL log and every replica processes it again through the full
pipeline, which re-qualified the alias. This made
`05237_with_expression_alias_scope_descendant` fail on every run under
`--replicated-database`.

Register every such alias right after the expression is visited, as the
direct child's alias was before, so it is inlined (`7 IN ([7] AS nested)`)
the way a top-level alias has been since #100042. The rule now lives in one
helper, `ApplyWithSubqueryVisitor::forEachWithExpressionAlias`, which the
narrow pass also calls.

The test gets back the mutation arms removed in the #114908 rework, each on
its own row, and a hand-written view arm.

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

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.

Creating a view using a CTE fails.

3 participants