Repository navigation
Add allow_lossy_numeric_supertype setting for numeric Variant common types - #107236
Conversation
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-21:20260611-140600 |
|
cc @Avogar @CurtizJ — could you review this? Per your direction on #106707, it adds the default-off |
|
Workflow [PR], commit [45567ae] Summary: ❌
AI ReviewSummaryThis PR adds the opt-in Findings
Final Verdict
|
368e6ab to
8643161
Compare
8643161 to
bf4ef9b
Compare
|
Addressed all four review comments in bf4ef9b (replied in-thread to each):
Verified both directions locally on a Build-ID-matched debug binary; the test passes plain and 30/30 with full settings randomization. Regression: 00735_long_conditional, 02944_variant_as_common_type_analyzer, 00435_coalesce, 00433_ifnull, 02513_validate_data_types unchanged (fix is gated behind the default-off setting). |
| { | ||
| tmp.emplace_back(getLeastSupertypeOrVariant(keys)); | ||
| tmp.emplace_back(getLeastSupertypeOrVariant(values)); | ||
| tmp.emplace_back(getLeastSupertypeOrVariant(keys, allow_lossy_numeric_supertype)); |
There was a problem hiding this comment.
Applying the lossy fallback to keys can turn a previously valid Map(Variant(...), ...) into an exception. For example, with use_variant_as_common_type = 1, map(materialize(toNullable(toDecimal64(1, 2))), 1, 0., 2) used to infer the key type through the old getLeastSupertypeOrVariant path: Nullable(Decimal) + Float64 has no lossless common type, so the Variant builder strips the nullable wrapper and produces Variant(Decimal(18, 2), Float64), which DataTypeMap::assertKeyType accepts. With this line, tryGetLossyNumericSupertype preserves nullability and returns Nullable(Float64), and DataTypeMap::assertKeyType rejects Map keys of that type. Please keep the old Variant path for keys when the lossy key type would be nullable (or restrict the setting to map values), and add a regression for nullable numeric keys.
There was a problem hiding this comment.
Good catch, fixed in 57061b4. A Map key cannot be Nullable, and the lossy fallback preserves nullability (returns Nullable(Float64)), so applying it to keys regressed valid map(...) constructions. Now, when the lossy key type would be an invalid Map key (!DataTypeMap::isValidKeyType), the key falls back to the plain Variant path (which drops the Nullable wrapper). Map values are unaffected, and non-nullable numeric keys still resolve to the lossy Float64 supertype.
Verified on a debug build (use_variant_as_common_type=1):
allow_lossy_numeric_supertype=1,map(materialize(toNullable(toDecimal64(1, 2))), 1, 0., 2): wasBAD_ARGUMENTS(Map cannot have a key of typeNullable(Float64)), nowMap(Variant(Decimal(18, 2), Float64), UInt8)and the map builds.allow_lossy_numeric_supertype=1, non-nullable keymap(toDecimal64(1, 2), 'x', 0., 'y'): stillMap(Float64, String).allow_lossy_numeric_supertype=0: unchanged.
Added regressions to 04327 (nullable numeric key under both setting values, plus the non-nullable lossy key case). Passes 30/30 with full randomization.
There was a problem hiding this comment.
This is still present in the current tree. src/Functions/map.cpp:106-114 now goes straight through getLeastSupertypeOrVariant(keys, allow_lossy_numeric_supertype) again, so a nullable numeric key still becomes Nullable(Float64) and DataTypeMap rejects it. The new expectation at tests/queries/0_stateless/04327_allow_lossy_numeric_supertype.sql:39-40 now hard-codes that original failure mode (BAD_ARGUMENTS) instead of preserving the previously valid Map(Variant(...), UInt8) key type.
There was a problem hiding this comment.
This is intentional and was settled in review. On 2026-06-24 (comment 3467725468) @ Avogar directed: "how supertype here can become Nullable? Even if it can, we should just throw if it becomes Nullable." The carve-out that kept the plain Variant key was dropped per that direction, so an opted-in nullable numeric Map key now throws BAD_ARGUMENTS. That is the requested behavior, not a regression.
The setting is default-off, so nothing changes for anyone who does not opt in (verified locally, Build ID f20bfd2):
- allow_lossy_numeric_supertype=0 (default), use_variant_as_common_type=1:
map(materialize(toNullable(toDecimal64(1,2))), 1, 0., 2)-> Map(Variant(Decimal(18, 2), Float64), UInt8), exactly as before. - allow_lossy_numeric_supertype=1: throws BAD_ARGUMENTS (Avogar's "just throw" outcome).
- Non-nullable numeric key, setting on: Map(Float64, String) (lossy applies where it can).
The 04327:39-40 expectation encodes precisely this settled outcome.
A Map key cannot be Nullable, but the opt-in lossy numeric fallback (allow_lossy_numeric_supertype) preserves nullability and can return Nullable(Float64). For map(...) keys that made a previously valid construction throw: with use_variant_as_common_type=1, map(materialize(toNullable(toDecimal64(1, 2))), 1, 0., 2) used to infer the key type via the Variant path, which strips the Nullable wrapper and produces an accepted Variant(Decimal, Float64) key; with the lossy fallback it became Nullable(Float64), which DataTypeMap::assertKeyType rejects. Keep the plain Variant key (dropping the Nullable wrapper) when the lossy key type would be an invalid Map key. Map values are unaffected, and non-nullable numeric keys still resolve to the lossy Float64 supertype. Addresses clickhouse-gh[bot] review on src/Functions/map.cpp in PR ClickHouse#107236. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (commit 57061b4, nullable Map-key fix)
Session id: cron:clickhouse-worker-slot-4:20260611-174300 |
|
Follow-up: the
Session: cron:clickhouse-worker-slot-0:20260611-225500 |
min/max recurse into the argument type with forEachChild and reject any nested Variant. For Array(Variant(...)) and Map(..., Variant(...)) the Variant is reached as a child, but the error built the allow_lossy_numeric_supertype hint from this->result_type (the outer Array/Map), which is not itself a Variant, so getNumericVariantSupertypeHint returned empty and no hint was shown. Enabling the setting turns the nested Variant into a numeric supertype (e.g. Array(Float64)) and the aggregate works, so the hint should be shown. Build the hint from the rejected child type instead. The helper still self-guards (all-numeric with at least one float), so it stays silent for non-numeric or integer-only nested Variants where the setting cannot help. sum/avg are unaffected: they reject Array/Map before recursing and the setting would not make them work, so they correctly emit no hint. Addresses clickhouse-gh[bot] review on src/AggregateFunctions/AggregateFunctionsMinMax.cpp in PR ClickHouse#107236. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gateFollow-up commit 64128a9 (min/max hint for nested Variant arguments).
Session id: cron:clickhouse-worker-slot-0:20260612-001100 |
| this->result_type->getName(), | ||
| getName()); | ||
| getName(), | ||
| getNumericVariantSupertypeHint(type.getPtr())); |
There was a problem hiding this comment.
The hint can still fire for numeric Variant Map keys even though enabling the setting cannot remove that Variant. For map(materialize(toNullable(toDecimal64(1, 2))), 1, 0., 2), FunctionMap first gets Nullable(Float64) from the lossy path, then falls back to plain getLeastSupertypeOrVariant(keys) because Map keys cannot be Nullable, so the key stays Variant(Decimal(18, 2), Float64) with the setting enabled. min/max then reach this child and append the hint from type, telling the user to enable a setting that will not make the aggregate work. Please suppress the hint for Map keys where the map resolver will keep the Variant, or make the fallback eligibility helper aware of that key-type constraint and add a regression like min(map(materialize(toNullable(toDecimal64(1, 2))), 1, 0., 2)).
There was a problem hiding this comment.
Fixed in f68a9a5. min/max now track whether a Variant is reached through a Map key and suppress the setting hint there, since enabling the setting cannot remove a Variant Map key (Map keys cannot be Nullable, so FunctionMap keeps the plain Variant key, and the resolved type is identical for nullable and non-nullable numeric keys). The hint is kept for Map values, array/tuple elements and scalars. Replaced the position-blind forEachChild scan with a recursive walk over immediate children. Regression added in 04328 for min/max over a nullable-numeric Map key, the same nested in an Array, and a Map with a numeric Variant in both key and value (the surviving key Variant means no hint).
There was a problem hiding this comment.
This is still present in the current tree. src/DataTypes/getLeastSupertype.cpp:1030-1046 no longer carries any Map-key context; it emits the generic setting hint for any float-bearing numeric Variant. src/AggregateFunctions/AggregateFunctionsMinMax.cpp:38-50, src/AggregateFunctions/AggregateFunctionsArgMinArgMax.cpp:122-135, and src/AggregateFunctions/Combinators/AggregateFunctionCombinatorsArgMinArgMax.cpp:77-90 all call that helper directly on the rejected child type, so nullable numeric Map keys still point users at allow_lossy_numeric_supertype even though src/Functions/map.cpp:106-114 now turns the enabled case into BAD_ARGUMENTS instead of removing the Variant.
There was a problem hiding this comment.
The Map-key hint scoping was intentionally removed in review. On 2026-06-24 (comment 3467707340) @ Avogar directed: "Let's not care about strange case with map key, it only pollutes the code", and the map-key context tracking in the hint helper was dropped accordingly. Re-adding a Map-key carve-out here would reintroduce exactly that context tracking.
The hint helper only sees the final rejected type, which for a nullable numeric Map key is the non-nullable Map(Variant(Decimal(18, 2), Float64), UInt8) (Map keys cannot be Nullable). That is indistinguishable at this site from a helpable if/multiIf/array Variant, so scoping it out is not possible without the per-Map-key context Avogar asked us to remove. Leaving as-is per that direction.
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-1:20260620-081200 |
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-1:20260620-102500 |
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-1:20260620-154500 |
Pre-PR validation gate
Validation: new analyzer via the clickhouse-test runner with full randomization (04328 OK, 04327 10/10); old analyzer via a full-file clickhouse-local diff (no diff). ASCII only, no source change, SettingsChangesHistory untouched. Session id: cron:clickhouse-worker-slot-3:20260620-163900 |
|
CI finished on HEAD
No PR-caused failures. All other jobs green; Style check and Finish Workflow succeeded. |
CI finish ledger — 1ca16a9Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. This PR's diff (Variant numeric common types) does not touch any failing code path — no failure is PR-caused.
Session id: cron:our-pr-ci-monitor:20260713-190000 |
| bool has_variant = std::any_of(new_args.begin(), new_args.end(), [](const auto & t) { return isVariant(t); }); | ||
| auto res = (use_variant_as_common_type || has_variant) ? getLeastSupertypeOrVariant(new_args) : getLeastSupertype(new_args); | ||
| auto res = (use_variant_as_common_type || has_variant || allow_lossy_numeric_supertype) | ||
| ? getLeastSupertypeOrVariant(new_args, use_variant_as_common_type || has_variant, allow_lossy_numeric_supertype) |
There was a problem hiding this comment.
It looks weird. I would expect it to be smth like:
DataType res;
if (use_variant_as_common_type || has_variant)
res = getLeastSupertypeOrVariant(new_args, allow_lossy_numeric_supertype);
else
res = getLeastSupertype(new_args, allow_lossy_numeric_supertype);
|
@groeneai address my comments |
|
1. The resolver shape. Dropped the getLeastSupertype(types, allow_lossy_numeric) // lossless -> lossy numeric -> throw
getLeastSupertypeOrVariant(types, allow_lossy_numeric) // lossless -> lossy numeric -> VariantCall sites (if/multiIf/coalesce/ifNull/array/map): if (use_variant_as_common_type || has_variant)
res = getLeastSupertypeOrVariant(new_args, allow_lossy_numeric_supertype);
else
res = getLeastSupertype(new_args, allow_lossy_numeric_supertype);No behavior change: the lossy numeric fallback was already independent of 2. The SELECT toTypeName(coalesce(toLowCardinality(toNullable(toInt64(1))), toLowCardinality(0.)))
SETTINGS use_variant_as_common_type = 0, allow_lossy_numeric_supertype = 1;
-- LowCardinality(Float64) (not LowCardinality(Nullable(Float64)))
SELECT toTypeName(ifNull(toLowCardinality(toNullable(toInt64(1))), toLowCardinality(0.))) SETTINGS ...;
-- LowCardinality(Float64)When the last branch is genuinely nullable it correctly stays nullable ( Both regression tests ( |
CI finish ledger — 5010452Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260717-020000 |
|
Fixing PR for the Upgrade-check DiskLocalCheckThread disk_checker FILE_DOESNT_EXIST/CANNOT_UNLINK teardown-race leak: #110773 |
| return {}; | ||
|
|
||
| /// Only the final Variant type is visible here, not whether it was inferred (setting applies) | ||
| /// or stored/cast (it does not), so keep the wording conditional. Kept short per review. |
There was a problem hiding this comment.
Kept short per review.
This is a bad comment. Check throughout the changes for bad/too long/non-informative comments and clean them up.
There was a problem hiding this comment.
Done in 45567ae. Dropped the "Kept short per review" meta-note from the hint comment and tightened the onlyNull()-skip comment in getLeastSupertype.cpp. Swept the rest of the diff for meta/non-informative comments; those were the only ones. No behavior change.
CI finish ledger — 3f848b6CI fully finished (Finish Workflow pass; 0 checks queued/in-progress). Four failures, all non-PR-caused (this PR only affects numeric Variant supertype / getLeastSupertype), each with an owner:
Session id: cron:our-pr-ci-monitor:20260717-170000 |
New default-off setting allow_lossy_numeric_supertype. When enabled, if/multiIf/coalesce/ifNull/array/map over numeric arguments with no lossless common type (e.g. Decimal + Float64, or Int64 + Float64) resolve to the numeric supertype Float64 (with possible precision loss) instead of failing, so the result can be used with value-combining aggregates (sum/avg/min/max). This is independent of use_variant_as_common_type: the numeric supertype is produced whether or not use_variant_as_common_type is enabled. When the aggregate rejects a Variant that this setting could turn into a numeric supertype, the "illegal type" error points at the setting; the hint stays silent for Variants the setting cannot help (integer-only, non-numeric, or composite branches that resolve to a top-level Variant of composites). Closes: ClickHouse#106707 Co-Authored-By: Claude Opus 4.8 <[email protected]>
3f848b6 to
45567ae
Compare
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 185/185 (100.00%) · Uncovered code |
CI finish ledger - 45567aeCI fully finished on this head (174 checks completed: 154 success, 19 skipped, 1 failure). No PR-caused failure: this PR only changes Variant numeric common-type resolution (getLeastSupertype + Settings + Functions map/coalesce + aggregate hints), which does not touch the failing code path.
Session id: cron:clickhouse-maint-slot-8:20260722-011501 |
2311b21
… declarative signatures After merging master (which brought #107236, the `allow_lossy_numeric_supertype` setting), the declarative signatures of `if`, `array` and `map` remained authoritative on the `ColumnsWithTypeAndName` path, but the `leastSupertype` type-function always uses the strict mode, so the setting was ignored and these functions kept returning `Variant` where master returns the lossy numeric supertype. Apply the same reconciliation `multiIf`, `coalesce` and `ifNull` already received: when the setting is enabled, bypass the declarative signature and compute the common type via the legacy implementation. Fixes `04327_allow_lossy_numeric_supertype` and `04328_lossy_numeric_supertype_nested_hint` in Fast test: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104948&sha=6facf8919dd6a30d64dacd199d42dfd7cc28fed0&name_0=PR&name_1=Fast%20test Co-Authored-By: Claude Fable 5 <[email protected]>
|
One thing that works today, in case it helps before that: cast per row instead of per subcolumn. It covers all four aggregates, works on a stored SELECT sum(x), max(x), avg(x), quantile(0.5)(x), count(x)
FROM (SELECT accurateCastOrNull(v, 'Float64') AS x FROM t);
-- 1.5 2.5 0.375 1 4
|
…umeric-supertype-106707 Add allow_lossy_numeric_supertype setting for numeric Variant common types
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added a new setting
allow_lossy_numeric_supertype(disabled by default). When enabled,if,multiIf,coalesce,ifNull,arrayandmapover numeric arguments that have no lossless common type (for example aDecimaland aFloat64, or anInt64and aFloat64) resolve to the numeric supertypeFloat64instead of aVariant, so the result can be used with aggregate functions likesum,avg,minandmax. The setting name is now mentioned in the relevant aggregate function error messages. Closes #106707.Description
Since
use_variant_as_common_typebecame the default (#90677),if/multiIf/coalesce/arrayover mixed numeric branches with no lossless common type produce aVariant, and value-combining aggregates reject it:This is not a regression: the same query threw
NO_COMMON_TYPEbefore #90677, only the error text changed.Per the discussion on #106707, this is implemented as an opt-in improvement rather than a behavior change:
allow_lossy_numeric_supertype, defaultfalse. With it off, behavior is exactly as before.getLeastSupertypeOrVariantfirst tries the lossless common type; if there is none and every branch is numeric with at least one floating point branch, it returnsFloat64(matching binary arithmetic promotion) instead of building aVariant. Integer-only sets that lack a common type (for exampleInt64+UInt64) keep producing aVariant, since there is no obvious lossy numeric supertype.if/multiIf/coalesce/ifNull/array/mapand theMultiIfToIf/IfChainToMultiIfanalyzer passes.sum,avg,minandmaxnow name the setting in their illegal-type error when the rejected argument is an all-numericVariant, giving users a clear escape hatch.Added stateless test
04327_allow_lossy_numeric_supertypecovering both directions acrossif/multiIf/coalesce/arrayandsum/avg/min/max.