-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Deleting objects using walk has side effects #2584
Comments
I already told you why that is 3 days ago on IRC. The Fixing it is as simple as changing the implementation from: # Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys_unsorted[] as $key
( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f
elif type == "array" then map( walk(f) ) | f
else f
end; To: # It would be nice to have max/1, min/1, add/1, etc in jq by the way...
def add(f): reduce f as $i (null; . + $i);
# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys_unsorted[] as $key
({}; add(., ($in[$key] | walk(f) | { ($key): . }))) | f
elif type == "array" then map( walk(f) ) | f
else f
end; I also told you you can use del(.. | select(isempty(.. | scalars))) |
A version of Whether or not the
Now maybe that's also not "correct" but for now, that's the way it is. |
I agree that whether or not my proposed fix for
Anyway, your The real reason why that returns nothing is because the rhs of the You will note that: def foo($x): "hi";
[foo(1,2,3)], [foo(range(4))], [foo(empty)] returns ["hi","hi","hi"]
["hi","hi","hi","hi"]
[]
And that: { a: 1, b: [] } |
.b = (6,7) returns {"a":1,"b":6}
{"a":1,"b":7} If you use { a: 1, b: [] } |
.b |= empty outputs {"a":1} Since the function is not using The reason why the That, in combination with the undocumented quirk that { a: 1, b: 2, c: 3 } |
[ walk(select(. != 1)) ],
[ walk(select(. != 2)) ],
[ walk(select(. != 3)) ] {"b":2,"c":3}
{"c":3}
null Also note that, in $ # at the $i==5 iteration, xyz returns two arrays instead of one:
$ # the first is the input array with 5 appended
$ # the second is the input array with 15 appended
$ # at every other iteration it just appends $i to the input array
$ jq -cn 'reduce range(10) as $i ([]; . + ($i | ., (select(. == 5) + 10) | [.]) | debug)'
["DEBUG:",[0]]
["DEBUG:",[0,1]]
["DEBUG:",[0,1,2]]
["DEBUG:",[0,1,2,3]]
["DEBUG:",[0,1,2,3,4]]
["DEBUG:",[0,1,2,3,4,5]]
["DEBUG:",[0,1,2,3,4,15]]
["DEBUG:",[0,1,2,3,4,15,6]]
["DEBUG:",[0,1,2,3,4,15,6,7]]
["DEBUG:",[0,1,2,3,4,15,6,7,8]]
["DEBUG:",[0,1,2,3,4,15,6,7,8,9]]
[0,1,2,3,4,15,6,7,8,9]
$ # for the $i>=6 iterations it used the last array returned by xyz
$ # instead of maybe continuing to iterate for both values and return
$ # two arrays at the end:
$ # [0,1,2,3,4,5,6,7,8,9]
$ # [0,1,2,3,4,15,6,7,8,9]
Thanks a lot for pointing that out! I have noticed substantial performance problems in deeply recursive function, and e.g. |
@emanuele6 wrote:
It's true that the implementation of In other words, it is important to understand the consistency of jq's
and so on. In fact, all three of the major implementations of jq (the C
and
And similarly for As you observe, '|=' is quite different because it is path-oriented. |
Yeah, you said:
That sounds like you're telling me there's a bug. So I'm reporting it. Sorry I didn't follow up in IRC with you. |
Resolves jqlang#2584; see also jqlang#2611
commit 9afda0a Merge: 4665e81 eeb08b5 Author: pkoppstein <pkoppstein@gmail.com> Date: Mon Jul 31 07:33:58 2023 -0400 Merge branch 'walk' of https://github.com/pkoppstein/jq into walk commit 4665e81 Author: pkoppstein <pkoppstein@gmail.com> Date: Mon Jul 31 07:26:03 2023 -0400 revamp walk/1: rebase, add test cases Note that according to the new def: `{x:0} | walk(.,1)` is equivalent to: {x:0} | walk(.), walk(1) commit bdec9c0 Author: pkoppstein <pkoppstein@gmail.com> Date: Wed Jul 5 02:00:59 2023 -0400 revamp walk/1 Resolves jqlang#2584; see also jqlang#2611 commit c8e28da Author: itchyny <itchyny@cybozu.co.jp> Date: Mon Jul 31 09:52:52 2023 +0900 Redesign website (jqlang#2628) * Bump up Bootstrap to v5.3.1, Bootstrap Icon to v1.10.5. * Use autoComplete.js to drop dependency on jQuery and typeahead.js. * Support dark mode. * New svg logo and icon with responsive color mode support. * Normalize section ids to lower kebab-case for easiness of linking. * Use relative paths for links for local development (--root /output). * Various markup cleanups and accessibility improvements. commit 4af3f99 Author: Owen Ou <o@owenou.com> Date: Sat Jul 29 07:20:48 2023 -0700 Update `bug_report.md` with Discord link commit 82f7f77 Author: Owen Ou <o@owenou.com> Date: Sat Jul 29 07:15:57 2023 -0700 Redirect questions to Discord We now have an official Discord server and most maintainers are hanging out there. It would be a good idea to redirect questions to Discord. commit f733a15 Author: Nicolas Williams <nico@cryptonector.com> Date: Mon Jul 10 18:29:03 2023 -0500 Use -Wno-cast-function-type to quiet many warnings commit c8b30df Author: Nicolas Williams <nico@cryptonector.com> Date: Mon Jul 10 18:28:33 2023 -0500 Add JQ_FALLTHROUGH and use it to quiet warnings commit a6eb055 Author: Emanuele Torre <torreemanuele6@gmail.com> Date: Sat Jul 29 21:57:40 2023 +0200 Fix typo in manual: "-seq" => "--seq" commit ee2a215 Author: Owen Ou <169064+owenthereal@users.noreply.github.com> Date: Sat Jul 29 07:38:08 2023 -0700 Backfill with references in NEWS.md (jqlang#2788) Backfill with references to PRs & issues in NEWS.md commit eeb08b5 Author: pkoppstein <pkoppstein@gmail.com> Date: Wed Jul 5 02:00:59 2023 -0400 revamp walk/1 Resolves jqlang#2584; see also jqlang#2611
Resolves jqlang#2584; also resolves jqlang#2611 and supersedes jqlang#2655 Note that according to the revised implementation: `{x:0} | walk(.,1)` is equivalent to `{x:0} | walk(.), walk(1)`
Describe the bug
Deleting objects using walk seems to cause side effects. I'm not sure of the details, so I'll just have to provide an example.
To Reproduce
walk(select(IN({}, []) | not))
input:
Expected behavior
Actual behavior
Environment (please complete the following information):
This seems to be reproducible in every environment I've tried, though.
Additional context
In addition, if the input is the semantically equivalent:
then the output is as-expected.
The text was updated successfully, but these errors were encountered: