Skip to content

Commit 9c3d25e

Browse files
committed
Fix JSON_OBJECTAGG uniquefying bug
Commit f4fb45d contained a bug in removing items with null values when unique keys are required, where the leading items that are sorted contained such values. Fix that and add a test for it. Discussion: https://postgr.es/m/CAJA4AWQ_XbSmsNbW226UqNyRLJ+wb=iQkQMj77cQyoNkqtf=2Q@mail.gmail.com
1 parent 5c854e7 commit 9c3d25e

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/backend/utils/adt/jsonb_util.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,8 +1959,18 @@ uniqueifyJsonbObject(JsonbValue *object, bool unique_keys, bool skip_nulls)
19591959

19601960
if (hasNonUniq || skip_nulls)
19611961
{
1962-
JsonbPair *ptr = object->val.object.pairs + 1,
1963-
*res = object->val.object.pairs;
1962+
JsonbPair *ptr, *res;
1963+
1964+
while (skip_nulls && object->val.object.nPairs > 0 &&
1965+
object->val.object.pairs->value.type == jbvNull)
1966+
{
1967+
/* If skip_nulls is true, remove leading items with null */
1968+
object->val.object.pairs++;
1969+
object->val.object.nPairs--;
1970+
}
1971+
1972+
ptr = object->val.object.pairs + 1;
1973+
res = object->val.object.pairs;
19641974

19651975
while (ptr - object->val.object.pairs < object->val.object.nPairs)
19661976
{

src/test/regress/expected/sqljson.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,13 @@ ERROR: duplicate JSON object key value
944944
SELECT JSON_OBJECTAGG(k: v ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb)
945945
FROM (VALUES (1, 1), (1, NULL), (2, 2)) foo(k, v);
946946
ERROR: duplicate JSON object key value
947+
SELECT JSON_OBJECTAGG(k: v ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb)
948+
FROM (VALUES (1, 1), (0, NULL),(4, null), (5, null),(6, null),(2, 2)) foo(k, v);
949+
json_objectagg
950+
------------------
951+
{"1": 1, "2": 2}
952+
(1 row)
953+
947954
-- Test JSON_OBJECT deparsing
948955
EXPLAIN (VERBOSE, COSTS OFF)
949956
SELECT JSON_OBJECT('foo' : '1' FORMAT JSON, 'bar' : 'baz' RETURNING json);

src/test/regress/sql/sqljson.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ FROM (VALUES (1, 1), (1, NULL), (2, 2)) foo(k, v);
281281
SELECT JSON_OBJECTAGG(k: v ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb)
282282
FROM (VALUES (1, 1), (1, NULL), (2, 2)) foo(k, v);
283283

284+
SELECT JSON_OBJECTAGG(k: v ABSENT ON NULL WITH UNIQUE KEYS RETURNING jsonb)
285+
FROM (VALUES (1, 1), (0, NULL),(4, null), (5, null),(6, null),(2, 2)) foo(k, v);
286+
284287
-- Test JSON_OBJECT deparsing
285288
EXPLAIN (VERBOSE, COSTS OFF)
286289
SELECT JSON_OBJECT('foo' : '1' FORMAT JSON, 'bar' : 'baz' RETURNING json);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy