Skip to content

Fix default expressions that read a subcolumn of an EPHEMERAL, ALIAS or omitted column - #127018

Open
novikd wants to merge 13 commits into
masterfrom
fix-subcolumn-dependencies-in-defaults
Open

novikd wants to merge 13 commits into
masterfrom
fix-subcolumn-dependencies-in-defaults

Conversation

@novikd

@novikd novikd commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

A DEFAULT or MATERIALIZED expression may read a subcolumn of another column, for example x Int64 MATERIALIZED j.a with j Tuple(a Int64, b Int64) (supported since #74403). Three places that collect the dependencies of such an expression looked the name j.a up only as a whole column, so the column it belongs to was never pulled in:

  • INSERT into a table whose parent column is EPHEMERAL or ALIAS failed with UNKNOWN_IDENTIFIER (Unknown expression or function identifier 'j.a'), while tupleElement(j, 'a') worked. Fixed in addDefaultRequiredExpressionsRecursively.
  • Reading old parts for a column added later with such a default (ALTER TABLE t ADD COLUMN x Int64 DEFAULT j.a, j ALIAS ...) failed the same way, because the reader did not read the inputs of the ALIAS expression. Fixed in injectRequiredColumnsRecursively and its mirror in ReadFromMergeTree, which now share a StorageSnapshot helper for the lookup.
  • INSERT omitting a stored parent column without a default (INSERT INTO t (line) with j Tuple(...), x MATERIALIZED j.a) failed with NOT_FOUND_COLUMN_IN_BLOCK: the parent was synthesised inside the defaults DAG, where no stage of the INSERT chain could extract its subcolumn. Fixed in addMissingDefaults.
  • A stored parent column without a default, added together with the dependent column (ALTER TABLE t ADD COLUMN j Tuple(a Int64, b Int64), ADD COLUMN x Int64 DEFAULT j.a), failed on read because the type default synthesised for the missing dependency carried the literal's type (Tuple(Int8, Int8)) instead of the declared one; it is now cast to the declared type.
  • ALTER TABLE ... RENAME COLUMN j TO jj left j.a unrenamed in defaults, TTL, skip indexes and constraints and failed; renaming a column whose subcolumn is part of a key is now refused with ALTER_OF_COLUMN_IS_FORBIDDEN like any other key column.

Each change comes with its own stateless test. The behaviour is the same on 26.4, 26.6, 26.8 and master (not a regression).

Closes: #127017

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 DEFAULT and MATERIALIZED expressions that read a subcolumn (for example j.a) of an EPHEMERAL or ALIAS column, or of a column omitted from the INSERT, which failed with UNKNOWN_IDENTIFIER or NOT_FOUND_COLUMN_IN_BLOCK; ALTER TABLE ... RENAME COLUMN now follows such references.

🤖 Generated with Claude Code


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

novikd and others added 8 commits October 20, 2026 18:02
A `DEFAULT` or `MATERIALIZED` expression such as `j.a` requires the column `j`;
`addDefaultRequiredExpressionsRecursively` looked the name up only as a whole column
and silently skipped it, so an `EPHEMERAL` or `ALIAS` parent was never computed and
the `INSERT` failed with `UNKNOWN_IDENTIFIER`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
… default reads

`injectRequiredColumnsRecursively` resolved the subcolumn among physical columns only, so
for an `ALIAS` parent the inputs of its expression were not read and the default of the
column missing in the part either failed with `UNKNOWN_IDENTIFIER` or, with the parent now
computed, used type defaults for the inputs.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ad them

When an `INSERT` omits a column without a default, `addMissingDefaults` fills it with the
type default inside its own DAG; a default such as `j.a` then needed a subcolumn input no
stage of the `INSERT` chain could provide, and the `INSERT` failed with
`NOT_FOUND_COLUMN_IN_BLOCK`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`RenameColumnVisitor` renamed an identifier only when its whole name was the column, so a
default, `TTL`, skip index or constraint written as `j.a` kept the old name and the
`ALTER` failed with `UNKNOWN_IDENTIFIER`; a column whose subcolumn is in a key is now
refused like any key column instead of failing in the key re-analysis.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The "default default" of a missing dependency was a literal typed by the
`Field` (a `Tuple(a Int64, b Int64)` default became `Tuple(Int8, Int8)`), so a
`DEFAULT` expression reading `j.a` on a stored parent without a default could
not resolve it while reading. Build `_CAST(<literal>, '<declared type>')` from
the start.

The test also pins the `MATERIALIZE COLUMN` and `EPHEMERAL`-parent read paths.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The reader and the read-size estimate shared a duplicated loop that follows a
subcolumn to the column it belongs to; it is now the `StorageSnapshot` helper
`getDefaultOfColumnOrSubcolumn`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`05430_hyperloglog_corrupted_state` landed on master after this branch was cut.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8a30072]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) FAIL
02317_distinct_in_order_optimization FAIL cidb
01414_push_predicate_when_contains_with_clause NOT_FAILED cidb
01283_max_threads_simple_query_optimization NOT_FAILED cidb

AI Review

Summary

The PR fixes dependency collection and evaluation for defaults that read subcolumns of EPHEMERAL, ALIAS, or omitted columns, and extends column renaming to subcolumn references. Review of head 8a3007220cd6ce5f585469aded3a1e72d4dd4c36 found two reproducible regressions: renaming can silently retarget a materialized expression, and default lookup can make reads of ordinary dotted columns fail recursively. Quoted references and dotted parent names also remain unsupported by the rename fix.

Findings

❌ Blockers

  • src/Interpreters/RenameColumnVisitor.cpp:24: Renaming a parent changes references to an unrelated ordinary dotted column. With j Tuple(a Int64) ALIAS tuple(100), an ordinary column named j.a, and x Int64 MATERIALIZED j.a, the expression correctly reads the ordinary column. Renaming j to jj rewrites it to jj.a even though ordinary j.a remains. In the PR's CI binary, inserting j.a = 7 before the rename and j.a = 8 afterward produces x = 7, 100, instead of 7, 8. Resolve the actual owning column before rewriting identifiers, preserving ordinary-column precedence.

  • src/Storages/StorageSnapshot.cpp:257: An exact ordinary column without a default inherits an unrelated parent's default. Create t (flag UInt8), insert one row, then add j Tuple(a Int64) ALIAS tuple(`j.a`) and ordinary j.a Int64 together. Reading ordinary j.a should return 0; the PR binary raises TOO_DEEP_RECURSION. The helper returns j's expression for missing ordinary j.a, so injectRequiredColumnsRecursively repeatedly follows the same identifier. This also reproduces after clearing ordinary j.a. Use the normal column/subcolumn resolution and return the resolved column's own default, including no default.

⚠️ Majors

  • src/Interpreters/RenameColumnVisitor.cpp:22: Supported subcolumn spellings still fail during rename. A quoted identifier such as MATERIALIZED `j.a` has one AST name part, so renaming j misses it. For n Nested(j Tuple(a Int64, b Int64)), the expression n.j.a belongs to physical column n.j, but the first AST name part is n; renaming n.j to n.k also misses it. Both operations raise UNKNOWN_IDENTIFIER on the PR binary. Resolve the full owning column and preserve the remaining subcolumn path.
Tests
  • ⚠️ Add focused stateless coverage for ordinary dotted-column precedence during rename and missing-column default lookup, plus quoted subcolumns and dotted parent names during rename. All four added tests pass in the Fast test report, but do not cover these inputs. The findings above were reproduced using that job's clickhouse-stripped binary.
Final Verdict

❌ Block. Preserve exact-column precedence in both rename and default lookup, handle the remaining subcolumn representations, and add regression coverage for these cases.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 20, 2026
Comment on lines +24 to +25
identifier.name_parts.front() = rename_to;
identifier.resetFullName();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Preserve references to ordinary dotted columns during rename

A dotted identifier is not necessarily a subcolumn of its first component: a real column named j.a takes precedence over the subcolumn of j. This valid example silently changes the materialized expression's input on the PR binary:

CREATE TABLE t
(
    j Tuple(a Int64) ALIAS tuple(100),
    `j.a` Int64,
    x Int64 MATERIALIZED j.a
) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t (`j.a`) VALUES (7);
ALTER TABLE t RENAME COLUMN j TO jj;
INSERT INTO t (`j.a`) VALUES (8);
SELECT x FROM t ORDER BY x;

The result is 7, 100, instead of 7, 8: this branch rewrites x to jj.a even though the ordinary j.a column was not renamed. This changes persisted values for subsequent inserts. Please resolve the identifier against the table's column metadata before deciding that it belongs to the renamed column, and add a regression test for this precedence rule.

Comment on lines +257 to +261
for (auto [name_in_storage, subcolumn_name] : Nested::getAllColumnAndSubcolumnPairs(column_name))
{
auto column = tryGetColumn(GetColumnsOptions::All, String(name_in_storage));
if (column && column->type->tryGetSubcolumnType(subcolumn_name))
return getDefault(column->name);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Preserve an exact column's lack of a default

getDefault(column_name) returning nothing does not mean the exact column is absent. When an ordinary column named j.a exists without a default, this loop incorrectly borrows the default of an ALIAS column j. On the PR binary:

CREATE TABLE t (flag UInt8) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t VALUES (1);
ALTER TABLE t
    ADD COLUMN j Tuple(a Int64) ALIAS tuple(`j.a`),
    ADD COLUMN `j.a` Int64;
SELECT `j.a` FROM t;

The read should return the ordinary column's type default, 0, but raises TOO_DEEP_RECURSION: injectRequiredColumnsRecursively looks up missing j.a, receives tuple(j.a), and recursively requests the same missing column. This also reproduces after CLEAR COLUMN j.a on a populated table. Please resolve the actual column/subcolumn with the normal metadata lookup, preserving exact-column precedence, and return that column's own default (including no default) rather than searching an unrelated parent. Cover the old-part read with a stateless test.

}

/// `j.a` is the subcolumn `a` of the column `j`.
if (identifier.compound() && identifier.name_parts.front() == column_name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Handle quoted subcolumns and dotted parent column names

Matching only name_parts.front misses two supported representations of the same dependency. In x Int64 MATERIALIZED `j.a` , the quoted identifier has one name part, so renaming j does not enter this branch. For a flattened Nested member, n.j.a belongs to the physical column n.j, but its first name part is n, so renaming n.j also misses the reference.

Both cases still fail with UNKNOWN_IDENTIFIER on the PR binary:

CREATE TABLE t1 (j Tuple(a Int64), x Int64 MATERIALIZED `j.a`)
ENGINE = MergeTree ORDER BY tuple();
ALTER TABLE t1 RENAME COLUMN j TO jj;

CREATE TABLE t2
    (n Nested(j Tuple(a Int64, b Int64)), x Array(Int64) MATERIALIZED n.j.a)
ENGINE = MergeTree ORDER BY tuple();
ALTER TABLE t2 RENAME COLUMN `n.j` TO `n.k`;

Please identify the owning column from the full resolved name and rewrite its prefix while preserving the subcolumn path. Add both forms to the rename regression coverage.

@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8a3007220 with master f830368c7 (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.

Compile time of recompiled translation units

1048 translation units recompiled, 7011 s compile time in total, 928 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/Storages/Kafka/KafkaConfigLoader.cpp: 5.9 s
  • src/Parsers/ASTSystemQuery.cpp: 5.7 s
  • src/Parsers/LogsQL/LogsQLPipes.cpp: 5.3 s

Job report

novikd and others added 5 commits November 5, 2026 21:06
…until the stack or memory is exhausted

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ries the table's defaults as ALIAS columns

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

1 participant