Repository navigation
Fix crash in ifNull with Variant argument used in GROUP BY - #96790
Merged
Merged
Conversation
`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]>
Contributor
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
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]>
…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]>
This was referenced Feb 15, 2026
Closed
Closed
Closed
Closed
This was referenced Apr 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).
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.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix crash in
ifNullwithVariantargument used inGROUP BY.Documentation entry for user-facing changes
ifNull'sgetReturnTypeImplreturned the first argument type as-is when it was notNullable. ButVarianttypes can contain NULLs viaNULL_DISCRIMINATORdespite not beingNullable, soexecuteImplstill constructed an innerif(isNotNull, assumeNotNull, alt)call. Theiffunction'sexecuteGenericindependently computed acommon_typethat included all variant types plus the alternative type, producing aColumnVariantwith more variants than the declared return type. DuringGROUP 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 inColumnVariant::localDiscriminatorByGlobal.The fix detects
Variantfirst arguments ingetReturnTypeImpland usesgetLeastSupertypeOrVariantto compute the correct return type that includes both theVariant's types and the alternative type.Closes #96664
🤖 Generated with Claude Code
Version info
26.2.1.741