Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different precedence of + operator than jq #72

Open
wheene opened this issue Feb 25, 2021 · 1 comment
Open

Different precedence of + operator than jq #72

wheene opened this issue Feb 25, 2021 · 1 comment
Labels
Milestone

Comments

@wheene
Copy link
Contributor

wheene commented Feb 25, 2021

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.

@eiiches
Copy link
Owner

eiiches commented Aug 8, 2021

Thanks for reporting the issue. The presence of as $foo seems to affect precedence of | and other operators in jq (jqlang/jq#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.

@eiiches eiiches added this to the 1.0.0 milestone Sep 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants