fix: return null for avg() of empty array#105
Open
spokodev wants to merge 1 commit into
Open
Conversation
avg() over an empty array returned NaN (0/0) instead of null. The JMESPath spec states an empty array produces a return value of null. Guard the empty case before the sum loop and add the compliance case avg(empty_list) = null.
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.
Bug
avg()over an empty array returnsNaNinstead ofnull:The cause is
_functionAvgcomputingsum / inputArray.length, which is0 / 0 = NaNwhen the array is empty.Spec
The JMESPath specification for
avg()states: "An empty array will produce a return value of null." (https://jmespath.org/specification.html#avg)The official compliance suite already has the case
avg(empty_list) -> nullinfunctions.json. The copy vendored in this repo'stest/compliance/functions.jsonpredated that case, so the gap went unnoticed.Fix
Guard the empty case at the top of
_functionAvg:Also adds the missing compliance case
avg(empty_list) -> nulltotest/compliance/functions.json(theempty_listinput already exists in the suite'sgivenblock).The added test fails before the fix (
NaN should loosely deep-equal null) and passes after. Full suite green: 889 passing.