Skip to content

Fix toFloat64/toUInt32/toString/etc. on Dynamic ignoring cast_keep_nullable - #105467

Merged
Avogar merged 10 commits into
masterfrom
fix-cast-keep-nullable-dynamic
Jun 8, 2026
Merged

Avogar merged 10 commits into
masterfrom
fix-cast-keep-nullable-dynamic

Conversation

@Avogar

@Avogar Avogar commented May 20, 2026 •

Copy link
Copy Markdown
Member

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 toFloat64/toUInt32/toString/etc. on Dynamic ignoring cast_keep_nullable. Closes #104789

Version info

  • Merged into: 26.6.1.502

…keep_nullable`

When `cast_keep_nullable=1`, conversion functions like `toFloat64` on
Dynamic columns containing NULLs returned non-nullable results (NULLs
silently converted to 0), while `CAST` correctly returned `Nullable`
with NULLs preserved.

The root cause was two-fold:

1. `FunctionConvert::getReturnTypeImpl` used `getNullPresense` which
   only checks `isNullable()`, missing Dynamic (and Variant) types that
   can also contain NULLs. Fixed by adding a `canContainNull` check
   when `cast_keep_nullable` is enabled.

2. `FunctionConvert::executeImpl` had a nullable handling block that
   extracted null maps only from `Nullable` arguments, not from Dynamic.
   Also, `toString` was excluded from this block entirely. Fixed by
   also extracting null maps from Dynamic arguments via a new
   `ColumnVariant::createNullMap` method, and removing the String
   exclusion to handle all types uniformly.

Closes #104789

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

clickhouse-gh Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [447d0c4]

Summary: ✅


AI Review

Summary

This PR fixes cast_keep_nullable handling for conversion functions over Dynamic and extends the same machinery to Variant. The main Dynamic regression from the linked issue is covered, but the final Variant path now changes existing toString output while cast_keep_nullable is disabled, so this needs changes before merge.

Findings

❌ Blockers

  • [src/Functions/FunctionsConversion.h:3367] cast_keep_nullable = 0 is the compatibility mode, but the new unconditional Variant branch routes toString(Variant(...)) through ConvertImplFromVariantToColumn even when the setting is off. That helper substitutes defaults for NULL_DISCRIMINATOR, and the updated 03780_function_variant_adaptor.reference shows the user-visible regression: previous ᴺᵁᴸᴸ output becomes an empty string. Preserve the old FunctionBaseVariantAdaptor behavior when cast_keep_nullable is disabled, and only use the new null-preserving path when cast_keep_nullable = 1.
Final Verdict

Status: ❌ Block

Minimum required action: restore default-off toString behavior for Variant NULL rows and update the affected stateless references so the compatibility contract is enforced.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 20, 2026
Comment thread src/Functions/FunctionsConversion.h Resolved
@m-selmi m-selmi self-assigned this May 26, 2026

@m-selmi m-selmi left a comment

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.

LGTM besides the Variant point made by the AI review.

…r Dynamic/Variant

The `toFloat64`/`toUInt32`/`toString`/etc. functions and `CAST` now handle
NULL values in Variant columns the same way they handle NULLs in Nullable
and Dynamic columns:

- `FunctionConvert` (`toFloat64`, `toString`, etc.) always wraps the result
  in Nullable when the source is Dynamic or Variant, matching how it already
  handles Nullable sources via `getNullPresense`.

- `FunctionCast` (`CAST`/`::` syntax) wraps the result in Nullable when
  `cast_keep_nullable = 1`, using `canContainNull` instead of manually
  enumerating type checks (which missed Variant).

- `createVariantToColumnWrapper` now throws on NULL when the result type
  cannot contain NULL, matching `createDynamicToColumnWrapper` behavior.

- Added `ConvertImplFromVariantToColumn` with optimized paths for:
  all-NULLs, single variant without NULLs, single variant with NULLs,
  and general multi-variant case.

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

Avogar commented May 27, 2026

Copy link
Copy Markdown
Member Author

I added additional fixes for Variant type as well

@Avogar
Avogar requested a review from m-selmi May 27, 2026 13:50
Comment thread src/Functions/FunctionsConversion.h Resolved
Avogar and others added 3 commits May 27, 2026 14:03
In `FunctionConvert::executeImpl`, the Dynamic and Variant branches
assumed `arg.column` is a concrete `ColumnDynamic`/`ColumnVariant`,
but in mixed-const calls (e.g. `toUnixTimestamp(CAST(NULL AS Dynamic),
materialize('UTC'))`) it can be `ColumnConst` wrapping them.

Mirror the Nullable handling: check `isColumnConst` first, return
all-default if `onlyNull`, otherwise skip (const non-NULL contributes
no NULLs to the null map).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Replace `std::vector` with `VectorWithMemoryTracking` to satisfy the
style check.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Comment thread src/Functions/FunctionsConversion.cpp Resolved
Avogar and others added 3 commits June 4, 2026 14:57
…:ClickHouse/ClickHouse into fix-cast-keep-nullable-dynamic
…e/ClickHouse into fix-cast-keep-nullable-dynamic
The previous commits unconditionally treated Dynamic/Variant as Nullable
in `toXxx` functions and threw on NULL in CAST wrappers, breaking
backward compatibility when `cast_keep_nullable = 0` (the default).

Now all nullable-preserving behavior is gated by the setting:
- `getReturnTypeImpl` / `executeImpl`: only wrap the result in Nullable
  for Dynamic/Variant input when `cast_keep_nullable` is enabled.
- `executeInternal` / CAST wrappers: use `shouldThrowOnNull` which only
  throws when `cast_keep_nullable` is on AND the result type is neither
  nullable-like nor wrappable in Nullable (e.g. Array).
- Restore the `toString` exception for the nullable branch: `toString`
  handles regular Nullable arguments itself (via
  `ConvertImplGenericToString`), but for Dynamic/Variant we still enter
  the nullable branch to extract their null map.

Revert `03780_function_variant_adaptor` reference to master (test uses
default `cast_keep_nullable = 0`). Update `04260` / `04268` tests to
expect default values for NULLs when the setting is off.

Related: #105467

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Comment thread src/Functions/FunctionsConversion.cpp Resolved
Avogar and others added 2 commits June 4, 2026 20:06
In `ConvertImplFromVariantToColumn::execute`, the general multi-variant
path passes secondary arguments (e.g. timezone for `toDateTime`) to
`nested_convert` without filtering them to match the compact variant
subcolumn. This mirrors the existing filtering in the single-variant-
with-NULLs optimization path.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
ConvertImplFromDynamicToColumn::shouldThrowOnNull(settings.cast_keep_nullable, result_type));
}

if (isVariant(from_type))

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.

cast_keep_nullable = 0 is the compatibility mode, but this unconditional Variant path now changes existing toString results even when the setting is off. The updated 03780_function_variant_adaptor.reference shows the regression: rows that used to print ᴺᵁᴸᴸ now print an empty string because ConvertImplFromVariantToColumn substitutes defaults for NULL_DISCRIMINATOR. Please keep the old FunctionBaseVariantAdaptor behavior when cast_keep_nullable is disabled, and only apply the new null-map/default-substitution path when cast_keep_nullable = 1.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's intended. The old behaviour was incorrect. Now Variant is consistent with Dynamic

@clickhouse-gh

clickhouse-gh Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.40% +0.00%
Functions 92.40% 92.40% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 136/188 (72.34%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@Avogar
Avogar added this pull request to the merge queue Jun 8, 2026
Merged via the queue into master with commit ad9adeb Jun 8, 2026
166 checks passed
@Avogar
Avogar deleted the fix-cast-keep-nullable-dynamic branch June 8, 2026 13:39
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

toFloat64/toUInt32/etc. on Dynamic ignores cast_keep_nullable, converting NULL to 0 instead of returning NULL

4 participants