feat: add array_sum scalar function#22542
Open
crm26 wants to merge 1 commit into
Open
Conversation
Adds `array_sum(array)` returning the sum of elements in a numeric array. Aliased as `list_sum`. Part of the per-function split sequence on tracking issue apache#21536, following the pattern of the already-merged PRs in this series (cosine_distance apache#21542, inner_product apache#21861, array_normalize apache#22013, array_scale apache#22466). Semantics: - NULL row in array -> NULL row out - NULL elements are skipped (SQL aggregate convention; matches PostgreSQL array_sum, DuckDB list_sum, Spark aggregate). A row whose every element is NULL yields NULL. - Empty array -> 0.0 (additive identity, matches SQL SUM over no rows conceptually, and DuckDB list_sum([]) = 0) Input is List/LargeList/FixedSizeList of any numeric type; elements are coerced to Float64. Output is Float64.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Partial of #21536 —
array_sum(first of the array aggregates in the series).Rationale for this change
Continues the per-function split sequence requested by @alamb on #21536. Four sibling PRs already merged:
cosine_distance(#21542),inner_product(#21861),array_normalize(#22013),array_scale(#22466).array_addis in flight as #22459 by @SubhamSinghal.array_sumis the first of the three array-aggregate functions (sum, product, avg). Its semantics set the pattern for the other two aggregates.What changes are included in this PR?
array_sum(array)indatafusion/functions-nested/src/array_sum.rsdatafusion/functions-nested/src/lib.rsdatafusion/sqllogictest/test_files/array_sum.sltdocs/source/user-guide/sql/scalar_functions.mdSignature: `List/LargeList/FixedSizeList` in, `Float64` out (one scalar per row). Numeric inner types coerced to `Float64`.
NULL semantics — SQL aggregate convention (deliberate divergence from binary-op siblings):
Empty array → `0.0` (additive identity; matches DuckDB `list_sum([]) = 0`).
Alias: `list_sum` (matches the precedent of `array_normalize`→`list_normalize`, `array_scale`→`list_scale`).
Are these changes tested?
Yes. SLT covers happy paths, empty arrays, NULL row, NULL elements (mix + all-NULL), all list variants (List/LargeList/FixedSizeList), numeric coercion (Float32/Int64/integer literals), multi-row composition, error paths, return type, and the `list_sum` alias.
Are there any user-facing changes?
Yes — new SQL scalar function `array_sum(array)` and its alias `list_sum`.