1 /*-------------------------------------------------------------------------
4 * This file contains some support routines required for array functions.
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/adt/arrayutils.c
13 *-------------------------------------------------------------------------
18 #include "catalog/pg_type.h"
19 #include "utils/array.h"
20 #include "utils/builtins.h"
21 #include "utils/memutils.h"
25 * Convert subscript list into linear element number (from 0)
27 * We assume caller has already range-checked the dimensions and subscripts,
28 * so no overflow is possible.
31 ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx)
37 for (i = n - 1; i >= 0; i--)
39 offset += (indx[i] - lb[i]) * scale;
46 * Same, but subscripts are assumed 0-based, and use a scale array
47 * instead of raw dimension data (see mda_get_prod to create scale array)
50 ArrayGetOffset0(int n, const int *tup, const int *scale)
55 for (i = 0; i < n; i++)
56 lin += tup[i] * scale[i];
61 * Convert array dimensions into number of elements
63 * This must do overflow checking, since it is used to validate that a user
64 * dimensionality request doesn't overflow what we can handle.
66 * We limit array sizes to at most about a quarter billion elements,
67 * so that it's not necessary to check for overflow in quite so many
68 * places --- for instance when palloc'ing Datum arrays.
70 * The multiplication overflow check only works on machines that have int64
71 * arithmetic, but that is nearly all platforms these days, and doing check
72 * divides for those that don't seems way too expensive.
75 ArrayGetNItems(int ndim, const int *dims)
80 #define MaxArraySize ((Size) (MaxAllocSize / sizeof(Datum)))
85 for (i = 0; i < ndim; i++)
89 /* A negative dimension implies that UB-LB overflowed ... */
92 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
93 errmsg("array size exceeds the maximum allowed (%d)",
94 (int) MaxArraySize)));
96 prod = (int64) ret *(int64) dims[i];
99 if ((int64) ret != prod)
101 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
102 errmsg("array size exceeds the maximum allowed (%d)",
103 (int) MaxArraySize)));
106 if ((Size) ret > MaxArraySize)
108 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
109 errmsg("array size exceeds the maximum allowed (%d)",
110 (int) MaxArraySize)));
115 * Compute ranges (sub-array dimensions) for an array slice
117 * We assume caller has validated slice endpoints, so overflow is impossible
120 mda_get_range(int n, int *span, const int *st, const int *endp)
124 for (i = 0; i < n; i++)
125 span[i] = endp[i] - st[i] + 1;
129 * Compute products of array dimensions, ie, scale factors for subscripts
131 * We assume caller has validated dimensions, so overflow is impossible
134 mda_get_prod(int n, const int *range, int *prod)
139 for (i = n - 2; i >= 0; i--)
140 prod[i] = prod[i + 1] * range[i + 1];
144 * From products of whole-array dimensions and spans of a sub-array,
145 * compute offset distances needed to step through subarray within array
147 * We assume caller has validated dimensions, so overflow is impossible
150 mda_get_offset_values(int n, int *dist, const int *prod, const int *span)
156 for (j = n - 2; j >= 0; j--)
158 dist[j] = prod[j] - 1;
159 for (i = j + 1; i < n; i++)
160 dist[j] -= (span[i] - 1) * prod[i];
165 * Generates the tuple that is lexicographically one greater than the current
166 * n-tuple in "curr", with the restriction that the i-th element of "curr" is
167 * less than the i-th element of "span".
169 * Returns -1 if no next tuple exists, else the subscript position (0..n-1)
170 * corresponding to the dimension to advance along.
172 * We assume caller has validated dimensions, so overflow is impossible
175 mda_next_tuple(int n, int *curr, const int *span)
182 curr[n - 1] = (curr[n - 1] + 1) % span[n - 1];
183 for (i = n - 1; i && curr[i] == 0; i--)
184 curr[i - 1] = (curr[i - 1] + 1) % span[i - 1];
195 * ArrayGetIntegerTypmods: verify that argument is a 1-D cstring array,
196 * and get the contents converted to integers. Returns a palloc'd array
197 * and places the length at *n.
200 ArrayGetIntegerTypmods(ArrayType *arr, int *n)
206 if (ARR_ELEMTYPE(arr) != CSTRINGOID)
208 (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
209 errmsg("typmod array must be type cstring[]")));
211 if (ARR_NDIM(arr) != 1)
213 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
214 errmsg("typmod array must be one-dimensional")));
216 if (array_contains_nulls(arr))
218 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
219 errmsg("typmod array must not contain nulls")));
221 /* hardwired knowledge about cstring's representation details here */
222 deconstruct_array(arr, CSTRINGOID,
224 &elem_values, NULL, n);
226 result = (int32 *) palloc(*n * sizeof(int32));
228 for (i = 0; i < *n; i++)
229 result[i] = pg_atoi(DatumGetCString(elem_values[i]),
230 sizeof(int32), '\0');