MCPcopy Create free account
hub / github.com/apache/cloudberry / trim_array

Function trim_array

src/backend/utils/adt/arrayfuncs.c:6800–6837  ·  view source on GitHub ↗

* Trim the last N elements from an array by building an appropriate slice. * Only the first dimension is trimmed. */

Source from the content-addressed store, hash-verified

6798 * Only the first dimension is trimmed.
6799 */
6800Datum
6801trim_array(PG_FUNCTION_ARGS)
6802{
6803 ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
6804 int n = PG_GETARG_INT32(1);
6805 int array_length = ARR_DIMS(v)[0];
6806 int16 elmlen;
6807 bool elmbyval;
6808 char elmalign;
6809 int lower[MAXDIM];
6810 int upper[MAXDIM];
6811 bool lowerProvided[MAXDIM];
6812 bool upperProvided[MAXDIM];
6813 Datum result;
6814
6815 /* Per spec, throw an error if out of bounds */
6816 if (n < 0 || n > array_length)
6817 ereport(ERROR,
6818 (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
6819 errmsg("number of elements to trim must be between 0 and %d",
6820 array_length)));
6821
6822 /* Set all the bounds as unprovided except the first upper bound */
6823 memset(lowerProvided, false, sizeof(lowerProvided));
6824 memset(upperProvided, false, sizeof(upperProvided));
6825 upper[0] = ARR_LBOUND(v)[0] + array_length - n - 1;
6826 upperProvided[0] = true;
6827
6828 /* Fetch the needed information about the element type */
6829 get_typlenbyvalalign(ARR_ELEMTYPE(v), &elmlen, &elmbyval, &elmalign);
6830
6831 /* Get the slice */
6832 result = array_get_slice(PointerGetDatum(v), 1,
6833 upper, lower, upperProvided, lowerProvided,
6834 -1, elmlen, elmbyval, elmalign);
6835
6836 PG_RETURN_DATUM(result);
6837}

Callers

nothing calls this directly

Calls 4

get_typlenbyvalalignFunction · 0.85
array_get_sliceFunction · 0.85
errcodeFunction · 0.50
errmsgFunction · 0.50

Tested by

no test coverage detected