Repository navigation
Support DEFAULT expressions inside Tuple data types - #108090
alexey-milovidov wants to merge 109 commits into
Conversation
Allow `DEFAULT` expressions for named elements of `Tuple` data types,
e.g. `Tuple(a UInt8, s String DEFAULT 'Hello')`. Such defaults exist only
at the syntax level: they are pulled up to the column level in
`InterpreterCreateQuery`, so the stored type is normalized without any
`DEFAULT` inside it. The example above becomes type `Tuple(a UInt8, s String)`
with a column-level `DEFAULT tuple(defaultValueOfTypeName('UInt8'), 'Hello')`.
A default expression may reference other columns, but not other elements of
the same tuple/nested; a reference that collides with an element name is
rejected as ambiguous. Building an actual data type while a default is set
throws.
`DEFAULT` inside `Nested` (which is `Array(Tuple(...))`) or `Array` is not
supported yet and is rejected with a clear error, because a scalar element
default cannot be represented as a static array column default.
Closes: #2797
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [e4ecff3] Summary: ❌
|
The PR adds a New Feature (DEFAULT expressions for named Tuple elements), which the CI `feature_docs.py` hook requires to be documented. Add a `DEFAULT expressions for Tuple elements` section to the Tuple data type docs describing the normalization (defaults pulled up to the column level), the restriction against referencing sibling elements, and the `NOT_IMPLEMENTED` rejection for `Nested`/`Array`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Addresses review feedback on #108090. `DEFAULT` expressions written inside `Tuple` data types were normalized only on the `CREATE TABLE` path. `ALTER TABLE ... ADD COLUMN` and `MODIFY COLUMN` parse the same `ASTColumnDeclaration` but build the data type directly via `DataTypeFactory::get`, so `DataTypeTuple::create` saw the embedded `ASTNameTypePair::default_expression` and threw before any normalization ran. As a result `ALTER TABLE t ADD COLUMN c Tuple(a UInt8 DEFAULT 1)` was rejected instead of being normalized to a column-level `DEFAULT tuple(1)`. Extract the pull-up normalization out of `InterpreterCreateQuery` into a shared free function `pullUpTupleElementDefaults` and call it from `AlterCommand::parse` for `ADD COLUMN` and `MODIFY COLUMN` before the data type is built, so all paths normalize identically. The clone owned by `command.ast` is normalized in place so the extracted type, the extracted default, and the stored AST all agree. Extend `04402_default_inside_tuple` with focused `ALTER ADD COLUMN` / `MODIFY COLUMN` coverage, including `system.columns` checks that the type is stripped and the column-level `DEFAULT` is stored, and that `DEFAULT` inside `Nested`/`Array` is rejected with `NOT_IMPLEMENTED` on the ALTER path too. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…mbiguity check lambda-scope-aware Two follow-ups for the AI review of #108090: * `Nullable(Tuple(...))` is a supported tuple wrapper (behind `enable_nullable_tuple_type`), and a `DEFAULT` inside it is representable as the same column-level `tuple(...)` default that is then cast to the nullable tuple type. `buildAndStripTupleDefaults` now recurses through `Nullable` instead of rejecting it as `NOT_IMPLEMENTED`. If the setting is off, the `Nullable(Tuple)` type itself is still rejected later during type validation. * `collectReferencedIdentifiers` treated every `ASTIdentifier` under a default expression as an external reference, so a lambda whose parameter is named like a tuple element (e.g. `Tuple(x UInt8, y Array(UInt8) DEFAULT arrayMap(x -> x + 1, [1, 2, 3]))`) was wrongly rejected as ambiguous. The walk is now lambda-scope-aware: it skips the parameter tuple, ignores identifiers bound by the lambda while walking its body, and still collects free identifiers from the body. Added stateless coverage for both: a `Nullable(Tuple(... DEFAULT ...))` column, a lambda parameter shadowing an element name, and a free identifier in a lambda body that collides with an element name and is still rejected. Documentation updated accordingly. The test is renumbered to avoid collision with tests added on master. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…mbiguity check lambda-scope-aware Two follow-ups for the AI review of #108090: * `Nullable(Tuple(...))` is a supported tuple wrapper (behind `enable_nullable_tuple_type`), and a `DEFAULT` inside it is representable as the same column-level `tuple(...)` default that is then cast to the nullable tuple type. `buildAndStripTupleDefaults` now recurses through `Nullable` instead of rejecting it as `NOT_IMPLEMENTED`. If the setting is off, the `Nullable(Tuple)` type itself is still rejected later during type validation. * `collectReferencedIdentifiers` treated every `ASTIdentifier` under a default expression as an external reference, so a lambda whose parameter is named like a tuple element (e.g. `Tuple(x UInt8, y Array(UInt8) DEFAULT arrayMap(x -> x + 1, [1, 2, 3]))`) was wrongly rejected as ambiguous. The walk is now lambda-scope-aware: it skips the parameter tuple, ignores identifiers bound by the lambda while walking its body, and still collects free identifiers from the body. Added stateless coverage for both: a `Nullable(Tuple(... DEFAULT ...))` column, a lambda parameter shadowing an element name, and a free identifier in a lambda body that collides with an element name and is still rejected. Documentation updated accordingly. The test is renumbered to avoid collision with tests added on master. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the two AI-review majors (pushed in 4c2d17d, on top of a fresh
Added focused stateless coverage in Verification: the changed TU compiles cleanly under the project's |
|
Status (continue-pr shepherding pass):
The sole CI red is not a regression:
No master re-merge: merge-base is ~1 day old and there are no conflicts, so a merge would not change the flake. PR is |
|
Status (continue-pr shepherding pass):
|
|
Status (continue-pr shepherding pass): the only non-green check is
Re-ran both failed sync jobs (private run |
|
Status (continue-pr shepherding pass): the reruns of the two private
No The only remaining gate is a human approving review. |
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
The |
…g the column type `fuzzQuery` calls `DataTypeFactory::get` on the raw column type AST in `QueryFuzzer::fuzzColumnDeclaration`, before `pullUpTupleElementDefaults` normalization ever runs, so a valid query like `CREATE TABLE t (c Tuple(a UInt8 DEFAULT 1)) ENGINE = Memory` threw `BAD_ARGUMENTS` from the `DataTypeTuple::create` fail-safe instead of being fuzzed. Strip `ASTNameTypePair::default_expression` there the same way as the main DDL paths, and add a `fuzzQuery` regression test. Addresses the AI review finding on #108090 Co-Authored-By: Claude Fable 5 <[email protected]>
|
The Related: #107619 |
|
Two more CI failures on
The remaining |
…ma for tuple-element defaults `AlterCommands::prepare` ignores `RENAME COLUMN IF EXISTS`, `DROP COLUMN IF EXISTS` and `ADD COLUMN IF NOT EXISTS` by checking the columns of the table before the query, and `AlterCommand::apply` then checks again against the columns so far (for `DROP COLUMN IF EXISTS`, only an exact column name, not a flattened `Nested` group). The simulated schema that tells an independent dotted column from a subcolumn checked only the columns so far, so a rename of a column added by the same query could invent an independent column, and a skipped drop could remove one. Apply both checks, as execution does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: the |
|
The fix is in progress: #114019 (open, mergeable, no review yet). It is the same race:
It is not caused by this PR: the same assertion also fired in public CI on two unrelated PRs, e.g. Stress test (arm_asan_ubsan). |
…mmands in tuple-element defaults An `ALTER` query runs as segments of consecutive commands of the same kind, and `runCommandSegments` calls `AlterCommands::prepare` for each segment of `AlterCommands` with the metadata committed by the preceding segments. A mutation between the commands therefore makes a column added before it count as existing for a later `RENAME COLUMN IF EXISTS` or `DROP COLUMN IF EXISTS`. The simulated schema in `pullUpTupleElementDefaultsInAlterQuery` used the columns from before the whole query instead, so it could reject a valid reference to a dotted column as a self-reference, or accept a reference to a column that the query drops. Reset the starting columns at each segment boundary. `AlterCommand::isAlterCommand` tells which commands go to `AlterCommands`; `AlterCommand::parse` asserts that it agrees. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 The |
…` as `no-replicated-database` The test runs an `ALTER` query that has an `UPDATE` mutation between other commands, because that is the case it checks. `Replicated` databases reject `ALTER` queries that mix command types with `QUERY_IS_PROHIBITED`, so the test always failed in the `DBReplicated` jobs. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=108090&sha=4d9a12e40e7c5a44438c99af1991d81d9381cc5f&name_0=PR PR: #108090 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… column type A `DEFAULT` written inside a tuple element that references `c.<x>` from inside the column `c` was rejected only if `<x>` was a subcolumn of the type as written. The type is not final at that point: the `NULL` modifier or `data_type_default_nullable` may wrap it in `Nullable` (adding the `null` subcolumn), and a later `MODIFY COLUMN` of the same `ALTER` may change it while keeping the generated default. Such defaults were accepted and failed on the first insert with `UNKNOWN_IDENTIFIER`. Now `c.<x>` is a self-reference unless a table column has exactly this name (the exact dotted-column precedence is preserved). A name that is neither a table column nor a subcolumn cannot be resolved anyway, so no valid default is rejected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-name columns A `DEFAULT` written inside a tuple element of the column `c` that referenced `c.n.x` was rejected as a self-reference unless a table column was named exactly `c.n.x`, although the name may be a subcolumn of another column with a dotted name, such as `c.n Tuple(x UInt8)` or `c.a Array(UInt8)` (for `c.a.size0`). Now such a name is resolved to its owning column: an exact column name still takes precedence, then a table column named by a dot-delimited prefix longer than `c` owns it. It is a self-reference if no other column owns it, or if `c` has a subcolumn with the same name too (ambiguous ownership, resolved by column order in `ColumnsDescription`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
… initiator lacks the table An `ALTER TABLE ... ON CLUSTER` can be submitted from a host that does not have the table. Then the initiator cannot tell whether `c.a` in a `DEFAULT` written inside the tuple `c` is an independent column or a subcolumn of `c`, and it rejected the query as a self-reference. Now such a declaration is left as written, and each host normalizes it against its own table when it executes the query. `data_type_default_nullable` wraps the type of such a declaration in `Nullable` as written. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g dotted-name column A default that references a subcolumn of another column with a dotted name (`c.n.x` of the column `c.n`) failed with `UNKNOWN_IDENTIFIER` when `c.n` was missing as well: `c.n` is computed by its own default under an alias, and an identifier is bound to an alias by its first part only. Rewrite such a reference as `getSubcolumn` of its column in `addDefaultRequiredExpressionsRecursively`. This also makes the column a required column of the default when reading parts that lack both columns. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n rewriting subcolumn references in defaults `replaceReferencesToSubcolumnsOfMissingColumns` treated every identifier of a default expression as a column reference, so an identifier bound to a lambda parameter (`arrayMap(x -> x.n.v, a)`) or to an expression alias (`(1 AS `x.a`) + `x.a``) was rewritten into `getSubcolumn` of an unrelated missing table column, silently changing the inserted values. A dotted lambda parameter declaration was rewritten as well and failed with `TYPE_MISMATCH`. Collect the aliases of the expression and the parameters of enclosing lambdas, skip identifiers bound to them by their whole name or leading parts, and do not rewrite lambda parameter declarations. Co-Authored-By: Claude Opus 5.5 <[email protected]>
After merging master, `DataTypeFactory::get` is `[[nodiscard]]`, and the build failed with `-Wunused-result`. The call is made only to validate the type. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he insert column list `replaceReferencesToSubcolumnsOfMissingColumns` looked up the owner of a subcolumn reference with `ColumnsDescription::tryGetColumnOrSubcolumn`, which returns whichever column registered the subcolumn name first. With `` `x.n` Tuple(v UInt8) `` and `x Tuple(n Tuple(v UInt8))` in one table, the reference `x.n.v` was rewritten to `getSubcolumn` of `x.n` when `x.n` was omitted from the insert, while the analyzer bound it through `x` when `x.n` was supplied. So the same default produced different values for different insert column lists, also for column-level defaults that worked before. The owner is now chosen the way the analyzer does it in `TableExpressionData::tryGetSubcolumnInfo`: the shortest prefix naming a physical column whose type has such a subcolumn. It depends neither on the columns of the block nor on the order of the columns in the table. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The header uses `String` but relied on a transitive include through `Core/Names.h`, which no longer provides it on current master, so the Fast test build failed with "unknown type name 'String'". https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=108090&sha=3b195139ffcab3bf55296c454a96462766f655f5&name_0=PR&name_1=Fast%20test #108090 Co-Authored-By: Claude Fable 5.2 <[email protected]>
Related: #2797
DEFAULTexpressions are now supported for named elements ofTupledata types, e.g.:Such defaults exist only at the syntax level: they are pulled up to the column level (for
CREATE TABLEas well asALTER TABLE ... ADD COLUMN/MODIFY COLUMN), so the stored schema is normalized without anyDEFAULTinside the data type. The column above is stored as typeTuple(a UInt8, s String)with a column-levelDEFAULT tuple(defaultValueOfTypeName('UInt8'), 'Hello').A default expression may reference other columns, but not other elements of the same tuple/nested; a reference that collides with an element name is rejected as ambiguous. Building an actual data type while a default is set throws.
DEFAULTinsideNested(which isArray(Tuple(...))) orArrayis not supported yet and is rejected with a clearNOT_IMPLEMENTEDerror, because a scalar element default cannot be represented as a static array column default. Note this is why issue #2797 (which asks specifically forDEFAULTonNestedelements) is linked asRelatedrather thanCloses.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Support
DEFAULTexpressions insideTupledata types, e.g.Tuple(a UInt8, s String DEFAULT 'Hello'). The default is normalized away from the type and pulled up to the column level. Works forCREATE TABLEandALTER TABLE ... ADD/MODIFY COLUMN.Documentation entry for user-facing changes
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/108090]
Workflow [PR]
Sync PR [sync-upstream/pr/108090]