]> granicus.if.org Git - postgresql/commitdiff
Remove create_singleton_array(), hard-coding the case in its sole caller.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 3 May 2017 00:41:37 +0000 (20:41 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 3 May 2017 00:41:37 +0000 (20:41 -0400)
create_singleton_array() was not really as useful as we perhaps thought
when we added it.  It had never accreted more than one call site, and is
only saving a dozen lines of code at that one, which is considerably less
bulk than the function itself.  Moreover, because of its insistence on
using the caller's fn_extra cache space, it's arguably a coding hazard.
text_to_array_internal() does not currently use fn_extra in any other way,
but if it did it would be subtly broken, since the conflicting fn_extra
uses could be needed within a single query, in the seldom-tested case that
the field separator varies during the query.  The same objection seems
likely to apply to any other potential caller.

The replacement code is a bit uglier, because it hardwires knowledge of
the storage parameters of type TEXT, but it's not like we haven't got
dozens or hundreds of other places that do the same.  Uglier seems like
a good tradeoff for smaller, faster, and safer.

Per discussion with Neha Khatri.

Discussion: https://postgr.es/m/CAFO0U+_fS5SRhzq6uPG+4fbERhoA9N2+nPrtvaC9mmeWivxbsA@mail.gmail.com

src/backend/utils/adt/array_userfuncs.c
src/backend/utils/adt/varlena.c
src/include/utils/array.h

index 8da3e0ac5b8c77121eaa48eb0b4a6259db9e51d9..dc2a1749db14e424134f2416030f8165c7f14afe 100644 (file)
@@ -454,79 +454,6 @@ array_cat(PG_FUNCTION_ARGS)
 }
 
 
-/*
- * create_singleton_array - make a one-element array
- *
- * If desired, the caller can ask for it to be higher than one-dimensional.
- * Caller's fcinfo must be passed in, as we use fn_extra for caching.
- */
-ArrayType *
-create_singleton_array(FunctionCallInfo fcinfo,
-                                          Oid element_type,
-                                          Datum element,
-                                          bool isNull,
-                                          int ndims)
-{
-       Datum           dvalues[1];
-       bool            nulls[1];
-       int16           typlen;
-       bool            typbyval;
-       char            typalign;
-       int                     dims[MAXDIM];
-       int                     lbs[MAXDIM];
-       int                     i;
-       ArrayMetaState *my_extra;
-
-       if (ndims < 1)
-               ereport(ERROR,
-                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                                errmsg("invalid number of dimensions: %d", ndims)));
-       if (ndims > MAXDIM)
-               ereport(ERROR,
-                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-                                errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
-                                               ndims, MAXDIM)));
-
-       dvalues[0] = element;
-       nulls[0] = isNull;
-
-       for (i = 0; i < ndims; i++)
-       {
-               dims[i] = 1;
-               lbs[i] = 1;
-       }
-
-       /*
-        * We arrange to look up info about element type only once per series of
-        * calls, assuming the element type doesn't change underneath us.
-        */
-       my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
-       if (my_extra == NULL)
-       {
-               fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
-                                                                                                         sizeof(ArrayMetaState));
-               my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
-               my_extra->element_type = ~element_type;
-       }
-
-       if (my_extra->element_type != element_type)
-       {
-               /* Get info about element type */
-               get_typlenbyvalalign(element_type,
-                                                        &my_extra->typlen,
-                                                        &my_extra->typbyval,
-                                                        &my_extra->typalign);
-               my_extra->element_type = element_type;
-       }
-       typlen = my_extra->typlen;
-       typbyval = my_extra->typbyval;
-       typalign = my_extra->typalign;
-
-       return construct_md_array(dvalues, nulls, ndims, dims, lbs, element_type,
-                                                         typlen, typbyval, typalign);
-}
-
-
 /*
  * ARRAY_AGG(anynonarray) aggregate function
  */
index c74c890b9b5530aab83688be6f35c16d874dfa08..0b0032787b24788252eb55b23408f57698743a86 100644 (file)
@@ -4218,12 +4218,23 @@ text_to_array_internal(PG_FUNCTION_ARGS)
                 */
                if (fldsep_len < 1)
                {
+                       Datum           elems[1];
+                       bool            nulls[1];
+                       int                     dims[1];
+                       int                     lbs[1];
+
                        text_position_cleanup(&state);
                        /* single element can be a NULL too */
                        is_null = null_string ? text_isequal(inputstring, null_string) : false;
-                       PG_RETURN_ARRAYTYPE_P(create_singleton_array(fcinfo, TEXTOID,
-                                                                                               PointerGetDatum(inputstring),
-                                                                                                                is_null, 1));
+
+                       elems[0] = PointerGetDatum(inputstring);
+                       nulls[0] = is_null;
+                       dims[0] = 1;
+                       lbs[0] = 1;
+                       /* XXX: this hardcodes assumptions about the text type */
+                       PG_RETURN_ARRAYTYPE_P(construct_md_array(elems, nulls,
+                                                                                                        1, dims, lbs,
+                                                                                                  TEXTOID, -1, false, 'i'));
                }
 
                start_posn = 1;
index b0ff73b7e0934addb81c444bfdb109b9eb7c6a40..552c08f0d508590a95a5e11d30134c91cdd85122 100644 (file)
@@ -443,13 +443,4 @@ extern ExpandedArrayHeader *DatumGetExpandedArrayX(Datum d,
 extern AnyArrayType *DatumGetAnyArray(Datum d);
 extern void deconstruct_expanded_array(ExpandedArrayHeader *eah);
 
-/*
- * prototypes for functions defined in array_userfuncs.c
- */
-extern ArrayType *create_singleton_array(FunctionCallInfo fcinfo,
-                                          Oid element_type,
-                                          Datum element,
-                                          bool isNull,
-                                          int ndims);
-
 #endif   /* ARRAY_H */