Skip to content

Fix UNKNOWN_IDENTIFIER on ALTER TABLE ... DROP COLUMN with aliased default expression - #109374

Merged
alexey-milovidov merged 6 commits into
masterfrom
fix-alter-drop-column-default-alias
Jul 14, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
fix-alter-drop-column-default-alias

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

Closes: #109370

When a column has a DEFAULT or MATERIALIZED expression that defines and later references an inline alias (for example f(...) AS a, ..., a), an ALTER TABLE ... DROP COLUMN of an unrelated column failed with UNKNOWN_IDENTIFIER, even though the very same expression is accepted at CREATE TABLE time.

The column-dependency check that runs before dropping a column resolves each remaining default expression as a standalone query-tree node via QueryAnalyzer::resolve. That function collected inline aliases into the resolution scope only when the node was a LIST, but not when it was a single expression node — which is exactly what buildQueryTree produces for a bare default expression. As a result the internal alias was never registered in the scope and the later reference to it could not be resolved.

The fix collects the aliases for the single-expression case as well, making standalone expression resolution consistent with list resolution (the path already used by CREATE TABLE validation).

A new stateless test 04502_alter_drop_column_default_with_alias covers both a simple aliased default and the nested-lambda case from the report.

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 on ALTER TABLE ... DROP COLUMN when another column has a DEFAULT or MATERIALIZED expression that defines and references an inline alias.

Version info

  • Merged into: 26.7.1.939 (included in 26.7 and later)
  • Backported to: 26.6.2.109, 26.5.6.71, 26.4.5.157, 26.3.17.74

…fault expression

When a column has a `DEFAULT` or `MATERIALIZED` expression that defines and later
references an inline alias (e.g. `f(...) AS a, ..., a`), `ALTER TABLE ... DROP COLUMN`
of an unrelated column failed with `UNKNOWN_IDENTIFIER`.

The column-dependency check performed for the dropped column resolves each remaining
default expression as a standalone query tree node via `QueryAnalyzer::resolve`. That
function collected inline aliases into the resolution scope only when the node was a
`LIST`, not when it was a single expression node (which is what `buildQueryTree`
produces for a bare default expression). As a result the internal alias was never
registered and the later reference to it could not be resolved, while the same
expression validates fine at `CREATE TABLE` time (where it is wrapped in a query node).

Collect the aliases for the single-node case as well, making standalone expression
resolution consistent with list resolution.

Closes: #109370

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9fe5eb7]

Summary: ✅


AI Review

Summary

This PR fixes standalone-expression alias resolution in QueryAnalyzer::resolve and QueryAnalyzer::resolveConstantExpression, which unblocks both ALTER TABLE ... DROP COLUMN checks for columns with inline aliases in their default expressions and the mergeTreeAnalyzeIndexes predicate path. I checked the current diff, the touched code paths, the new tests, and the full prior discussion on the PR, including the previously raised resolveConstantExpression sibling issue; that issue is fixed in the current head, and I did not find any remaining correctness, safety, or evidence gaps that warrant a review comment.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 3, 2026
`lowerUTF8` requires ICU, which is excluded from the Fast test build, so
the test failed there with `Function with name 'lowerUTF8' does not exist`.

The function is applied to `hex(MD5(url))`, which is ASCII hex, so `lower`
produces an identical result. It is used in the `MATERIALIZED md5` column,
which is dropped by the test, so the expected output is unchanged.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109374&sha=4fdec8aa98df6b893755f3590d0dbd8a1b05f465&name_0=PR&name_1=Fast%20test

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread src/Analyzer/Resolve/QueryAnalyzer.cpp Resolved
alexey-milovidov and others added 2 commits July 11, 2026 20:10
Address review: the fix in `QueryAnalyzer::resolve` (which runs
`QueryExpressionsAliasVisitor` before resolving a standalone expression
node so that inline aliases like `f(...) AS a, ..., a` are collected into
the scope) was missing from the sibling `QueryAnalyzer::resolveConstantExpression`.

`StorageMergeTreeAnalyzeIndexes` builds a query tree from a user-supplied
predicate and resolves it via `resolveConstantExpression`, so a predicate
that defines and later references an inline alias hit the same
`UNKNOWN_IDENTIFIER` failure, e.g.:

    SELECT * FROM mergeTreeAnalyzeIndexes(currentDatabase(), data,
        concat(substring(toString(key) AS h, 1, 1), h) = '11')

    Code: 47. Unknown expression or function identifier `h` ...
    While executing MergeTreeAnalyzeIndexes. (UNKNOWN_IDENTIFIER)

Mirror the same alias-collection pre-pass in `resolveConstantExpression`
and add a focused regression test that exercises the
`mergeTreeAnalyzeIndexes` predicate path.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.90% +0.10%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.10% +0.10%

Changed lines: Changed C/C++ lines covered: 10/11 (90.91%) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

@alexey-milovidov alexey-milovidov self-assigned this Jul 14, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 14, 2026
Merged via the queue into master with commit 64ca48d Jul 14, 2026
177 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-alter-drop-column-default-alias branch July 14, 2026 17:19
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 14, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 23, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 23, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 23, 2026
Backport #109374 to 26.6: Fix UNKNOWN_IDENTIFIER on ALTER TABLE ... DROP COLUMN with aliased default expression
PedroTadim added a commit that referenced this pull request Jul 24, 2026
Backport #109374 to 26.3: Fix UNKNOWN_IDENTIFIER on ALTER TABLE ... DROP COLUMN with aliased default expression
PedroTadim added a commit that referenced this pull request Jul 24, 2026
Backport #109374 to 26.4: Fix UNKNOWN_IDENTIFIER on ALTER TABLE ... DROP COLUMN with aliased default expression
PedroTadim added a commit that referenced this pull request Jul 24, 2026
Backport #109374 to 26.5: Fix UNKNOWN_IDENTIFIER on ALTER TABLE ... DROP COLUMN with aliased default expression
UnamedRus pushed a commit to UnamedRus/ClickHouse that referenced this pull request Aug 12, 2026
…ABLE ... DROP COLUMN with aliased default expression
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…ABLE ... DROP COLUMN with aliased default expression
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…ABLE ... DROP COLUMN with aliased default expression
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…ABLE ... DROP COLUMN with aliased default expression
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…olumn-default-alias

Fix UNKNOWN_IDENTIFIER on ALTER TABLE ... DROP COLUMN with aliased default expression
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
STOR: DROP COLUMN when a DEFAULT/MATERIALIZED expression defines an inline alias -> exc.
Effect in the production build a394e92: wrong/failing (the upstream test fails on it and passes on official 26.8.8.8; evidence/fix_ci_20260925/upstream_repro/residual/109374.PROD263.txt).
Upstream patch unchanged, with its test(s): 04502_alter_drop_column_default_with_alias, 04514_mergetree_analyze_indexes_alias_predicate.

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-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.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I can create a table so that later ALTER TABLE ... DROP COLUMN gives syntax error

4 participants