Repository navigation
Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns - #110584
Conversation
…functions_to_subcolumns optimize_functions_to_subcolumns rewrites isNull(x)/isNotNull(x)/count(x) into reads of the x.null subcolumn. After a metadata-only ALTER MODIFY COLUMN x T -> Nullable(T), parts written before the conversion still store non-nullable data on disk and have no .null substream. The reader treated x.null as a fully-missing column and synthesized it from the storage type Nullable(T), whose default value is NULL, so existing not-null rows were reported as NULL (null-map 1 instead of 0). SELECT x, x.null returned 42, 1 on the same row. This differs from the case handled in ClickHouse#66243, where the whole Nullable column is absent after ADD COLUMN and a null-map of 1 is correct. Here the parent column is physically present, so the null-map for existing rows must be 0, derived from the parent. Fix, in the reader path: - injectRequiredColumnsRecursively: when a requested subcolumn's parent column is present in the part but the part-type lacks that subcolumn, inject the parent full column so it is read and converted to the storage type. - fillMissingColumns: do not default-fill such a subcolumn; leave it for evaluateMissingDefaults, which reads the injected parent, converts it (giving an all-zero null-map for the existing rows) and extracts the subcolumn. Covers Compact and Wide parts, with and without DEFAULT NULL, and isNull / isNotNull / count. Closes: ClickHouse#110555 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-5:20260715-143300 |
|
cc @CurtizJ @Avogar — could you review this? It fixes a wrong |
|
Workflow [PR], commit [53cd81b] Summary: ❌
AI ReviewSummaryThis PR fixes missing-subcolumn reads after metadata-only Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block |
The reader fix leaves a schema-evolved .null subcolumn null for the MergeTree reader's follow-up evaluateMissingDefaults pass. The Memory engine treats fillMissingColumns as the terminal fill and asserts no nulls remain, so the deferred-null column tripped a LOGICAL_ERROR abort on ALTER MODIFY COLUMN T -> Nullable(T) over a pre-ALTER in-RAM block. Two changes: - tryGetSubcolumnFromBlock (getColumnFromBlock.cpp): cast the block column to the requested storage type before extracting the subcolumn on ANY type mismatch, not only dynamic types. This produces the correct .null map (all-0) for the Memory path at source. Dynamic subcolumns keep their no-final-cast behavior. - fillMissingColumns (inplaceBlockConversions.cpp): only defer the schema-evolved subcolumn when a storage_snapshot is present, i.e. the caller has a follow-up evaluateMissingDefaults pass (the MergeTree reader). Callers without it (Memory) must not leave the column null. Adds a Memory-engine case to the 04161 regression test. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pushed 4fce20e fixing a PR-caused server abort the earlier revision introduced. The reader fix leaves a schema-evolved Two changes:
Audited both |
…lumn read The .null/subcolumn reader fix in this PR also corrects the Memory engine's subcolumn read after a metadata-only ALTER MODIFY COLUMN T -> Variant(T). Previously Memory returned y.UInt64 = NULL for pre-alter rows while the full column read y returned the value (the "minor data inconsistency after alter" the test comment documented). Memory now reads y.UInt64 = value, matching the full-column read and the MergeTree engine output byte-for-byte. Reference regenerated from actual output; the stale inconsistency comment removed. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Fixes two corner cases flagged in review of the .null subcolumn fix: 1. getColumnFromBlock (tryGetSubcolumnFromBlock): the whole-parent cast now fires only when the subcolumn is dynamic OR the block's (older) type lacks the requested subcolumn. A subcolumn still present in the old type (e.g. a tuple element after a sibling element's type changed) is extracted directly, so a non-convertible sibling value no longer throws before extraction. Also unwrap a possible ColumnConst before extracting the subcolumn. 2. fillMissingColumns: the schema-evolved-subcolumn defer now also considers parent columns produced by earlier pipeline steps (previous_header), threaded in via IMergeTreeReader::fillMissingColumns. This fixes wrong IS NULL results under apply_mutations_on_fly + optimize_functions_to_subcolumns, where the full parent is materialized by an on-fly UPDATE and the later step reads only x.null. evaluateMissingDefaults now extracts such subcolumns via tryGetSubcolumnFromBlock so the parent is converted to storage type first. Adds apply_mutations_on_fly and tuple-element regression cases to 04161. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Follow-up fix for the two bot review comments, pushed in 68068ed. Updated pre-PR validation gate: Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260716-084200 |
| /// would throw before we ever extract the still-readable subcolumn. Fall through to extract the | ||
| /// subcolumn directly and cast only that. | ||
| bool block_type_has_subcolumn = elem->type->tryGetSubcolumnType(subcolumn_name) != nullptr; | ||
| if (!elem->type->equals(*requested_column_type) && (is_dynamic || !block_type_has_subcolumn)) |
There was a problem hiding this comment.
The new !block_type_has_subcolumn check is still too coarse for deep paths. It treats t.a.null as "old type lacks the subcolumn" and casts the whole parent before extracting anything. A metadata-only ALTER MODIFY COLUMN t Tuple(a Nullable(UInt8), b UInt64) over an old Tuple(a UInt8, b String) block will therefore still throw on b = 'x' for SELECT t.a.null or isNull(t.a), even though a is directly readable and its null-map can be derived without touching b.
This needs to descend to the deepest existing ancestor (t.a here) and cast only that branch, not the whole parent t.
There was a problem hiding this comment.
Fixed in 4e35f82. tryGetSubcolumnFromBlock now descends to the deepest ancestor of the requested subcolumn that still resolves in the blocks (older) type, casts only that branch to its requested type, and extracts the remaining suffix. For t.a.null it casts only the t.a branch, so a non-convertible sibling t.b no longer throws. Verified with old Tuple(a UInt8, b String) -> metadata Tuple(a Nullable(UInt8), b UInt64): SELECT t.a.null and isNull(t.a) return 0 (was CANNOT_PARSE_TEXT on b='x'); a doubly-deep t.a.x.null with two non-convertible siblings also works. Dynamic subcolumns keep the whole-parent cast. Added the deep-tuple case to 04161 (fails on pristine HEAD, passes with the fix).
There was a problem hiding this comment.
Current src/Interpreters/getColumnFromBlock.cpp:57-60 still re-enters the whole-parent cast whenever the old type lacks the full a.null path. For Tuple(a UInt8, b String) -> Tuple(a Nullable(UInt8), b UInt64), block_type_has_subcolumn is false for a.null, so castColumn({source_column, elem->type, \"\"}, requested_column_type) casts the entire tuple and throws on old b = 'x' before a is extracted. That is the same deep-path case from the original review, so the 4e35f82 ancestor-only cast is no longer present on the current head.
…column cast When deriving a subcolumn whose parent's (older) block type lacks it after a metadata-only ALTER, casting the whole parent could throw on a non-convertible sibling for deep paths (e.g. t.a.null when sibling t.b String -> UInt64 holds a non-numeric value), even though t.a is directly readable. Descend to the deepest ancestor of the requested subcolumn that still resolves in the block's type, cast only that branch, then extract the remaining suffix. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (bot review round: deep-subcolumn CR3 + CR4)Pre-PR validation gate (click to expand)
CR4 (Nested Session id: cron:clickhouse-worker-slot-2:20260716-101300 |
CI finish ledger — 4e35f82Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
The earlier PR-caused reds on prior revisions (Fast test "Server died", 02941_variant_type_alters, StorageMemory LOGICAL_ERROR) were fixed in commits 4fce20e / f7c6ead and are green on this head. The AST fuzzer "Not-ready Set" (STID 0250-41a5) is a chronic trunk bug (external fixing PR #109722 / issue #107619) and did not fire on this head. Session id: cron:our-pr-ci-monitor:20260716-163000 |
Derive a requested subcolumn that is absent from a part's (older) type from the physically-present parent instead of the storage-type default: inject the parent column so it is read and converted, defer the subcolumn in `fillMissingColumns` (also when the parent comes from an earlier reader-chain step, e.g. an on-fly `UPDATE`), and extract it in `evaluateMissingDefaults` via `tryGetSubcolumnFromBlock`, which casts the parent to the storage type first. Drops the deep-nested-tuple ancestor-descent path. Covers compact/wide/`String` metadata-only `T` -> `Nullable(T)`, the `Memory` engine, and `apply_mutations_on_fly`. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Avogar
left a comment
There was a problem hiding this comment.
I refactored the implementation, removed unneeded code (especially related to uncastable source column, which is very stragne edge case we should not care about). If review bot finds something, wait for me to confirm if I want to fix it
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 52/54 (96.30%) · Uncovered code |
CI finish ledger — 53cd81bEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task Both reds are the same stale-base artifact and neither is caused by this PR's diff (this PR
I am not pushing a master merge just to clear these two lines: the PR is already approved, Session id: cron:our-pr-ci-monitor:20260727-043000 |
|
Hi @Avogar, is anything need to be done in this PR to merge it? There is a customer escalation affected by the same error: https://github.com/ClickHouse/support-escalation/issues/8321. Once it's merged, we might need to back-port this PR to 26.4 to resolve the customer escalation. |
|
We can merge it. |
|
@ahmadov Re-deriving the expansion just now with On affectedness, I ran this PR's test ( For the escalation: if the customer is on 26.4, the path is an upgrade to 26.5 or newer rather than a 26.4 backport. Tell me if the escalation needs a branch I have not listed. |
445b826
…le(T) ALTER with optimize_functions_to_subcolumns
Backport #110584 to 26.6: Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns
Backport #110584 to 26.7: Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns
Backport #110584 to 26.5: Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns
… to Nullable(T) ALTER with optimize_functions_to_subcolumns
While a RENAME COLUMN mutation is still pending, a part stores the column under its previous name but the table metadata already carries the new one. Reading the `.null` subcolumn of a column that also went through an earlier metadata-only `T` -> `Nullable(T)` ALTER then returned 1 (NULL) for every row that holds real data, so `IS NULL`, `IS NOT NULL`, `count()` and an explicit `x.null` read all reported live rows as NULL. Neither a failpoint nor SYSTEM STOP MERGES is needed to reach it: on a 200000-row table the rename mutation is simply still running, and `WHERE x IS NULL` returned 200000 where the answer is 0. It also does not depend on optimize_functions_to_subcolumns, because a plain `SELECT x.null` is wrong with that setting never consulted. isSubcolumnOfAvailableColumn decides whether such a subcolumn should be deferred to evaluateMissingDefaults and derived from its parent, or filled from the storage type's default. It resolved the parent name through the storage snapshot, so that name is in the current-metadata namespace, but compared it against `available_columns`, which IMergeTreeReader:: fillMissingColumns builds from `columns_to_read`. Those names went through getStorageAndSubcolumnNameInPart, which maps a pending rename back to the old name, so under a pending rename the list holds `y` and `y.null` while the lookup asks for `x`. The lookup missed and the subcolumn was filled with the default null-map of 1, while the parent column that ClickHouse#110584 injects for exactly this purpose was read, converted and then ignored. Compare against `requested_columns` instead. It is already a parameter of fillMissingColumns, already index-aligned with `res_columns`, and already in the metadata namespace, and it holds the same set of entries as `available_columns` because `columns_to_read` is built one-to-one from the requested columns. The part-namespace term is dropped rather than kept as an extra alternative: the metadata namespace is the one the consumer of this decision uses, since evaluateMissingDefaults builds its block from `original_requested_columns` and looks the parent up by its metadata name, so a parent that is available only under a part name is not findable there. An additive OR was built and measured, and it does fix the defect, but it would leave in place a comparison that answers a question no consumer asks. `available_columns` itself is deliberately untouched and still in the part namespace, because collectOffsetsColumns consumes the same list to compute stream file names and legitimately needs the part's names and types. An instrumented build was used to confirm the two terms disagree exactly in the failing shape and agree in the shape ClickHouse#110584 fixed, that the shared Nested offsets path returns before either term is consulted, and that the merge path reaches the predicate with both terms agreeing. The regression test extends the existing 04161 test with a Compact and a Wide pending-rename section rather than adding a second file. min_bytes_for_wide_part is pinned at table level in the compact section because the test runner randomizes it to 0 in roughly a third of runs.
Port the merged master fix "Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns" (#110584) onto 26.3, which is the only supported branch still missing it. On 26.3, a part written before a metadata-only ALTER TABLE t MODIFY COLUMN x T -> Nullable(T) has no .null substream. With optimize_functions_to_subcolumns = 1 (on by default) IS NULL, IS NOT NULL and count(x) are rewritten to read x.null, and that subcolumn is default-filled from the storage type instead of being derived from the present parent column, so rows that hold real data are reported as NULL. Measured on official 26.3.25.1: SELECT count() FROM t WHERE id = 1 AND x IS NULL returns 1 where the answer is 0, and SELECT x, x.null returns "42 1" instead of "42 0". The robot cherry-pick #112447 could not be applied and was closed by a timer rather than by a decision. All three conflicts have one cause: fillMissingColumns gained a master-only bool share_nested_offsets parameter after the 26.3 cut, and this fix appends a further parameter after it, so both sides edit the same parameter list. share_nested has zero occurrences in src/ on 26.3, so that parameter is dropped and additional_available_columns is kept. Nothing else is adapted: the guard body, the evaluateMissingDefaults rewrite, both MergeTree call sites and all four test files land byte-identical to master. MergeTreeLazilyReader does not exist on 26.3, and MergeTreeReadersChain runs fillMissingColumns before performRequiredConversions exactly as master does, so the fix is exercised in the order it was designed against. Related: #110584 Related: #112447 Co-Authored-By: Claude Opus 5 <[email protected]>
…-pick-110584 Resolve the 26.3 cherry-pick of #110584: Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong
IS NULL/IS NOT NULL/count()results withoptimize_functions_to_subcolumnson a column that was madeNullableby a metadata-onlyALTER MODIFY COLUMN TtoNullable(T). For parts written before the conversion, the.nullsubcolumn was filled from the storage-type default (NULL) instead of being derived from the physically-present parent column, so existing not-null rows were wrongly reported as NULL.Closes: #110555
Description
optimize_functions_to_subcolumnsrewritesx IS NULLinto a read of thex.nullsubcolumn. After a metadata-onlyALTER MODIFY COLUMN x T -> Nullable(T), old parts still store the non-nullable data on disk and have no.nullsubstream. The reader treatedx.nullas a fully-missing column and synthesized it from the storage typeNullable(T), whose default value is NULL, producing a null-map of 1 for rows that are actually not-null.SELECT x, x.nullreturned42, 1on the same row.This differs from the case handled in #66243 (where the whole
Nullablecolumn is absent afterADD COLUMN, so a null-map of 1 is correct): here the parent column is physically present, so the null-map for existing rows must be 0, derived from the parent.Fix (reader path), in two coordinated places:
injectRequiredColumnsRecursively: when a requested subcolumn's parent column is present in the part but the part-type lacks that subcolumn, inject the parent full column so it is read and converted to the storage type.fillMissingColumns: do not default-fill such a subcolumn; leave it forevaluateMissingDefaults, which reads the injected parent, converts it (producing an all-zero null-map for the existing rows), and extracts the subcolumn.Covers Compact and Wide parts, with and without
DEFAULT NULL, and affectsisNull,isNotNullandcount. Reproduction requires an old non-nullable part retained after the conversion (e.g.auto_statistics_types = '', per the issue). A new stateless regression test is added; it fails on master and passes with the fix.Version info
26.7.2.37,26.6.2.127,26.5.7.27