public inbox for [email protected]  
help / color / mirror / Atom feed
From: Erik Wienhold <[email protected]>
To: David E. Wheeler <[email protected]>
Cc: [email protected]
Subject: Re: JSON Path and GIN Questions
Date: Sun, 17 Sep 2023 00:13:56 +0200 (CEST)
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
	<[email protected]>
	<[email protected]>

On 16/09/2023 22:26 CEST David E. Wheeler <[email protected]> wrote:

> I’ve started work on this; there’s so much to learn! Here’s a new example
> that surprised me a bit. Using the GPS tracker example from the docs [1]
> loaded into a `:json` psql variable, this output of this query makes perfect
> sense to me:
>
> david=# select jsonb_path_query(:'json', '$.track.segments.location[*] ? (@ < 14)');
>  jsonb_path_query
> ------------------
>  13.4034
>  13.2635
>
> Because `[*]` selects all the values. This, however, I did not expect:
>
> david=# select jsonb_path_query(:'json', '$.track.segments.location ? (@[*] < 14)');
>  jsonb_path_query
> ------------------
>  13.4034
>  13.2635
> (2 rows)
>
> I had expected it to return two single-value arrays, instead:
>
>  [13.4034]
>  [13.2635]
>
> It appears that the filter expression is doing some sub-selection, too.
> Is that expected?

Looks like the effect of lax mode which may unwrap arrays when necessary [1].
The array unwrapping looks like the result of jsonb_array_elements().

It kinda works in strict mode:

	SELECT jsonb_path_query(:'json', 'strict $.track.segments[*].location ? (@[*] < 14)');
	
	   jsonb_path_query
	-----------------------
	 [47.763, 13.4034]
	 [47.706, 13.2635]
	(2 rows)

But it does not remove elements from the matching arrays.  Which I don't even
expect here because the path specifies the location array as the object to be
returned.  The filter expression then only decides whether to return the
location array or not.  Nowhere in the docs does it say that the filter
expression itself removes any elements from a matched array.

Here's a query that filter's out individual array elements.  It's quite a
mouthful (especially to preserve the order of array elements):

	WITH location AS (
	  SELECT loc, row_number() OVER () AS array_num
	  FROM jsonb_path_query(:'json', 'strict $.track.segments[*].location') loc
	),
	element AS (
	  SELECT array_num, e.num AS elem_num, e.elem
	  FROM location
	    CROSS JOIN jsonb_array_elements(loc) WITH ORDINALITY AS e (elem, num)
	)
	SELECT jsonb_agg(elem ORDER BY elem_num)
	FROM element
	WHERE jsonb_path_exists(elem, '$ ? (@ < 14)')
	GROUP BY array_num;
	
	   jsonb_agg
	---------------
	 [13.2635]
	 [13.4034]
	(2 rows)

[1] https://www.postgresql.org/docs/current/functions-json.html#STRICT-AND-LAX-MODES

--
Erik






view thread (11+ messages)  latest in thread

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 using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: JSON Path and GIN Questions
  In-Reply-To: <[email protected]>

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

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