Skip to content

Fix crash in ifNull with Variant argument used in GROUP BY - #96790

Merged
alexey-milovidov merged 7 commits into
masterfrom
fix-ifnull-variant-crash
Feb 15, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
fix-ifnull-variant-crash

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 12, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix crash in ifNull with Variant argument used in GROUP BY.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

ifNull's getReturnTypeImpl returned the first argument type as-is when it was not Nullable. But Variant types can contain NULLs via NULL_DISCRIMINATOR despite not being Nullable, so executeImpl still constructed an inner if(isNotNull, assumeNotNull, alt) call. The if function's executeGeneric independently computed a common_type that included all variant types plus the alternative type, producing a ColumnVariant with more variants than the declared return type. During GROUP BY, keys were serialized with global discriminators from this wider column, but deserialization created a narrower column from the declared type, causing an out-of-bounds access in ColumnVariant::localDiscriminatorByGlobal.

The fix detects Variant first arguments in getReturnTypeImpl and uses getLeastSupertypeOrVariant to compute the correct return type that includes both the Variant's types and the alternative type.

Closes #96664

🤖 Generated with Claude Code

Version info

  • Merged into: 26.2.1.741

`ifNull`'s `getReturnTypeImpl` returned the first argument type as-is
when it was not `Nullable`. But `Variant` types can contain NULLs via
`NULL_DISCRIMINATOR` despite not being `Nullable`, so `executeImpl`
still constructed an inner `if(isNotNull, assumeNotNull, alt)` call.
The `if` function's `executeGeneric` independently computed a
`common_type` that included all variant types plus the alternative
type, producing a `ColumnVariant` with more variants than the declared
return type. During GROUP BY, keys were serialized with global
discriminators from this wider column, but deserialization created a
narrower column from the declared type, causing an out-of-bounds
access in `ColumnVariant::localDiscriminatorByGlobal`.

The fix detects `Variant` first arguments in `getReturnTypeImpl` and
uses `getLeastSupertypeOrVariant` to compute the correct return type
that includes both the `Variant`'s types and the alternative type.

Closes #96664

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8c2ac53]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Feb 12, 2026
@alexey-milovidov
alexey-milovidov marked this pull request as draft February 12, 2026 21:54
Use `canContainNull` instead of `isNullable` in `ifNull`, and
`getLeastSupertypeOrVariant` instead of `getLeastSupertype` in both
`ifNull` and `coalesce`, so that Variant and Dynamic types that can
contain NULLs are handled correctly.

Also fix `getMonotonicityForRange` in `ifNull`, `coalesce`, and
`assumeNotNull` to use `canContainNull` instead of the manual
`isNullable() || isLowCardinalityNullable()` pattern.

Add comprehensive tests for `ifNull` and `coalesce` with Variant
types and incompatible type arguments.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@alexey-milovidov
alexey-milovidov marked this pull request as ready for review February 12, 2026 21:56
Only use `getLeastSupertypeOrVariant` when `use_variant_as_common_type`
is enabled or when one of the arguments is already a Variant type.
Otherwise fall back to `getLeastSupertype` which throws on incompatible
types, preserving the previous behavior.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@antaljanosbenjamin antaljanosbenjamin self-assigned this Feb 13, 2026
alexey-milovidov and others added 4 commits February 13, 2026 20:17
…roducing queries

Queries using `if()` with incompatible types need explicit
`use_variant_as_common_type = 1` to reliably produce Variant,
otherwise settings randomization in the test runner could disable it.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
The test uses `use_variant_as_common_type` with `if()` which only
produces Variant types with the analyzer. Without this setting,
the old analyzer returns `UInt8` instead of `Variant(...)`.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@alexey-milovidov
alexey-milovidov merged commit 6a7e26a into master Feb 15, 2026
130 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-ifnull-variant-crash branch February 15, 2026 14:51
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 15, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Feb 15, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Feb 25, 2026
@rschu1ze rschu1ze added the post-approved Approved, but after the PR is merged. label Mar 8, 2026
il9ue pushed a commit to il9ue/ClickHouse that referenced this pull request Jun 8, 2026
…OLUMN

Address review on ClickHouse#102156.

The previous fix built the intermediate cast target by string-concatenating
"Nullable(" + type->getName() + ")". For a LowCardinality(T) target this
produced Nullable(LowCardinality(T)), which is invalid (LowCardinality cannot
be nested inside Nullable), so _CAST threw before ifNull could replace NULLs.

Use makeNullableOrLowCardinalityNullable(required_column.type)->getName()
instead, which yields LowCardinality(Nullable(T)) for LowCardinality targets
and Nullable(T) otherwise, and is a no-op for already-nullable targets. The
constructed expression is now ifNull(_CAST(col, 'Nullable(T)'),
_CAST(default, 'T')), documented inline in place of the prose comment.

Extend the stateless test with a Nullable(UInt8) -> LowCardinality(String)
case, and set use_variant_as_common_type = 0 so it reproduces the
NO_COMMON_TYPE failure on master (otherwise Variant resolution via ClickHouse#96790
masks the bug and bugfix-validation passes without the fix).
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…OLUMN

Address review on ClickHouse#102156.

The previous fix built the intermediate cast target by string-concatenating
"Nullable(" + type->getName() + ")". For a LowCardinality(T) target this
produced Nullable(LowCardinality(T)), which is invalid (LowCardinality cannot
be nested inside Nullable), so _CAST threw before ifNull could replace NULLs.

Use makeNullableOrLowCardinalityNullable(required_column.type)->getName()
instead, which yields LowCardinality(Nullable(T)) for LowCardinality targets
and Nullable(T) otherwise, and is a no-op for already-nullable targets. The
constructed expression is now ifNull(_CAST(col, 'Nullable(T)'),
_CAST(default, 'T')), documented inline in place of the prose comment.

Extend the stateless test with a Nullable(UInt8) -> LowCardinality(String)
case, and set use_variant_as_common_type = 0 so it reproduces the
NO_COMMON_TYPE failure on master (otherwise Variant resolution via ClickHouse#96790
masks the bug and bugfix-validation passes without the fix).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

post-approved Approved, but after the PR is merged. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix 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.

Received signal 6 (STID: 4384-4ece)

5 participants