From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Brian Mendoza <brian@rotamap.net>
To: pgsql-general@postgresql.org
Subject: Re: JSONB operator unanticipated behaviour
Date: Thu, 18 May 2023 07:41:16 -0700
Message-ID: <c8663245-c568-9071-817b-06a288fd4e0d@aklaver.com> (raw)
In-Reply-To: <CALWFwZ0GG_5fUCXfLMTMSZjO0VnrL=r2sHPJ_9Sr1623_5MP=w@mail.gmail.com>
References: <CALWFwZ0GG_5fUCXfLMTMSZjO0VnrL=r2sHPJ_9Sr1623_5MP=w@mail.gmail.com>
On 5/18/23 06:17, Brian Mendoza wrote:
> Hello,
>
> I have encountered unanticipated behaviour with a JSONB operator, and
> wanted to make sure I am not misunderstanding its intended use.
>
> When using the @> operator, I get this result.
>
> select '{"a": [1]}'::jsonb @> '{"a": []}'::jsonb;
> ?column?
> ----------
> t
> (1 row)
>
> However, given the phrasing in the documentation, I would have expected
> False.
>
> "Does the left JSON value contain the right JSON path/value entries at
> the top level?"
>
> Particularly given the following:
>
> select '[1]'::jsonb = '[]'::jsonb;
> ?column?
> ----------
> f
> (1 row)
>
> So the keys are the same, the values (when compared directly) are not,
> but @> returns True. Have I misunderstood the usage of the operator?
>
> The above queries have been run on postgres 14, if that helps.
Have you looked at the containment examples?:
https://www.postgresql.org/docs/current/datatype-json.html#JSON-CONTAINMENT
I'm thinking this:
-- A top-level key and an empty object is contained:
SELECT '{"foo": {"bar": "baz"}}'::jsonb @> '{"foo": {}}'::jsonb;
and/or this
"The general principle is that the contained object must match the
containing object as to structure and data contents, possibly after
discarding some non-matching array elements or object key/value pairs
from the containing object. "
applies.
>
> --
> Brian Mendoza
> brian@rotamap.net <mailto:brian@rotamap.net>
>
> Rotamap
> www.rotamap.net <https://www.rotamap.net;
> 020 7631 1555
> 3 Tottenham Street London W1T 2AF
> Registered in England No. 04551928
--
Adrian Klaver
adrian.klaver@aklaver.com
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, brian@rotamap.net
Subject: Re: JSONB operator unanticipated behaviour
In-Reply-To: <c8663245-c568-9071-817b-06a288fd4e0d@aklaver.com>
References: <CALWFwZ0GG_5fUCXfLMTMSZjO0VnrL=r2sHPJ_9Sr1623_5MP=w@mail.gmail.com> <c8663245-c568-9071-817b-06a288fd4e0d@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