feat: add array_avg scalar function#23168
Open
crm26 wants to merge 1 commit into
Open
Conversation
Adds array_avg, the final function from the split-PR pipeline in apache#21536. Computes the arithmetic mean of the elements of a numeric array, returned as Float64. Templated from the merged array_sum (apache#22542) with the same SQL aggregate NULL conventions. Semantics: - NULL elements are skipped (per SQL aggregate convention). Both the sum AND the count exclude NULL elements. So array_avg([1, NULL, 3]) = (1 + 3) / 2 = 2 — not (1 + 3) / 3. - NULL row → NULL row out. - All-NULL array → NULL. - Empty array → NULL (matches PostgreSQL AVG, DuckDB list_avg, and the SQL Standard SUM/AVG-of-empty-set rule. Same behaviour as the merged array_sum, array_product siblings). - Return type is always Float64. - Inner numeric types (Float32, Int*, UInt*) are coerced to Float64. - Supported list shapes: List, LargeList, FixedSizeList. - Alias: list_avg (matches the list_sum / list_product naming). SLT coverage: basic positive/negative/cancel cases, single element, non-integer mean, empty array, bare NULL, NULL elements skipped, single non-NULL among NULL, all-NULL row, all 3 list shapes, float and integer inner types, integer mean that is non-integer, multi-row mix, error paths (non-list input, zero args, two args), return type assertion, and the list_avg alias. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Jefffrey
approved these changes
Jun 25, 2026
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.
Summary
Adds
array_avg, the last function from the split-PR pipeline tracked in #21536.Computes the arithmetic mean (sum divided by count) of the elements of a numeric array, returned as
Float64. Templated from the mergedarray_sum(#22542) with the same SQL aggregate NULL conventions; sibling ofarray_sum,array_product,array_subtract,array_add,array_scale, andarray_normalize.Semantics
NULL semantics — SQL aggregate convention (deliberate divergence from binary-op siblings):
AVG, DuckDBlist_avg, Sparkaggregate. Soarray_avg([1, NULL, 3]) = (1+3) / 2 = 2, not(1+3) / 3.AVG(...)over an all-NULL column)array_sumfeat: add array_sum scalar function #22542 andarray_productAddarray_productUDF #22703, PostgreSQL, DuckDBlist_avg, SQL Standard AVG-of-empty-set)Type coercion:
Float32,Int*,UInt*) coerced toFloat64. Integer-arg literals are coerced too.Float64(since avg of integers can be non-integer).List shapes supported:
List,LargeList,FixedSizeList.Alias:
list_avg(matches thelist_sum/list_productpattern).Test coverage
SLT (
array_avg.slt) covers:[1,2] → 1.5)NULLrow, NULL elements skipped, single non-NULL among NULLs, all-NULL arrayList,LargeList,FixedSizeList)Float64)list_avgaliasCloses
The last open slot in #21536 — completes the 8-function split-PR pipeline from the originally-too-big PRs #21371 / #21376.