Repository navigation
Read the default of a column that a part does not hold, not a subcolumn of that name - #118242
alexey-milovidov wants to merge 98 commits into
Conversation
…mn of that name
`ALTER TABLE ... ADD COLUMN` may add a column whose name is also the name of a
generated subcolumn of another column, such as `a.size0` next to an `Array`
column `a`. The parts written before the ALTER hold no such column, and the
reader looked its name up among the part's columns *and their subcolumns*, so it
answered with the array's sizes instead of the column's default:
CREATE TABLE t (a Array(UInt64)) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t VALUES ([0]), ([1, 2]);
ALTER TABLE t ADD COLUMN `a.size0` UInt64 DEFAULT 7;
INSERT INTO t (a, `a.size0`) VALUES ([3], 7);
SELECT a, `a.size0` FROM t; -- 1, 2 and 7: one query, two meanings of one name
A merge then stored those values, so the wrong result outlived the mixed-parts
state.
The lookup goes through subcolumns because for a wide part the flattened columns
of a `Nested` column are collected back into one column, whose subcolumns they
become. That answer is now accepted only for a column the part really stores
under its own name, or when a subcolumn is what was requested.
Closes: #114588
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [67bdeab] Summary: ❌
AI ReviewSummaryThe PR rejects new column/subcolumn collisions introduced by Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block Fix omitted-parent default evaluation and constraint extraction, and fix or explicitly track the remaining TTL source mismatch before approval. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes31 object files changed (+80.42 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units1160 translation units recompiled, 6291 s compile time in total, 1160 of them have a recent master baseline. |
The `Nested` non-regression cases were far too small to leave `Compact`, so they never reached the `getColumnsDescriptionWithCollectedNested` branch that `IMergeTreeReader::tryGetColumnInPart` has to leave alone - the branch that collects the flattened columns of a `Nested` column back into one column of a `Wide` part and reads them as its subcolumns. Force those two tables to `Wide` with `min_bytes_for_wide_part = 0`, `min_rows_for_wide_part = 0` and `min_level_for_wide_part = 0` - all three, because a part is `Compact` when any one of the three thresholds is not met - and assert the resulting `part_type`, so a future default cannot silently take the coverage away again. Keep the `Compact` shape too, as a third table with an explicit large `min_bytes_for_wide_part`, so both readers stay covered. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Pushed: merged The two reds on the previous commit are unrelated to this change:
|
|
Already picked up: I filed #113292 and the fix is open as #113515 ( That red (STID SELECT count() FROM (SELECT k FROM mt__fuzz_5 RIGHT JOIN (SELECT toUInt64(toLowCardinality(1000000000)) AS k) AS r USING (k) WHERE k = 1048576)For a #113515 is green on |
|
📊 Cloud Performance Report ✅ AI verdict: I downgraded all four flagged improvements to not_sure, because this PR cannot plausibly speed these queries up: ClickBench Q33 (-9.3%), Q34 (-10.6%), and TPC-H Q1 (-20.0%) and Q7 (-24.7%). The PR is a correctness fix: a table column named like another column's subcolumn (for example clickbenchFlagged queries (2 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_officialFlagged queries (2 of 22)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
`05087` was taken by `05087_create_view_inferred_suspicious_type` on master while this branch waited. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Thanks — acknowledged, #113515 already covers the AST fuzzer red (#113292), so nothing to duplicate from here. The analysis matches what the report showed: Pushed in this pass: merged Related: #113515 |
The reader now fills a column the part does not hold from its default, even when the part has a same-named subcolumn of another column (`a.size0` next to an `Array` column `a`). The planning code still asked `IMergeTreeDataPart::tryGetColumn`, which resolves such a name to the generated subcolumn, so `estimateReadBytes`, `getColumnSizesForPrewhere`, `MergeTreeReadPoolBase` task sizing, `MergeTreePrefetchedReadPool` prefetch budgeting and `MergeTreeBlockSizePredictor` costed a read that never happens. Add `IMergeTreeDataPart::tryGetColumnForTable`, which answers a name the table declares as a column in its own right only with a column the part stores under that name, and use it in those callers. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118242&sha=98981483a2d9235747ba3026cb90de6f23e65fb1&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 ( |
|
Fix in progress: #117235. #113009 is only the diagnostic: it restores the This run's own log confirms the mechanism I wrote up in #114819, on Those 9 lines are logged by Nothing here is specific to your change:
|
… of a shadowed column The test pinned `MATERIALIZE COLUMN m` computing `a.size0 + 1` from the offsets of the `Array` column `a` (giving 4 and 2), because the part holds `a.size0` only as a missing-column marker and the reader used to resolve the name to the subcolumn. With this change the column reads its default `0`, so `m` is `1`, which is also what the same test already expects after `CLEAR COLUMN`. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118242&sha=cddd3845285b476d690cbbac3ac528d104b54e6a&name_0=PR&name_1=Fast%20test #118242 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Thanks @groeneai — noted, #117235 is the fix for the Pushed in this pass: merged Related: #117235 |
… on `ALTER` A pair of a column `a.x` and a prefix column `a` was considered only when `a` had the subcolumn `x`, and only the type of that subcolumn had to stay the same. That missed two ways to move a deeper name such as `a.x.y` from one column to the other: - changing the dotted column itself (`MODIFY COLUMN a.x Tuple(y Dynamic)` next to a `JSON` column `a`), which introduces a regular subcolumn that takes precedence over the dynamic path of `a`; - adding a prefix column whose subcolumn is under the dotted name without being the dotted name itself (`ADD COLUMN a Tuple(x.y UInt64)` next to a `JSON` column `a.x`). Now a pair is detected when the prefix column has the subcolumn `x` or any subcolumn under `x.`, all those subcolumns with their types must stay the same, and when the prefix column can resolve names under `x`, the subcolumns of the dotted column must stay the same as well. Also mark `ProjectionDescription::providesColumn` as `[[nodiscard]]` for clang-tidy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…to its prefix column When a prefix column (a `JSON` column `a`, or a `Tuple` column `a` with an element `x.y`) can resolve names under the name of a column `a.x`, dropping or renaming `a.x` while `a` stays moved `a.x` and the names of its subcolumns (`a.x.y`) to `a`, while projections, skip indices and statistics kept values computed from the old column. Such an `ALTER` is now refused; both columns can be dropped in one `ALTER`. Dropping the marker column `a.size0` next to an `Array` column `a` stays allowed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… `Nested` leaf A column `a.x.y` of type `Array` is requested as the subcolumn `x.y` of a `Nested` column `a`. When the part has another kind of column `a` (a `JSON` column), `IMergeTreeReader::tryGetColumnInPart` accepted any stored column with a longer prefix of the requested name as evidence that the leaf is in the part. A stored column `a.x` thus stood in for a missing `a.x.y`, and the path `x.y` of the `JSON` column `a` was read instead of the default of `a.x.y`. Now `getStorageAndSubcolumnNameInPart` also reports the name in the part of the flattened leaf column (taking pending renames into account), and the leaf counts as present only if the part stores that exact column. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… shorter name A part holds a `JSON` column `a` and a column `a.x`, but not `a.x.y`; reading `a.x.y` gives its default rather than the path `x.y` of `a`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…refix` deterministic The query sorted by the part name, so the order of the columns of the single part was unspecified and the test failed under randomized settings. Sort by the column name instead. CI report: #118242 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…as deeper names `resolves_deeper_names` in `checkAlterEligibility` looked only at the subcolumn type and at the listed subcolumn names of the prefix column, so it missed names that the prefix type generates dynamically: a `Map(String, ...)` column `a` resolves `a.key_x.y` as the key `x.y`, although `key_x` is a plain `UInt64`. Dropping or renaming `a.key_x`, or changing its type, then moved `a.key_x.y` between the two columns while projections and the primary key kept the values computed from the old source. Now a name under the subcolumn is probed with `tryGetSubcolumnType`. Add `Map` regressions for the drop/projection and the type change/key paths. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118242&sha=daff0bf4f225016aa3e09e8871714205737124d7&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20ParallelReplicas%2C%20plan-based%2C%20s3%20storage%2C%20parallel%29 ( |
|
Acknowledged. I'm investigating the |
|
This is not caused by this PR. It is #122607, the kept-keys cutoff of the trivial
It only shows when the remote replicas get a share of the marks. Locally the original test never got there (the remote queries were cancelled before reading in 40 of 40 runs), so it passed, which fits your x86 result. With the two inserts scaled to
#126139's own test |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118242&sha=dcf0d5b61acc8949bffc5abfd9dea91679e93863&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%205%2F6%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. In |
|
This is not caused by this PR. It is the disk-space window right after
#131429 sets |
…e's identity A column of the table whose name is also the name of a subcolumn of another column (`a.size0` next to an `Array` column `a`, `a.x` next to a `JSON` column `a`) was still looked up in a part by the raw name in a few places: - `can_serve_lazy_read` in `optimizeUseNormalProjections` accepted a projection that stores only `a` for a lazily read column `a.size0`. It now uses `ProjectionDescription::providesColumn`, as the check of the eagerly read columns does. - `haveMutationsOfDynamicColumns` took a column `a.x` that the part does not hold for the path `x` of a `JSON` column `a`, which forced a rewrite of all columns of the part and disabled the coalescing of row mask updates. - `MutationsInterpreter` took the old type of such a column from the subcolumn before it consulted the recorded missing column. - `isSerializationDefinedSubcolumn` tried every split of the name; when the metadata resolves the name as a subcolumn, only the split through that parent counts now. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…names On `INSERT`, a subcolumn required by the sorting key, the partition key, a skip index, or the sign / version column of the engine was taken with `Block::getSubcolumnByName`, which returns the subcolumn of the first column of the block that offers one. Next to a `JSON` column `a` and a `Tuple` column `a.x`, the subcolumn `a.x.y` was taken from the path `x.y` of `a`, while reads and merges take it from `a.x`. A skip index over `toUInt64(a.x.y)` then held the values of the `JSON` path and dropped granules that match (0 rows with `use_skip_indexes = 1`, 1 row with 0), and an index over `a.x.y` of another type failed the `INSERT` with `BAD_GET`. `ColumnsDescription::getSubcolumnFromBlock` resolves the name as the table does and extracts the subcolumn from that column of the block. The test also covers two cases from the review that had no test in the current tree: a projection computed from the recorded default of a missing column stays usable (`force_optimize_projection`), and a skip index over a subcolumn of a missing column returns the same result with and without the index. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…default-114588 Conflicts in `ReadFromMergeTree::getColumnSizesForPrewhere` and `MergeTreeData::getColumnSizes`: master moved the sizing of the subcolumns of the parts into `MergeTreeData::addColumnSizesOfParts` and `IMergeTreeDataPart::getColumnAndSubcolumnSizes`, which resolve the names with `tryGetColumn`. Both now take the columns of the table and resolve the names with `tryGetColumnForTable`, so that a column of the table which shadows a subcolumn of another column is still not taken for that subcolumn in the parts that do not hold it. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…lso in projections, merges and mutations Next to a `JSON` column `a` and a `Tuple` column `a.x`, the table resolves the subcolumn `a.x.y` as the element `y` of `a.x`. The insert already computed the sorting key and the skip indices from it, but three places still took the first column of the block that offers such a subcolumn, which is the path `x.y` of `a`: - `ProjectionDescription::calculateByQuery` filled the block of a normal projection with `Block::getColumnOrSubcolumnByName`, so the key of the projection was computed from the path, and `addSubcolumnsFromSortingKeyAndSkipIndicesExpression` then found the wrong column already in the block. A query answered by the projection missed rows and read them in the wrong order. - `createSubcolumnsExtractionActions`, used by merges, mutations and `TTL ... GROUP BY` to compute the sorting key and the skip indices, so the primary index and the skip indices written by a merge dropped matching granules. It now takes the columns of the table and tries the column that the table gives the subcolumn first. - `getColumnNameInStorage` in `MergeTask`, which chooses the columns to read as key columns in a merge. Addresses the review: #118242 (comment) Co-Authored-By: Claude Fable 5.2 <[email protected]>
`skip_empty_columns_on_insert` never omits a column with a `DEFAULT` expression, so the columns `a.size0 DEFAULT 7`, `a.x DEFAULT 7` and `a.x DEFAULT tuple(7)` were written by the inserts and the cases did not test a missing column. The columns now have no default expression and each case asserts with `system.parts_columns` that the part does not hold the column. Every mutation (`MODIFY COLUMN`, `UPDATE`, `CLEAR COLUMN`, `MATERIALIZE COLUMN`) starts from a new part without the column; the default expression that `MATERIALIZE COLUMN` requires is added when the part already exists. Addresses the review: #118242 (comment) Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
🕵 Pushed f27f71f and 6c917ed, addressing both open review threads:
The only test failure on the previous head 26d3dd0, |
|
|
||
| for (auto [column_name, subcolumn_name] : Nested::getAllColumnAndSubcolumnPairs(required_column)) | ||
| auto column_and_subcolumn_pairs = Nested::getAllColumnAndSubcolumnPairs(required_column); | ||
|
|
There was a problem hiding this comment.
[Blocker] This exact-name / storage-parent rule still does not reach the DEFAULT / MATERIALIZED insert path.
createSubcolumnsExtractionActions only prefers the table's owner column when table_columns is passed, but AddingDefaultsTransform and InsertDependenciesBuilder still call it without metadata (src/Processors/Transforms/AddingDefaultsTransform.cpp:337, src/Interpreters/InsertDependenciesBuilder.cpp:699, src/Interpreters/InsertDependenciesBuilder.cpp:1876), and AddingDefaultsTransform::getColumnOfSubcolumn keeps the same shortest-prefix scan (src/Processors/Transforms/AddingDefaultsTransform.cpp:145).
With a table like:
CREATE TABLE t
(
a JSON,
`a.x` Tuple(y UInt64),
m UInt64 MATERIALIZED toUInt64(`a.x.y`)
) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t (a, `a.x`) VALUES ('{"x":{"y":11}}', tuple(44));the insert path still extracts a.x.y from a because Nested::getAllColumnAndSubcolumnPairs tries a before a.x, so m is materialized as 11 instead of 44. Since CREATE TABLE with this collision stays allowed, we still persist wrong computed values for a supported table shape.
Please thread table_columns through the default/materialized evaluation path as well, and switch getColumnOfSubcolumn to the table-aware resolution (ColumnsDescription::tryGetColumnOrSubcolumn(...)) instead of the first matching prefix.
There was a problem hiding this comment.
🕵 Confirmed and fixed in 67bdeab. Before the change, d UInt64 DEFAULT toUInt64(a.x.y) and m UInt64 MATERIALIZED toUInt64(a.x.y) stored 11 (the path x.y of the JSON column a) on INSERT ... VALUES, INSERT ... SELECT, INSERT ... FORMAT JSONEachRow and on an insert through a materialized view.
AddingDefaultsTransformand both places inInsertDependenciesBuildernow pass the columns of the table tocreateSubcolumnsExtractionActions.getColumnOfSubcolumnresolves the name withColumnsDescription::tryGetColumnOrSubcolumninstead of the first matching prefix.
New test: 05872_column_shadowing_subcolumn_in_default_on_insert, covering these four insert paths.
There was a problem hiding this comment.
[Blocker] The storage-parent issue remains on 67bdeab83e1d when the insert omits the parent column. src/Interpreters/InsertDependenciesBuilder.cpp:1877 passes the original input block to extraction; at src/Interpreters/createSubcolumnsExtractionActions.cpp:48–50 the preferred parent is absent, so extraction continues through a and supplies the JSON path to the dependent defaults.
Reproduced with this head's Fast test binary:
CREATE TABLE t
(
a JSON,
`a.x` Tuple(y UInt64),
d UInt64 DEFAULT toUInt64(`a.x.y`),
m UInt64 MATERIALIZED toUInt64(`a.x.y`)
) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t (a) VALUES ('{"x":{"y":11}}');
SELECT `a.x.y`, d, m FROM t;
-- 0, 11, 11; expected 0, 0, 0With a.x DEFAULT tuple(toUInt64(44)), the result is 44, 11, 11. The same mismatch reproduces with INSERT ... SELECT and an insert through a materialized view; the JSONEachRow path gives the correct values. Resolve and materialize the omitted parent's default before extracting its subcolumn for dependent expressions, and add an omitted-parent case to 05872.
The test lists the columns of a part from `system.parts_columns` to show that the part does not hold a column. With the randomized MergeTree setting `enable_block_number_column` (or `enable_block_offset_column`), a mutated part also lists `_block_number` (`_block_offset`), and the result differed from the reference in every stateless job. The listing now skips the names that start with an underscore. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118242&sha=6c917ed30002f966bf4b50491578aa20f09bb4cc&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29 #118242 Co-Authored-By: Claude Fable 5.2 <[email protected]>
…` expression from the column that the table gives the name On `INSERT`, `createSubcolumnsExtractionActions` was called without the columns of the table when it prepared the subcolumns for the default expressions (`AddingDefaultsTransform` and both places in `InsertDependenciesBuilder`), so it took the subcolumn from the first inserted column that offers it. Next to a `JSON` column `a` and a `Tuple` column `a.x`, `m UInt64 MATERIALIZED toUInt64(`a.x.y`)` stored the path `x.y` of `a` instead of the element `y` of `a.x`. The columns of the table are now passed in these places, and `getColumnOfSubcolumn` in `AddingDefaultsTransform` resolves the name with `ColumnsDescription::tryGetColumnOrSubcolumn` instead of the first matching prefix. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
| const Block & available_columns, | ||
| const Names & required_columns, | ||
| const ContextPtr & context, | ||
| const ColumnsDescription * table_columns = nullptr); |
There was a problem hiding this comment.
[Blocker] Apply the storage-parent rule to constraint evaluation too.
Leaving this metadata argument optional keeps ConstraintsDescription::getExpressions on the old lookup (src/Storages/ConstraintsDescription.cpp:184 calls this helper without metadata). Consequently a CHECK over a regular subcolumn still checks another column's JSON path and accepts rows that violate the declared constraint.
Reproduced with the Fast test binary for 67bdeab83e1d:
CREATE TABLE t
(
a JSON,
`a.x` Tuple(y Dynamic),
CONSTRAINT c CHECK toUInt64(`a.x.y`) = 44
) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t VALUES ('{"x":{"y":44}}', tuple(11::UInt64));
-- Succeeds: the constraint checks the JSON value 44.
SELECT `a.x.y` FROM t;
-- 11: the stored value violates the constraint.CREATE TABLE with this pair remains supported, so the ALTER rejection cannot protect this path. Have constraint extraction use the same resolved parent as table reads (the source column descriptions are available there), and cover both acceptance of a valid tuple value and rejection of an invalid one.
ALTER TABLE ... ADD COLUMNcould add a column whose name is also the name of a subcolumn of another column, such asa.size0next to anArraycolumna. The parts written before the ALTER hold no such column, and the reader looked its name up among the part's columns and their subcolumns, so it answered with the array's sizes instead of the column's default:A merge then stored those values, so the wrong result outlived the mixed-parts state. Keys, skip indices, projections, statistics and TTL infos of the old parts may also have been computed from the subcolumn, so the name cannot simply change its meaning for them.
Following the review (#118242 (review), option 1), such an
ALTERis now refused:ADD COLUMN,RENAME COLUMN, orMODIFY COLUMNfromALIAS, may not give a stored column the name of a subcolumn of another stored column (a.size0of anArray,t.xof aTuple, anyj.xof aJSONcolumn,s.nullof aNullable).ALIAScolumns andCREATE TABLEstay allowed. This rejects anALTERthat worked before.Parts may still miss such a column: in a table created with it and
skip_empty_columns_on_insert, or in a table where an older version added it. Every place that looks up a column in a part (the reader, size estimates, projection and skip index checks, mutation fast paths) now matches a column of the table only by its exact name, so these parts read the column's default. A projection that selects such a column stores it as a column of its own; before, it computed the other column's subcolumn in its place, and with the column in the projection key an insert failed with a logical error. OnINSERT, in merges and in mutations, a subcolumn required by a key (of the table or of a projection) or a skip index is taken from the column that the table gives the name (a.x.yfrom aTuplecolumna.x, not from the pathx.yof aJSONcolumna); before, the primary index, a skip index or a projection over it was built from the other column and dropped matching granules. In tables where an older version already added such a column, the old parts now read the default, also for keys, TTL and projections computed from the subcolumn; the values read there were wrong anyway.Closes: #114588
Related: #113420
Related: #92453
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
ALTER TABLEcan no longer add a stored column (or rename one) under the name of a subcolumn of another column, such asa.size0next to anArraycolumnaorj.xnext to aJSONcolumnj: the existing parts returned the subcolumn's values instead of the column's default. Use another name or anALIAScolumn. Parts that do not hold such a column (for example, in tables where it was added by an older version) now read its default.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/118242]