BUG #16489: Cannot strictly compare arrays in jsonb_path_query call that uses vars

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: jeremydahan(at)protonmail(dot)com
Subject: BUG #16489: Cannot strictly compare arrays in jsonb_path_query call that uses vars
Date: 2020-06-11 02:29:49
Message-ID: 16489-d3b890452bd13422@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 16489
Logged by: Dahan Jeremy
Email address: jeremydahan(at)protonmail(dot)com
PostgreSQL version: 12.1
Operating system: macOS 10.14
Description:

Hi,
I found this very strange behavior of the jsonb_path_query function :

Example :
```
SELECT jsonb_path_query('{"a":["b","c"]}'::jsonb, '$.a ? (@ == $d)' ,
'{"d":["b","c","e"]}'::jsonb)
```
When using the jsonb_path_query function with a vars argument that includes
an array (here with key d), I cannot compare the queried array against the
vars array.
Instead, I get a per element comparison, which means instead of returning
["b","c"]

the query returns
"b"
"c"

just like the query
```
SELECT jsonb_path_query('{"a":["b","c"]}'::jsonb, '$.a[*] ? (@ == $d)' ,
'{"d":["b","c","e"]}'::jsonb)
```
(notice the $.a[*] instead of the $.a)

Interestingly, this means that the following query also returns the same
result
```
SELECT jsonb_path_query('{"a":["b","c"]}'::jsonb, '$.a[*] ? (@ != $d)' ,
'{"d":["b","c","e"]}'::jsonb)
```
(notice the != instead of the ==)

I could not find mentions of this strange behavior in the documentation, and
I would love to know if there is a way of comparing an array against another
array in a jsonb_path_query function call (without using a WHERE clause).
By the way, the current behavior can be useful for finding intersections of
arrays.

Thank you for this truly incredible project

Best regards,

Jeremy Dahan

Browse pgsql-bugs by date

  From Date Subject
Next Message Kyle Kingsbury 2020-06-11 03:14:42 Re: Potential G2-item cycles under serializable isolation
Previous Message Peter Geoghegan 2020-06-11 01:10:25 Re: Potential G2-item cycles under serializable isolation