From 709f1d694a2268c1be36118bcce0285fa2ea823c Mon Sep 17 00:00:00 2001 From: Sasha Mitchell Date: Sat, 3 Oct 2026 05:19:01 +0700 Subject: [PATCH] Return null from avg of an empty array The spec says an empty array is null. The zero sum divided by zero was NaN. --- compliance/functions.json | 4 ++++ functions.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/compliance/functions.json b/compliance/functions.json index 8b8db36..2f48184 100644 --- a/compliance/functions.json +++ b/compliance/functions.json @@ -63,6 +63,10 @@ "expression": "avg(numbers)", "result": 2.75 }, + { + "expression": "avg(empty_list)", + "result": null + }, { "expression": "avg(array)", "error": "invalid-type" diff --git a/functions.go b/functions.go index e9770e8..64eb2a9 100644 --- a/functions.go +++ b/functions.go @@ -428,6 +428,10 @@ func jpfAvg(arguments []interface{}) (interface{}, error) { // We've already type checked the value so we can safely use // type assertions. args := arguments[0].([]interface{}) + // An empty array is null. The zero sum divided by zero was NaN. + if len(args) == 0 { + return nil, nil + } length := float64(len(args)) numerator := 0.0 for _, n := range args {