From 8a09826a12f406b13fb2efd7e028f6bdf76c36f6 Mon Sep 17 00:00:00 2001 From: itchyny Date: Sat, 16 Nov 2024 20:39:02 +0900 Subject: [PATCH] fix reduce syntax to emit results for each initial value --- cli/test.yaml | 43 +++++++++++++++++++++++++++++++++++++++++++ compiler.go | 6 +++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/cli/test.yaml b/cli/test.yaml index 72d4bf34..efbba4b2 100644 --- a/cli/test.yaml +++ b/cli/test.yaml @@ -3379,6 +3379,22 @@ expected: | 4.5 +- name: reduce with select in update + args: + - 'reduce range(5) as $x (0; . + $x | select($x != 2))' + input: 'null' + expected: | + 8 + +- name: reduce with query in start + args: + - 'reduce range(5) as $x (range(3); . + $x)' + input: 'null' + expected: | + 10 + 11 + 12 + - name: reduce with variable binding args: - 'reduce .[] as $x (0; . + $x) as $x | $x' @@ -3438,6 +3454,33 @@ 0 1 +- name: foreach with select in update + args: + - -c + - 'foreach range(5) as $i (0; . + $i | select($i != 2); [$i, .])' + input: 'null' + expected: | + [0,0] + [1,1] + [3,4] + [4,8] + +- name: foreach with query in start + args: + - -c + - 'foreach range(3) as $i (range(3); . + $i; [$i, .])' + input: 'null' + expected: | + [0,0] + [1,1] + [2,3] + [0,1] + [1,2] + [2,4] + [0,2] + [1,3] + [2,5] + - name: foreach with unary operator args: - '[-foreach -.[] as $i (0; . + $i)]' diff --git a/compiler.go b/compiler.go index 3fcb83f2..cea452e3 100644 --- a/compiler.go +++ b/compiler.go @@ -726,9 +726,6 @@ func (c *compiler) compileTry(e *Try) error { func (c *compiler) compileReduce(e *Reduce) error { c.appendCodeInfo(e) defer c.newScopeDepth()() - setfork := c.lazy(func() *code { - return &code{op: opfork, v: len(c.codes)} - }) c.append(&code{op: opdup}) v := c.newVariable() f := c.newScopeDepth() @@ -737,6 +734,9 @@ func (c *compiler) compileReduce(e *Reduce) error { } f() c.append(&code{op: opstore, v: v}) + setfork := c.lazy(func() *code { + return &code{op: opfork, v: len(c.codes)} + }) if err := c.compileQuery(e.Query); err != nil { return err }