Skip to content

Commit 58da581

Browse files
authored
Merge branch 'master' into perf/min-max-median-mean
2 parents 4e035dd + 556b1d1 commit 58da581

4 files changed

Lines changed: 18 additions & 0 deletions

File tree

builtin/builtin_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ func TestBuiltin(t *testing.T) {
188188
{`reduce(1..9, # + #acc)`, 45},
189189
{`reduce([.5, 1.5, 2.5], # + #acc, 0)`, 4.5},
190190
{`reduce([], 5, 0)`, 0},
191+
{`reduce(10..1, # + #acc, 100)`, 100},
192+
{`reduce([], # + #acc, 42)`, 42},
191193
{`concat(ArrayOfString, ArrayOfInt)`, []any{"foo", "bar", "baz", 1, 2, 3}},
192194
{`concat(PtrArrayWithNil, [nil])`, []any{42, nil}},
193195
{`flatten([["a", "b"], [1, 2]])`, []any{"a", "b", 1, 2}},

compiler/compiler.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,9 +1124,18 @@ func (c *compiler) BuiltinNode(node *ast.BuiltinNode) {
11241124
c.derefInNeeded(node.Arguments[2])
11251125
c.emit(OpSetAcc)
11261126
} else {
1127+
// When no initial value is provided, we use the first element as the
1128+
// accumulator. But first we must check if the array is empty to avoid
1129+
// an index out of range panic.
1130+
empty := c.emit(OpJumpIfEnd, placeholder)
11271131
c.emit(OpPointer)
11281132
c.emit(OpIncrementIndex)
11291133
c.emit(OpSetAcc)
1134+
jumpPastError := c.emit(OpJump, placeholder)
1135+
c.patchJump(empty)
1136+
c.emit(OpPush, c.addConstant(fmt.Errorf("reduce of empty array with no initial value")))
1137+
c.emit(OpThrow)
1138+
c.patchJump(jumpPastError)
11301139
}
11311140
c.emitLoop(func() {
11321141
c.compile(node.Arguments[1])

expr_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,12 @@ func TestExpr_error(t *testing.T) {
14851485
| ArrayOfAny[-7]
14861486
| ..........^`,
14871487
},
1488+
{
1489+
`reduce(10..1, # + #acc)`,
1490+
`reduce of empty array with no initial value (1:1)
1491+
| reduce(10..1, # + #acc)
1492+
| ^`,
1493+
},
14881494
}
14891495

14901496
for _, tt := range tests {

test/fuzz/fuzz_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func FuzzExpr(f *testing.F) {
5454
regexp.MustCompile(`operator "in" not defined on .*`),
5555
regexp.MustCompile(`cannot sum .*`),
5656
regexp.MustCompile(`index out of range: .* \(array length is .*\)`),
57+
regexp.MustCompile(`reduce of empty array with no initial value`),
5758
regexp.MustCompile(`cannot use <nil> as argument \(type .*\) to call .*`),
5859
regexp.MustCompile(`illegal base64 data at input byte .*`),
5960
}

0 commit comments

Comments
 (0)