Skip to content

Commit 8a09826

Browse files
committed
fix reduce syntax to emit results for each initial value
1 parent b685aac commit 8a09826

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

cli/test.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,6 +3379,22 @@
33793379
expected: |
33803380
4.5
33813381
3382+
- name: reduce with select in update
3383+
args:
3384+
- 'reduce range(5) as $x (0; . + $x | select($x != 2))'
3385+
input: 'null'
3386+
expected: |
3387+
8
3388+
3389+
- name: reduce with query in start
3390+
args:
3391+
- 'reduce range(5) as $x (range(3); . + $x)'
3392+
input: 'null'
3393+
expected: |
3394+
10
3395+
11
3396+
12
3397+
33823398
- name: reduce with variable binding
33833399
args:
33843400
- 'reduce .[] as $x (0; . + $x) as $x | $x'
@@ -3438,6 +3454,33 @@
34383454
0
34393455
1
34403456
3457+
- name: foreach with select in update
3458+
args:
3459+
- -c
3460+
- 'foreach range(5) as $i (0; . + $i | select($i != 2); [$i, .])'
3461+
input: 'null'
3462+
expected: |
3463+
[0,0]
3464+
[1,1]
3465+
[3,4]
3466+
[4,8]
3467+
3468+
- name: foreach with query in start
3469+
args:
3470+
- -c
3471+
- 'foreach range(3) as $i (range(3); . + $i; [$i, .])'
3472+
input: 'null'
3473+
expected: |
3474+
[0,0]
3475+
[1,1]
3476+
[2,3]
3477+
[0,1]
3478+
[1,2]
3479+
[2,4]
3480+
[0,2]
3481+
[1,3]
3482+
[2,5]
3483+
34413484
- name: foreach with unary operator
34423485
args:
34433486
- '[-foreach -.[] as $i (0; . + $i)]'

compiler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,6 @@ func (c *compiler) compileTry(e *Try) error {
726726
func (c *compiler) compileReduce(e *Reduce) error {
727727
c.appendCodeInfo(e)
728728
defer c.newScopeDepth()()
729-
setfork := c.lazy(func() *code {
730-
return &code{op: opfork, v: len(c.codes)}
731-
})
732729
c.append(&code{op: opdup})
733730
v := c.newVariable()
734731
f := c.newScopeDepth()
@@ -737,6 +734,9 @@ func (c *compiler) compileReduce(e *Reduce) error {
737734
}
738735
f()
739736
c.append(&code{op: opstore, v: v})
737+
setfork := c.lazy(func() *code {
738+
return &code{op: opfork, v: len(c.codes)}
739+
})
740740
if err := c.compileQuery(e.Query); err != nil {
741741
return err
742742
}

0 commit comments

Comments
 (0)