I looked at your 0001.  My 0001 are some trivial comment cleanups to
that.

I scrolled through all of jsonfuncs.c to see if there was a better place
for the new function than the end of the file.  Man, is that one ugly
file.  There are almost no comments!  I almost wish you would create a
new file so that you don't have to put this new function in such bad
company.  But maybe it'll improve someday, so ... whatever.

In the original code, the functions here being (re)moved do not need to
return a type output function in a few cases.  This works okay when the
functions are each contained in a single file (because each function
knows that the respective datum_to_json/datum_to_jsonb user of the
returned values won't need the function OID in those other cases); but
as an exported function, that strange API doesn't seem great.  (It only
works for 0002 because the only thing that the executor does with these
cached values is call datum_to_json/b).  That seems easy to solve, since
we can return the hardcoded output function OID in those cases anyway.
A possible complaint about this is that the OID so returned would be
untested code, so they might be wrong and we'd never know.  However,
ISTM it's better to make a promise about always returning a function OID
and later fixing any bogus function OID if we ever discover that we
return one, rather than having to document in the function's comment
that "we only return function OIDs in such and such cases".  So I made
this change my 0002.

A similar complain can be made about which casts we look for.  Right
now, only an explicit cast to JSON is useful, so that's the only thing
we do.  But maybe one day a cast to JSONB would become useful if there's
no cast to JSON for some datatype (in the is_jsonb case only?); and
maybe another type of cast would be useful.  However, that seems like
going too much into uncharted territory with no useful use case, so
let's just not go there for now.  Maybe in the future we can improve
this aspect of it, if need arises.

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
>From 44504a8fe96ad76a375c77e5f53e4f897e3ca2f2 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvhe...@alvh.no-ip.org>
Date: Thu, 13 Jul 2023 18:06:07 +0200
Subject: [PATCH 1/2] trivial fixups

---
 src/backend/utils/adt/jsonfuncs.c | 5 ++---
 src/include/utils/jsonfuncs.h     | 8 ++++----
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/backend/utils/adt/jsonfuncs.c 
b/src/backend/utils/adt/jsonfuncs.c
index 612bbf06a3..764d48505b 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -5691,9 +5691,8 @@ json_get_first_token(text *json, bool throw_error)
  * Determine how we want to print values of a given type in datum_to_json(b).
  *
  * Given the datatype OID, return its JsonTypeCategory, as well as the type's
- * output function OID.  If the returned category is JSONTYPE_CAST or
- * JSOBTYPE_CASTJSON, we return the OID of the type->JSON cast function
- * instead.
+ * output function OID.  If the returned category is JSONTYPE_CAST, we return
+ * the OID of the type->JSON cast function instead.
  */
 void
 json_categorize_type(Oid typoid, bool is_jsonb,
diff --git a/src/include/utils/jsonfuncs.h b/src/include/utils/jsonfuncs.h
index 27c2d20610..27a25ba283 100644
--- a/src/include/utils/jsonfuncs.h
+++ b/src/include/utils/jsonfuncs.h
@@ -63,7 +63,7 @@ extern Jsonb *transform_jsonb_string_values(Jsonb *jsonb, 
void *action_state,
 extern text *transform_json_string_values(text *json, void *action_state,
                                                                                
  JsonTransformStringValuesAction transform_action);
 
-/* Type categories for datum_to_json[b] and friends. */
+/* Type categories returned by json_categorize_type */
 typedef enum
 {
        JSONTYPE_NULL,                          /* null, so we didn't bother to 
identify */
@@ -72,8 +72,8 @@ typedef enum
        JSONTYPE_DATE,                          /* we use special formatting 
for datetimes */
        JSONTYPE_TIMESTAMP,
        JSONTYPE_TIMESTAMPTZ,
-       JSONTYPE_JSON,                          /* JSON and JSONB */
-       JSONTYPE_JSONB,                         /* JSONB (for datum_to_jsonb) */
+       JSONTYPE_JSON,                          /* JSON (and JSONB, if not 
is_jsonb) */
+       JSONTYPE_JSONB,                         /* JSONB (if is_jsonb) */
        JSONTYPE_ARRAY,                         /* array */
        JSONTYPE_COMPOSITE,                     /* composite */
        JSONTYPE_CAST,                          /* something with an explicit 
cast to JSON */
@@ -81,6 +81,6 @@ typedef enum
 } JsonTypeCategory;
 
 void json_categorize_type(Oid typoid, bool is_jsonb,
-                                        JsonTypeCategory *tcategory, Oid 
*outfuncoid);
+                                                 JsonTypeCategory *tcategory, 
Oid *outfuncoid);
 
 #endif
-- 
2.30.2

>From fd025b1482223370cc10e82029468fdd99932368 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvhe...@alvh.no-ip.org>
Date: Thu, 13 Jul 2023 18:06:46 +0200
Subject: [PATCH 2/2] make the output generally usable, not just for
 datum_to_json[b]

---
 src/backend/utils/adt/jsonfuncs.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/backend/utils/adt/jsonfuncs.c 
b/src/backend/utils/adt/jsonfuncs.c
index 764d48505b..a4bfa5e404 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -5705,16 +5705,10 @@ json_categorize_type(Oid typoid, bool is_jsonb,
 
        *outfuncoid = InvalidOid;
 
-       /*
-        * We need to get the output function for everything except date and
-        * timestamp types, booleans, array and composite types, json and jsonb,
-        * and non-builtin types where there's a cast to json. In this last case
-        * we return the oid of the cast function instead.
-        */
-
        switch (typoid)
        {
                case BOOLOID:
+                       *outfuncoid = F_BOOLOUT;
                        *tcategory = JSONTYPE_BOOL;
                        break;
 
@@ -5729,26 +5723,27 @@ json_categorize_type(Oid typoid, bool is_jsonb,
                        break;
 
                case DATEOID:
+                       *outfuncoid = F_DATE_OUT;
                        *tcategory = JSONTYPE_DATE;
                        break;
 
                case TIMESTAMPOID:
+                       *outfuncoid = F_TIMESTAMP_OUT;
                        *tcategory = JSONTYPE_TIMESTAMP;
                        break;
 
                case TIMESTAMPTZOID:
+                       *outfuncoid = F_TIMESTAMPTZ_OUT;
                        *tcategory = JSONTYPE_TIMESTAMPTZ;
                        break;
 
                case JSONOID:
-                       if (!is_jsonb)
-                               getTypeOutputInfo(typoid, outfuncoid, 
&typisvarlena);
+                       getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
                        *tcategory = JSONTYPE_JSON;
                        break;
 
                case JSONBOID:
-                       if (!is_jsonb)
-                               getTypeOutputInfo(typoid, outfuncoid, 
&typisvarlena);
+                       getTypeOutputInfo(typoid, outfuncoid, &typisvarlena);
                        *tcategory = is_jsonb ? JSONTYPE_JSONB : JSONTYPE_JSON;
                        break;
 
@@ -5756,9 +5751,15 @@ json_categorize_type(Oid typoid, bool is_jsonb,
                        /* Check for arrays and composites */
                        if (OidIsValid(get_element_type(typoid)) || typoid == 
ANYARRAYOID
                                || typoid == ANYCOMPATIBLEARRAYOID || typoid == 
RECORDARRAYOID)
+                       {
+                               *outfuncoid = F_ARRAY_OUT;
                                *tcategory = JSONTYPE_ARRAY;
+                       }
                        else if (type_is_rowtype(typoid))       /* includes 
RECORDOID */
+                       {
+                               *outfuncoid = F_RECORD_OUT;
                                *tcategory = JSONTYPE_COMPOSITE;
+                       }
                        else
                        {
                                /*
-- 
2.30.2

Reply via email to