1/*-------------------------------------------------------------------------
4 * This file contains some support routines required for array functions.
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/adt/arrayutils.c
13 *-------------------------------------------------------------------------
26 * Convert subscript list into linear element number (from 0)
28 * We assume caller has already range-checked the dimensions and subscripts,
29 * so no overflow is possible.
38 for (
i = n - 1;
i >= 0;
i--)
40 offset += (indx[
i] - lb[
i]) *
scale;
47 * Convert array dimensions into number of elements
49 * This must do overflow checking, since it is used to validate that a user
50 * dimensionality request doesn't overflow what we can handle.
52 * The multiplication overflow check only works on machines that have int64
53 * arithmetic, but that is nearly all platforms these days, and doing check
54 * divides for those that don't seems way too expensive.
63 * This entry point can return the error into an ErrorSaveContext
64 * instead of throwing an exception. -1 is returned after an error.
75 for (
i = 0;
i < ndim;
i++)
79 /* A negative dimension implies that UB-LB overflowed ... */
82 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
83 errmsg(
"array size exceeds the maximum allowed (%d)",
89 if ((
int64) ret != prod)
91 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
92 errmsg(
"array size exceeds the maximum allowed (%d)",
98 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
99 errmsg(
"array size exceeds the maximum allowed (%d)",
105 * Verify sanity of proposed lower-bound values for an array
107 * The lower-bound values must not be so large as to cause overflow when
108 * calculating subscripts, e.g. lower bound 2147483640 with length 10
109 * must be disallowed. We actually insist that dims[i] + lb[i] be
110 * computable without overflow, meaning that an array with last subscript
111 * equal to INT_MAX will be disallowed.
113 * It is assumed that the caller already called ArrayGetNItems, so that
114 * overflowed (negative) dims[] values have been eliminated.
123 * This entry point can return the error into an ErrorSaveContext
124 * instead of throwing an exception.
128 struct Node *escontext)
132 for (
i = 0;
i < ndim;
i++)
134 /* PG_USED_FOR_ASSERTS_ONLY prevents variable-isn't-read warnings */
139 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
140 errmsg(
"array lower bound is too large: %d",
148 * Compute ranges (sub-array dimensions) for an array slice
150 * We assume caller has validated slice endpoints, so overflow is impossible
157 for (
i = 0;
i < n;
i++)
158 span[
i] = endp[
i] - st[
i] + 1;
162 * Compute products of array dimensions, ie, scale factors for subscripts
164 * We assume caller has validated dimensions, so overflow is impossible
172 for (
i = n - 2;
i >= 0;
i--)
173 prod[
i] = prod[
i + 1] *
range[
i + 1];
177 * From products of whole-array dimensions and spans of a sub-array,
178 * compute offset distances needed to step through subarray within array
180 * We assume caller has validated dimensions, so overflow is impossible
189 for (
j = n - 2;
j >= 0;
j--)
191 dist[
j] = prod[
j] - 1;
192 for (
i =
j + 1;
i < n;
i++)
193 dist[
j] -= (span[
i] - 1) * prod[
i];
198 * Generates the tuple that is lexicographically one greater than the current
199 * n-tuple in "curr", with the restriction that the i-th element of "curr" is
200 * less than the i-th element of "span".
202 * Returns -1 if no next tuple exists, else the subscript position (0..n-1)
203 * corresponding to the dimension to advance along.
205 * We assume caller has validated dimensions, so overflow is impossible
215 curr[n - 1] = (curr[n - 1] + 1) % span[n - 1];
216 for (
i = n - 1;
i && curr[
i] == 0;
i--)
217 curr[
i - 1] = (curr[
i - 1] + 1) % span[
i - 1];
228 * ArrayGetIntegerTypmods: verify that argument is a 1-D cstring array,
229 * and get the contents converted to integers. Returns a palloc'd array
230 * and places the length at *n.
241 (
errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
242 errmsg(
"typmod array must be type cstring[]")));
246 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
247 errmsg(
"typmod array must be one-dimensional")));
251 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
252 errmsg(
"typmod array must not contain nulls")));
258 for (
i = 0;
i < *n;
i++)
bool array_contains_nulls(ArrayType *array)
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
void mda_get_offset_values(int n, int *dist, const int *prod, const int *span)
int ArrayGetNItemsSafe(int ndim, const int *dims, struct Node *escontext)
bool ArrayCheckBoundsSafe(int ndim, const int *dims, const int *lb, struct Node *escontext)
int ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx)
void mda_get_range(int n, int *span, const int *st, const int *endp)
int ArrayGetNItems(int ndim, const int *dims)
void mda_get_prod(int n, const int *range, int *prod)
void ArrayCheckBounds(int ndim, const int *dims, const int *lb)
int mda_next_tuple(int n, int *curr, const int *span)
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
#define PG_USED_FOR_ASSERTS_ONLY
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereturn(context, dummy_value,...)
#define ereport(elevel,...)
Assert(PointerIsAligned(start, uint64))
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
void pfree(void *pointer)
int32 pg_strtoint32(const char *s)
static char * DatumGetCString(Datum X)
static struct cvec * range(struct vars *v, chr a, chr b, int cases)