Re: BUG #16828: duplicate results when using ** recursive expression in JSON path

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: shammat@gmx.net
Cc: Alexander Korotkov <aekorotkov@gmail.com>, pgsql-bugs@lists.postgresql.org
Date: 2021-01-17T19:42:01Z
Lists: pgsql-bugs
PG Bug reporting form <noreply@postgresql.org> writes:
> The following query 

>   select jsonb_path_query_array(col, '$.**.itemName')
>   from (
>     values ('{"items": [{"itemName": "a", "items": [{"itemName":
> "b"}]}]}'::jsonb)
>   ) as t(col) 

> returns ["a", "a", "b", "b"] but should return only ["a", "b"] as each value
> only appears once in the JSON value. 

jsonpath_exec.c is a muddle of undocumented code, but I think
I found where the duplication is occurring: in executeAnyItem(),
the first occurrence comes out while recursing down from the
executeItemOptUnwrapTarget call at line 1419 (as of HEAD), and then
the duplicate comes out while recursing down from the executeAnyItem
call at line 1439.  So I'd say that that logic for
"ignoreStructuralErrors" needs a rethink.  (BTW, why is half of this
code relying on cxt->ignoreStructuralErrors while the other half
receives a passed-down flag?  That seems like a recipe for bugs,
especially with the lack of commentary about the reason for it.)

Alexander, git blame says all this code is your fault ...

			regards, tom lane



Commits

  1. Document behavior of the .** jsonpath accessor in the lax mode