From 29a2ca508541628df9e902be60b3ec1ea81424cf Mon Sep 17 00:00:00 2001
From: Andy Fan <zhihui.fan1213@gmail.com>
Date: Fri, 15 Sep 2023 09:37:32 +0800
Subject: [PATCH v14 2/2] try to apply jsonb extraction optimization to  _tz
 functions.

both jsonb_path_query_tz and jsonb_path_query_tz_first returns
the element which is timestamp comparable, but such element is
impossible to be cast to numeric or boolean IIUC. Just provides
this commit for communication purpose only.
---
 src/backend/utils/adt/jsonb.c         |  8 +++++++-
 src/backend/utils/adt/jsonpath_exec.c | 12 ++++++++++++
 src/include/catalog/pg_proc.dat       |  8 ++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c
index e2efb28a685..aad3b7f542f 100644
--- a/src/backend/utils/adt/jsonb.c
+++ b/src/backend/utils/adt/jsonb.c
@@ -2089,7 +2089,6 @@ jsonb_cast_support(PG_FUNCTION_ARGS)
 			/* not the desired pattern. */
 			PG_RETURN_POINTER(NULL);
 
-
 		switch (input_func_id)
 		{
 			case F_JSONB_OBJECT_FIELD:
@@ -2106,9 +2105,16 @@ jsonb_cast_support(PG_FUNCTION_ARGS)
 				new_func_id = F_JSONB_PATH_QUERY_START;
 				retset = true;
 				break;
+			case F_JSONB_PATH_QUERY_TZ:
+				new_func_id =  F_JSONB_PATH_QUERY_TZ_START;
+				retset = true;
+				break;
 			case F_JSONB_PATH_QUERY_FIRST:
 				new_func_id = F_JSONB_PATH_QUERY_FIRST_START;
 				break;
+			case F_JSONB_PATH_QUERY_FIRST_TZ:
+				new_func_id = F_JSONB_PATH_QUERY_FIRST_TZ_START;
+				break;
 			default:
 				new_func_id = InvalidOid;
 				break;
diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c
index 5eef14f93ed..9b7108418be 100644
--- a/src/backend/utils/adt/jsonpath_exec.c
+++ b/src/backend/utils/adt/jsonpath_exec.c
@@ -456,6 +456,12 @@ jsonb_path_query_start(PG_FUNCTION_ARGS)
 	return jsonb_path_query_internal(fcinfo, false, JsonbValue_AsJsonbValue);
 }
 
+Datum
+jsonb_path_query_tz_start(PG_FUNCTION_ARGS)
+{
+	return jsonb_path_query_internal(fcinfo, true, JsonbValue_AsJsonbValue);
+}
+
 /*
  * jsonb_path_query_array
  *		Executes jsonpath for given jsonb document and returns result as
@@ -530,6 +536,12 @@ jsonb_path_query_first_start(PG_FUNCTION_ARGS)
 	return jsonb_path_query_first_internal(fcinfo, false, JsonbValue_AsJsonbValue);
 }
 
+Datum
+jsonb_path_query_first_tz_start(PG_FUNCTION_ARGS)
+{
+	return jsonb_path_query_first_internal(fcinfo, true, JsonbValue_AsJsonbValue);
+}
+
 /********************Execute functions for JsonPath**************************/
 
 /*
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index ef719aac6bb..7e76d611c06 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -10167,6 +10167,10 @@
   proname => 'jsonb_path_query_start', prorows => '1000', proretset => 't',
   prorettype => 'internal', proargtypes => 'internal jsonpath jsonb bool',
   prosrc => 'jsonb_path_query_start' },
+{ oid => '4556', descr => 'jsonpath query tz as jsonbvalue',
+  proname => 'jsonb_path_query_tz_start', prorows => '1000', proretset => 't',
+  prorettype => 'internal', proargtypes => 'internal jsonpath jsonb bool',
+  prosrc => 'jsonb_path_query_tz_start' },
 { oid => '4007', descr => 'jsonpath query wrapped into array',
   proname => 'jsonb_path_query_array', prorettype => 'jsonb',
   proargtypes => 'jsonb jsonpath jsonb bool',
@@ -10179,6 +10183,10 @@
   proname => 'jsonb_path_query_first_start', prorettype => 'internal',
   proargtypes => 'internal jsonpath jsonb bool',
   prosrc => 'jsonb_path_query_first_start' },
+{ oid => '4551', descr => 'jsonpath query tz first item as jsonbvalue',
+  proname => 'jsonb_path_query_first_tz_start', prorettype => 'internal',
+  proargtypes => 'internal jsonpath jsonb bool',
+  prosrc => 'jsonb_path_query_first_tz_start'},
 { oid => '4009', descr => 'jsonpath match',
   proname => 'jsonb_path_match', prorettype => 'bool',
   proargtypes => 'jsonb jsonpath jsonb bool', prosrc => 'jsonb_path_match' },
-- 
2.21.0

