Re: Shouldn't jsonpath .string() Unwrap?

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Jeevan Chalke <jeevan(dot)chalke(at)enterprisedb(dot)com>
Subject: Re: Shouldn't jsonpath .string() Unwrap?
Date: 2024-06-13 19:53:17
Message-ID: 39703ed0-2cc2-4e6b-9488-3725f14f609d@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On 2024-06-12 We 16:02, David G. Johnston wrote:
> On Sat, Jun 8, 2024 at 3:50 PM David E. Wheeler
> <david(at)justatheory(dot)com> wrote:
>
> Hackers,
>
> Most of the jsonpath methods auto-unwrap in lax mode:
>
> david=# select jsonb_path_query('[-2,5]', '$.abs()');
>  jsonb_path_query
> ------------------
>  2
>  5
> (2 rows)
>
> The obvious exceptions are size() and type(), which apply directly
> to arrays, so no need to unwrap:
>
> david=# select jsonb_path_query('[-2,5]', '$.size()');
>  jsonb_path_query
> ------------------
>  2
> (1 row)
>
> david=# select jsonb_path_query('[-2,5]', '$.type()');
>  jsonb_path_query
> ------------------
>  "array"
>
> But what about string()? Is there some reason it doesn’t unwrap?
>
> david=# select jsonb_path_query('[-2,5]', '$.string()');
> ERROR:  jsonpath item method .string() can only be applied to a
> bool, string, numeric, or datetime value
>
> What I expect:
>
> david=# select jsonb_path_query('[-2,5]', '$.string()');
>  jsonb_path_query
> —————————
>  "2"
>  "5"
> (2 rows)
>
> However, I do see a test[1] for this behavior, so maybe there’s a
> reason for it?
>
>
> Adding Andrew.
>
> I'm willing to call this an open item against this feature as I don't
> see any documentation explaining that string() behaves differently
> than the others.
>
>

Hmm. You might be right. Many of these items have this code, but the
string() branch does not:

if (unwrap && JsonbType(jb) == jbvArray)
    return executeItemUnwrapTargetArray(cxt, jsp, jb, found,
                                        false);

cheers

andrew

--
Andrew Dunstan
EDB:https://www.enterprisedb.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2024-06-13 20:04:04 Re: RFC: adding pytest as a supported test framework
Previous Message Nathan Bossart 2024-06-13 19:48:11 improve predefined roles documentation