Repository navigation
Allow aggregate functions that don't support Variant to be applied to Variant arguments - #125726
Conversation
…to `Variant` arguments
If an aggregate function rejects a `Variant` argument with `ILLEGAL_TYPE_OF_ARGUMENT`,
`AggregateFunctionFactory` applies it to `Nullable` of the least common supertype of the
alternatives, through a new `AggregateFunctionVariantAdapter` that casts the argument
columns and shares the state of the nested function:
f(variant) == f(CAST(variant AS Nullable(supertype(T1, ..., TN))))
Functions that support `Variant` natively (`count`, `any`, `groupArray`, ...) are not affected.
This is a simpler reimplementation of #109369
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [0526eb3] Summary: ✅
AI ReviewSummaryReviewed #125726 at The PR's Fast test binary passed 95 focused behavior comparisons. Another 18 boundary probes confirmed the remaining issue and exercised state and container consumers. Adapted states were checked through Findings❌ Blockers
Final VerdictStatus: ❌ Block. Correct or reject the |
…VariantAdapter` The batch methods converted the whole input column regardless of `row_begin` and `row_end`. Ordered window frames add one row at a time, so a block of N rows was converted N times. Now the columns are cut to the requested range before the conversion when the range is not the whole block. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…binators like `-Merge` `sumMerge` of `AggregateFunction(sum, Variant(UInt64))` failed, because the `Variant` argument appears only after `-Merge` strips the state type, and the nested function was created without the adapter. Now the adapter is applied to the nested function when the arguments of the combinator itself have no `Variant` (so it cannot be applied around the combinator). This also makes `sumArray` and `maxForEach` work with arrays of `Variant`. Add tests for ordered window frames, an explicitly declared state type, and combinators. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes166 object files changed (+144.30 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units334 translation units recompiled, 2534 s compile time in total, 334 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.95 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Everything else LGTM.
… of try/catch Add `AggregateFunctionProperties::variant_arguments_conversion` that declares which `Variant` arguments an aggregate function cannot process natively (`All` for `sum`, `avg`, `min`, `max`, the `quantile` family, the statistical functions, etc., `AllExceptFirst` for `argMin`, `argMax`, `topKWeighted`, `approx_top_sum`, `groupArrayInsertAt`), and `IAggregateFunctionCombinator::needsVariantArgumentsConversion` for combinators that check the type of their own arguments (`-ArgMin`, `-ArgMax`, `-Resample`). `AggregateFunctionFactory::get` probes them with `needsVariantArgumentsConversion`, which walks through the combinators, instead of catching `ILLEGAL_TYPE_OF_ARGUMENT`. The probe sees the arguments of the nested function, so combinators like `-Merge` and `-Array` don't need special handling anymore, and `getNested` and `getWithoutVariantAdapter` are removed. #125726 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/AggregateFunctions/AggregateFunctionsMinMax.cpp
…ter rejects an argument When the alternatives of a `Variant` argument have no common supertype, `AggregateFunctionFactory` throws its own `ILLEGAL_TYPE_OF_ARGUMENT` before the nested function is created, so the hint added on master by `getNumericVariantSupertypeHint` (checked by `04328_lossy_numeric_supertype_nested_hint`) was lost. Append the same hint to this exception. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125726&sha=latest&name_0=PR&name_1=Fast%20test PR: #125726 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nversion `AggregateFunctionFactory::get` applies the `Null` combinator before constructing a function with a combinator, so e.g. the condition of `-If` reaches `transformArguments` without `Nullable`. `needsVariantArgumentsConversion` probed combinators with the original types, so `uniqIf(v, toNullable(cond))` threw `ILLEGAL_TYPE_OF_ARGUMENT`. Address review: #125726 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he nested function of combinators `AggregateFunctionVariantAdapter` converted every `Variant` argument and was applied around the whole combined function when any nested function needed the conversion. This changed the payload of `argMax`, `topKWeighted`, `-ArgMax` and `-Resample` (including its `NULL` handling, e.g. `groupArrayArgMax(v, k)` dropped the row with the maximum key), and made `-Distinct` collect converted values, so `sum(DISTINCT v)` treated `1::UInt8` and `1::UInt16` alternatives as equal. Now only the argument positions that the function or the outermost combinator itself does not support are converted (`getVariantArgumentsToConvert`). The arguments passed to a nested function are converted by the adapter applied to it, as nested functions are created by `AggregateFunctionFactory::get` too. Since the adapter can now be inside -If, it applies the `if` argument itself instead of passing it to the nested `Null` combinator, which does not support it. Address review: #125726 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failures: |
|
Both already have fixes, one merged and one open:
Local A/B with this seed query on the same table shape ( |
…ax`, `-Resample` and `-OrDefault` When the nested function converted the `Variant` arguments, the implicit `Null` combinator ended up inside these combinators: `-ArgMin`/`-ArgMax` selected rows with NULL payload, and `-Resample`/`-OrNull`/`-OrDefault` produced nullable states for empty groups. The results differed from the ones for the arguments explicitly cast to `Nullable`. Such combinators now convert the arguments that the nested function would convert around themselves (looking through `-If`), so that the `Null` combinator is applied at the outermost level, as for explicit `Nullable` arguments. `-Distinct`, `-State` and native payloads still see the original values. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…guments conversion `stochasticLinearRegression` and `stochasticLogisticRegression` trained on `Variant` arguments produce states that `evalMLMethod` cannot use, because neither `AggregateFunctionVariantAdapter` nor the nested `Null` adapter forwards `getReturnTypeToPredict` and `predictValues`. Leave these functions rejecting `Variant` arguments, as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rgMax`, keep `groupBitmap` opted out `-State` and `-SimpleState` now forward the `Variant` arguments conversion to an outer combinator like `-ArgMax`, so that `sumStateArgMax` skips NULL values before selecting the row, as for explicitly `Nullable` arguments. `groupBitmap` no longer converts `Variant` arguments, because the bitmap functions cannot consume the resulting states. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ple` and `-Distinct` `-Tuple` passed the elements to one `AggregateFunctionVariantAdapter` per element row by row, and `-Distinct` passed every newly seen value to the nested adapter separately, so the conversion was done for every row (`sumTuple` and `sumDistinct` over 1M `Variant` rows took 7-8 s). `-Tuple` now converts the `Variant` elements around itself (`ConvertedAround`), with the new `convertVariantArgumentsAroundMultipleNestedFunctions` that probes every nested function independently. `-Distinct` still collects the distinct keys over the original `Variant` values, but now passes the newly seen rows of a batch to the nested function together. Both queries now take 0.02-0.2 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125726&sha=19f6e90988c4dd33438e600f107791363c5f8d9f&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29 ( |
|
The fix is #127990. This report is the race it fixes: the embedded client thread writes |
…d aggregation `addBatch`, `addBatchWithNonNullPlaces` and `addBatchLookupTable8` of `AggregateFunctionDistinct` called `add` row by row, so a nested `AggregateFunctionVariantAdapter` converted the `Variant` arguments once per newly seen row: `sumDistinct(number::Variant(UInt64)) ... GROUP BY number::UInt8` took 8 s on 1M rows. Now the newly seen rows of a block are collected with their places and passed to the nested function together (0.2 s). `AggregateFunctionDistinct` derives from `IAggregateFunctionHelper` now, because `addBatchLookupTable8` of `IAggregateFunctionDataHelper` is final. This also fixes the clang-tidy `cppcoreguidelines-init-variables` error. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ents of combinators `containsVariant` did not look inside `Nullable`, so `sumMap` of a nullable tuple of `Variant` value arrays threw `ILLEGAL_TYPE_OF_ARGUMENT`. And when a combinator converted a nested argument around itself, the converted type replaced the original `Nullable` argument, dropping the `Nullable` wrapper (`sumMapArgMax` with a null tuple threw `CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN`). Now the combinators convert the arguments without `Nullable`, and `AggregateFunctionFactory` restores it, so the `Null` combinator skips the rows with NULL as for the explicit cast. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…with a `UInt8` key - `AggregateFunctionDistinct` now overrides `addBatchSinglePlaceNotNull`, so the null-aware entrypoint (e.g. `sumDistinctIf` with a `Nullable` condition) forwards the newly seen rows in one call instead of row by row. - The single-place path no longer allocates on every call: while the new rows are contiguous they are passed as a range, and the row numbers are materialized only when they are not. This fixes the `window_functions_distinct` performance regression, where window functions add one row at a time. - `AggregateFunctionIfNullVariadic` overrides `addBatchLookupTable8`: it resolves the places and uses the existing grouped batch implementation instead of calling `add` row by row. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125726&sha=latest&name_0=PR&name_1=Performance%20Comparison%20(arm_release%2C%20master_head%2C%201%2F6) #125726 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lup_group_not_split` from master Co-Authored-By: Claude Opus 5.5 <[email protected]>
`AggregateFunctionTuple` added the rows to the nested functions one by one, so `sumDistinctTuple` over a `Variant` argument reached `AggregateFunctionDistinct::add` for every row, and the nested `AggregateFunctionVariantAdapter` converted every new value separately (8-9 s for 1M rows instead of 0.3 s with an explicit `Nullable` cast). Now the batch methods of `-Tuple` (`addBatch`, `addBatchWithNonNullPlaces`, `addBatchSinglePlace`, `addBatchSinglePlaceNotNull`, and the new override of `addBatchLookupTable8` for a `UInt8` key) pass the whole batch to the matching batch method of every nested function whose element columns are not sparse. Sparse elements and batches with a condition keep the row-by-row path: the nested `Nullable` wrappers don't forward the condition column in every batch method. With `max_threads = 1` on 1M rows, `sumDistinctTuple(tuple(number::Variant(UInt64)))` takes 0.28 s, and the same with `GROUP BY number::UInt8` takes 0.33 s (it was 6.5 s). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: Performance Comparison (arm_release, master_head, 4/6) — This is not caused by this PR: the query is The |
|
The fix is #128387 (mine, open; its Performance Comparison (arm_release) shards are running now). On c72ae8f the PR server read 229 marks for |
…nversion` After merging master, `getAssociatedFunctionByNullsAction` returns a pointer. Hold the found registry entry by pointer, as `getPropertiesImpl` does, so the entry is not copied and `arm_tidy` does not fail. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125726&sha=latest&name_0=PR&name_1=Build%20(arm_tidy%2C%201%2F4) #125726 Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/AggregateFunctions/Combinators/AggregateFunctionState.cpp
|
🕵 Merged |
|
🕵 Fixed the |
Related: #109369
A simpler reimplementation of #109369.
If an aggregate function rejects a
Variantargument withILLEGAL_TYPE_OF_ARGUMENT,AggregateFunctionFactoryapplies it toNullableof the least common supertype of the alternatives:This is done by a new
AggregateFunctionVariantAdapter, which casts the argument columns in the batch methods and shares the state of the nested function. The adapter is applied only as the outermost wrapper, around the combinators (sosumIf,sumState,maxArgMinetc. work as usual). If there is no common supertype (or it cannot be insideNullable), the original exception is thrown.Unlike #109369, this PR does not change the behavior of the functions that already support
Variantnatively (count,any,groupArray,uniq, ...), does not add settings, and does not use the lossyFloat64supertype. It only makes the queries that previously failed work.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Aggregate functions that don't support the
Varianttype natively, such assum,avg,min,max,quantile, can now be applied toVariantarguments. They are calculated overNullableof the least common supertype of the alternatives. Previously such queries threw an exception likeIllegal type Variant(...) of argument for aggregate function sum.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125726]
Version info
26.11.1.1597-master(included in26.11and later)