Re: jsonb contains behaviour weirdness - Mailing list pgsql-hackers

From Josh Berkus
Subject Re: jsonb contains behaviour weirdness
Date
Msg-id 541339B5.5030000@agliodbs.com
Whole thread Raw
In response to jsonb contains behaviour weirdness  (Alexander Korotkov <aekorotkov@gmail.com>)
Responses Re: jsonb contains behaviour weirdness  (Peter Geoghegan <pg@heroku.com>)
List pgsql-hackers
On 09/12/2014 06:40 AM, Alexander Korotkov wrote:
> Hi!
> 
> Let's consider some examples.
> 
> # select '[1,2]'::jsonb @> '[1,2,2]'::jsonb;
>  ?column?
> ----------
>  f
> (1 row)
> 
> One may think it's because second jsonb array contain two "2". So,
> contains takes care about count of equal elements.

JSONB arrays are allowed to have repleating elements.  It's keys which
are not allowed to repeat.

> 
> # select '[1,1,2]'::jsonb @> '[1,2,2]'::jsonb;
>  ?column?
> ----------
>  t
> (1 row)
> 
> But, it's not. Jsonb contains takes care only about length of array.

OK, now, that's messed up.

> 
> # select '[[1,2]]'::jsonb @> '[[1,2,2]]'::jsonb;
>  ?column?
> ----------
>  t
> (1 row)
> 
> Even more weird :)
> The reason why jsonb contains behaves so is check in the beginning
> of jsonb_contains. It makes fast check of jsonb type and elements count
> before calling JsonbDeepContains.
> 
> if (JB_ROOT_COUNT(val) < JB_ROOT_COUNT(tmpl) ||
> JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl))
> PG_RETURN_BOOL(false);
> 
> It's likely that "JB_ROOT_COUNT(val) < JB_ROOT_COUNT(tmpl)" should be
> checked only for objects, not arrays.

Yeah, agreed.


-- 
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com



pgsql-hackers by date:

Previous
From: Peter Geoghegan
Date:
Subject: Re: jsonb contains behaviour weirdness
Next
From: Abhijit Menon-Sen
Date:
Subject: Re: pgcrypto: PGP signatures