Hi Ishjaap, Thanks for the patch. I tested v1 (the version cfbot built) on top of master at debfb3c8a8c9.
- It applies cleanly, builds, and `make check` passes (243 of 243). - With the new regress tests copied onto an unpatched master build, the jsonpath test fails as expected. For example `(-$.a).abs() + 1` is printed as `(-$."a".abs() + 1)` on master and as `((-$."a").abs() + 1)` with the patch. - On master, `($.a + 1).abs() == 2` is printed as `($."a" + 1.abs() == 2)`, which cannot be read back (`trailing junk after numeric literal`). With the patch it prints `(($."a" + 1).abs() == 2)` and round trips. - I also ran 13 paths against 4 sample documents, comparing the result of the original path with the result of the printed path re-read as a jsonpath. On master 9 of the 52 comparisons differ (for example `(-$.a).abs() + 1` gives [3] originally and [-1] after the round trip). With the patch all 52 match. - Paths without an accessor print the same as on master (for example `$.a + 1` and `-$.a`). One small remark: in the new regress test, in the expected file of the patch, the row for `$ ? (!((-$.a).abs() == 2))` returns an empty result both before and after the reparse on the sample document, so it does not show a difference there. Another sample document, or a path that returns a value, would make it more useful. What I did not test: other platforms (only Linux, --without-icu), installcheck, and the TAP tests. My comparison used the diff that cfbot built (cfbot-7621.diff). I did not compare the v1 attachment (208684) with that diff byte by byte. Thanks again. Joao Rito On Tue, Nov 10, 2026 06:16 PM, Ishjaap Singh <[email protected]> wrote: > Hi hackers, > > When a jsonpath operation is followed by an accessor or method and is > itself an operand of another operator, the output function drops its > parentheses, so the printed path means something else: > > SELECT '(-$.a).abs() + 1'::jsonpath; > jsonpath > -------------------- > (-$."a".abs() + 1) > > For {"a": -2} the original path returns 3 and the printed one -1. > > Some printed forms can't be read back. '($.a + 1).abs() == 2' prints > as ($."a" + 1.abs() == 2), and on v15 and later reading that back fails > with "trailing junk after numeric literal". "!", "is unknown" and > "exists" followed by an accessor lose their outer parentheses even at > the top level: '(exists ($.a)).type()' prints as exists ($."a").type(), > which fails to parse on every version I tried. > > Since pg_dump and pg_get_viewdef() use this output, a view using such a > path returns different results after a dump and restore. I saw that on > master, 19rc1 and 14.24. A jsonpath column holding a value whose printed > form doesn't parse fails to restore: the COPY errors out and the table > comes back empty. With the example above that happens on master and > 19rc1; 14.24 accepts its printed form, but not the "exists" one. > > The missing parentheses around operations go back to when jsonpath was > added in v12. > > The attached patch parenthesizes any operation or predicate that has an > accessor after it, the same way printJsonPathItem() already handles > numeric literals. Output only changes for paths that print wrong today. > The new tests print each kind and compare query results before and > after a reparse; they fail without the code change. make check passes > and pgindent is clean. > > I found this with a small fuzzer that prints random jsonpath > expressions and parses the output again. On master, every mismatch it > found was this bug; with the patch, three runs of 20,000 random > expressions found none. > > The patch applies to master and REL_17 to REL_19. REL_14 to REL_16 need > small context changes; I can send versions for those if it's going to > be back-patched. > > Regards, > Ishjaap Singh >
