Skip to content

Fix default expression evaluation when referencing other columns with defaults - #94615

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-default-expression-dependency-evaluation
Jan 26, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-default-expression-dependency-evaluation

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jan 19, 2026 •

Copy link
Copy Markdown
Member

When inserting partial rows where one column's default expression references another column that also has a default, the default expression was incorrectly using the literal default value instead of the provided value.

For example, with table:
n UInt8 DEFAULT 42
s String DEFAULT concat('test', CAST(n, 'String'))

Inserting {"n": 2} would produce s='test42' instead of s='test2'.

The issue was that AddingDefaultsTransform erased ALL columns with defaults from the evaluation block, so when evaluating s's default expression, column n wasn't available and the code fell back to n's literal default value (42).

The fix processes default expressions in dependency order using topological sort: first compute defaults for columns with no dependencies, mix them into the result block, then compute defaults for dependent columns which can now see the correct values.

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 expression evaluation when referencing other columns with computed defaults.

Version info

  • Merged into: 26.1.1.880

… defaults

When inserting partial rows where one column's default expression
references another column that also has a default, the default
expression was incorrectly using the literal default value instead
of the provided value.

For example, with table:
  n UInt8 DEFAULT 42
  s String DEFAULT concat('test', CAST(n, 'String'))

Inserting {"n": 2} would produce s='test42' instead of s='test2'.

The issue was that `AddingDefaultsTransform` erased ALL columns with
defaults from the evaluation block, so when evaluating s's default
expression, column n wasn't available and the code fell back to n's
literal default value (42).

The fix processes default expressions in dependency order using
topological sort: first compute defaults for columns with no
dependencies, mix them into the result block, then compute defaults
for dependent columns which can now see the correct values.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jan 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2a03f0a]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug) failure
Logical error: Unexpected return type from A. Expected B. Got C. Action: (STID: 1611-41f8) FAIL cidb
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: (STID: 1941-1bfa) FAIL cidb, issue

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jan 19, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

#94851

@alexey-milovidov alexey-milovidov self-assigned this Jan 26, 2026
@alexey-milovidov
alexey-milovidov merged commit d553ae4 into master Jan 26, 2026
126 of 128 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-default-expression-dependency-evaluation branch January 26, 2026 13:11
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jan 26, 2026
@vdimir vdimir self-assigned this Mar 19, 2026
@vdimir vdimir added the post-approved Approved, but after the PR is merged. label Mar 19, 2026
/// and column `n` has DEFAULT 42, when inserting {"n": 2} we need to first compute
/// n's defaults (for rows where n is missing), mix them into the block, and only
/// then compute s's defaults so that s can see the correct values of n.
std::unordered_map<String, NameSet> dependencies;

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.

Matching by name does not always work, sometimes we can depend on subcolumn:

DROP TABLE IF EXISTS test_defaults_tuple;
CREATE TABLE test_defaults_tuple (
    a Tuple(x Int32, y Int32) DEFAULT (b.x * 10, b.y * 10),
    b Tuple(x Int32, y Int32) DEFAULT (1, 2)
) ENGINE = Memory;

INSERT INTO test_defaults_tuple FORMAT JSONEachRow
{"b": {"x": 5, "y": 6}}
{};

SELECT * FROM test_defaults_tuple;
-- Returns:
-- (10,20)	(5,6)
-- (10,20)	(1,2)

-- Expected:
-- (50,60)	(5,6)
-- (10,20)	(1,2)

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

Also, since code became a bit more complicated, lets add more testcases for different types and circular dependencies #100048

@jrdi

jrdi commented May 12, 2026

Copy link
Copy Markdown
Contributor

@alexey-milovidov can this fix be backported to LTS versions? At least the previous one to this fix, CH 25.8

zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 10, 2026
Antalya 25.8 Backport of ClickHouse#94615 - Fix default expression evaluation when referencing other columns with defaults
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 10, 2026
Stable 25.8 Backport of ClickHouse#94615 - Fix default expression evaluation when referencing other columns with defaults
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

post-approved Approved, but after the PR is merged. 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.

4 participants