jackson-jq
jackson-jq copied to clipboard
Different precedence of + operator than jq
Hi, first off thanks for creating jackson-jq, it is great.
I encountered this case where jackson-jq behaves differently than jq
>> jq --version
jq-1.6
>> echo '{"a": 1, "b": 2}' | jq -c '{"x": 10} + . as {"a": $a} | {"y": $a}'
{"x":10,"y":1}
>> echo '{"a": 1, "b": 2}' | java -jar /tmp/jackson-jq-cli-0.0.12.jar -c '{"x": 10} + . as {"a": $a} | {"y": $a}'
{"y":1}
I suspect the problem lies in difference operator precedence than jq has as when quoted like this, the output matches that of jq
>> echo '{"a": 1, "b": 2}' | java -jar /tmp/jackson-jq-cli-0.0.12.jar -c '{"x": 10} + (. as {"a": $a} | {"y": $a})'
{"x":10,"y":1}
The example seems contrived but I tried to use similar construct with real data when I encountered this problem and after some investigation this was the simplest example I could shrink it down to.
Thanks for reporting the issue. The presence of as $foo
seems to affect precedence of |
and other operators in jq (https://github.com/stedolan/jq/issues/1928):
$ jq -n '1 + 3 | (. * 2) # interpreted as (1 + 3) | (. * 2)
8
$ jq -n '1 + 3 as $a | ($a * 2) # interpreted as 1 + (3 as $a | ($a * 2))
7
whereas jackson-jq consistently interprets them as (1 + 3)
whether as $a
is used or not:
$ java -jar jackson-jq-cli-1.0.0-preview.20210610.jar -n '1 + 3 | (. * 2) # interpreted as (1 + 3) | (. * 2)
8
$ java -jar jackson-jq-cli-1.0.0-preview.20210610.jar -n '1 + 3 as $a | ($a * 2) # interpreted as (1 + 3) as $a | ($a * 2)
8
The issue is confirmed, but I'm yet to figure out what to do about it. While jackson-jq behaving differently than jq is definitely a bug in jackson-jq side, I'm a little hesitant to fix it because as $foo
affecting operator precedence in jq is counterintuive to me and the change might bring more confusion to current jackson-jq users (also I'm not sure how much work is needed to fix this).
In the meantime, let me update README and mention this in a list of compatibility issues for now as it'll probably take some time to resolve this anyway.