From fc47b5d91f31e6c1092ba6222921df3158b1bc16 Mon Sep 17 00:00:00 2001 From: Ishjaap Singh Date: Mon, 30 Nov 2026 11:56:43 +0000 Subject: [PATCH v1] Fix jsonpath LAST after an array subscript inside a filter An array subscript sets innermostArraySize for LAST in the subscripts that follow and restores it afterwards, but two exits skipped the restore: the early return taken when no result is wanted (as inside exists()), and the out-of-bounds error when errors are suppressed. So a subscript inside a filter predicate left its own array's size behind, and a LAST later in the same subscript list used it: select jsonb_path_query_array('[10, 20, 30]', '$[0 ? (exists($x[0])), last]', '{"x": [1, 2]}'); returned [10, 20] instead of [10, 30]. Restore innermostArraySize on both paths. --- src/backend/utils/adt/jsonpath_exec.c | 10 +++++++++- src/test/regress/expected/jsonb_jsonpath.out | 19 +++++++++++++++++++ src/test/regress/sql/jsonb_jsonpath.sql | 4 ++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c index 8a2a41ebb0e..c5729f50a03 100644 --- a/src/backend/utils/adt/jsonpath_exec.c +++ b/src/backend/utils/adt/jsonpath_exec.c @@ -980,9 +980,13 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, (index_from < 0 || index_from > index_to || index_to >= size)) + { + /* the error may be suppressed, so restore first */ + cxt->innermostArraySize = innermostArraySize; RETURN_ERROR(ereport(ERROR, (errcode(ERRCODE_INVALID_SQL_JSON_SUBSCRIPT), errmsg("jsonpath array subscript is out of bounds")))); + } if (index_from < 0) index_from = 0; @@ -1010,7 +1014,11 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, } if (!hasNext && !found) - return jperOk; + { + /* innermostArraySize must be restored first */ + res = jperOk; + break; + } res = executeNextItem(cxt, jsp, &elem, v, found); diff --git a/src/test/regress/expected/jsonb_jsonpath.out b/src/test/regress/expected/jsonb_jsonpath.out index 56aea472ba1..dbb803f214f 100644 --- a/src/test/regress/expected/jsonb_jsonpath.out +++ b/src/test/regress/expected/jsonb_jsonpath.out @@ -479,6 +479,25 @@ select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "string")]', silent => ------------------ (0 rows) +-- last must not see the size of an array subscripted inside a filter +select jsonb_path_query_array('[10, 20, 30]', '$[0 ? (exists($x[0])), last]', '{"x": [1, 2]}'); + jsonb_path_query_array +------------------------ + [10, 30] +(1 row) + +select jsonb_path_query_array('{"a": [1, 2, 3], "b": [7]}', '$.a[0 ? (exists($.b[0])), last]'); + jsonb_path_query_array +------------------------ + [1, 3] +(1 row) + +select jsonb_path_query_array('[10, 20, 30]', 'strict $[0 ? (exists($x[5]) || @ == 0), last]', '{"x": [1, 2]}'); + jsonb_path_query_array +------------------------ + [10, 30] +(1 row) + select * from jsonb_path_query('{"a": 10}', '$'); jsonb_path_query ------------------ diff --git a/src/test/regress/sql/jsonb_jsonpath.sql b/src/test/regress/sql/jsonb_jsonpath.sql index 136b439b596..11d87f8f476 100644 --- a/src/test/regress/sql/jsonb_jsonpath.sql +++ b/src/test/regress/sql/jsonb_jsonpath.sql @@ -95,6 +95,10 @@ select jsonb_path_query('[1,2,3]', '$[last - 1]'); select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "number")]'); select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "string")]'); select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "string")]', silent => true); +-- last must not see the size of an array subscripted inside a filter +select jsonb_path_query_array('[10, 20, 30]', '$[0 ? (exists($x[0])), last]', '{"x": [1, 2]}'); +select jsonb_path_query_array('{"a": [1, 2, 3], "b": [7]}', '$.a[0 ? (exists($.b[0])), last]'); +select jsonb_path_query_array('[10, 20, 30]', 'strict $[0 ? (exists($x[5]) || @ == 0), last]', '{"x": [1, 2]}'); select * from jsonb_path_query('{"a": 10}', '$'); select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)'); -- 2.43.0