Skip to content

Return NULL from accurateCastOrNull for a number that is not a value of the Enum - #132256

Open
lazerg wants to merge 9 commits into
ClickHouse:masterfrom
lazerg:fix/issue-132250-accurate-cast-number-to-enum-or-null
Open

lazerg wants to merge 9 commits into
ClickHouse:masterfrom
lazerg:fix/issue-132250-accurate-cast-number-to-enum-or-null

Conversation

@lazerg

@lazerg lazerg commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #132250
Closes: #133242
Related: #119183

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

Fixed accurateCastOrNull and accurateCastOrDefault of a number to an Enum (also inside a Tuple) throwing UNKNOWN_ELEMENT_OF_ENUM or CANNOT_CONVERT_TYPE instead of returning NULL or the default value when the number is not a value of the Enum or does not fit its type. accurateCast of a fractional number to an Enum now throws instead of truncating it, and a number that does not fit an Enum set key in IN is no longer a member.

Description

createNumberToEnumWrapper ignored the cast type and threw for every value it could not convert. Under accurateCastOrNull it now marks such rows as NULL in a Nullable result, the same way numeric conversions do, so the scalar, Tuple and accurateCastOrDefault paths all pick it up. CAST and accurateCast still throw. accurateCast now also rejects a fractional number, like other accurate conversions. Internal accurateOrNull casts, such as IN set keys, no longer take the old toEnum path, which wrapped 257 to 1. With check_conversion_from_numbers_to_enum = 0, accurateCastOrNull skips the Enum value check, like CAST does, but still returns NULL for a number that does not fit the type or is fractional. A String that is not an element is left to #119183. The new test fails on master (26.11.1.2511); I didn't build the patched server locally.


Workflow [PR]
Sync PR [sync-upstream/pr/132256]

@clickhouse-gh clickhouse-gh Bot added comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization labels Nov 17, 2026
@tuanpach tuanpach added the can be tested Allows running workflows for external contributors label Nov 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f615b35]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug, targeted) FAIL
Logical error: '(isConst() || isSparse() || isReplicated() || rhs.isConst() || rhs.isSparse() || rhs.isReplicated()) ? getDataType() == rhs.getDataType() : typeid(*this) == typeid(rhs)' (STID: 2508-361e) FAIL cidb
AST fuzzer (amd_debug, targeted, old_compatibility) FAIL
Logical error: Unexpected return type from A. Expected B. Got C (STID: 3344-4ec5) FAIL cidb

AI Review

Summary

The PR makes numeric conversions to Enum8 and Enum16 honor the failure behavior of accurateCastOrNull and accurateCastOrDefault, rejects fractional and non-finite inputs in accurate conversions, and preserves numeric accuracy in internal casts independently of enum-membership validation. Reviewed head f615b35f4e46ad99d826a8a2f8760afc3877ec2b, the complete diff, surrounding code, linked issues, and all prior discussion. Earlier findings are addressed; no new actionable findings remain. The head's Fast test report confirms 05819_accurate_cast_number_to_enum_or_null passes.

Consumers checked for the newly nullable conversion result:

  • prepareRemoveNullable merges conversion failures with source null maps; createTupleWrapper extracts element failures into the tuple null map.
  • Array and map wrappers request strict conversion for non-nullable elements; explicitly nullable elements retain their null maps. LowCardinality sources are materialized before numeric-to-enum conversion.
  • Variant and Dynamic conversion wrappers unify converted-column nullability and assemble results using the converted column type.
  • accurateCastOrDefault consumes the failure map and substitutes the requested default.
  • Set, analyzer set construction, KeyCondition, key-value filter extraction, and constant-expression analysis inspect conversion null maps before using converted values. JSON extraction explicitly handles nullable results; dictionary constant comparisons reject failed conversions.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 20, 2026
Comment thread src/Functions/FunctionsConversion.cpp Resolved
Comment thread src/Functions/FunctionsConversion.cpp Resolved
Comment thread src/Functions/FunctionsConversion.cpp Resolved
Comment thread src/Functions/FunctionsConversion.cpp Resolved
@PedroTadim

Copy link
Copy Markdown
Member

@lazerg check the CI failures

@lazerg

lazerg commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

The Fast test and arm_tidy failures were mine: a compiler error about an uninitialized variable in the enum cast. I fixed it in 117cc21.

clickhouse-gh[bot]
clickhouse-gh Bot previously approved these changes Nov 21, 2026
Comment thread src/Functions/FunctionsConversion.cpp Resolved
@clickhouse-gh
clickhouse-gh Bot dismissed their stale review November 21, 2026 05:49

AI review of a later commit does not approve the PR, see the AI Review comment.

@PedroTadim

Copy link
Copy Markdown
Member

@lazerg merge with master one more time, the AST fuzzer failures may be unrelated

@lazerg

lazerg commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Merged master in f615b35. The AST fuzzer query fails the same way on master 26.12.1.217, and so does accurateCastOrNull(tuple([toInt16(1)]), 'Nullable(Tuple(Array(Int8)))'), so it does not come from this PR.

@PedroTadim

Copy link
Copy Markdown
Member

#132543 has to be merged first

@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f615b35f4 with master d0aed4d39 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

3 object files changed (+53.19 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Functions/FunctionsConversion.cpp.o 25.07 MiB 25.12 MiB +53.19 KiB (+0.21%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

40 translation units recompiled, 214 s compile time in total, 40 of them have a recent master baseline.

Job report

This branch has not been deployed

No deployments
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 comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

3 participants