Re: [PATCH] Fix jsonb comparison for raw scalar pseudo arrays
Re: [PATCH] Fix jsonb comparison for raw scalar pseudo arrays
Dear PostgreSQL Hackers,
Problem Description
I encountered an issue with the B-Tree ordering of `jsonb` values. According to the PostgreSQL documentation[1], the ordering should follow this precedence:
`Object > Array > Boolean > Number > String > Null`
However, empty arrays (`[]`) are currently considered smaller than `null`, which violates the documented rules. This occurs due to improper handling of the `rawScalar` flag when comparing arrays in the `compareJsonbContainers()` function in `src/backend/utils/adt/jsonb_util.c`.
I agree that this is a (10 year old) bug:
- if (va.val.array.nElems != vb.val.array.nElems)
+ else if (va.val.array.nElems != vb.val.array.nElems)
But I don't think we can fix it, because there could well be indexes that would no longer be valid if we change the sort order. Given that, I think the best we can do is adjust the documentation to mention the anomaly.
So the actual sort order as implemented is, AIUI,
Object > Non-Empty-Array > Boolean > Number > String > Null > Empty-Arraywhich is ugly, but fortunately not many apps rely on jsonb sort order.
cheers
andrew
-- Andrew Dunstan EDB: https://www.enterprisedb.com
Re: [PATCH] Fix jsonb comparison for raw scalar pseudo arrays
On Sun, Dec 8, 2024 at 10:58 PM Andrew Dunstan wrote: > > So the actual sort order as implemented is, AIUI, > > Object > Non-Empty-Array > Boolean > Number > String > Null > Empty-Array > > which is ugly, but fortunately not many apps rely on jsonb sort order. > > Nobody else has commented, so I propose to apply this patch documenting the anomaly. > while at it. we can fix the appearance of jsonb null. since select jsonb 'Null'; select jsonb 'NULL'; will fail. so maybe change Null in section and NULL to null
Re: [PATCH] Fix jsonb comparison for raw scalar pseudo arrays
On 2024-12-09 Mo 11:16 AM, jian he wrote: > On Mon, Dec 9, 2024 at 9:27 PM Yan Chengpeng wrote: >> Sorry, I uploaded the wrong file. I uploaded a new patch with the modified document. Please take a review. Thanks! >> >> > sorry. maybe i didn't mention it explicitly. > i mean something like: > > diff --git a/doc/src/sgml/json.sgml b/doc/src/sgml/json.sgml > index 54648c459c..d9b24e413e 100644 > --- a/doc/src/sgml/json.sgml > +++ b/doc/src/sgml/json.sgml > @@ -584,12 +584,13 @@ SELECT jdoc->'guid', jdoc->'name' FROM api WHERE > jdoc @@ '$.tags[*] == "qui"'; > The btree ordering for jsonb > datums is seldom > of great interest, but for completeness it is: > > -Object > Array >> Boolean > > Number > String >> Null > +Object > Array >> Boolean > > Number > String >> null > Object with n pairs > object > with n - 1 pairs > > Array with n elements > array > with n - 1 elements > > + with the exception that (for historical reasons) an empty array > sorts less than null. > Objects with equal numbers of pairs are compared in the order: > > key-1, value-1, > key-2 ... Pushed something along these lines. In master I also added a code comment so nobody might be tempted to "fix" the anomaly. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com
Re: [PATCH] Fix jsonb comparison for raw scalar pseudo arrays
Andrew Dunstan writes: > Pushed something along these lines. In master I also added a code > comment so nobody might be tempted to "fix" the anomaly. There is still a commitfest entry [1] pointing at this thread. Should it be closed as committed, or is there more to do? regards, tom lane [1] https://commitfest.postgresql.org/51/5394/
Re: [PATCH] Fix jsonb comparison for raw scalar pseudo arrays
On 2025-01-18 Sa 1:17 PM, Tom Lane wrote: > Andrew Dunstan writes: >> Pushed something along these lines. In master I also added a code >> comment so nobody might be tempted to "fix" the anomaly. > There is still a commitfest entry [1] pointing at this thread. > Should it be closed as committed, or is there more to do? > > regards, tom lane > > [1] https://commitfest.postgresql.org/51/5394/ Nope, that was it. I have marked the item as committed, thanks for noticing. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com