Re: BUG #17440: not expected result from jsonb_path_query

From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: edouard(dot)hibon(at)free(dot)fr, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17440: not expected result from jsonb_path_query
Date: 2022-03-24 22:40:27
Message-ID: CAPpHfdsjdwArnbGoChhehd_d_T16of7APhpigK0MhZ13xMp1Pw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi!

On Tue, Mar 15, 2022 at 8:34 PM PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 17440
> Logged by: Edouard HIBON
> Email address: edouard(dot)hibon(at)free(dot)fr
> PostgreSQL version: 14.0
> Operating system: windows 10
> Description:
>
> Query 1 : SELECT jsonb_path_query('[1,2,3]' :: jsonb, '$') results in 1
> value '[1,2,3]' :: jsonb as expected
> Query 2 : SELECT jsonb_path_query('[1,2,3]' :: jsonb, '$[*]') results in a
> set of 3 values '1', '2', '3' as expected
> Query 3 : SELECT jsonb_path_query('[1,2,3]' :: jsonb, '$[*] ? (@ <> null)')
> results in a set of 3 values '1', '2', '3' as expected
> Query 4 : SELECT jsonb_path_query('[1,2,3]' :: jsonb, '$ ? (@ <> null)')
> results in a set of 3 values '1', '2', '3' as for the queries 2 & 3 whereas
> I would expect only one value '[1,2,3]' as for the query 1 because there is
> no member accessor nor array element accessor in this jsonpath, and @ should
> represent the initial jsonb value being queried $
>
> demo :
> https://dbfiddle.uk/?rdbms=postgres_14&fiddle=f09164502df5ee1bb620057689e6f810

The default jsonpath mode is lax. Lax mode automatically unwraps
arrays. Unwrapping array in filter affects the result value as well.
Strict mode works as you expected.

SELECT jsonb_path_query('[1,2,3]' :: jsonb, 'strict $ ? (@ <> null)')
[1, 2, 3]

My personal opinion is that lax mode is generally prone to
javascript-style weirdness, and one should always use strict.

------
Regards,
Alexander Korotkov

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2022-03-25 07:52:57 BUG #17448: In Windows 10, version 1703 and later, huge_pages doesn't work.
Previous Message PG Bug reporting form 2022-03-24 19:09:33 BUG #17447: uninstaller fails