jsonb contains behaviour weirdness

From: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: jsonb contains behaviour weirdness
Date: 2014-09-12 13:40:29
Message-ID: CAPpHfdut22-=EbhO0zVaTzh1CHvwDhV=32NECowOatdaoTg2xA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

# 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.

# 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. Also, should JsonbDeepContains does
same fast check when it deals with nested objects?

------
With best regards,
Alexander Korotkov.

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Stephen Frost 2014-09-12 13:42:57 Re: expanded mode is still broken
Previous Message Tom Lane 2014-09-12 13:38:43 Re: Patch to support SEMI and ANTI join removal