From 84e268108100461f08ca6c909139777d50f14582 Mon Sep 17 00:00:00 2001
From: Andy Fan <zhihui.fan1213@gmail.com>
Date: Fri, 18 Aug 2023 15:38:50 +0800
Subject: [PATCH v10 2/3] convert anyelement to internal.

---
 src/backend/nodes/makefuncs.c       | 29 -----------------------------
 src/backend/utils/adt/jsonb.c       | 10 ++++++----
 src/backend/utils/adt/jsonfuncs.c   |  2 +-
 src/include/catalog/catversion.h    |  2 +-
 src/include/catalog/pg_proc.dat     | 12 ++++++------
 src/include/nodes/makefuncs.h       |  2 --
 src/test/regress/expected/jsonb.out | 12 ++++++------
 7 files changed, 20 insertions(+), 49 deletions(-)

diff --git a/src/backend/nodes/makefuncs.c b/src/backend/nodes/makefuncs.c
index 9cb9178f01a..a41fdddc662 100644
--- a/src/backend/nodes/makefuncs.c
+++ b/src/backend/nodes/makefuncs.c
@@ -353,35 +353,6 @@ makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
 					 typByVal);
 }
 
-/*
- * makeDummyConst
- *	 create a Const node with the specified type/typmod.
- *
- * This is a convenience routine to create a Const which only the
- * type is interesting but make sure the value is accessible.
- */
-Const *
-makeDummyConst(Oid consttype, int32 consttypmod, Oid constcollid)
-{
-	int16		typLen;
-	bool		typByVal;
-	Const		*c;
-	Datum		val = 0;
-
-
-	get_typlenbyval(consttype, &typLen, &typByVal);
-
-	if (consttype == NUMERICOID)
-		val = DirectFunctionCall1(numeric_in, CStringGetDatum("0"));
-	else if (!typByVal)
-		elog(ERROR, "create dummy const for type %u is not supported.", consttype);
-
-	/* XXX: here I assume constvalue=0 is accessible for constbyval.*/
-	c = makeConst(consttype, consttypmod, 0, (int) typLen, val, false, typByVal);
-
-	return c;
-}
-
 /*
  * makeBoolConst -
  *	  creates a Const node representing a boolean value (can be NULL too)
diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c
index 148c1e2e195..d893e9c14b5 100644
--- a/src/backend/utils/adt/jsonb.c
+++ b/src/backend/utils/adt/jsonb.c
@@ -2091,10 +2091,12 @@ jsonb_cast_support(PG_FUNCTION_ARGS)
 
 		if (OidIsValid(new_func_id))
 		{
-			Const	*target =  makeDummyConst(fexpr->funcresulttype, 0, InvalidOid);
+			Const * target_typ = makeConst(OIDOID, -1, InvalidOid, sizeof(Oid),
+										   ObjectIdGetDatum(fexpr->funcresulttype),
+										   false, true);
 			fexpr->funcid = new_func_id;
 			fexpr->args = opexpr->args;
-			fexpr->args = list_insert_nth(fexpr->args, 0, target);
+			fexpr->args = list_insert_nth(fexpr->args, 0, (void *) target_typ);
 		}
 
 		PG_RETURN_POINTER(fexpr);
@@ -2164,7 +2166,7 @@ cast_jsonbvalue_to_type(JsonbValue *v, Oid targetOid)
 Datum
 jsonb_object_field_type(PG_FUNCTION_ARGS)
 {
-	Oid			targetOid = get_fn_expr_argtype(fcinfo->flinfo, 0);
+	Oid			targetOid = PG_GETARG_OID(0);
 	Jsonb	   *jb = PG_GETARG_JSONB_P(1);
 	text	   *key = PG_GETARG_TEXT_PP(2);
 
@@ -2188,7 +2190,7 @@ jsonb_object_field_type(PG_FUNCTION_ARGS)
 Datum
 jsonb_array_element_type(PG_FUNCTION_ARGS)
 {
-	Oid			targetOid = get_fn_expr_argtype(fcinfo->flinfo, 0);
+	Oid			targetOid = PG_GETARG_OID(0);
 	Jsonb	   *jb = PG_GETARG_JSONB_P(1);
 	int			element = PG_GETARG_INT32(2);
 
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index bb4ca807d74..02db9f53b47 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -1477,7 +1477,7 @@ get_scalar(void *state, char *token, JsonTokenType tokentype)
 Datum
 jsonb_extract_path_type(PG_FUNCTION_ARGS)
 {
-	Oid			targetOid = get_fn_expr_argtype(fcinfo->flinfo, 0);
+	Oid			targetOid = PG_GETARG_OID(0);
 	Jsonb	   *jb = PG_GETARG_JSONB_P(1);
 	ArrayType  *path = PG_GETARG_ARRAYTYPE_P(2);
 
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index 8a896f9aad2..8a919e1178b 100644
--- a/src/include/catalog/catversion.h
+++ b/src/include/catalog/catversion.h
@@ -57,6 +57,6 @@
  */
 
 /*							yyyymmddN */
-#define CATALOG_VERSION_NO	202308171
+#define CATALOG_VERSION_NO	202308211
 
 #endif
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index b6844537529..66d1af71586 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -9930,8 +9930,8 @@
   proargtypes => 'jsonb text', proargnames => '{from_json, field_name}',
   prosrc => 'jsonb_object_field_text' },
 { oid => '3813', descr => 'return a given type specified in desired_type from jsonb field',
-  proname => 'jsonb_object_field_type', prorettype => 'anyelement',
-  proargtypes => 'anyelement jsonb text', proargnames => '{target_type, from_json, field_name}',
+  proname => 'jsonb_object_field_type', prorettype => 'internal',
+  proargtypes => 'internal jsonb text', proargnames => '{target_type, from_json, field_name}',
   prosrc => 'jsonb_object_field_type'},
 { oid => '3814', descr => 'planner support for numeric(jsonb)',
   proname => 'jsonb_cast_support', prorettype => 'internal',
@@ -9945,8 +9945,8 @@
   proargtypes => 'jsonb int4', proargnames => '{from_json, element_index}',
   prosrc => 'jsonb_array_element_text' },
 { oid => '4549', descr => 'cast an array element to given type',
-  proname => 'jsonb_array_element_type', prorettype => 'anyelement',
-  proargtypes => 'anyelement jsonb int4', proargnames => '{target_type, from_json, element_index}',
+  proname => 'jsonb_array_element_type', prorettype => 'internal',
+  proargtypes => 'internal jsonb int4', proargnames => '{target_type, from_json, element_index}',
   prosrc => 'jsonb_array_element_type' },
 { oid => '3217', descr => 'get value from jsonb with path elements',
   proname => 'jsonb_extract_path', provariadic => 'text', prorettype => 'jsonb',
@@ -9961,8 +9961,8 @@
   prosrc => 'jsonb_extract_path_text' },
 { oid => '4551', descr => 'cast value from jsonb as text with path elements to given type',
   proname => 'jsonb_extract_path_type', provariadic => 'text',
-  prorettype => 'anyelement', proargtypes => 'anyelement jsonb _text',
-  proallargtypes => '{anyelement,jsonb,_text}', proargmodes => '{i,i,v}',
+  prorettype => 'internal', proargtypes => 'internal jsonb _text',
+  proallargtypes => '{internal,jsonb,_text}', proargmodes => '{i,i,v}',
   proargnames => '{target_type,from_json,path_elems}',
   prosrc => 'jsonb_extract_path_type' },
 { oid => '3219', descr => 'elements of a jsonb array',
diff --git a/src/include/nodes/makefuncs.h b/src/include/nodes/makefuncs.h
index cfbe5b26196..31807030055 100644
--- a/src/include/nodes/makefuncs.h
+++ b/src/include/nodes/makefuncs.h
@@ -58,8 +58,6 @@ extern Const *makeConst(Oid consttype,
 
 extern Const *makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid);
 
-extern Const *makeDummyConst(Oid consttype, int32 consttypmod, Oid constcollid);
-
 extern Node *makeBoolConst(bool value, bool isnull);
 
 extern Expr *makeBoolExpr(BoolExprType boolop, List *args, int location);
diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out
index 12daacb3b80..8ed80a11176 100644
--- a/src/test/regress/expected/jsonb.out
+++ b/src/test/regress/expected/jsonb.out
@@ -510,10 +510,10 @@ SELECT test_json -> 2, test_json -> 0 FROM test_jsonb WHERE json_type = 'scalar'
 
 explain (verbose, costs off)
 SELECT (test_json -> 0)::int4, test_json -> 0 FROM test_jsonb WHERE json_type = 'scalarint';
-                              QUERY PLAN                               
------------------------------------------------------------------------
+                                        QUERY PLAN                                        
+------------------------------------------------------------------------------------------
  Seq Scan on pg_temp.test_jsonb
-   Output: jsonb_array_element_type(0, test_json, 0), (test_json -> 0)
+   Output: pg_catalog.jsonb_array_element_type('23'::oid, test_json, 0), (test_json -> 0)
    Filter: (test_jsonb.json_type = 'scalarint'::text)
 (3 rows)
 
@@ -3571,10 +3571,10 @@ SELECT (j->'a')::numeric,
 (j #> '{"a"}')::numeric,
 (j->0)::numeric
 FROM testjsonb;
-                                                                                                                                                                                                                                            QUERY PLAN                                                                                                                                                                                                                                             
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+                                                                                                                                                                                                                                                                                     QUERY PLAN                                                                                                                                                                                                                                                                                     
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Seq Scan on public.testjsonb
-   Output: jsonb_object_field_type('0'::numeric, j, 'a'::text), jsonb_object_field_type('0'::smallint, j, 'a'::text), jsonb_object_field_type(0, j, 'a'::text), jsonb_object_field_type('0'::bigint, j, 'a'::text), jsonb_object_field_type('0'::real, j, 'a'::text), jsonb_object_field_type('0'::double precision, j, 'a'::text), jsonb_object_field_type(false, j, 'a'::text), pg_catalog.jsonb_extract_path_type('0'::numeric, j, '{a}'::text[]), jsonb_array_element_type('0'::numeric, j, 0)
+   Output: pg_catalog.jsonb_object_field_type('1700'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('21'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('23'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('20'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('700'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('701'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('16'::oid, j, 'a'::text), pg_catalog.jsonb_extract_path_type('1700'::oid, j, '{a}'::text[]), pg_catalog.jsonb_array_element_type('1700'::oid, j, 0)
 (2 rows)
 
 -- nested tests
-- 
2.21.0

