1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-24 00:23:06 +03:00

Add construct_array_builtin, deconstruct_array_builtin

There were many calls to construct_array() and deconstruct_array() for
built-in types, for example, when dealing with system catalog columns.
These all hardcoded the type attributes necessary to pass to these
functions.

To simplify this a bit, add construct_array_builtin(),
deconstruct_array_builtin() as wrappers that centralize this hardcoded
knowledge.  This simplifies many call sites and reduces the amount of
hardcoded stuff that is spread around.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/2914356f-9e5f-8c59-2995-5997fc48bcba%40enterprisedb.com
This commit is contained in:
Peter Eisentraut
2022-07-01 10:51:45 +02:00
parent 7c2d6f8d34
commit d746021de1
51 changed files with 284 additions and 298 deletions

View File

@@ -3330,6 +3330,92 @@ construct_array(Datum *elems, int nelems,
elmtype, elmlen, elmbyval, elmalign);
}
/*
* Like construct_array(), where elmtype must be a built-in type, and
* elmlen/elmbyval/elmalign is looked up from hardcoded data. This is often
* useful when manipulating arrays from/for system catalogs.
*/
ArrayType *
construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
{
int elmlen;
bool elmbyval;
char elmalign;
switch (elmtype)
{
case CHAROID:
elmlen = 1;
elmbyval = true;
elmalign = TYPALIGN_CHAR;
break;
case CSTRINGOID:
elmlen = -2;
elmbyval = false;
elmalign = TYPALIGN_CHAR;
break;
case FLOAT4OID:
elmlen = sizeof(float4);
elmbyval = true;
elmalign = TYPALIGN_INT;
break;
case INT2OID:
elmlen = sizeof(int16);
elmbyval = true;
elmalign = TYPALIGN_SHORT;
break;
case INT4OID:
elmlen = sizeof(int32);
elmbyval = true;
elmalign = TYPALIGN_INT;
break;
case INT8OID:
elmlen = sizeof(int64);
elmbyval = FLOAT8PASSBYVAL;
elmalign = TYPALIGN_DOUBLE;
break;
case NAMEOID:
elmlen = NAMEDATALEN;
elmbyval = false;
elmalign = TYPALIGN_CHAR;
break;
case OIDOID:
case REGTYPEOID:
elmlen = sizeof(Oid);
elmbyval = true;
elmalign = TYPALIGN_INT;
break;
case TEXTOID:
elmlen = -1;
elmbyval = false;
elmalign = TYPALIGN_INT;
break;
case TIDOID:
elmlen = sizeof(ItemPointerData);
elmbyval = false;
elmalign = TYPALIGN_SHORT;
break;
default:
elog(ERROR, "type %u not supported by construct_array_builtin()", elmtype);
/* keep compiler quiet */
elmlen = 0;
elmbyval = false;
elmalign = 0;
}
return construct_array(elems, nelems, elmtype, elmlen, elmbyval, elmalign);
}
/*
* construct_md_array --- simple method for constructing an array object
* with arbitrary dimensions and possible NULLs
@@ -3483,9 +3569,9 @@ construct_empty_expanded_array(Oid element_type,
* be pointers into the array object.
*
* NOTE: it would be cleaner to look up the elmlen/elmbval/elmalign info
* from the system catalogs, given the elmtype. However, in most current
* uses the type is hard-wired into the caller and so we can save a lookup
* cycle by hard-wiring the type info as well.
* from the system catalogs, given the elmtype. However, the caller is
* in a better position to cache this info across multiple uses, or even
* to hard-wire values if the element type is hard-wired.
*/
void
deconstruct_array(ArrayType *array,
@@ -3548,6 +3634,75 @@ deconstruct_array(ArrayType *array,
}
}
/*
* Like deconstruct_array(), where elmtype must be a built-in type, and
* elmlen/elmbyval/elmalign is looked up from hardcoded data. This is often
* useful when manipulating arrays from/for system catalogs.
*/
void
deconstruct_array_builtin(ArrayType *array,
Oid elmtype,
Datum **elemsp, bool **nullsp, int *nelemsp)
{
int elmlen;
bool elmbyval;
char elmalign;
switch (elmtype)
{
case CHAROID:
elmlen = 1;
elmbyval = true;
elmalign = TYPALIGN_CHAR;
break;
case CSTRINGOID:
elmlen = -2;
elmbyval = false;
elmalign = TYPALIGN_CHAR;
break;
case FLOAT8OID:
elmlen = sizeof(float8);
elmbyval = FLOAT8PASSBYVAL;
elmalign = TYPALIGN_DOUBLE;
break;
case INT2OID:
elmlen = sizeof(int16);
elmbyval = true;
elmalign = TYPALIGN_SHORT;
break;
case OIDOID:
elmlen = sizeof(Oid);
elmbyval = true;
elmalign = TYPALIGN_INT;
break;
case TEXTOID:
elmlen = -1;
elmbyval = false;
elmalign = TYPALIGN_INT;
break;
case TIDOID:
elmlen = sizeof(ItemPointerData);
elmbyval = false;
elmalign = TYPALIGN_SHORT;
break;
default:
elog(ERROR, "type %u not supported by deconstruct_array_builtin()", elmtype);
/* keep compiler quiet */
elmlen = 0;
elmbyval = false;
elmalign = 0;
}
deconstruct_array(array, elmtype, elmlen, elmbyval, elmalign, elemsp, nullsp, nelemsp);
}
/*
* array_contains_nulls --- detect whether an array has any null elements
*

View File

@@ -249,10 +249,7 @@ ArrayGetIntegerTypmods(ArrayType *arr, int *n)
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("typmod array must not contain nulls")));
/* hardwired knowledge about cstring's representation details here */
deconstruct_array(arr, CSTRINGOID,
-2, false, TYPALIGN_CHAR,
&elem_values, NULL, n);
deconstruct_array_builtin(arr, CSTRINGOID, &elem_values, NULL, n);
result = (int32 *) palloc(*n * sizeof(int32));

View File

@@ -151,7 +151,7 @@ get_hba_options(HbaLine *hba)
Assert(noptions <= MAX_HBA_OPTIONS);
if (noptions > 0)
return construct_array(options, noptions, TEXTOID, -1, false, TYPALIGN_INT);
return construct_array_builtin(options, noptions, TEXTOID);
else
return NULL;
}

View File

@@ -1450,9 +1450,7 @@ json_object(PG_FUNCTION_ARGS)
errmsg("wrong number of array subscripts")));
}
deconstruct_array(in_array,
TEXTOID, -1, false, TYPALIGN_INT,
&in_datums, &in_nulls, &in_count);
deconstruct_array_builtin(in_array, TEXTOID, &in_datums, &in_nulls, &in_count);
count = in_count / 2;
@@ -1526,13 +1524,8 @@ json_object_two_arg(PG_FUNCTION_ARGS)
if (nkdims == 0)
PG_RETURN_DATUM(CStringGetTextDatum("{}"));
deconstruct_array(key_array,
TEXTOID, -1, false, TYPALIGN_INT,
&key_datums, &key_nulls, &key_count);
deconstruct_array(val_array,
TEXTOID, -1, false, TYPALIGN_INT,
&val_datums, &val_nulls, &val_count);
deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count);
deconstruct_array_builtin(val_array, TEXTOID, &val_datums, &val_nulls, &val_count);
if (key_count != val_count)
ereport(ERROR,

View File

@@ -1378,9 +1378,7 @@ jsonb_object(PG_FUNCTION_ARGS)
errmsg("wrong number of array subscripts")));
}
deconstruct_array(in_array,
TEXTOID, -1, false, TYPALIGN_INT,
&in_datums, &in_nulls, &in_count);
deconstruct_array_builtin(in_array, TEXTOID, &in_datums, &in_nulls, &in_count);
count = in_count / 2;
@@ -1466,13 +1464,8 @@ jsonb_object_two_arg(PG_FUNCTION_ARGS)
if (nkdims == 0)
goto close_object;
deconstruct_array(key_array,
TEXTOID, -1, false, TYPALIGN_INT,
&key_datums, &key_nulls, &key_count);
deconstruct_array(val_array,
TEXTOID, -1, false, TYPALIGN_INT,
&val_datums, &val_nulls, &val_count);
deconstruct_array_builtin(key_array, TEXTOID, &key_datums, &key_nulls, &key_count);
deconstruct_array_builtin(val_array, TEXTOID, &val_datums, &val_nulls, &val_count);
if (key_count != val_count)
ereport(ERROR,

View File

@@ -885,9 +885,7 @@ gin_extract_jsonb_query(PG_FUNCTION_ARGS)
int i,
j;
deconstruct_array(query,
TEXTOID, -1, false, TYPALIGN_INT,
&key_datums, &key_nulls, &key_count);
deconstruct_array_builtin(query, TEXTOID, &key_datums, &key_nulls, &key_count);
entries = (Datum *) palloc(sizeof(Datum) * key_count);

View File

@@ -53,8 +53,7 @@ jsonb_exists_any(PG_FUNCTION_ARGS)
bool *key_nulls;
int elem_count;
deconstruct_array(keys, TEXTOID, -1, false, TYPALIGN_INT,
&key_datums, &key_nulls, &elem_count);
deconstruct_array_builtin(keys, TEXTOID, &key_datums, &key_nulls, &elem_count);
for (i = 0; i < elem_count; i++)
{
@@ -86,8 +85,7 @@ jsonb_exists_all(PG_FUNCTION_ARGS)
bool *key_nulls;
int elem_count;
deconstruct_array(keys, TEXTOID, -1, false, TYPALIGN_INT,
&key_datums, &key_nulls, &elem_count);
deconstruct_array_builtin(keys, TEXTOID, &key_datums, &key_nulls, &elem_count);
for (i = 0; i < elem_count; i++)
{

View File

@@ -1000,8 +1000,7 @@ get_path_all(FunctionCallInfo fcinfo, bool as_text)
if (array_contains_nulls(path))
PG_RETURN_NULL();
deconstruct_array(path, TEXTOID, -1, false, TYPALIGN_INT,
&pathtext, &pathnulls, &npath);
deconstruct_array_builtin(path, TEXTOID, &pathtext, &pathnulls, &npath);
tpath = palloc(npath * sizeof(char *));
ipath = palloc(npath * sizeof(int));
@@ -1456,8 +1455,7 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
if (array_contains_nulls(path))
PG_RETURN_NULL();
deconstruct_array(path, TEXTOID, -1, false, TYPALIGN_INT,
&pathtext, &pathnulls, &npath);
deconstruct_array_builtin(path, TEXTOID, &pathtext, &pathnulls, &npath);
res = jsonb_get_element(jb, pathtext, npath, &isnull, as_text);
@@ -4370,8 +4368,7 @@ jsonb_delete_array(PG_FUNCTION_ARGS)
if (JB_ROOT_COUNT(in) == 0)
PG_RETURN_JSONB_P(in);
deconstruct_array(keys, TEXTOID, -1, false, TYPALIGN_INT,
&keys_elems, &keys_nulls, &keys_len);
deconstruct_array_builtin(keys, TEXTOID, &keys_elems, &keys_nulls, &keys_len);
if (keys_len == 0)
PG_RETURN_JSONB_P(in);
@@ -4523,8 +4520,7 @@ jsonb_set(PG_FUNCTION_ARGS)
if (JB_ROOT_COUNT(in) == 0 && !create)
PG_RETURN_JSONB_P(in);
deconstruct_array(path, TEXTOID, -1, false, TYPALIGN_INT,
&path_elems, &path_nulls, &path_len);
deconstruct_array_builtin(path, TEXTOID, &path_elems, &path_nulls, &path_len);
if (path_len == 0)
PG_RETURN_JSONB_P(in);
@@ -4635,8 +4631,7 @@ jsonb_delete_path(PG_FUNCTION_ARGS)
if (JB_ROOT_COUNT(in) == 0)
PG_RETURN_JSONB_P(in);
deconstruct_array(path, TEXTOID, -1, false, TYPALIGN_INT,
&path_elems, &path_nulls, &path_len);
deconstruct_array_builtin(path, TEXTOID, &path_elems, &path_nulls, &path_len);
if (path_len == 0)
PG_RETURN_JSONB_P(in);
@@ -4681,8 +4676,7 @@ jsonb_insert(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("cannot set path in scalar")));
deconstruct_array(path, TEXTOID, -1, false, TYPALIGN_INT,
&path_elems, &path_nulls, &path_len);
deconstruct_array_builtin(path, TEXTOID, &path_elems, &path_nulls, &path_len);
if (path_len == 0)
PG_RETURN_JSONB_P(in);

View File

@@ -538,10 +538,7 @@ pg_blocking_pids(PG_FUNCTION_ARGS)
/* Assert we didn't overrun arrayelems[] */
Assert(narrayelems <= lockData->nlocks);
/* Construct array, using hardwired knowledge about int4 type */
PG_RETURN_ARRAYTYPE_P(construct_array(arrayelems, narrayelems,
INT4OID,
sizeof(int32), true, TYPALIGN_INT));
PG_RETURN_ARRAYTYPE_P(construct_array_builtin(arrayelems, narrayelems, INT4OID));
}
@@ -579,10 +576,7 @@ pg_safe_snapshot_blocking_pids(PG_FUNCTION_ARGS)
else
blocker_datums = NULL;
/* Construct array, using hardwired knowledge about int4 type */
PG_RETURN_ARRAYTYPE_P(construct_array(blocker_datums, num_blockers,
INT4OID,
sizeof(int32), true, TYPALIGN_INT));
PG_RETURN_ARRAYTYPE_P(construct_array_builtin(blocker_datums, num_blockers, INT4OID));
}

View File

@@ -314,11 +314,7 @@ current_schemas(PG_FUNCTION_ARGS)
}
list_free(search_path);
array = construct_array(names, i,
NAMEOID,
NAMEDATALEN, /* sizeof(Name) */
false, /* Name is not by-val */
TYPALIGN_CHAR); /* alignment of Name */
array = construct_array_builtin(names, i, NAMEOID);
PG_RETURN_POINTER(array);
}

View File

@@ -759,12 +759,10 @@ percentile_disc_multi_final(PG_FUNCTION_ARGS)
PG_RETURN_NULL();
param = PG_GETARG_ARRAYTYPE_P(1);
deconstruct_array(param, FLOAT8OID,
/* hard-wired info on type float8 */
sizeof(float8), FLOAT8PASSBYVAL, TYPALIGN_DOUBLE,
&percentiles_datum,
&percentiles_null,
&num_percentiles);
deconstruct_array_builtin(param, FLOAT8OID,
&percentiles_datum,
&percentiles_null,
&num_percentiles);
if (num_percentiles == 0)
PG_RETURN_POINTER(construct_empty_array(osastate->qstate->sortColType));
@@ -883,12 +881,10 @@ percentile_cont_multi_final_common(FunctionCallInfo fcinfo,
PG_RETURN_NULL();
param = PG_GETARG_ARRAYTYPE_P(1);
deconstruct_array(param, FLOAT8OID,
/* hard-wired info on type float8 */
sizeof(float8), FLOAT8PASSBYVAL, TYPALIGN_DOUBLE,
&percentiles_datum,
&percentiles_null,
&num_percentiles);
deconstruct_array_builtin(param, FLOAT8OID,
&percentiles_datum,
&percentiles_null,
&num_percentiles);
if (num_percentiles == 0)
PG_RETURN_POINTER(construct_empty_array(osastate->qstate->sortColType));

View File

@@ -214,9 +214,7 @@ binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS)
int ndatums;
int i;
deconstruct_array(textArray,
TEXTOID, -1, false, TYPALIGN_INT,
&textDatums, NULL, &ndatums);
deconstruct_array_builtin(textArray, TEXTOID, &textDatums, NULL, &ndatums);
for (i = 0; i < ndatums; i++)
{
char *extName = TextDatumGetCString(textDatums[i]);

View File

@@ -2406,9 +2406,8 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
if (isnull)
elog(ERROR, "null indkey for index %u", indexId);
deconstruct_array(DatumGetArrayTypeP(cols),
INT2OID, 2, true, TYPALIGN_SHORT,
&keys, NULL, &nKeys);
deconstruct_array_builtin(DatumGetArrayTypeP(cols), INT2OID,
&keys, NULL, &nKeys);
for (j = keyatts; j < nKeys; j++)
{
@@ -2531,9 +2530,8 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
elog(ERROR, "null conexclop for constraint %u",
constraintId);
deconstruct_array(DatumGetArrayTypeP(val),
OIDOID, sizeof(Oid), true, TYPALIGN_INT,
&elems, NULL, &nElems);
deconstruct_array_builtin(DatumGetArrayTypeP(val), OIDOID,
&elems, NULL, &nElems);
operators = (Oid *) palloc(nElems * sizeof(Oid));
for (i = 0; i < nElems; i++)
@@ -2587,9 +2585,8 @@ decompile_column_index_array(Datum column_index_array, Oid relId,
int j;
/* Extract data from array of int16 */
deconstruct_array(DatumGetArrayTypeP(column_index_array),
INT2OID, 2, true, TYPALIGN_SHORT,
&keys, NULL, &nKeys);
deconstruct_array_builtin(DatumGetArrayTypeP(column_index_array), INT2OID,
&keys, NULL, &nKeys);
for (j = 0; j < nKeys; j++)
{
@@ -12752,9 +12749,8 @@ get_reloptions(StringInfo buf, Datum reloptions)
int noptions;
int i;
deconstruct_array(DatumGetArrayTypeP(reloptions),
TEXTOID, -1, false, TYPALIGN_INT,
&options, NULL, &noptions);
deconstruct_array_builtin(DatumGetArrayTypeP(reloptions), TEXTOID,
&options, NULL, &noptions);
for (i = 0; i < noptions; i++)
{

View File

@@ -308,8 +308,7 @@ tsvector_setweight_by_filter(PG_FUNCTION_ARGS)
memcpy(tsout, tsin, VARSIZE(tsin));
entry = ARRPTR(tsout);
deconstruct_array(lexemes, TEXTOID, -1, false, TYPALIGN_INT,
&dlexemes, &nulls, &nlexemes);
deconstruct_array_builtin(lexemes, TEXTOID, &dlexemes, &nulls, &nlexemes);
/*
* Assuming that lexemes array is significantly shorter than tsvector we
@@ -586,8 +585,7 @@ tsvector_delete_arr(PG_FUNCTION_ARGS)
Datum *dlexemes;
bool *nulls;
deconstruct_array(lexemes, TEXTOID, -1, false, TYPALIGN_INT,
&dlexemes, &nulls, &nlex);
deconstruct_array_builtin(lexemes, TEXTOID, &dlexemes, &nulls, &nlex);
/*
* In typical use case array of lexemes to delete is relatively small. So
@@ -694,10 +692,8 @@ tsvector_unnest(PG_FUNCTION_ARGS)
1));
}
values[1] = PointerGetDatum(construct_array(positions, posv->npos,
INT2OID, 2, true, TYPALIGN_SHORT));
values[2] = PointerGetDatum(construct_array(weights, posv->npos,
TEXTOID, -1, false, TYPALIGN_INT));
values[1] = PointerGetDatum(construct_array_builtin(positions, posv->npos, INT2OID));
values[2] = PointerGetDatum(construct_array_builtin(weights, posv->npos, TEXTOID));
}
else
{
@@ -733,7 +729,7 @@ tsvector_to_array(PG_FUNCTION_ARGS)
arrin[i].len));
}
array = construct_array(elements, tsin->size, TEXTOID, -1, false, TYPALIGN_INT);
array = construct_array_builtin(elements, tsin->size, TEXTOID);
pfree(elements);
PG_FREE_IF_COPY(tsin, 0);
@@ -757,7 +753,7 @@ array_to_tsvector(PG_FUNCTION_ARGS)
datalen = 0;
char *cur;
deconstruct_array(v, TEXTOID, -1, false, TYPALIGN_INT, &dlexemes, &nulls, &nitems);
deconstruct_array_builtin(v, TEXTOID, &dlexemes, &nulls, &nitems);
/*
* Reject nulls and zero length strings (maybe we should just ignore them,
@@ -833,8 +829,7 @@ tsvector_filter(PG_FUNCTION_ARGS)
int cur_pos = 0;
char mask = 0;
deconstruct_array(weights, CHAROID, 1, true, TYPALIGN_CHAR,
&dweights, &nulls, &nweights);
deconstruct_array_builtin(weights, CHAROID, &dweights, &nulls, &nweights);
for (i = 0; i < nweights; i++)
{

View File

@@ -4017,9 +4017,9 @@ xpath_internal(text *xpath_expr_text, xmltype *data, ArrayType *namespaces,
Assert(ARR_ELEMTYPE(namespaces) == TEXTOID);
deconstruct_array(namespaces, TEXTOID, -1, false, TYPALIGN_INT,
&ns_names_uris, &ns_names_uris_nulls,
&ns_count);
deconstruct_array_builtin(namespaces, TEXTOID,
&ns_names_uris, &ns_names_uris_nulls,
&ns_count);
Assert((ns_count % 2) == 0); /* checked above */
ns_count /= 2; /* count pairs only */