v11-0001-misc-refactoring-based-on-v11_01_to_06.no-cfbot

application/octet-stream

Filename: v11-0001-misc-refactoring-based-on-v11_01_to_06.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: SQL:2023 JSON simplified accessor support
From c038cc5456a36af8bf221a6dad4d83be4a394c0b Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Wed, 25 Jun 2025 13:31:56 +0800
Subject: [PATCH v11 1/1] misc refactoring based on v11_01_to_06

---
 src/backend/utils/adt/jsonbsubs.c   | 33 ++++++++++-------------------
 src/test/regress/expected/jsonb.out |  4 ++--
 2 files changed, 13 insertions(+), 24 deletions(-)

diff --git a/src/backend/utils/adt/jsonbsubs.c b/src/backend/utils/adt/jsonbsubs.c
index 3588a1d062f..cf04d6fe14a 100644
--- a/src/backend/utils/adt/jsonbsubs.c
+++ b/src/backend/utils/adt/jsonbsubs.c
@@ -110,12 +110,6 @@ coerce_jsonpath_subscript(ParseState *pstate, Node *subExpr, Oid numtype)
 						  COERCION_IMPLICIT,
 						  COERCE_IMPLICIT_CAST,
 						  -1);
-	if (subExpr == NULL)
-		ereport(ERROR,
-				(errcode(ERRCODE_DATATYPE_MISMATCH),
-				 errmsg("jsonb subscript must have text type"),
-				 parser_errposition(pstate, exprLocation(subExpr))));
-
 	return subExpr;
 }
 
@@ -196,30 +190,22 @@ static JsonPathParseItem *
 make_jsonpath_item_expr(ParseState *pstate, Node *expr, List **exprs)
 {
 	Const	   *cnst;
+	int32		val;
 
 	expr = transformExpr(pstate, expr, pstate->p_expr_kind);
 
-	if (!IsA(expr, Const))
+	if ((!IsA(expr, Const)) || (((Const *) expr)->consttype != INT4OID))
 		ereport(ERROR,
-				(errcode(ERRCODE_DATATYPE_MISMATCH),
-				 errmsg("jsonb simplified accessor supports subscripting in const int4, got type: %s",
-						format_type_be(exprType(expr))),
-				 parser_errposition(pstate, exprLocation(expr))));
+				errcode(ERRCODE_DATATYPE_MISMATCH),
+				errmsg("only integer constants are supported in jsonb simplified accessor subscripting"),
+				parser_errposition(pstate, exprLocation(expr)));
 
 	cnst = (Const *) expr;
+	Assert(!cnst->constisnull);
 
-	if (cnst->consttype == INT4OID && !cnst->constisnull)
-	{
-		int32		val = DatumGetInt32(cnst->constvalue);
+	val = DatumGetInt32(cnst->constvalue);
 
-		return make_jsonpath_item_int(val, exprs);
-	}
-
-	ereport(ERROR,
-			(errcode(ERRCODE_DATATYPE_MISMATCH),
-			 errmsg("jsonb simplified accessor supports subscripting in type: INT4, got type: %s",
-					format_type_be(cnst->consttype)),
-			 parser_errposition(pstate, exprLocation(expr))));
+	return make_jsonpath_item_int(val, exprs);
 }
 
 /*
@@ -329,6 +315,9 @@ jsonb_subscript_make_jsonpath(ParseState *pstate, List **indirection,
 			*lexprs = lappend(*lexprs, NULL);
 	}
 
+	list_free(*indirection);
+	*indirection = NIL;
+
 	*indirection = list_delete_first_n(*indirection, pathlen);
 
 	jsp = jsonPathFromParseResult(&jpres, 0, NULL);
diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out
index 91a7b825764..114200e0e43 100644
--- a/src/test/regress/expected/jsonb.out
+++ b/src/test/regress/expected/jsonb.out
@@ -5163,7 +5163,7 @@ select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d']['a'];
 (1 row)
 
 select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb).d['a'];
-ERROR:  jsonb simplified accessor supports subscripting in type: INT4, got type: unknown
+ERROR:  only integer constants are supported in jsonb simplified accessor subscripting
 LINE 1: select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb).d['a'];
                                                                ^
 select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb).d.a;
@@ -5233,7 +5233,7 @@ select ('{"a": ["a1", {"b1": ["aaa", "bbb", "ccc"]}], "b": "bb"}'::jsonb).a[1].b
 (1 row)
 
 select ('{"a": 1}'::jsonb)['a':'b']; -- fails
-ERROR:  jsonb simplified accessor supports subscripting in type: INT4, got type: unknown
+ERROR:  only integer constants are supported in jsonb simplified accessor subscripting
 LINE 1: select ('{"a": 1}'::jsonb)['a':'b'];
                                    ^
 select ('[1, "2", null]'::jsonb)[1:2];
-- 
2.34.1