You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
last/1 on an empty generator produces no output; jq produces null #349
first/1 agrees between the two implementations: first(empty) produces
no output in both (jq defines it via label/break). Only last differs.
In jq, last(g) is defined in src/builtin.jq as def last(g): reduce g as $item (null; $item);, so an empty generator
reduces to the initial value, null. gojq implements last/1 through its
internal _last function, which appears to emit nothing when the generator
is empty.
This difference is not mentioned in the README's "Difference to jq"
section, so I am reporting it in case it is unintended.
Disclosure: this investigation was done with AI assistance (differential
testing of gojq against jq).
I've reproduced this on current main. last(empty) prints nothing, whereas jq 1.7 prints null. The same happens with last(range(0)) and last(.[]) on [].
The issue is in compileLast in compiler.go. When the generator yields no values, the $x flag stays true, causing the compiled code to backtrack without producing any output. jq defines last(g) as reduce g as $item (null; $item), so an empty generator reduces to null.
I'd like to take this one. My plan is to have compileLast push null and return instead of backtracking when no value was seen. I'll add CLI test cases for last(empty), [last(empty)], and last(range(0)), and update the existing empty-input last case to expect [null]. I'll check all of these against jq 1.7 and run the full test suite before opening the PR.
last(empty) should yield nothing. This issue and linked PR are truly awful. They don’t even bother to check the behavior of the latest release version of jq, nor do they use git blame to look into the history of the functions they've fixed. See 94dd99a (Why do I have to take my time to link this commit for you). If last(empty) can yield null, simpler definition using reduce is enough. Sigh. Honestly, wasting time on this AI-generated slop is just absolute bullshit. Close #350.
Activity
g4lb commentedon Nov 30, 2026
This matches jq 1.8.0, which changed
last(empty)to output nothing (#3179). jq 1.7 givesnull.daichunghy commentedon Nov 30, 2026
I've reproduced this on current main.
last(empty)prints nothing, whereas jq 1.7 printsnull. The same happens withlast(range(0))andlast(.[])on[].The issue is in
compileLastin compiler.go. When the generator yields no values, the$xflag stays true, causing the compiled code to backtrack without producing any output. jq defineslast(g)asreduce g as $item (null; $item), so an empty generator reduces tonull.I'd like to take this one. My plan is to have
compileLastpushnulland return instead of backtracking when no value was seen. I'll add CLI test cases forlast(empty),[last(empty)], andlast(range(0)), and update the existing empty-inputlastcase to expect[null]. I'll check all of these against jq 1.7 and run the full test suite before opening the PR.wader commentedon Nov 30, 2026
As jq is a generator based language it makes sense for functions like
lastto emit no values.itchyny commentedon Nov 30, 2026
last(empty) should yield nothing. This issue and linked PR are truly awful. They don’t even bother to check the behavior of the latest release version of jq, nor do they use
git blameto look into the history of the functions they've fixed. See 94dd99a (Why do I have to take my time to link this commit for you). If last(empty) can yield null, simpler definition using reduce is enough. Sigh. Honestly, wasting time on this AI-generated slop is just absolute bullshit. Close #350.