From 60192d372af14cad71c1c1937e53fd95babfb3e2 Mon Sep 17 00:00:00 2001 From: Abed Alawiyeh Date: Sat, 5 Dec 2026 22:10:13 +0000 Subject: [PATCH v1 2/2] Use the last duplicate key at every level of json path extraction For json values with duplicate object keys, the processing functions consider the last value as the operative one, as documented, and a chain of -> operators does so. But #>, #>>, json_extract_path() and json_extract_path_text() kept a value found under an earlier duplicate of an intermediate key when the later duplicate did not contain the rest of the path. For example '{"a": {"b": 1}, "a": {"c": 2}}'::json #> '{a,b}' returned 1, while -> 'a' -> 'b' and the jsonb equivalent return NULL. Fix by discarding any result found so far whenever an intermediate key of the path matches again. Reported-by: Yibo Dong Discussion: https://postgr.es/m/19970-ce2ab938d31adac4@postgresql.org --- src/backend/utils/adt/jsonfuncs.c | 6 +++ src/test/regress/expected/json.out | 61 ++++++++++++++++++++++++++++++ src/test/regress/sql/json.sql | 11 ++++++ 3 files changed, 78 insertions(+) diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index ed2617d6..7eab3029 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -1213,6 +1213,12 @@ get_object_field_start(void *state, char *fname, bool isnull) { /* if not at end of path just mark path ok */ _state->pathok[lex_level] = true; + + /* + * This object overrides any previous object with the same key, so + * forget anything found inside an earlier one. + */ + _state->tresult = NULL; } else { diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out index 6bdbeabb..5a703a65 100644 --- a/src/test/regress/expected/json.out +++ b/src/test/regress/expected/json.out @@ -1280,6 +1280,67 @@ select '[[1, 2, 3], [4, 5]]'::json #> array['1','-1']; 5 (1 row) +-- with duplicate keys, the last value is used, as with -> +select '{"a": {"b": 1}, "a": {"c": 2}}'::json #> array['a','b']; + ?column? +---------- + +(1 row) + +select '{"a": {"b": 1}, "a": {"b": 2}}'::json #> array['a','b']; + ?column? +---------- + 2 +(1 row) + +select '{"a": {"b": 1}, "a": 5}'::json #> array['a','b']; + ?column? +---------- + +(1 row) + +select '{"a": {"b": {"c": 1}}, "a": {"b": {"d": 2}}}'::json #> array['a','b','c']; + ?column? +---------- + +(1 row) + +select '{"a": [1, 2], "a": [3]}'::json #> array['a','1']; + ?column? +---------- + +(1 row) + +select '{"a": [1, 2, 3], "a": [4]}'::json #> array['a','-1']; + ?column? +---------- + 4 +(1 row) + +select '{"a": [4], "a": [1, 2, 3]}'::json #> array['a','-1']; + ?column? +---------- + 3 +(1 row) + +select '{"a": {"b": 1}, "a": {"c": 2}}'::json #>> array['a','b']; + ?column? +---------- + +(1 row) + +select json_extract_path('{"a": {"b": 1}, "a": {"c": 2}}', 'a', 'b'); + json_extract_path +------------------- + +(1 row) + +select json_extract_path_text('{"a": {"b": "x"}, "a": {"b": "y"}}', 'a', 'b'); + json_extract_path_text +------------------------ + y +(1 row) + select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}'; ?column? --------------------------- diff --git a/src/test/regress/sql/json.sql b/src/test/regress/sql/json.sql index d7e53f73..9b0d799d 100644 --- a/src/test/regress/sql/json.sql +++ b/src/test/regress/sql/json.sql @@ -394,6 +394,17 @@ select '42'::json #> array['0']; select '{"x": [1, 2, 3], "a": [4, 5]}'::json #> array['a','-1']; select '{"x": [1, 2, 3], "a": [4, 5]}'::json #>> array['a','-2']; select '[[1, 2, 3], [4, 5]]'::json #> array['1','-1']; +-- with duplicate keys, the last value is used, as with -> +select '{"a": {"b": 1}, "a": {"c": 2}}'::json #> array['a','b']; +select '{"a": {"b": 1}, "a": {"b": 2}}'::json #> array['a','b']; +select '{"a": {"b": 1}, "a": 5}'::json #> array['a','b']; +select '{"a": {"b": {"c": 1}}, "a": {"b": {"d": 2}}}'::json #> array['a','b','c']; +select '{"a": [1, 2], "a": [3]}'::json #> array['a','1']; +select '{"a": [1, 2, 3], "a": [4]}'::json #> array['a','-1']; +select '{"a": [4], "a": [1, 2, 3]}'::json #> array['a','-1']; +select '{"a": {"b": 1}, "a": {"c": 2}}'::json #>> array['a','b']; +select json_extract_path('{"a": {"b": 1}, "a": {"c": 2}}', 'a', 'b'); +select json_extract_path_text('{"a": {"b": "x"}, "a": {"b": "y"}}', 'a', 'b'); select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}'; select '[1,2,3]'::json #>> '{}'; -- 2.39.5