Repository navigation
Cherry pick #110584 to 26.3: Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns - #112447
Closed
robot-clickhouse-ci-2 wants to merge 7 commits into
Closed
robot-clickhouse-ci-2 wants to merge 7 commits into
robot-clickhouse-ci-2 wants to merge 7 commits into
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 #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: #110555 Co-Authored-By: Claude Opus 4.8 <[email protected]>
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]>
…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]>
…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]>
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]>
…tadata-nullable-alter Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns
Contributor
Contributor
PedroTadim
pushed a commit
that referenced
this pull request
Sep 10, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #110584
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job