BUG #18265: jsonb comparison order is inconsistent

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: neil(dot)chen(at)protonbase(dot)io
Subject: BUG #18265: jsonb comparison order is inconsistent
Date: 2024-01-02 09:09:42
Message-ID: 18265-fd06fa2f9ad3d1db@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 18265
Logged by: neil chen
Email address: neil(dot)chen(at)protonbase(dot)io
PostgreSQL version: 14.1
Operating system: operating system irrelevant
Description:

As the docuemnt saied, for jsonb type, Object > Array > Boolean > Number >
String > Null, but actually empty json array is sort to be the smallest
value.
And this is true only when it is the outmost json value, that is,
'[]'::jsonb is less than 'null'::jsonb.
but when it is a inner element of json array, the comparison result will
changed, that is, '[[]]'::jsonb is greater than '[null]'.
This behavior looks strange, that maybe a mistake in
compareJsonbContainers().
/*
* This could be a "raw scalar" pseudo array. That's
* a special case here though, since we still want the
* general type-based comparisons to apply, and as far
* as we're concerned a pseudo array is just a scalar.
*/
if (va.val.array.rawScalar != vb.val.array.rawScalar)
// Here one is json array, and another is not json array, maybe we should
simply compare their type.
// And the comparison result should not be overwritten by the following if
statement.
res = (va.val.array.rawScalar) ? -1 : 1;
if (va.val.array.nElems != vb.val.array.nElems)
res = (va.val.array.nElems > vb.val.array.nElems) ? 1 : -1;
break;
postgres=# select * from
(values('null'::jsonb),('[]'::jsonb),('[null]'::jsonb),('[[]]'::jsonb)) as
t(j) order by j;
j
--------
[]
null
[null]
[[]]

Browse pgsql-bugs by date

  From Date Subject
Next Message Peter Eisentraut 2024-01-02 09:22:34 Re: BUG #18252: Assert in CheckOpSlotCompatibility() fails when recursive union filters tuples in non-recursive term
Previous Message Alexander Korotkov 2024-01-02 08:08:45 Re: BUG #18261: Inconsistent results of SELECT affected by joined subqueries