Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9c3d25e

Browse files
committed
Fix JSON_OBJECTAGG uniquefying bug
Commitf4fb45d contained a bug in removing items with null values whenunique keys are required, where the leading items that are sortedcontained such values. Fix that and add a test for it.Discussion:https://postgr.es/m/CAJA4AWQ_XbSmsNbW226UqNyRLJ+wb=iQkQMj77cQyoNkqtf=2Q@mail.gmail.com
1 parent5c854e7 commit9c3d25e

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 ABSENTONNULL WITH UNIQUE KEYS RETURNING jsonb)
282282
FROM (VALUES (1,1), (1,NULL), (2,2)) foo(k, v);
283283

284+
SELECT JSON_OBJECTAGG(k: v ABSENTONNULL 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)

[8]ページ先頭

©2009-2025 Movatter.jp