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)
 
void ArrayCheckBounds (int ndim, const int *dims, const int *lb)
 
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)))

Referenced by ArrayGetNItems().

Function Documentation

◆ ArrayCheckBounds()

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

Definition at line 128 of file arrayutils.c.

References ereport, errcode(), errmsg(), ERROR, i, pg_add_s32_overflow(), and PG_USED_FOR_ASSERTS_ONLY.

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

129 {
130  int i;
131 
132  for (i = 0; i < ndim; i++)
133  {
134  /* PG_USED_FOR_ASSERTS_ONLY prevents variable-isn't-read warnings */
136 
137  if (pg_add_s32_overflow(dims[i], lb[i], &sum))
138  ereport(ERROR,
139  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
140  errmsg("array lower bound is too large: %d",
141  lb[i])));
142  }
143 }
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:104
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:155

◆ ArrayGetIntegerTypmods()

int32* ArrayGetIntegerTypmods ( ArrayType arr,
int *  n 
)

Definition at line 231 of file arrayutils.c.

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

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

232 {
233  int32 *result;
234  Datum *elem_values;
235  int i;
236 
237  if (ARR_ELEMTYPE(arr) != CSTRINGOID)
238  ereport(ERROR,
239  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
240  errmsg("typmod array must be type cstring[]")));
241 
242  if (ARR_NDIM(arr) != 1)
243  ereport(ERROR,
244  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
245  errmsg("typmod array must be one-dimensional")));
246 
247  if (array_contains_nulls(arr))
248  ereport(ERROR,
249  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
250  errmsg("typmod array must not contain nulls")));
251 
252  /* hardwired knowledge about cstring's representation details here */
253  deconstruct_array(arr, CSTRINGOID,
254  -2, false, TYPALIGN_CHAR,
255  &elem_values, NULL, n);
256 
257  result = (int32 *) palloc(*n * sizeof(int32));
258 
259  for (i = 0; i < *n; i++)
260  result[i] = pg_strtoint32(DatumGetCString(elem_values[i]));
261 
262  pfree(elem_values);
263 
264  return result;
265 }
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
#define DatumGetCString(X)
Definition: postgres.h:610
uintptr_t Datum
Definition: postgres.h:411
#define ereport(elevel,...)
Definition: elog.h:157
#define ARR_NDIM(a)
Definition: array.h:283
int32 pg_strtoint32(const char *s)
Definition: numutils.c:263
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3490
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3557
#define ARR_ELEMTYPE(a)
Definition: array.h:285

◆ ArrayGetNItems()

int ArrayGetNItems ( int  ndim,
const int *  dims 
)

Definition at line 76 of file arrayutils.c.

References Assert, ereport, errcode(), errmsg(), ERROR, i, and MaxArraySize.

Referenced by _arrq_cons(), _lca(), _lt_q_regex(), _ltree_compress(), accumArrayResultArr(), array_cardinality(), array_cat(), array_cmp(), array_contain_compare(), array_contains_nulls(), array_create_iterator(), array_eq(), array_fill_internal(), array_get_slice(), array_in(), array_insert_slice(), array_iterator(), array_map(), array_out(), array_recv(), array_replace_internal(), array_send(), array_set_element(), array_set_element_expanded(), array_set_slice(), array_slice_size(), array_to_json_internal(), array_to_jsonb_internal(), array_to_text_internal(), array_unnest(), arrq_cons(), clause_is_strict_for(), construct_md_array(), convert_saop_to_hashed_saop_walker(), count_nulls(), deconstruct_array(), EA_get_flat_size(), estimate_array_length(), ExecEvalArrayExpr(), ExecEvalHashedScalarArrayOp(), ExecEvalScalarArrayOp(), get_text_array_contents(), getWeights(), hash_array(), hash_array_extended(), is_strict_saop(), lt_q_regex(), makeArrayResultArr(), pg_isolation_test_session_is_blocked(), predicate_classify(), sanity_check_tid_array(), width_bucket_array_fixed(), width_bucket_array_float8(), and width_bucket_array_variable().

77 {
78  int32 ret;
79  int i;
80 
81 #define MaxArraySize ((Size) (MaxAllocSize / sizeof(Datum)))
82 
83  if (ndim <= 0)
84  return 0;
85  ret = 1;
86  for (i = 0; i < ndim; i++)
87  {
88  int64 prod;
89 
90  /* A negative dimension implies that UB-LB overflowed ... */
91  if (dims[i] < 0)
92  ereport(ERROR,
93  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
94  errmsg("array size exceeds the maximum allowed (%d)",
95  (int) MaxArraySize)));
96 
97  prod = (int64) ret * (int64) dims[i];
98 
99  ret = (int32) prod;
100  if ((int64) ret != prod)
101  ereport(ERROR,
102  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
103  errmsg("array size exceeds the maximum allowed (%d)",
104  (int) MaxArraySize)));
105  }
106  Assert(ret >= 0);
107  if ((Size) ret > MaxArraySize)
108  ereport(ERROR,
109  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
110  errmsg("array size exceeds the maximum allowed (%d)",
111  (int) MaxArraySize)));
112  return (int) ret;
113 }
#define MaxArraySize
int errcode(int sqlerrcode)
Definition: elog.c:698
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i

◆ ArrayGetOffset()

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

Definition at line 32 of file arrayutils.c.

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().

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:189
int i

◆ ArrayGetOffset0()

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

Definition at line 51 of file arrayutils.c.

References i.

Referenced by ReadArrayStr().

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 }
int scale
Definition: pgbench.c:189
int i

◆ mda_get_offset_values()

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

Definition at line 181 of file arrayutils.c.

References i.

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

182 {
183  int i,
184  j;
185 
186  dist[n - 1] = 0;
187  for (j = n - 2; j >= 0; j--)
188  {
189  dist[j] = prod[j] - 1;
190  for (i = j + 1; i < n; i++)
191  dist[j] -= (span[i] - 1) * prod[i];
192  }
193 }
int i

◆ mda_get_prod()

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

Definition at line 165 of file arrayutils.c.

References i.

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

166 {
167  int i;
168 
169  prod[n - 1] = 1;
170  for (i = n - 2; i >= 0; i--)
171  prod[i] = prod[i + 1] * range[i + 1];
172 }
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
int i

◆ mda_get_range()

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

Definition at line 151 of file arrayutils.c.

References i.

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

152 {
153  int i;
154 
155  for (i = 0; i < n; i++)
156  span[i] = endp[i] - st[i] + 1;
157 }
int i

◆ mda_next_tuple()

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

Definition at line 206 of file arrayutils.c.

References i.

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

207 {
208  int i;
209 
210  if (n <= 0)
211  return -1;
212 
213  curr[n - 1] = (curr[n - 1] + 1) % span[n - 1];
214  for (i = n - 1; i && curr[i] == 0; i--)
215  curr[i - 1] = (curr[i - 1] + 1) % span[i - 1];
216 
217  if (i)
218  return i;
219  if (curr[0])
220  return 0;
221 
222  return -1;
223 }
int i