PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
array_userfuncs.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
Include dependency graph for array_userfuncs.c:

Go to the source code of this file.

Functions

static Datum array_position_common (FunctionCallInfo fcinfo)
 
static ExpandedArrayHeaderfetch_array_arg_replace_nulls (FunctionCallInfo fcinfo, int argno)
 
Datum array_append (PG_FUNCTION_ARGS)
 
Datum array_prepend (PG_FUNCTION_ARGS)
 
Datum array_cat (PG_FUNCTION_ARGS)
 
Datum array_agg_transfn (PG_FUNCTION_ARGS)
 
Datum array_agg_finalfn (PG_FUNCTION_ARGS)
 
Datum array_agg_array_transfn (PG_FUNCTION_ARGS)
 
Datum array_agg_array_finalfn (PG_FUNCTION_ARGS)
 
Datum array_position (PG_FUNCTION_ARGS)
 
Datum array_position_start (PG_FUNCTION_ARGS)
 
Datum array_positions (PG_FUNCTION_ARGS)
 

Function Documentation

Datum array_agg_array_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 586 of file array_userfuncs.c.

References AggCheckCallContext(), Assert, CurrentMemoryContext, makeArrayResultArr(), NULL, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_DATUM, PG_RETURN_NULL, and result.

587 {
588  Datum result;
590 
591  /* cannot be called directly because of internal-type argument */
592  Assert(AggCheckCallContext(fcinfo, NULL));
593 
595 
596  if (state == NULL)
597  PG_RETURN_NULL(); /* returns null iff no input values */
598 
599  /*
600  * Make the result. We cannot release the ArrayBuildStateArr because
601  * sometimes aggregate final functions are re-executed. Rather, it is
602  * nodeAgg.c's responsibility to reset the aggcontext when it's safe to do
603  * so.
604  */
605  result = makeArrayResultArr(state, CurrentMemoryContext, false);
606 
607  PG_RETURN_DATUM(result);
608 }
Datum makeArrayResultArr(ArrayBuildStateArr *astate, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5341
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
Definition: regguts.h:298
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum array_agg_array_transfn ( PG_FUNCTION_ARGS  )

Definition at line 542 of file array_userfuncs.c.

References accumArrayResultArr(), AggCheckCallContext(), elog, ereport, errcode(), errmsg(), ERROR, get_fn_expr_argtype(), initArrayResultArr(), InvalidOid, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, and PG_RETURN_POINTER.

543 {
544  Oid arg1_typeid = get_fn_expr_argtype(fcinfo->flinfo, 1);
545  MemoryContext aggcontext;
547 
548  if (arg1_typeid == InvalidOid)
549  ereport(ERROR,
550  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
551  errmsg("could not determine input data type")));
552 
553  /*
554  * Note: we do not need a run-time check about whether arg1_typeid is a
555  * valid array type, because the parser would have verified that while
556  * resolving the input/result types of this polymorphic aggregate.
557  */
558 
559  if (!AggCheckCallContext(fcinfo, &aggcontext))
560  {
561  /* cannot be called directly because of internal-type argument */
562  elog(ERROR, "array_agg_array_transfn called in non-aggregate context");
563  }
564 
565 
566  if (PG_ARGISNULL(0))
567  state = initArrayResultArr(arg1_typeid, InvalidOid, aggcontext, false);
568  else
569  state = (ArrayBuildStateArr *) PG_GETARG_POINTER(0);
570 
571  state = accumArrayResultArr(state,
572  PG_GETARG_DATUM(1),
573  PG_ARGISNULL(1),
574  arg1_typeid,
575  aggcontext);
576 
577  /*
578  * The transition type for array_agg() is declared to be "internal", which
579  * is a pass-by-value type the same size as a pointer. So we can safely
580  * pass the ArrayBuildStateArr pointer through nodeAgg.c's machinations.
581  */
582  PG_RETURN_POINTER(state);
583 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
ArrayBuildStateArr * initArrayResultArr(Oid array_type, Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5138
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1909
ArrayBuildStateArr * accumArrayResultArr(ArrayBuildStateArr *astate, Datum dvalue, bool disnull, Oid array_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5184
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
Definition: regguts.h:298
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Datum array_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 507 of file array_userfuncs.c.

References AggCheckCallContext(), Assert, CurrentMemoryContext, makeMdArrayResult(), ArrayBuildState::nelems, NULL, PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_DATUM, PG_RETURN_NULL, and result.

508 {
509  Datum result;
511  int dims[1];
512  int lbs[1];
513 
514  /* cannot be called directly because of internal-type argument */
515  Assert(AggCheckCallContext(fcinfo, NULL));
516 
517  state = PG_ARGISNULL(0) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(0);
518 
519  if (state == NULL)
520  PG_RETURN_NULL(); /* returns null iff no input values */
521 
522  dims[0] = state->nelems;
523  lbs[0] = 1;
524 
525  /*
526  * Make the result. We cannot release the ArrayBuildState because
527  * sometimes aggregate final functions are re-executed. Rather, it is
528  * nodeAgg.c's responsibility to reset the aggcontext when it's safe to do
529  * so.
530  */
531  result = makeMdArrayResult(state, 1, dims, lbs,
533  false);
534 
535  PG_RETURN_DATUM(result);
536 }
Datum makeMdArrayResult(ArrayBuildState *astate, int ndims, int *dims, int *lbs, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5086
return result
Definition: formatting.c:1633
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
Definition: regguts.h:298
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum array_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 461 of file array_userfuncs.c.

References accumArrayResult(), AggCheckCallContext(), elog, ereport, errcode(), errmsg(), ERROR, get_fn_expr_argtype(), initArrayResult(), InvalidOid, PG_ARGISNULL, PG_GETARG_DATUM, PG_GETARG_POINTER, and PG_RETURN_POINTER.

462 {
463  Oid arg1_typeid = get_fn_expr_argtype(fcinfo->flinfo, 1);
464  MemoryContext aggcontext;
466  Datum elem;
467 
468  if (arg1_typeid == InvalidOid)
469  ereport(ERROR,
470  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
471  errmsg("could not determine input data type")));
472 
473  /*
474  * Note: we do not need a run-time check about whether arg1_typeid is a
475  * valid array element type, because the parser would have verified that
476  * while resolving the input/result types of this polymorphic aggregate.
477  */
478 
479  if (!AggCheckCallContext(fcinfo, &aggcontext))
480  {
481  /* cannot be called directly because of internal-type argument */
482  elog(ERROR, "array_agg_transfn called in non-aggregate context");
483  }
484 
485  if (PG_ARGISNULL(0))
486  state = initArrayResult(arg1_typeid, aggcontext, false);
487  else
488  state = (ArrayBuildState *) PG_GETARG_POINTER(0);
489 
490  elem = PG_ARGISNULL(1) ? (Datum) 0 : PG_GETARG_DATUM(1);
491 
492  state = accumArrayResult(state,
493  elem,
494  PG_ARGISNULL(1),
495  arg1_typeid,
496  aggcontext);
497 
498  /*
499  * The transition type for array_agg() is declared to be "internal", which
500  * is a pass-by-value type the same size as a pointer. So we can safely
501  * pass the ArrayBuildState pointer through nodeAgg.c's machinations.
502  */
503  PG_RETURN_POINTER(state);
504 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:4951
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1909
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
Definition: regguts.h:298
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:4990
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
Datum array_append ( PG_FUNCTION_ARGS  )

Definition at line 100 of file array_userfuncs.c.

References array_set_element(), ExpandedArrayHeader::dims, EOHPGetRWDatum, ereport, errcode(), errmsg(), ERROR, fetch_array_arg_replace_nulls(), ExpandedArrayHeader::hdr, ExpandedArrayHeader::lbound, ExpandedArrayHeader::ndims, PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_DATUM, result, ArrayMetaState::typalign, ArrayMetaState::typbyval, and ArrayMetaState::typlen.

101 {
102  ExpandedArrayHeader *eah;
103  Datum newelem;
104  bool isNull;
105  Datum result;
106  int *dimv,
107  *lb;
108  int indx;
109  ArrayMetaState *my_extra;
110 
111  eah = fetch_array_arg_replace_nulls(fcinfo, 0);
112  isNull = PG_ARGISNULL(1);
113  if (isNull)
114  newelem = (Datum) 0;
115  else
116  newelem = PG_GETARG_DATUM(1);
117 
118  if (eah->ndims == 1)
119  {
120  /* append newelem */
121  int ub;
122 
123  lb = eah->lbound;
124  dimv = eah->dims;
125  ub = dimv[0] + lb[0] - 1;
126  indx = ub + 1;
127 
128  /* overflow? */
129  if (indx < ub)
130  ereport(ERROR,
131  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
132  errmsg("integer out of range")));
133  }
134  else if (eah->ndims == 0)
135  indx = 1;
136  else
137  ereport(ERROR,
138  (errcode(ERRCODE_DATA_EXCEPTION),
139  errmsg("argument must be empty or one-dimensional array")));
140 
141  /* Perform element insertion */
142  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
143 
144  result = array_set_element(EOHPGetRWDatum(&eah->hdr),
145  1, &indx, newelem, isNull,
146  -1, my_extra->typlen, my_extra->typbyval, my_extra->typalign);
147 
148  PG_RETURN_DATUM(result);
149 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
int errcode(int sqlerrcode)
Definition: elog.c:575
Datum array_set_element(Datum arraydatum, int nSubscripts, int *indx, Datum dataValue, bool isNull, int arraytyplen, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:2186
return result
Definition: formatting.c:1633
bool typbyval
Definition: array.h:221
static ExpandedArrayHeader * fetch_array_arg_replace_nulls(FunctionCallInfo fcinfo, int argno)
#define ERROR
Definition: elog.h:43
ExpandedObjectHeader hdr
Definition: array.h:102
int16 typlen
Definition: array.h:220
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
char typalign
Definition: array.h:222
#define EOHPGetRWDatum(eohptr)
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum array_cat ( PG_FUNCTION_ARGS  )

Definition at line 223 of file array_userfuncs.c.

References ARR_DATA_OFFSET, ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_LBOUND, ARR_NDIM, ARR_NULLBITMAP, ARR_OVERHEAD_NONULLS, ARR_OVERHEAD_WITHNULLS, ARR_SIZE, array_bitmap_copy(), ArrayGetNItems(), ArrayType::dataoffset, ArrayType::elemtype, ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), i, ArrayType::ndim, palloc(), palloc0(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_RETURN_ARRAYTYPE_P, PG_RETURN_NULL, result, and SET_VARSIZE.

224 {
225  ArrayType *v1,
226  *v2;
227  ArrayType *result;
228  int *dims,
229  *lbs,
230  ndims,
231  nitems,
232  ndatabytes,
233  nbytes;
234  int *dims1,
235  *lbs1,
236  ndims1,
237  nitems1,
238  ndatabytes1;
239  int *dims2,
240  *lbs2,
241  ndims2,
242  nitems2,
243  ndatabytes2;
244  int i;
245  char *dat1,
246  *dat2;
247  bits8 *bitmap1,
248  *bitmap2;
249  Oid element_type;
250  Oid element_type1;
251  Oid element_type2;
252  int32 dataoffset;
253 
254  /* Concatenating a null array is a no-op, just return the other input */
255  if (PG_ARGISNULL(0))
256  {
257  if (PG_ARGISNULL(1))
258  PG_RETURN_NULL();
259  result = PG_GETARG_ARRAYTYPE_P(1);
260  PG_RETURN_ARRAYTYPE_P(result);
261  }
262  if (PG_ARGISNULL(1))
263  {
264  result = PG_GETARG_ARRAYTYPE_P(0);
265  PG_RETURN_ARRAYTYPE_P(result);
266  }
267 
268  v1 = PG_GETARG_ARRAYTYPE_P(0);
269  v2 = PG_GETARG_ARRAYTYPE_P(1);
270 
271  element_type1 = ARR_ELEMTYPE(v1);
272  element_type2 = ARR_ELEMTYPE(v2);
273 
274  /* Check we have matching element types */
275  if (element_type1 != element_type2)
276  ereport(ERROR,
277  (errcode(ERRCODE_DATATYPE_MISMATCH),
278  errmsg("cannot concatenate incompatible arrays"),
279  errdetail("Arrays with element types %s and %s are not "
280  "compatible for concatenation.",
281  format_type_be(element_type1),
282  format_type_be(element_type2))));
283 
284  /* OK, use it */
285  element_type = element_type1;
286 
287  /*----------
288  * We must have one of the following combinations of inputs:
289  * 1) one empty array, and one non-empty array
290  * 2) both arrays empty
291  * 3) two arrays with ndims1 == ndims2
292  * 4) ndims1 == ndims2 - 1
293  * 5) ndims1 == ndims2 + 1
294  *----------
295  */
296  ndims1 = ARR_NDIM(v1);
297  ndims2 = ARR_NDIM(v2);
298 
299  /*
300  * short circuit - if one input array is empty, and the other is not, we
301  * return the non-empty one as the result
302  *
303  * if both are empty, return the first one
304  */
305  if (ndims1 == 0 && ndims2 > 0)
307 
308  if (ndims2 == 0)
310 
311  /* the rest fall under rule 3, 4, or 5 */
312  if (ndims1 != ndims2 &&
313  ndims1 != ndims2 - 1 &&
314  ndims1 != ndims2 + 1)
315  ereport(ERROR,
316  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
317  errmsg("cannot concatenate incompatible arrays"),
318  errdetail("Arrays of %d and %d dimensions are not "
319  "compatible for concatenation.",
320  ndims1, ndims2)));
321 
322  /* get argument array details */
323  lbs1 = ARR_LBOUND(v1);
324  lbs2 = ARR_LBOUND(v2);
325  dims1 = ARR_DIMS(v1);
326  dims2 = ARR_DIMS(v2);
327  dat1 = ARR_DATA_PTR(v1);
328  dat2 = ARR_DATA_PTR(v2);
329  bitmap1 = ARR_NULLBITMAP(v1);
330  bitmap2 = ARR_NULLBITMAP(v2);
331  nitems1 = ArrayGetNItems(ndims1, dims1);
332  nitems2 = ArrayGetNItems(ndims2, dims2);
333  ndatabytes1 = ARR_SIZE(v1) - ARR_DATA_OFFSET(v1);
334  ndatabytes2 = ARR_SIZE(v2) - ARR_DATA_OFFSET(v2);
335 
336  if (ndims1 == ndims2)
337  {
338  /*
339  * resulting array is made up of the elements (possibly arrays
340  * themselves) of the input argument arrays
341  */
342  ndims = ndims1;
343  dims = (int *) palloc(ndims * sizeof(int));
344  lbs = (int *) palloc(ndims * sizeof(int));
345 
346  dims[0] = dims1[0] + dims2[0];
347  lbs[0] = lbs1[0];
348 
349  for (i = 1; i < ndims; i++)
350  {
351  if (dims1[i] != dims2[i] || lbs1[i] != lbs2[i])
352  ereport(ERROR,
353  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
354  errmsg("cannot concatenate incompatible arrays"),
355  errdetail("Arrays with differing element dimensions are "
356  "not compatible for concatenation.")));
357 
358  dims[i] = dims1[i];
359  lbs[i] = lbs1[i];
360  }
361  }
362  else if (ndims1 == ndims2 - 1)
363  {
364  /*
365  * resulting array has the second argument as the outer array, with
366  * the first argument inserted at the front of the outer dimension
367  */
368  ndims = ndims2;
369  dims = (int *) palloc(ndims * sizeof(int));
370  lbs = (int *) palloc(ndims * sizeof(int));
371  memcpy(dims, dims2, ndims * sizeof(int));
372  memcpy(lbs, lbs2, ndims * sizeof(int));
373 
374  /* increment number of elements in outer array */
375  dims[0] += 1;
376 
377  /* make sure the added element matches our existing elements */
378  for (i = 0; i < ndims1; i++)
379  {
380  if (dims1[i] != dims[i + 1] || lbs1[i] != lbs[i + 1])
381  ereport(ERROR,
382  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
383  errmsg("cannot concatenate incompatible arrays"),
384  errdetail("Arrays with differing dimensions are not "
385  "compatible for concatenation.")));
386  }
387  }
388  else
389  {
390  /*
391  * (ndims1 == ndims2 + 1)
392  *
393  * resulting array has the first argument as the outer array, with the
394  * second argument appended to the end of the outer dimension
395  */
396  ndims = ndims1;
397  dims = (int *) palloc(ndims * sizeof(int));
398  lbs = (int *) palloc(ndims * sizeof(int));
399  memcpy(dims, dims1, ndims * sizeof(int));
400  memcpy(lbs, lbs1, ndims * sizeof(int));
401 
402  /* increment number of elements in outer array */
403  dims[0] += 1;
404 
405  /* make sure the added element matches our existing elements */
406  for (i = 0; i < ndims2; i++)
407  {
408  if (dims2[i] != dims[i + 1] || lbs2[i] != lbs[i + 1])
409  ereport(ERROR,
410  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
411  errmsg("cannot concatenate incompatible arrays"),
412  errdetail("Arrays with differing dimensions are not "
413  "compatible for concatenation.")));
414  }
415  }
416 
417  /* Do this mainly for overflow checking */
418  nitems = ArrayGetNItems(ndims, dims);
419 
420  /* build the result array */
421  ndatabytes = ndatabytes1 + ndatabytes2;
422  if (ARR_HASNULL(v1) || ARR_HASNULL(v2))
423  {
424  dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
425  nbytes = ndatabytes + dataoffset;
426  }
427  else
428  {
429  dataoffset = 0; /* marker for no null bitmap */
430  nbytes = ndatabytes + ARR_OVERHEAD_NONULLS(ndims);
431  }
432  result = (ArrayType *) palloc0(nbytes);
433  SET_VARSIZE(result, nbytes);
434  result->ndim = ndims;
435  result->dataoffset = dataoffset;
436  result->elemtype = element_type;
437  memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
438  memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
439  /* data area is arg1 then arg2 */
440  memcpy(ARR_DATA_PTR(result), dat1, ndatabytes1);
441  memcpy(ARR_DATA_PTR(result) + ndatabytes1, dat2, ndatabytes2);
442  /* handle the null bitmap if needed */
443  if (ARR_HASNULL(result))
444  {
446  bitmap1, 0,
447  nitems1);
448  array_bitmap_copy(ARR_NULLBITMAP(result), nitems1,
449  bitmap2, 0,
450  nitems2);
451  }
452 
453  PG_RETURN_ARRAYTYPE_P(result);
454 }
#define ARR_OVERHEAD_NONULLS(ndims)
Definition: array.h:291
#define ARR_SIZE(a)
Definition: array.h:270
void array_bitmap_copy(bits8 *destbitmap, int destoffset, const bits8 *srcbitmap, int srcoffset, int nitems)
Definition: arrayfuncs.c:4624
int32 dataoffset
Definition: array.h:80
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
#define ARR_OVERHEAD_WITHNULLS(ndims, nitems)
Definition: array.h:293
signed int int32
Definition: c.h:256
#define ARR_DATA_OFFSET(a)
Definition: array.h:297
#define ARR_LBOUND(a)
Definition: array.h:277
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
#define ERROR
Definition: elog.h:43
Oid elemtype
Definition: array.h:81
#define ARR_DIMS(a)
Definition: array.h:275
#define ARR_DATA_PTR(a)
Definition: array.h:303
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ARR_HASNULL(a)
Definition: array.h:272
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
#define ereport(elevel, rest)
Definition: elog.h:122
uint8 bits8
Definition: c.h:275
void * palloc0(Size size)
Definition: mcxt.c:878
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define ARR_NDIM(a)
Definition: array.h:271
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define ARR_NULLBITMAP(a)
Definition: array.h:281
#define PG_RETURN_NULL()
Definition: fmgr.h:305
int ndim
Definition: array.h:79
Datum array_position ( PG_FUNCTION_ARGS  )

Definition at line 619 of file array_userfuncs.c.

References array_position_common().

620 {
621  return array_position_common(fcinfo);
622 }
static Datum array_position_common(FunctionCallInfo fcinfo)
static Datum array_position_common ( FunctionCallInfo  fcinfo)
static

Definition at line 638 of file array_userfuncs.c.

References ARR_ELEMTYPE, ARR_LBOUND, ARR_NDIM, array_contains_nulls(), array_create_iterator(), array_free_iterator(), array_iterate(), array_iterator(), DatumGetBool, ArrayMetaState::element_type, TypeCacheEntry::eq_opr_finfo, ereport, errcode(), errmsg(), ERROR, FunctionCallInfoData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, format_type_be(), FunctionCall2Coll(), get_typlenbyvalalign(), lookup_type_cache(), MemoryContextAlloc(), NULL, OidIsValid, PG_ARGISNULL, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_GETARG_INT32, PG_NARGS, PG_RETURN_INT32, PG_RETURN_NULL, ArrayMetaState::proc, ArrayMetaState::typalign, ArrayMetaState::typbyval, TYPECACHE_EQ_OPR_FINFO, ArrayMetaState::typlen, and value.

Referenced by array_position(), and array_position_start().

639 {
640  ArrayType *array;
641  Oid collation = PG_GET_COLLATION();
642  Oid element_type;
643  Datum searched_element,
644  value;
645  bool isnull;
646  int position,
647  position_min;
648  bool found = false;
649  TypeCacheEntry *typentry;
650  ArrayMetaState *my_extra;
651  bool null_search;
653 
654  if (PG_ARGISNULL(0))
655  PG_RETURN_NULL();
656 
657  array = PG_GETARG_ARRAYTYPE_P(0);
658  element_type = ARR_ELEMTYPE(array);
659 
660  /*
661  * We refuse to search for elements in multi-dimensional arrays, since we
662  * have no good way to report the element's location in the array.
663  */
664  if (ARR_NDIM(array) > 1)
665  ereport(ERROR,
666  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
667  errmsg("searching for elements in multidimensional arrays is not supported")));
668 
669  if (PG_ARGISNULL(1))
670  {
671  /* fast return when the array doesn't have nulls */
672  if (!array_contains_nulls(array))
673  PG_RETURN_NULL();
674  searched_element = (Datum) 0;
675  null_search = true;
676  }
677  else
678  {
679  searched_element = PG_GETARG_DATUM(1);
680  null_search = false;
681  }
682 
683  position = (ARR_LBOUND(array))[0] - 1;
684 
685  /* figure out where to start */
686  if (PG_NARGS() == 3)
687  {
688  if (PG_ARGISNULL(2))
689  ereport(ERROR,
690  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
691  errmsg("initial position must not be null")));
692 
693  position_min = PG_GETARG_INT32(2);
694  }
695  else
696  position_min = (ARR_LBOUND(array))[0];
697 
698  /*
699  * We arrange to look up type info for array_create_iterator only once per
700  * series of calls, assuming the element type doesn't change underneath
701  * us.
702  */
703  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
704  if (my_extra == NULL)
705  {
706  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
707  sizeof(ArrayMetaState));
708  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
709  my_extra->element_type = ~element_type;
710  }
711 
712  if (my_extra->element_type != element_type)
713  {
714  get_typlenbyvalalign(element_type,
715  &my_extra->typlen,
716  &my_extra->typbyval,
717  &my_extra->typalign);
718 
719  typentry = lookup_type_cache(element_type, TYPECACHE_EQ_OPR_FINFO);
720 
721  if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
722  ereport(ERROR,
723  (errcode(ERRCODE_UNDEFINED_FUNCTION),
724  errmsg("could not identify an equality operator for type %s",
725  format_type_be(element_type))));
726 
727  my_extra->element_type = element_type;
728  fmgr_info_cxt(typentry->eq_opr_finfo.fn_oid, &my_extra->proc,
729  fcinfo->flinfo->fn_mcxt);
730  }
731 
732  /* Examine each array element until we find a match. */
733  array_iterator = array_create_iterator(array, 0, my_extra);
734  while (array_iterate(array_iterator, &value, &isnull))
735  {
736  position++;
737 
738  /* skip initial elements if caller requested so */
739  if (position < position_min)
740  continue;
741 
742  /*
743  * Can't look at the array element's value if it's null; but if we
744  * search for null, we have a hit and are done.
745  */
746  if (isnull || null_search)
747  {
748  if (isnull && null_search)
749  {
750  found = true;
751  break;
752  }
753  else
754  continue;
755  }
756 
757  /* not nulls, so run the operator */
758  if (DatumGetBool(FunctionCall2Coll(&my_extra->proc, collation,
759  searched_element, value)))
760  {
761  found = true;
762  break;
763  }
764  }
765 
766  array_free_iterator(array_iterator);
767 
768  /* Avoid leaking memory when handed toasted input */
769  PG_FREE_IF_COPY(array, 0);
770 
771  if (!found)
772  PG_RETURN_NULL();
773 
774  PG_RETURN_INT32(position);
775 }
ArrayIterator array_create_iterator(ArrayType *arr, int slice_ndim, ArrayMetaState *mstate)
Definition: arrayfuncs.c:4255
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
MemoryContext fn_mcxt
Definition: fmgr.h:65
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2021
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define TYPECACHE_EQ_OPR_FINFO
Definition: typcache.h:115
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1047
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define ARR_LBOUND(a)
Definition: array.h:277
bool typbyval
Definition: array.h:221
FmgrInfo * flinfo
Definition: fmgr.h:79
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
bool array_iterate(ArrayIterator iterator, Datum *value, bool *isnull)
Definition: arrayfuncs.c:4334
#define ERROR
Definition: elog.h:43
static struct @121 value
static bool array_iterator(ArrayType *la, PGCALL2 callback, void *param, ltree **found)
Definition: _ltree_op.c:37
int16 typlen
Definition: array.h:220
#define DatumGetBool(X)
Definition: postgres.h:399
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
FmgrInfo eq_opr_finfo
Definition: typcache.h:67
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
Oid fn_oid
Definition: fmgr.h:59
void array_free_iterator(ArrayIterator iterator)
Definition: arrayfuncs.c:4417
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_NARGS()
Definition: fmgr.h:168
void * fn_extra
Definition: fmgr.h:64
#define ARR_NDIM(a)
Definition: array.h:271
char typalign
Definition: array.h:222
int errmsg(const char *fmt,...)
Definition: elog.c:797
FmgrInfo proc
Definition: array.h:226
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
Oid element_type
Definition: array.h:219
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3542
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum array_position_start ( PG_FUNCTION_ARGS  )

Definition at line 625 of file array_userfuncs.c.

References array_position_common().

626 {
627  return array_position_common(fcinfo);
628 }
static Datum array_position_common(FunctionCallInfo fcinfo)
Datum array_positions ( PG_FUNCTION_ARGS  )

Definition at line 789 of file array_userfuncs.c.

References accumArrayResult(), ARR_ELEMTYPE, ARR_LBOUND, ARR_NDIM, array_contains_nulls(), array_create_iterator(), array_free_iterator(), array_iterate(), array_iterator(), CurrentMemoryContext, DatumGetBool, ArrayMetaState::element_type, TypeCacheEntry::eq_opr_finfo, ereport, errcode(), errmsg(), ERROR, fmgr_info_cxt(), FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, format_type_be(), FunctionCall2Coll(), get_typlenbyvalalign(), initArrayResult(), Int32GetDatum, INT4OID, lookup_type_cache(), makeArrayResult(), MemoryContextAlloc(), NULL, OidIsValid, PG_ARGISNULL, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_RETURN_DATUM, PG_RETURN_NULL, ArrayMetaState::proc, ArrayMetaState::typalign, ArrayMetaState::typbyval, TYPECACHE_EQ_OPR_FINFO, ArrayMetaState::typlen, and value.

790 {
791  ArrayType *array;
792  Oid collation = PG_GET_COLLATION();
793  Oid element_type;
794  Datum searched_element,
795  value;
796  bool isnull;
797  int position;
798  TypeCacheEntry *typentry;
799  ArrayMetaState *my_extra;
800  bool null_search;
802  ArrayBuildState *astate = NULL;
803 
804  if (PG_ARGISNULL(0))
805  PG_RETURN_NULL();
806 
807  array = PG_GETARG_ARRAYTYPE_P(0);
808  element_type = ARR_ELEMTYPE(array);
809 
810  position = (ARR_LBOUND(array))[0] - 1;
811 
812  /*
813  * We refuse to search for elements in multi-dimensional arrays, since we
814  * have no good way to report the element's location in the array.
815  */
816  if (ARR_NDIM(array) > 1)
817  ereport(ERROR,
818  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
819  errmsg("searching for elements in multidimensional arrays is not supported")));
820 
821  astate = initArrayResult(INT4OID, CurrentMemoryContext, false);
822 
823  if (PG_ARGISNULL(1))
824  {
825  /* fast return when the array doesn't have nulls */
826  if (!array_contains_nulls(array))
828  searched_element = (Datum) 0;
829  null_search = true;
830  }
831  else
832  {
833  searched_element = PG_GETARG_DATUM(1);
834  null_search = false;
835  }
836 
837  /*
838  * We arrange to look up type info for array_create_iterator only once per
839  * series of calls, assuming the element type doesn't change underneath
840  * us.
841  */
842  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
843  if (my_extra == NULL)
844  {
845  fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
846  sizeof(ArrayMetaState));
847  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
848  my_extra->element_type = ~element_type;
849  }
850 
851  if (my_extra->element_type != element_type)
852  {
853  get_typlenbyvalalign(element_type,
854  &my_extra->typlen,
855  &my_extra->typbyval,
856  &my_extra->typalign);
857 
858  typentry = lookup_type_cache(element_type, TYPECACHE_EQ_OPR_FINFO);
859 
860  if (!OidIsValid(typentry->eq_opr_finfo.fn_oid))
861  ereport(ERROR,
862  (errcode(ERRCODE_UNDEFINED_FUNCTION),
863  errmsg("could not identify an equality operator for type %s",
864  format_type_be(element_type))));
865 
866  my_extra->element_type = element_type;
867  fmgr_info_cxt(typentry->eq_opr_finfo.fn_oid, &my_extra->proc,
868  fcinfo->flinfo->fn_mcxt);
869  }
870 
871  /*
872  * Accumulate each array position iff the element matches the given
873  * element.
874  */
875  array_iterator = array_create_iterator(array, 0, my_extra);
876  while (array_iterate(array_iterator, &value, &isnull))
877  {
878  position += 1;
879 
880  /*
881  * Can't look at the array element's value if it's null; but if we
882  * search for null, we have a hit.
883  */
884  if (isnull || null_search)
885  {
886  if (isnull && null_search)
887  astate =
888  accumArrayResult(astate, Int32GetDatum(position), false,
890 
891  continue;
892  }
893 
894  /* not nulls, so run the operator */
895  if (DatumGetBool(FunctionCall2Coll(&my_extra->proc, collation,
896  searched_element, value)))
897  astate =
898  accumArrayResult(astate, Int32GetDatum(position), false,
900  }
901 
902  array_free_iterator(array_iterator);
903 
904  /* Avoid leaking memory when handed toasted input */
905  PG_FREE_IF_COPY(array, 0);
906 
908 }
ArrayIterator array_create_iterator(ArrayType *arr, int slice_ndim, ArrayMetaState *mstate)
Definition: arrayfuncs.c:4255
MemoryContext fn_mcxt
Definition: fmgr.h:65
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:4951
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2021
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define TYPECACHE_EQ_OPR_FINFO
Definition: typcache.h:115
#define INT4OID
Definition: pg_type.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1047
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define PG_GET_COLLATION()
Definition: fmgr.h:163
#define ARR_LBOUND(a)
Definition: array.h:277
bool typbyval
Definition: array.h:221
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
bool array_iterate(ArrayIterator iterator, Datum *value, bool *isnull)
Definition: arrayfuncs.c:4334
#define ERROR
Definition: elog.h:43
static struct @121 value
static bool array_iterator(ArrayType *la, PGCALL2 callback, void *param, ltree **found)
Definition: _ltree_op.c:37
int16 typlen
Definition: array.h:220
#define DatumGetBool(X)
Definition: postgres.h:399
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
#define ereport(elevel, rest)
Definition: elog.h:122
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5054
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
FmgrInfo eq_opr_finfo
Definition: typcache.h:67
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:191
Oid fn_oid
Definition: fmgr.h:59
void array_free_iterator(ArrayIterator iterator)
Definition: arrayfuncs.c:4417
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define ARR_NDIM(a)
Definition: array.h:271
char typalign
Definition: array.h:222
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:4990
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
FmgrInfo proc
Definition: array.h:226
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
Oid element_type
Definition: array.h:219
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3542
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum array_prepend ( PG_FUNCTION_ARGS  )

Definition at line 157 of file array_userfuncs.c.

References array_set_element(), Assert, EOHPGetRWDatum, ereport, errcode(), errmsg(), ERROR, fetch_array_arg_replace_nulls(), ExpandedArrayHeader::hdr, ExpandedArrayHeader::lbound, ExpandedArrayHeader::ndims, PG_ARGISNULL, PG_GETARG_DATUM, PG_RETURN_DATUM, result, ArrayMetaState::typalign, ArrayMetaState::typbyval, and ArrayMetaState::typlen.

158 {
159  ExpandedArrayHeader *eah;
160  Datum newelem;
161  bool isNull;
162  Datum result;
163  int *lb;
164  int indx;
165  int lb0;
166  ArrayMetaState *my_extra;
167 
168  isNull = PG_ARGISNULL(0);
169  if (isNull)
170  newelem = (Datum) 0;
171  else
172  newelem = PG_GETARG_DATUM(0);
173  eah = fetch_array_arg_replace_nulls(fcinfo, 1);
174 
175  if (eah->ndims == 1)
176  {
177  /* prepend newelem */
178  lb = eah->lbound;
179  indx = lb[0] - 1;
180  lb0 = lb[0];
181 
182  /* overflow? */
183  if (indx > lb[0])
184  ereport(ERROR,
185  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
186  errmsg("integer out of range")));
187  }
188  else if (eah->ndims == 0)
189  {
190  indx = 1;
191  lb0 = 1;
192  }
193  else
194  ereport(ERROR,
195  (errcode(ERRCODE_DATA_EXCEPTION),
196  errmsg("argument must be empty or one-dimensional array")));
197 
198  /* Perform element insertion */
199  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
200 
201  result = array_set_element(EOHPGetRWDatum(&eah->hdr),
202  1, &indx, newelem, isNull,
203  -1, my_extra->typlen, my_extra->typbyval, my_extra->typalign);
204 
205  /* Readjust result's LB to match the input's, as expected for prepend */
206  Assert(result == EOHPGetRWDatum(&eah->hdr));
207  if (eah->ndims == 1)
208  {
209  /* This is ok whether we've deconstructed or not */
210  eah->lbound[0] = lb0;
211  }
212 
213  PG_RETURN_DATUM(result);
214 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
int errcode(int sqlerrcode)
Definition: elog.c:575
Datum array_set_element(Datum arraydatum, int nSubscripts, int *indx, Datum dataValue, bool isNull, int arraytyplen, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:2186
return result
Definition: formatting.c:1633
bool typbyval
Definition: array.h:221
static ExpandedArrayHeader * fetch_array_arg_replace_nulls(FunctionCallInfo fcinfo, int argno)
#define ERROR
Definition: elog.h:43
ExpandedObjectHeader hdr
Definition: array.h:102
int16 typlen
Definition: array.h:220
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define Assert(condition)
Definition: c.h:675
char typalign
Definition: array.h:222
#define EOHPGetRWDatum(eohptr)
int errmsg(const char *fmt,...)
Definition: elog.c:797
static ExpandedArrayHeader* fetch_array_arg_replace_nulls ( FunctionCallInfo  fcinfo,
int  argno 
)
static

Definition at line 41 of file array_userfuncs.c.

References AggCheckCallContext(), construct_empty_expanded_array(), CurrentMemoryContext, ArrayMetaState::element_type, ereport, errcode(), errmsg(), ERROR, FunctionCallInfoData::flinfo, FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_element_type(), get_fn_expr_argtype(), InvalidOid, MemoryContextAlloc(), MemoryContextSwitchTo(), NULL, OidIsValid, PG_ARGISNULL, and PG_GETARG_EXPANDED_ARRAYX.

Referenced by array_append(), and array_prepend().

42 {
44  Oid element_type;
45  ArrayMetaState *my_extra;
46  MemoryContext resultcxt;
47 
48  /* If first time through, create datatype cache struct */
49  my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
50  if (my_extra == NULL)
51  {
52  my_extra = (ArrayMetaState *)
54  sizeof(ArrayMetaState));
55  my_extra->element_type = InvalidOid;
56  fcinfo->flinfo->fn_extra = my_extra;
57  }
58 
59  /* Figure out which context we want the result in */
60  if (!AggCheckCallContext(fcinfo, &resultcxt))
61  resultcxt = CurrentMemoryContext;
62 
63  /* Now collect the array value */
64  if (!PG_ARGISNULL(argno))
65  {
66  MemoryContext oldcxt = MemoryContextSwitchTo(resultcxt);
67 
68  eah = PG_GETARG_EXPANDED_ARRAYX(argno, my_extra);
69  MemoryContextSwitchTo(oldcxt);
70  }
71  else
72  {
73  /* We have to look up the array type and element type */
74  Oid arr_typeid = get_fn_expr_argtype(fcinfo->flinfo, argno);
75 
76  if (!OidIsValid(arr_typeid))
77  ereport(ERROR,
78  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
79  errmsg("could not determine input data type")));
80  element_type = get_element_type(arr_typeid);
81  if (!OidIsValid(element_type))
82  ereport(ERROR,
83  (errcode(ERRCODE_DATATYPE_MISMATCH),
84  errmsg("input data type is not an array")));
85 
86  eah = construct_empty_expanded_array(element_type,
87  resultcxt,
88  my_extra);
89  }
90 
91  return eah;
92 }
MemoryContext fn_mcxt
Definition: fmgr.h:65
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2484
#define PG_GETARG_EXPANDED_ARRAYX(n, metacache)
Definition: array.h:250
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
FmgrInfo * flinfo
Definition: fmgr.h:79
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1909
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define ereport(elevel, rest)
Definition: elog.h:122
ExpandedArrayHeader * construct_empty_expanded_array(Oid element_type, MemoryContext parentcontext, ArrayMetaState *metacache)
Definition: arrayfuncs.c:3441
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition: nodeAgg.c:4017
void * fn_extra
Definition: fmgr.h:64
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
Oid element_type
Definition: array.h:219