pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Erik Wienhold <ewie@ewie.name>
To: jian he <jian.universality@gmail.com>
To: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: ​jsonb @@ jsonpath operator doc: ​Only the first item of the result is taken into account
Date: Sun, 2 Apr 2023 08:40:14 -0700
Message-ID: <48ed779b-c405-ca4f-c2d1-5ecc0312d9a3@aklaver.com> (raw)
In-Reply-To: <202761461.525027.1680441711777@office.mailbox.org>
References: <CACJufxE01sxgvtG4QEvRZPzs_roggsZeVvBSGpjM5tzE5hMCLA@mail.gmail.com>
	<202761461.525027.1680441711777@office.mailbox.org>

On 4/2/23 06:21, Erik Wienhold wrote:
>> On 01/04/2023 08:02 CEST jian he <jian.universality@gmail.com> wrote:
>>
>> Hi,
>> https://www.postgresql.org/docs/current/functions-json.html
>>> jsonb @@ jsonpath → boolean
>>> Returns the result of a JSON path predicate check for the specified JSON
>>> value. Only the first item of the result is taken into account. If the
>>> result is not Boolean, then NULL is returned.
>>> '{"a":[1,2,3,4,5]}'::jsonb @@ '$.a[*] > 2' → t
>>
>> select jsonb_path_query('{"a":[1,2,3,4,5]}', '$.a[*]');
>> return
>>
>>> jsonb_path_query
>>> ------------------
>>>   1
>>>   2
>>>   3
>>>   4
>>>   5
>>> (5 rows)
>>
>> I don't understand:"Only the first item of the result is taken into account.".
>>
>> Here,JSON path predicate check for the specified JSON valuereturn true, some
>> return false. (1 > 2 is false, 2 > 2 is false).
> 
> The result is true if any array element matches the predicate because predicates
> are evaluated on sequences.  The documentation for executePredicate in
> src/backend/utils/adt/jsonpath_exec.c explains it:
> 
>> Predicates have existence semantics, because their operands are item
>> sequences.  Pairs of items from the left and right operand's sequences are
>> checked.  TRUE returned only if any pair satisfying the condition is found.
>> In strict mode, even if the desired pair has already been found, all pairs
>> still need to be examined to check the absence of errors.  If any error
>> occurs, UNKNOWN (analogous to SQL NULL) is returned.
> 
> https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/adt/jsonpath_exec.c;h...
> 
> Difference between using a predicate as path expression vs filter expression:
> 
> 	=# select jsonb_path_query('{"a":[1,2,3,4,5]}', '$.a[*] > 2');
> 	 jsonb_path_query
> 	------------------
> 	 true
> 	(1 row)
> 
> 	=# select jsonb_path_query('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ > 2)');
> 	 jsonb_path_query
> 	------------------
> 	 3
> 	 4
> 	 5
> 	(3 rows)
> 
> If you want the predicate result for each element, you must apply the predicate
> to the rows returned from jsonb_path_query:
> 
> 	=# select elem, elem::float > 2 as pred from jsonb_path_query('{"a":[1,2,3,4,5]}', '$.a[*]') elem;
> 	 elem | pred
> 	------+------
> 	 1    | f
> 	 2    | f
> 	 3    | t
> 	 4    | t
> 	 5    | t
> 	(5 rows)

That is a long way from:

jsonb @@ jsonpath → boolean

Returns the result of a JSON path predicate check for the specified JSON 
value. Only the first item of the result is taken into account. If the 
result is not Boolean, then NULL is returned.


> 
> --
> Erik
> 
> 

-- 
Adrian Klaver
adrian.klaver@aklaver.com






view thread (14+ messages)  latest in thread

Message-ID: <48ed779b-c405-ca4f-c2d1-5ecc0312d9a3@aklaver.com>
Permalink:  ../48ed779b-c405-ca4f-c2d1-5ecc0312d9a3@aklaver.com/
Also on:    postgresql.org/message-id/48ed779b-c405-ca4f-c2d1-5ecc0312d9a3@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, ewie@ewie.name, jian.universality@gmail.com
  Subject: Re: ​jsonb @@ jsonpath operator doc: ​Only the first item of the result is taken into account
  In-Reply-To: <48ed779b-c405-ca4f-c2d1-5ecc0312d9a3@aklaver.com>
  References: <CACJufxE01sxgvtG4QEvRZPzs_roggsZeVvBSGpjM5tzE5hMCLA@mail.gmail.com> <202761461.525027.1680441711777@office.mailbox.org> <48ed779b-c405-ca4f-c2d1-5ecc0312d9a3@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox