PostgreSQL Source Code  git master
arrayutils.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "common/int.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
Include dependency graph for arrayutils.c:

Go to the source code of this file.

Macros

#define MaxArraySize   ((Size) (MaxAllocSize / sizeof(Datum)))
 

Functions

int ArrayGetOffset (int n, const int *dim, const int *lb, const int *indx)
 
int ArrayGetOffset0 (int n, const int *tup, const int *scale)
 
int ArrayGetNItems (int ndim, const int *dims)
 
int ArrayGetNItemsSafe (int ndim, const int *dims, struct Node *escontext)
 
void ArrayCheckBounds (int ndim, const int *dims, const int *lb)
 
bool ArrayCheckBoundsSafe (int ndim, const int *dims, const int *lb, struct Node *escontext)
 
void mda_get_range (int n, int *span, const int *st, const int *endp)
 
void mda_get_prod (int n, const int *range, int *prod)
 
void mda_get_offset_values (int n, int *dist, const int *prod, const int *span)
 
int mda_next_tuple (int n, int *curr, const int *span)
 
int32ArrayGetIntegerTypmods (ArrayType *arr, int *n)
 

Macro Definition Documentation

◆ MaxArraySize

#define MaxArraySize   ((Size) (MaxAllocSize / sizeof(Datum)))

Function Documentation

◆ ArrayCheckBounds()

void ArrayCheckBounds ( int  ndim,
const int *  dims,
const int *  lb 
)

Definition at line 138 of file arrayutils.c.

139 {
140  (void) ArrayCheckBoundsSafe(ndim, dims, lb, NULL);
141 }
bool ArrayCheckBoundsSafe(int ndim, const int *dims, const int *lb, struct Node *escontext)
Definition: arrayutils.c:148

References ArrayCheckBoundsSafe().

Referenced by array_cat(), array_fill_internal(), array_recv(), array_set_element(), array_set_element_expanded(), array_set_slice(), construct_md_array(), ExecEvalArrayExpr(), and makeArrayResultArr().

◆ ArrayCheckBoundsSafe()

bool ArrayCheckBoundsSafe ( int  ndim,
const int *  dims,
const int *  lb,
struct Node escontext 
)

Definition at line 148 of file arrayutils.c.

150 {
151  int i;
152 
153  for (i = 0; i < ndim; i++)
154  {
155  /* PG_USED_FOR_ASSERTS_ONLY prevents variable-isn't-read warnings */
157 
158  if (pg_add_s32_overflow(dims[i], lb[i], &sum))
159  ereturn(escontext, false,
160  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
161  errmsg("array lower bound is too large: %d",
162  lb[i])));
163  }
164 
165  return true;
166 }
signed int int32
Definition: c.h:430
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int i
Definition: isn.c:73

References ereturn, errcode(), errmsg(), i, pg_add_s32_overflow(), and PG_USED_FOR_ASSERTS_ONLY.

Referenced by array_in(), and ArrayCheckBounds().

◆ ArrayGetIntegerTypmods()

int32* ArrayGetIntegerTypmods ( ArrayType arr,
int *  n 
)

Definition at line 254 of file arrayutils.c.

255 {
256  int32 *result;
257  Datum *elem_values;
258  int i;
259 
260  if (ARR_ELEMTYPE(arr) != CSTRINGOID)
261  ereport(ERROR,
262  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
263  errmsg("typmod array must be type cstring[]")));
264 
265  if (ARR_NDIM(arr) != 1)
266  ereport(ERROR,
267  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
268  errmsg("typmod array must be one-dimensional")));
269 
270  if (array_contains_nulls(arr))
271  ereport(ERROR,
272  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
273  errmsg("typmod array must not contain nulls")));
274 
275  deconstruct_array_builtin(arr, CSTRINGOID, &elem_values, NULL, n);
276 
277  result = (int32 *) palloc(*n * sizeof(int32));
278 
279  for (i = 0; i < *n; i++)
280  result[i] = pg_strtoint32(DatumGetCString(elem_values[i]));
281 
282  pfree(elem_values);
283 
284  return result;
285 }
#define ARR_NDIM(a)
Definition: array.h:283
#define ARR_ELEMTYPE(a)
Definition: array.h:285
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3738
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3668
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void pfree(void *pointer)
Definition: mcxt.c:1306
void * palloc(Size size)
Definition: mcxt.c:1199
int32 pg_strtoint32(const char *s)
Definition: numutils.c:187
static char * DatumGetCString(Datum X)
Definition: postgres.h:683
uintptr_t Datum
Definition: postgres.h:412

References ARR_ELEMTYPE, ARR_NDIM, array_contains_nulls(), DatumGetCString(), deconstruct_array_builtin(), ereport, errcode(), errmsg(), ERROR, i, palloc(), pfree(), and pg_strtoint32().

Referenced by anybit_typmodin(), anychar_typmodin(), anytime_typmodin(), anytimestamp_typmodin(), intervaltypmodin(), and numerictypmodin().

◆ ArrayGetNItems()

◆ ArrayGetNItemsSafe()

int ArrayGetNItemsSafe ( int  ndim,
const int *  dims,
struct Node escontext 
)

Definition at line 86 of file arrayutils.c.

87 {
88  int32 ret;
89  int i;
90 
91 #define MaxArraySize ((Size) (MaxAllocSize / sizeof(Datum)))
92 
93  if (ndim <= 0)
94  return 0;
95  ret = 1;
96  for (i = 0; i < ndim; i++)
97  {
98  int64 prod;
99 
100  /* A negative dimension implies that UB-LB overflowed ... */
101  if (dims[i] < 0)
102  ereturn(escontext, -1,
103  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
104  errmsg("array size exceeds the maximum allowed (%d)",
105  (int) MaxArraySize)));
106 
107  prod = (int64) ret * (int64) dims[i];
108 
109  ret = (int32) prod;
110  if ((int64) ret != prod)
111  ereturn(escontext, -1,
112  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
113  errmsg("array size exceeds the maximum allowed (%d)",
114  (int) MaxArraySize)));
115  }
116  Assert(ret >= 0);
117  if ((Size) ret > MaxArraySize)
118  ereturn(escontext, -1,
119  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
120  errmsg("array size exceeds the maximum allowed (%d)",
121  (int) MaxArraySize)));
122  return (int) ret;
123 }
#define MaxArraySize
size_t Size
Definition: c.h:541
Assert(fmt[strlen(fmt) - 1] !='\n')

References Assert(), ereturn, errcode(), errmsg(), i, and MaxArraySize.

Referenced by array_in(), and ArrayGetNItems().

◆ ArrayGetOffset()

int ArrayGetOffset ( int  n,
const int *  dim,
const int *  lb,
const int *  indx 
)

Definition at line 32 of file arrayutils.c.

33 {
34  int i,
35  scale = 1,
36  offset = 0;
37 
38  for (i = n - 1; i >= 0; i--)
39  {
40  offset += (indx[i] - lb[i]) * scale;
41  scale *= dim[i];
42  }
43  return offset;
44 }
int scale
Definition: pgbench.c:190

References i, and scale.

Referenced by array_extract_slice(), array_get_element(), array_get_element_expanded(), array_insert_slice(), array_set_element(), array_set_element_expanded(), and array_slice_size().

◆ ArrayGetOffset0()

int ArrayGetOffset0 ( int  n,
const int *  tup,
const int *  scale 
)

Definition at line 51 of file arrayutils.c.

52 {
53  int i,
54  lin = 0;
55 
56  for (i = 0; i < n; i++)
57  lin += tup[i] * scale[i];
58  return lin;
59 }

References i, and scale.

Referenced by ReadArrayStr().

◆ mda_get_offset_values()

void mda_get_offset_values ( int  n,
int *  dist,
const int *  prod,
const int *  span 
)

Definition at line 204 of file arrayutils.c.

205 {
206  int i,
207  j;
208 
209  dist[n - 1] = 0;
210  for (j = n - 2; j >= 0; j--)
211  {
212  dist[j] = prod[j] - 1;
213  for (i = j + 1; i < n; i++)
214  dist[j] -= (span[i] - 1) * prod[i];
215  }
216 }
int j
Definition: isn.c:74

References i, and j.

Referenced by array_extract_slice(), array_insert_slice(), and array_slice_size().

◆ mda_get_prod()

void mda_get_prod ( int  n,
const int *  range,
int *  prod 
)

Definition at line 188 of file arrayutils.c.

189 {
190  int i;
191 
192  prod[n - 1] = 1;
193  for (i = n - 2; i >= 0; i--)
194  prod[i] = prod[i + 1] * range[i + 1];
195 }
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412

References i, and range().

Referenced by array_extract_slice(), array_insert_slice(), array_slice_size(), and ReadArrayStr().

◆ mda_get_range()

void mda_get_range ( int  n,
int *  span,
const int *  st,
const int *  endp 
)

Definition at line 174 of file arrayutils.c.

175 {
176  int i;
177 
178  for (i = 0; i < n; i++)
179  span[i] = endp[i] - st[i] + 1;
180 }

References i.

Referenced by array_extract_slice(), array_get_slice(), array_insert_slice(), array_set_slice(), and array_slice_size().

◆ mda_next_tuple()

int mda_next_tuple ( int  n,
int *  curr,
const int *  span 
)

Definition at line 229 of file arrayutils.c.

230 {
231  int i;
232 
233  if (n <= 0)
234  return -1;
235 
236  curr[n - 1] = (curr[n - 1] + 1) % span[n - 1];
237  for (i = n - 1; i && curr[i] == 0; i--)
238  curr[i - 1] = (curr[i - 1] + 1) % span[i - 1];
239 
240  if (i)
241  return i;
242  if (curr[0])
243  return 0;
244 
245  return -1;
246 }

References i.

Referenced by array_extract_slice(), array_insert_slice(), and array_slice_size().