Repository navigation
Aggregate function combinator -Tuple - #98190
Conversation
|
Workflow [PR], commit [272d6b9] Summary: ✅
AI ReviewSummaryThis PR adds the Missing context / blind spots
Final Verdict
|
There was a problem hiding this comment.
❌ serialize/deserialize in AggregateFunctionTuple currently ignore the incoming version and effectively force nested functions to use std::nullopt (current default format).
This breaks versioned aggregate-state compatibility during rolling upgrades: ...State produced for an older revision can be serialized/deserialized with a mismatched nested format.
Please pass the received version through to nested calls in both methods, similar to other combinators (for example -Array / -ForEach).
10b3627 to
02c23d4
Compare
02c23d4 to
bd13b7d
Compare
nihalzp
left a comment
There was a problem hiding this comment.
Looks good! Thanks for working on this. I have left a few comments.
Additionally, let's add test for the following cases:
Nullable tuple elements, parametric aggregate function (e.g. quantileTuple(0.5)(t)), multiple nested combinators (e.g. sumTupleDistinctIf), multi argument base function.
|
The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix. |
c52fa61 to
7039448
Compare
7039448 to
c580e49
Compare
c580e49 to
58d4022
Compare
|
@nihalzp I investigated the CI failures. Summary: none are caused by this PR. Per-failure attribution (head
CI report (example, AST fuzzer): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98190&sha=7af0692e1f4dd366b8215a0ca918c27a7a5d0ea5&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%29 The earlier |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 402/442 (90.95%) · Uncovered code |
1a42340
…, parametric `-Merge` Address reviewer feedback on PR ClickHouse#98190 from @nihalzp 2026-04-23: 1. `sumTuple(x)` on `Tuple(Int64, Float64)` column stored with `ratio_of_defaults_for_sparse_serialization` < 1 would crash with `Bad cast from type DB::ColumnSparse to DB::ColumnVector<long>`. MergeTree may keep individual Tuple-element streams as `ColumnSparse` while the outer `ColumnTuple` is dense, so nested aggregate functions would receive sparse columns they can't `assert_cast`. Override `addBatch`/`addBatchSinglePlace`/`addManyDefaults` (and add a defensive fallback inside `add`) to call `recursiveRemoveSparse` once per batch. 2. `sumTuple(tuple(NULL, toInt64(1)))` returned `(NULL,NULL)` of type `Tuple(Nullable(Nothing), Nullable(Nothing))` instead of `(NULL,1)` of type `Tuple(Nullable(Nothing), Int64)`. `transformArguments` was returning `elem_types[0]`, so when the first element was `Nullable(Nothing)` the recursive `get()` collapsed the representative function to `AggregateFunctionNothing*` and its name (`nothingNull`) became the base name used for every per-element nested function. Pick the first non-`onlyNull` element as the representative instead. 3. `quantilesTDigestTupleMerge(0.9)(quantilesTDigestTupleState(0.5)(...))` threw `ILLEGAL_TYPE_OF_ARGUMENT` because the default `haveSameStateRepresentationImpl` compares full state types, which include `AggregateFunctionTuple`'s own parameters (`0.5` vs `0.9`). Override `haveSameStateRepresentationImpl` to delegate to the nested functions pairwise, mirroring how `quantilesTDigest` already ignores parameters that affect only finalization. Add regression tests for all three cases in `04003_tuple_combinator`.
Previously the batch overrides materialized the outer `ColumnTuple` once via `recursiveRemoveSparse`, but then delegated to `IAggregateFunctionHelper::addBatch`, which loops and calls `add` per row. `add` itself calls `recursiveRemoveSparse(columns[0]->getPtr())`, and `recursiveRemoveSparse` always builds a new `ColumnTuple` (it unconditionally reassembles the elements), so we still paid a per-row allocation in the hot aggregation loop. That regresses the main path for `-Tuple` aggregation, not just sparse corner cases, and contradicts the comment in the code claiming the batch paths materialize once. Split out a `addRowFromMaterialized` helper that takes an already materialized `ColumnTuple`, and call it directly from the row loop in `addBatch`/`addBatchSinglePlace`. The fallback `add` still materializes defensively for callers that bypass the batch overrides. Addresses review thread `PRRT_kwDOA5dJV86DdTyt` on PR ClickHouse#98190.
…eType` `getNormalizedStateType` bound a reference to `*nested_functions.front()->getNormalizedStateType()`, but that nested call returns a freshly allocated `DataTypePtr` that owns the only reference to the `DataTypeAggregateFunction`. Binding a reference to the pointee does not extend the temporary's lifetime, so the object was destroyed at the end of the statement and the subsequent `getParameters` call read freed memory while copying the parameters `Array` (`std::vector<Field>`), as reported by AddressSanitizer in the flaky check. Hold the nested normalized state type in a named local variable so it stays alive until the parameters are copied. CI report: ClickHouse#98190 ASan: heap-use-after-free in std::vector<DB::Field>::vector(const&) via DataTypeAggregateFunction::getParameters -> AggregateFunctionTuple::getNormalizedStateType (STID 1388-406d, flaky check amd_asan_ubsan) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Aggregate function combinator `-Tuple`
Closes #98011
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add aggregate function combinator
-Tuple, which applies the underlying aggregate function to each element of aTuplecolumn independently and returns aTupleof the results, preserving element names:sumTuple(t)fort = (a, b)returns(sum(a), sum(b)). Aggregate functions with several arguments take one tuple per argument, paired by position:corrTuple((a1, a2), (b1, b2))returns(corr(a1, b1), corr(a2, b2)). Unlike-ForEachover arrays, elements may have different types, and per-element result types and names are preserved.Documentation entry for user-facing changes
Version info
26.7.1.648(included in26.7and later)