Skip to content

Cherry pick #110584 to 25.8: Fix IS NULL after metadata-only T to Nullable(T) ALTER with optimize_functions_to_subcolumns - #112446

Closed
robot-clickhouse-ci-2 wants to merge 7 commits into
backport/25.8/110584from
cherrypick/25.8/110584
Closed

robot-clickhouse-ci-2 wants to merge 7 commits into
backport/25.8/110584from
cherrypick/25.8/110584

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 7 commits July 15, 2026 15:50
…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
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Jul 29, 2026
@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Dear @Avogar, this cherry-pick PR has not been updated for 3d0h24m23s. Please resolve the conflicts to backport #110584, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Dear @Avogar, this cherry-pick PR has not been updated for 7d0h2m5s. Closing automatically. If you still want to backport #110584, please resolve the conflicts and reopen this PR.

@maxknv
maxknv deleted the cherrypick/25.8/110584 branch November 13, 2026 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants