From 61f3d28f0ebe16491dec7e8f504c6ea0c4697804 Mon Sep 17 00:00:00 2001 From: Abed Alawiyeh Date: Sat, 5 Dec 2026 22:21:29 +0000 Subject: [PATCH v1 1/2] Resolve negative json path subscripts only for the array on the path get_array_start() converted a negative subscript in a json path to a non-negative one by modifying path_indexes in place, for the first array it met at that level of nesting, whether or not that array was on the path. A later array at the same level, including the one that was actually on the path, was then compared with the converted subscript. So for example '{"x": [1, 2, 3], "a": [4, 5]}'::json #> '{a,-1}' returned NULL instead of 5. #>, #>>, json_extract_path() and json_extract_path_text() were affected; jsonb was not. Fix by resolving the subscript only when the array is on the path, and by keeping the result in a separate per-level array, so that the subscript given by the caller is left intact. Discussion: https://postgr.es/m/19970-ce2ab938d31adac4@postgresql.org --- src/backend/utils/adt/jsonfuncs.c | 26 +++++++++++++++++++------- src/test/regress/expected/json.out | 19 +++++++++++++++++++ src/test/regress/sql/json.sql | 4 ++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index 25f14f92..ed2617d6 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -97,6 +97,8 @@ typedef struct GetState bool *pathok; /* is path matched to current depth? */ int *array_cur_index; /* current element index at each path * level */ + int *array_target_index; /* path_indexes, with negative subscripts + * resolved for the current array */ } GetState; /* state for json_array_length */ @@ -1120,6 +1122,7 @@ get_worker(text *json, state->path_indexes = ipath; state->pathok = palloc0_array(bool, npath); state->array_cur_index = palloc_array(int, npath); + state->array_target_index = palloc_array(int, npath); if (npath > 0) state->pathok[0] = true; @@ -1299,12 +1302,18 @@ get_array_start(void *state) if (lex_level < _state->npath) { + int target = _state->path_indexes[lex_level]; + /* Initialize counting of elements in this array */ _state->array_cur_index[lex_level] = -1; - /* INT_MIN value is reserved to represent invalid subscript */ - if (_state->path_indexes[lex_level] < 0 && - _state->path_indexes[lex_level] != INT_MIN) + /* + * A negative subscript counts from the end of this array, so resolve + * it for this array only, and only if the array is on the path. Keep + * path_indexes intact for any other array at this level. INT_MIN + * value is reserved to represent invalid subscript. + */ + if (_state->pathok[lex_level] && target < 0 && target != INT_MIN) { /* Negative subscript -- convert to positive-wise subscript */ JsonParseErrorType error; @@ -1314,9 +1323,10 @@ get_array_start(void *state) if (error != JSON_SUCCESS) json_errsave_error(error, _state->lex, NULL); - if (-_state->path_indexes[lex_level] <= nelements) - _state->path_indexes[lex_level] += nelements; + if (-target <= nelements) + target += nelements; } + _state->array_target_index[lex_level] = target; } else if (lex_level == 0 && _state->npath == 0) { @@ -1363,7 +1373,8 @@ get_array_element_start(void *state, bool isnull) if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_indexes != NULL && - _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1]) + _state->array_cur_index[lex_level - 1] == + _state->array_target_index[lex_level - 1]) { if (lex_level < _state->npath) { @@ -1408,7 +1419,8 @@ get_array_element_end(void *state, bool isnull) if (lex_level <= _state->npath && _state->pathok[lex_level - 1] && _state->path_indexes != NULL && - _state->array_cur_index[lex_level - 1] == _state->path_indexes[lex_level - 1]) + _state->array_cur_index[lex_level - 1] == + _state->array_target_index[lex_level - 1]) { if (lex_level < _state->npath) { diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out index 461c8781..6bdbeabb 100644 --- a/src/test/regress/expected/json.out +++ b/src/test/regress/expected/json.out @@ -1261,6 +1261,25 @@ select '42'::json #> array['0']; (1 row) +-- a negative subscript counts from the end of the array on the path only +select '{"x": [1, 2, 3], "a": [4, 5]}'::json #> array['a','-1']; + ?column? +---------- + 5 +(1 row) + +select '{"x": [1, 2, 3], "a": [4, 5]}'::json #>> array['a','-2']; + ?column? +---------- + 4 +(1 row) + +select '[[1, 2, 3], [4, 5]]'::json #> array['1','-1']; + ?column? +---------- + 5 +(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 dc880460..d7e53f73 100644 --- a/src/test/regress/sql/json.sql +++ b/src/test/regress/sql/json.sql @@ -390,6 +390,10 @@ select '[{"b": "c"}, {"b": null}]'::json #> array['1','b']; select '"foo"'::json #> array['z']; select '42'::json #> array['f2']; select '42'::json #> array['0']; +-- a negative subscript counts from the end of the array on the path only +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']; select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}'; select '[1,2,3]'::json #>> '{}'; -- 2.39.5