Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 23c6eb0

Browse files
committed
Remove create_singleton_array(), hard-coding the case in its sole caller.
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
1 parent 9209e07 commit 23c6eb0

File tree

3 files changed

+14
-85
lines changed

3 files changed

+14
-85
lines changed

src/backend/utils/adt/array_userfuncs.c

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -454,79 +454,6 @@ array_cat(PG_FUNCTION_ARGS)
454454
}
455455

456456

457-
/*
458-
* create_singleton_array - make a one-element array
459-
*
460-
* If desired, the caller can ask for it to be higher than one-dimensional.
461-
* Caller's fcinfo must be passed in, as we use fn_extra for caching.
462-
*/
463-
ArrayType *
464-
create_singleton_array(FunctionCallInfo fcinfo,
465-
Oid element_type,
466-
Datum element,
467-
bool isNull,
468-
int ndims)
469-
{
470-
Datum dvalues[1];
471-
bool nulls[1];
472-
int16 typlen;
473-
bool typbyval;
474-
char typalign;
475-
int dims[MAXDIM];
476-
int lbs[MAXDIM];
477-
int i;
478-
ArrayMetaState *my_extra;
479-
480-
if (ndims < 1)
481-
ereport(ERROR,
482-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
483-
errmsg("invalid number of dimensions: %d", ndims)));
484-
if (ndims > MAXDIM)
485-
ereport(ERROR,
486-
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
487-
errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
488-
ndims, MAXDIM)));
489-
490-
dvalues[0] = element;
491-
nulls[0] = isNull;
492-
493-
for (i = 0; i < ndims; i++)
494-
{
495-
dims[i] = 1;
496-
lbs[i] = 1;
497-
}
498-
499-
/*
500-
* We arrange to look up info about element type only once per series of
501-
* calls, assuming the element type doesn't change underneath us.
502-
*/
503-
my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
504-
if (my_extra == NULL)
505-
{
506-
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
507-
sizeof(ArrayMetaState));
508-
my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
509-
my_extra->element_type = ~element_type;
510-
}
511-
512-
if (my_extra->element_type != element_type)
513-
{
514-
/* Get info about element type */
515-
get_typlenbyvalalign(element_type,
516-
&my_extra->typlen,
517-
&my_extra->typbyval,
518-
&my_extra->typalign);
519-
my_extra->element_type = element_type;
520-
}
521-
typlen = my_extra->typlen;
522-
typbyval = my_extra->typbyval;
523-
typalign = my_extra->typalign;
524-
525-
return construct_md_array(dvalues, nulls, ndims, dims, lbs, element_type,
526-
typlen, typbyval, typalign);
527-
}
528-
529-
530457
/*
531458
* ARRAY_AGG(anynonarray) aggregate function
532459
*/

src/backend/utils/adt/varlena.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,12 +4218,23 @@ text_to_array_internal(PG_FUNCTION_ARGS)
42184218
*/
42194219
if (fldsep_len < 1)
42204220
{
4221+
Datum elems[1];
4222+
bool nulls[1];
4223+
int dims[1];
4224+
int lbs[1];
4225+
42214226
text_position_cleanup(&state);
42224227
/* single element can be a NULL too */
42234228
is_null = null_string ? text_isequal(inputstring, null_string) : false;
4224-
PG_RETURN_ARRAYTYPE_P(create_singleton_array(fcinfo, TEXTOID,
4225-
PointerGetDatum(inputstring),
4226-
is_null, 1));
4229+
4230+
elems[0] = PointerGetDatum(inputstring);
4231+
nulls[0] = is_null;
4232+
dims[0] = 1;
4233+
lbs[0] = 1;
4234+
/* XXX: this hardcodes assumptions about the text type */
4235+
PG_RETURN_ARRAYTYPE_P(construct_md_array(elems, nulls,
4236+
1, dims, lbs,
4237+
TEXTOID, -1, false, 'i'));
42274238
}
42284239

42294240
start_posn = 1;

src/include/utils/array.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,4 @@ extern ExpandedArrayHeader *DatumGetExpandedArrayX(Datum d,
443443
extern AnyArrayType *DatumGetAnyArray(Datum d);
444444
extern void deconstruct_expanded_array(ExpandedArrayHeader *eah);
445445

446-
/*
447-
* prototypes for functions defined in array_userfuncs.c
448-
*/
449-
extern ArrayType *create_singleton_array(FunctionCallInfo fcinfo,
450-
Oid element_type,
451-
Datum element,
452-
bool isNull,
453-
int ndims);
454-
455446
#endif /* ARRAY_H */

0 commit comments

Comments
 (0)