Skip to content
Myr

fix: reduce/foreach state variable should not be reset each iteration (#3205)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When the UPDATE query in a reduce or foreach syntax does not emit a value, it should simply skip updating the state variable. But currently it resets the state variable to null.

authored by itchyny · committed by GitHub · parent 8619f8a · Browse files · 2 files+10 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+10 added−2 removed

  • tests:+8 added−0 removed
  • other:+2 added−2 removed

2 files+10 added−2 removed

modified:src/compile.c+2 added-2 removed
830 unmodified lines
831 block res_var = gen_op_var_fresh(STOREV, "reduce");832 block loop = BLOCK(gen_op_simple(DUPN),833 source,834 bind_alternation_matchers(matcher,835 BLOCK(gen_op_bound(LOADVN, res_var),835 BLOCK(gen_op_bound(LOADV, res_var),836 body,837 gen_op_bound(STOREV, res_var))),838 gen_op_simple(BACKTRACK));839 return BLOCK(gen_op_simple(DUP),
13 unmodified lines
853 // destructure the value into variable(s) for all the code854 // in the body to see855 bind_alternation_matchers(matcher,856 // load the loop state variable857 BLOCK(gen_op_bound(LOADVN, state_var),857 BLOCK(gen_op_bound(LOADV, state_var),858 // generate updated state859 update,860 // save the updated state for value extraction861 gen_op_simple(DUP),
539 unmodified lines
modified:tests/jq.test+8 added
320 unmodified lines
321[foreach .[] as {a:$a} (0; . + $a; -.)]322[{"a":1}, {"b":2}, {"a":3, "b":4}]323[-1, -1, -4]324325[foreach range(5) as $x (0; .+$x | select($x!=2); [$x,.])]326null327[[0,0],[1,1],[3,4],[4,8]]328329[limit(3; .[])]330[11,22,33,44,55,66,77,88,99]331[11,22,33]332
495 unmodified lines
828reduce . as $n (.; .)829null830null831832reduce range(5) as $x (0; .+$x | select($x!=2))833null8348835836# Destructuring837. as {$a, b: [$c, {$d}]} | [$a, $c, $d]838{"a":1, "b":[2,{"d":3}]}839[1,2,3]
1416 unmodified lines