Skip to content

Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns - #110584

Merged
Avogar merged 6 commits into
ClickHouse:masterfrom
groeneai:fix-110555-null-subcolumn-metadata-nullable-alter
Jul 29, 2026
Merged

Avogar merged 6 commits into
ClickHouse:masterfrom
groeneai:fix-110555-null-subcolumn-metadata-nullable-alter

Conversation

@groeneai

@groeneai groeneai commented Jul 15, 2026 •

Copy link
Copy Markdown
Collaborator

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 wrong IS NULL / IS NOT NULL / count() results with optimize_functions_to_subcolumns on a column that was made Nullable by a metadata-only ALTER MODIFY COLUMN T to Nullable(T). For parts written before the conversion, the .null subcolumn 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_subcolumns rewrites x IS NULL into a read of the x.null subcolumn. After a metadata-only ALTER MODIFY COLUMN x T -> Nullable(T), old parts still store the 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, producing a null-map of 1 for rows that are actually not-null. 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, 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 for evaluateMissingDefaults, 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 affects isNull, isNotNull and count. 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

  • Backported to: 26.7.2.37, 26.6.2.127, 26.5.7.27

…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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CREATE TABLE t (id UInt8, x UInt256) ENGINE=MergeTree ORDER BY tuple() SETTINGS auto_statistics_types=''; INSERT VALUES (1,42); ALTER MODIFY COLUMN x Nullable(UInt256) SETTINGS mutations_sync=2; then SELECT count() FROM t WHERE x IS NULL SETTINGS optimize_functions_to_subcolumns=1 returns 1 every time (expected 0). SELECT x, x.null returns 42, 1.
b Root cause explained? The query reads only the x.null subcolumn. The old part stores x as non-nullable UInt256, which has no .null substream. injectRequiredColumnsRecursively does not schedule the present parent (its part-type has no .null), and x's default is a bare NULL literal, so the parent is never read. fillMissingColumns / evaluateMissingDefaults then synthesize x.null from the storage type Nullable(UInt256) whose default value is NULL, giving null-map = 1 for the not-null rows.
c Fix matches root cause? Yes. It makes the reader read the physically-present parent and derive the subcolumn from the part-type -> storage-type conversion (all-zero null-map for existing rows), instead of default-filling. No band-aid (no widened bounds, no tags, no data reduction).
d Test intent preserved / new tests added? New stateless test 04161_null_subcolumn_after_metadata_only_nullable_alter covers Compact and Wide parts, with and without DEFAULT NULL, and isNull/isNotNull/count. Existing 03203_fill_missed_subcolumns still passes.
e Both directions demonstrated? Yes. New test FAILS on pristine master (many 1 where 0 expected) and PASSES with the fix; verified by revert+rebuild+run.
f Fix is general across code paths? Yes. Fix is in the shared reader plumbing (injectRequiredColumnsRecursively + fillMissingColumns) used by both Compact and Wide readers, and by the evaluateMissingDefaults extraction path, so all subcolumn read paths for a present-parent/absent-subcolumn part are covered. The fix reads the parent at the source rather than guarding a symptom.
g Fix generalizes across inputs (params/datatypes/wrappers)? Verified for UInt256, String -> Nullable(String), Nullable(UInt32) -> Nullable(UInt256), both with/without DEFAULT NULL, Compact and Wide. Regressions checked: #66243 ADD-COLUMN (whole column absent) still gives null-map 1 correctly (count(y)=5), native Nullable columns unaffected. The condition is generic (subcolumn requested, parent present in part, part-type lacks subcolumn), not type-specific.
h Backward compatible? Yes. Read-path-only change; no setting default, on-disk/wire format, or metadata change; no new validation. Old parts keep their on-disk form.
i Invariants and contracts preserved? Yes. The injected parent is a normal required column read + converted through the existing performRequiredConversions path; evaluateMissingDefaults already extracts subcolumns from full columns (type_in_storage->getSubcolumn(...)). No block-structure, ordering, or concurrency invariant is touched; the subcolumn is left null in fillMissingColumns exactly as the default-expression path already does.

Session id: cron:clickhouse-worker-slot-5:20260715-143300

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ @Avogar — could you review this? It fixes a wrong .null subcolumn read (used by optimize_functions_to_subcolumns for isNull/isNotNull/count) for parts written before a metadata-only ALTER MODIFY COLUMN T -> Nullable(T): the reader now reads the present parent column and derives the subcolumn from the conversion instead of default-filling it. Follows the same missing-subcolumn area as #66243.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jul 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [53cd81b]

Summary: ❌

job_name test_name status info comment
Upgrade check (amd_release) FAIL
Changed settings are not reflected in the settings changes history (see changed_settings.txt) FAIL cidb IGNORED
New settings are not reflected in settings changes history (see new_settings.txt) FAIL cidb IGNORED

AI Review

Summary

This PR fixes missing-subcolumn reads after metadata-only ALTER MODIFY COLUMN ... -> Nullable(...) by reading the physically present parent and deriving the requested subcolumn from the converted value. The current head still has a correctness regression on deep tuple paths, and the new parent-availability check still misses renamed old parts, so this revision should not merge as-is.

Findings

❌ Blockers

  • [src/Interpreters/getColumnFromBlock.cpp:57-60] The current head reintroduces the deep-subcolumn bug that 4e35f82 fixed. For a metadata-only change like Tuple(a UInt8, b String) -> Tuple(a Nullable(UInt8), b UInt64), block_type_has_subcolumn is false for a.null, so this branch casts the whole tuple before extracting a.null. A non-convertible old sibling such as b = 'x' then throws before the readable a branch is reached. Suggested fix: restore the deepest-existing-ancestor cast (or an equivalent branch-local cast) instead of casting the entire parent whenever only a descendant subcolumn is missing.

  • [dismissed by author -- https://github.com/Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns #110584#discussion_r3594314113] [src/Interpreters/inplaceBlockConversions.cpp:459-462] The same name-normalization assumption is still reachable after RENAME COLUMN a TO b followed by a metadata-only MODIFY COLUMN b Nullable(...). available_columns here are built from columns_to_read, which stay in the old part name (a) after alter_conversions, but this check compares the current storage name (b). The defer branch therefore does not fire, and b.null is default-filled to all-1 again instead of being derived from the physically present parent. Suggested fix: normalize the available-parent names to the current storage names before this comparison (or compare against both current and part-era names), and cover the rename path with a focused regression.

Tests
  • ⚠️ Restore the removed deep tuple regression in 04161_null_subcolumn_after_metadata_only_nullable_alter or add an equivalent focused test. The current head deleted the one case that fails once the whole-parent cast is reintroduced.
  • ⚠️ Add a RENAME COLUMN a TO b + metadata-only MODIFY COLUMN b Nullable(...) regression against an old part; the new defer logic has no coverage for that name-mapping path.
Final Verdict

Status: ❌ Block

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 15, 2026
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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pushed 4fce20e fixing a PR-caused server abort the earlier revision introduced.

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 a metadata-only ALTER MODIFY COLUMN T -> Nullable(T) over a pre-ALTER in-RAM block tripped LOGICAL_ERROR in MemorySource::fillPhysicalColumns (this is what caused the Fast test 'Server died', which in turn dropped the Bugfix validation jobs).

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, producing the correct all-0 .null map for the Memory path at source. Dynamic subcolumns keep their existing no-final-cast behavior.
  • fillMissingColumns (inplaceBlockConversions.cpp): only defer the schema-evolved subcolumn when a storage_snapshot is present (the MergeTree reader has a follow-up evaluateMissingDefaults); terminal-fill callers like Memory must not be left with a null column.

Audited both fillMissingColumns callers (MergeTree reader = deferred; Memory = terminal, now correct) and the sole tryGetSubcolumnFromBlock caller (Memory). Added a Memory-engine case to the 04161 regression test. Verified: Memory metadata-alter now returns isNull=0 / x.null=0 / count(x)=1; genuinely-absent ADD COLUMN Nullable still returns isNull=1 (unaffected); MergeTree original repro still correct.

…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]>
Comment thread src/Interpreters/getColumnFromBlock.cpp Resolved
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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Follow-up fix for the two bot review comments, pushed in 68068ed. Updated pre-PR validation gate:

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CR2: Memory Tuple(a UInt8, b String) -> metadata Tuple(a UInt8, b UInt64), SELECT t.a throws CANNOT_PARSE_TEXT on the non-convertible old b='x'. CR1: MergeTree, UPDATE x=0 kept pending (SYSTEM STOP MERGES + mutations_sync=0) then metadata MODIFY COLUMN x Nullable(UInt8); count() WHERE x IS NULL SETTINGS apply_mutations_on_fly=1, optimize_functions_to_subcolumns=1 returns 1 (should be 0).
b Root cause explained? CR2: tryGetSubcolumnFromBlock cast the WHOLE parent to the requested type on any type mismatch, so a non-convertible sibling tuple element threw before the still-readable element was extracted. CR1: fillMissingColumns only deferred a schema-evolved subcolumn when the parent was in the CURRENT step's available_columns; under apply_mutations_on_fly the parent x is produced by an earlier step (previous_header), so the defer branch never fired and x.null was default-filled to all-1.
c Fix matches root cause? Yes. CR2: narrow the whole-parent cast to fire only when the subcolumn is dynamic OR the block's older type lacks the subcolumn; otherwise extract directly and cast only the subcolumn. CR1: thread previous_header column names through IMergeTreeReader::fillMissingColumns into the defer check; evaluateMissingDefaults extracts the subcolumn via tryGetSubcolumnFromBlock (casts the parent to storage type first, unwraps ColumnConst).
d Test intent preserved / new tests added? Yes. Added apply_mutations_on_fly and tuple-element regression cases to 04161. Existing Memory/MergeTree/Wide/String/Variant cases unchanged.
e Both directions demonstrated? Yes. Both CRs FAIL (CR2 throws, CR1 crashed with Bad cast ColumnConst->ColumnNullable) before the fix and PASS after, on Build-ID-verified debug binaries.
f Fix is general across code paths? Yes. The defer decision covers both fillMissingColumns callers (MergeTree reader with follow-up, Memory terminal). evaluateMissingDefaults uses the same tryGetSubcolumnFromBlock helper as the Memory path, so both go through one converted-parent extraction.
g Fix generalizes across inputs? Verified: Nullable(UInt256/UInt8/String), Variant(UInt64,String) (element gained), LowCardinality(Nullable(String)), Array(Nullable(UInt8)) size0, Tuple element (still-present subcolumn), Memory + MergeTree + Wide + apply_mutations_on_fly. Each either takes the converted-parent path or falls through to direct extraction correctly.
h Backward compatible? Yes. No setting default, on-disk/wire format, or validation change. Pure reader-path correctness fix.
i Invariants and contracts preserved? Yes. tryGetSubcolumnFromBlock still returns nullptr when the subcolumn can't be produced (caller treats as missing). The narrowed cast preserves the dynamic-subcolumn contract (no extra cast on dynamic). ColumnConst unwrap only affects the direct-extraction branch, matching what subcolumn serialization assert_casts expect.

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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (bot review round: deep-subcolumn CR3 + CR4)

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CR3: Tuple(a UInt8, b String) -> metadata Tuple(a Nullable(UInt8), b UInt64) on Memory, then SELECT t.a.null threw CANNOT_PARSE_TEXT on sibling b='x' every run.
b Root cause explained? Yes. The subcolumn-absent branch cast the WHOLE parent t to the requested type before extracting; a non-convertible sibling element (b String -> UInt64) fails the cast even though t.a is directly readable.
c Fix matches root cause? Yes. Descend to the deepest ancestor of the requested subcolumn that still resolves in the block's (older) type, cast only that branch, then extract the remaining suffix. For t.a.null only the t.a branch is cast.
d Test intent preserved / new tests added? Yes. Added a deep-tuple case (incl. t.a.null, isNull(t.a), mixed old/new rows) to 04161. No existing assertion weakened.
e Demonstrated both directions? Yes. Deep-tuple case throws on pristine HEAD (68068ed rebuilt), passes with the fix (a869c4e).
f Fix general, not a narrow patch? Yes. Descent handles arbitrary depth (verified t.a.x.null with two non-convertible siblings). Dynamic subcolumns keep the whole-parent cast (unchanged).
g Generalizes across inputs / wrappers? Yes. Matrix re-verified: Nullable(UInt256/UInt8/String), LowCardinality(Nullable), Variant element-gained, tuple-element-only (no nullability), deep + doubly-deep tuple; Memory + MergeTree + Wide + apply_mutations_on_fly.
h Backward compatible? Yes. No format/setting/default change; reader-side extraction only.
i Invariants and contracts preserved? Yes. tryGetSubcolumnFromBlock still returns a column of requested_subcolumn.type; the tuple-element (CR2) and Memory-terminal (storage_snapshot == nullptr) paths are unchanged; falls back to the existing direct-extract path when no strict ancestor differs.

CR4 (Nested n.a.null naming): could not reproduce a wrong result. Element-level nullability is never a metadata-only conversion (canSkipConversionToNullable needs a top-level DataTypeNullable; isMetadataOnlyConversion needs both sides Nullable at the same level), so the part is fully rewritten and the defer branch is never reached. See in-thread reply.

Session id: cron:clickhouse-worker-slot-2:20260716-101300

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 4e35f82

Every 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 CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Integration tests (amd_tsan, 3/6) / test_replicated_database::test_replicated_table_structure_alter flaky (68 master hits + 15+ unrelated PRs in 30d) #110030 (ours, open)

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

@Avogar Avogar self-assigned this Jul 17, 2026
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 Avogar 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.

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

@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.20% 78.30% +0.10%

Changed lines: Changed C/C++ lines covered: 52/54 (96.30%) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 53cd81b

Every 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 CH Inc sync is exempt.

Both reds are the same stale-base artifact and neither is caused by this PR's diff (this PR
touches no settings; grep join_runtime_filter_min_probe_rows over its diff returns nothing).

Check / test Reason Owner / fixing PR
Upgrade check (amd_release) / Changed settings are not reflected in the settings changes history (join_runtime_filter_min_probe_rows, 1000 vs NULL) trunk regression: the introducing PR placed the history entry in the wrong version section. This branch's merge-base is 2026-07-15, so the build predates the correction #111671 (ours, merged 2026-07-24)
Upgrade check (amd_release) / New settings are not reflected in settings changes history (join_runtime_filter_min_probe_rows) same cause, second assertion of the same job #111671 (ours, merged 2026-07-24)
Sync - CH Inc sync (private, not actionable)

Finish Workflow is green; Fast test, Style check, Build (*), Stateless, Integration,
Stress and all four Bugfix-validation legs pass on this head.

I am not pushing a master merge just to clear these two lines: the PR is already approved,
the failure is a pure stale-base artifact that disappears once this lands on a current master,
and every fork push costs a maintainer a manual workflow approval.

Session id: cron:our-pr-ci-monitor:20260727-043000

@ahmadov

ahmadov commented Jul 29, 2026

Copy link
Copy Markdown
Member

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.

@Avogar

Avogar commented Jul 29, 2026

Copy link
Copy Markdown
Member

We can merge it.

@Avogar
Avogar added this pull request to the merge queue Jul 29, 2026
@Avogar Avogar added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jul 29, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

@ahmadov pr-must-backport is already on this PR and covers every active release branch, so no label change is needed once it merges. It cannot produce a 26.4 backport though, because 26.4 is no longer active: its release PR 103297 was closed on 2026-07-24, SECURITY.md marks 26.4 unsupported, and the branch's last commit is from 2026-07-24.

Re-deriving the expansion just now with select_backport_branches from tests/ci/cherry_pick_branches.py against the head refs of the open release-labelled PRs, the current label yields 25.8, 26.3, 26.5, 26.6, 26.7. Switching to v26.4-must-backport would be strictly worse: it means "26.4 and every newer active branch", so it expands to 26.5, 26.6, 26.7 only, dropping 25.8 and 26.3, and it still does not create a 26.4 backport.

On affectedness, I ran this PR's test (04161_null_subcolumn_after_metadata_only_nullable_alter) against the per-branch binaries from builds.clickhouse.com: 25.8, 26.3, 26.4, 26.5, 26.6, 26.7 and master all fail with byte-identical wrong output (md5 47ad3b22dea4cb5a551742e29ec52156 after stripping the launcher banner), and this PR's head 53cd81b passes. So it is one bug across the whole range, and the current label delivers the fix to every branch that is still open. (On 25.8 I dropped auto_statistics_types = '' from the test, since that setting does not exist there.)

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.

Merged via the queue into ClickHouse:master with commit 445b826 Jul 29, 2026
176 of 178 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 29, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 29, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Jul 29, 2026
…le(T) ALTER with optimize_functions_to_subcolumns
clickhouse-gh Bot added a commit that referenced this pull request Jul 29, 2026
Backport #110584 to 26.6: Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns
clickhouse-gh Bot added a commit that referenced this pull request Jul 29, 2026
Backport #110584 to 26.7: Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 8, 2026
alexey-milovidov added a commit that referenced this pull request Aug 9, 2026
Backport #110584 to 26.5: Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
… to Nullable(T) ALTER with optimize_functions_to_subcolumns
middas-0706 pushed a commit to middas-0706/ClickHouse that referenced this pull request Sep 4, 2026
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.
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]>
PedroTadim added a commit that referenced this pull request Sep 10, 2026
…-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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

Wrong IS NULL result after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns

6 participants