Repository navigation
Add aggregate function gini - #112280
Add aggregate function gini#112280
gini#112280Conversation
Calculates the Gini coefficient of a numeric column, a measure of inequality in a distribution. The result ranges from 0 (perfect equality) to a maximum approaching 1 as the group grows. The state collects all values and sorts them at finalize time, so the function consumes O(n) memory like quantileExact. Returns NaN when there are fewer than 2 values or the sum of values is 0. NaN inputs are skipped. Co-Authored-By: Claude Fable 5 <[email protected]>
59ffa1d to
bc8aa31
Compare
|
Workflow [PR], commit [0783f8e] Summary: ✅
AI ReviewSummaryThis PR adds the aggregate function Final Verdict✅ No new findings. |
Correct the `ARRAY JOIN` placement and the expected `GROUP BY` results. CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112280&sha=bc8aa310d348ff036fe3b6ab5260fd1dcf62c9c3&name_0=PR&name_1=Fast%20test PR: ClickHouse#112280
Reject negative inputs so results stay in the documented range. Return NaN for all-NULL groups, matching groups with fewer than two values.
Keep values in their input type and normalize pairwise-difference accumulation to avoid `Float64` coercion, overflow, and cancellation.
Compare results within a tolerance because floating-point rounding differs between Linux and Darwin.
Reject oversized states consistently while adding, merging, serializing, and deserializing.
Restart CI after the unrelated STID 3293-4e7f failure and a hung `Fast test (arm_darwin)` job.
|
CI retry note: The same run also had |
Use the function-owned null adapter for both Nullable numeric inputs and Nullable(Nothing), so an empty state still finalizes to Float64 NaN.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 143/170 (84.12%) · Uncovered code |
|
Hi @amirreza1307 @Manerone — this PR appears to contain a backward incompatible change but is categorized as Why: The nine combinator files changed here are shared by every aggregate function. Suggested category: Could you check if the changelog category should be updated? This helps users know what to expect when upgrading. Ignore this if the change is intentionally non-breaking. |
|
Hi @amirreza1307 @Manerone — a couple of suggestions to make this PR easier for reviewers and the changelog reader. Description (currently
Ignore this if the current title/body is intentional. |
|
Hi @amirreza1307 @Manerone — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
This reverts merge f75ec00 (ClickHouse#112280) in full, restoring all 15 files to their pre-merge content. Besides adding `gini`, that pull request added nine `getArgumentsThatCanBeOnlyNull` overrides across eight combinator files and changed `-If`'s existing one to union the nested set, so a combinator now forwards that set from the function it wraps. The `Null` combinator uses the set to decide whether an argument that can only be NULL folds the whole aggregate into `AggregateFunctionNothing`, so forwarding it removed the fold one level up and changed the result type and value of pre-existing expressions that chain one of those combinators on top of `-If`. A combinator that gained no override keeps the empty set that `IAggregateFunction::getArgumentsThatCanBeOnlyNull` returns by default, so it contributes nothing of its own: `-Merge`, `-Array`, `-ForEach`, `-Tuple` and `-Map` gained none, and `sumIfForEach([number, number], [NULL, NULL])` still returns `Array(UInt64)` `[0, 0]`. Measured on debug builds of this branch and of its parent commit, which contains the merge: `countIfOrNull(number, NULL)` moved from `UInt64` `0` to `Nullable(UInt64)` `NULL`, `sumIfResample(0, 2, 1)(number, NULL, number % 2)` from a scalar `Nullable(Nothing)` to `Array(UInt64)` `[0, 0]`, and `sumIfState(number, NULL)` from a folded `Nothing` to a real `AggregateFunction(sumIf, UInt64, Nullable(Nothing))` state. Those two trees are equivalent to the merge and to its first parent on this surface, by measurement rather than by assumption: for all 15 files the merge touched, the blobs at the parent commit equal the blobs at the merge, and the blobs at this branch's head equal the blobs at f75ec00^1; no file under `src/AggregateFunctions/Combinators/` differs between f75ec00^1 and this head; and of the 46 commits between the merge and this branch's parent, the three that touch `src/AggregateFunctions/` all belong to the unrelated PromQL `deriv` scaling pull request 112360. Four of the new overrides also insert their own column index, and the two outside the `-If` family reach expressions with no `-If` at all, such as `sumArgMin(1, NULL)`. A partial revert is not available. `gini` declares argument 0 in `getArgumentsThatCanBeOnlyNull` itself, and the same forwarding path carries both that declaration and the `-If` filter index, so removing the propagation while keeping `gini` would silently change `gini`'s own combinator results and contradict assertions in its committed test. `gini` is unreleased: the merge is not an ancestor of 26.7, 26.6, 26.5, 26.3 or 25.8, and it never reached `CHANGELOG.md`, so the revert returns 26.8-dev to its pre-merge state without withdrawing released behaviour. A behaviour-neutral re-land of `gini` follows in a separate pull request. Verified by byte identity rather than by a new test: all 15 files hash equal to their `f75ec003a3ee^1` blobs, which is the whole correctness criterion for a revert. Closes: ClickHouse#113763
Adds
gini, an aggregate function for measuring inequality over finite, non-negative numeric values. UnlikearrayNormalizedGini, it works directly over a column and can be used naturally withGROUP BY, aggregate combinators, and distributed aggregation.The state retains samples in their original numeric or decimal type and sorts them at finalization. It computes the coefficient from normalized adjacent pairwise differences in
long doublebefore returningFloat64, avoiding earlyFloat64precision loss and overflow for large finite values while preserving one-unit differences in wide integers and decimals. The function usesO(n)memory per group, skipsNaNvalues, rejects negative and infinite inputs, and returnsNaNfor fewer than two values or a zero sum.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added aggregate function
ginito calculate the Gini coefficient of finite, non-negative numeric values, including grouped and distributed aggregations.