1 /*-------------------------------------------------------------------------
4 * Misc user-visible array support functions
6 * Copyright (c) 2003-2005, PostgreSQL Global Development Group
9 * $PostgreSQL: pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.18 2005/11/19 01:50:08 tgl Exp $
11 *-------------------------------------------------------------------------
15 #include "catalog/pg_proc.h"
16 #include "catalog/pg_type.h"
17 #include "utils/array.h"
18 #include "utils/builtins.h"
19 #include "utils/lsyscache.h"
20 #include "utils/syscache.h"
23 /*-----------------------------------------------------------------------------
25 * push an element onto either end of a one-dimensional array
26 *----------------------------------------------------------------------------
29 array_push(PG_FUNCTION_ARGS)
42 Oid arg0_typeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
43 Oid arg1_typeid = get_fn_expr_argtype(fcinfo->flinfo, 1);
46 ArrayMetaState *my_extra;
48 if (arg0_typeid == InvalidOid || arg1_typeid == InvalidOid)
50 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
51 errmsg("could not determine input data types")));
53 arg0_elemid = get_element_type(arg0_typeid);
54 arg1_elemid = get_element_type(arg1_typeid);
56 if (arg0_elemid != InvalidOid)
59 v = construct_empty_array(arg0_elemid);
61 v = PG_GETARG_ARRAYTYPE_P(0);
62 isNull = PG_ARGISNULL(1);
66 newelem = PG_GETARG_DATUM(1);
68 else if (arg1_elemid != InvalidOid)
71 v = construct_empty_array(arg1_elemid);
73 v = PG_GETARG_ARRAYTYPE_P(1);
74 isNull = PG_ARGISNULL(0);
78 newelem = PG_GETARG_DATUM(0);
82 /* Shouldn't get here given proper type checking in parser */
84 (errcode(ERRCODE_DATATYPE_MISMATCH),
85 errmsg("neither input type is an array")));
86 PG_RETURN_NULL(); /* keep compiler quiet */
89 element_type = ARR_ELEMTYPE(v);
96 if (arg0_elemid != InvalidOid)
99 int ub = dimv[0] + lb[0] - 1;
105 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
106 errmsg("integer out of range")));
110 /* prepend newelem */
115 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
116 errmsg("integer out of range")));
119 else if (ARR_NDIM(v) == 0)
123 (errcode(ERRCODE_DATA_EXCEPTION),
124 errmsg("argument must be empty or one-dimensional array")));
127 * We arrange to look up info about element type only once per series of
128 * calls, assuming the element type doesn't change underneath us.
130 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
131 if (my_extra == NULL)
133 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
134 sizeof(ArrayMetaState));
135 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
136 my_extra->element_type = ~element_type;
139 if (my_extra->element_type != element_type)
141 /* Get info about element type */
142 get_typlenbyvalalign(element_type,
145 &my_extra->typalign);
146 my_extra->element_type = element_type;
148 typlen = my_extra->typlen;
149 typbyval = my_extra->typbyval;
150 typalign = my_extra->typalign;
152 result = array_set(v, 1, &indx, newelem, isNull,
153 -1, typlen, typbyval, typalign);
156 * Readjust result's LB to match the input's. This does nothing in the
157 * append case, but it's the simplest way to implement the prepend case.
159 if (ARR_NDIM(v) == 1)
160 ARR_LBOUND(result)[0] = ARR_LBOUND(v)[0];
162 PG_RETURN_ARRAYTYPE_P(result);
165 /*-----------------------------------------------------------------------------
167 * concatenate two nD arrays to form an nD array, or
168 * push an (n-1)D array onto the end of an nD array
169 *----------------------------------------------------------------------------
172 array_cat(PG_FUNCTION_ARGS)
203 /* Concatenating a null array is a no-op, just return the other input */
208 result = PG_GETARG_ARRAYTYPE_P(1);
209 PG_RETURN_ARRAYTYPE_P(result);
213 result = PG_GETARG_ARRAYTYPE_P(0);
214 PG_RETURN_ARRAYTYPE_P(result);
217 v1 = PG_GETARG_ARRAYTYPE_P(0);
218 v2 = PG_GETARG_ARRAYTYPE_P(1);
220 element_type1 = ARR_ELEMTYPE(v1);
221 element_type2 = ARR_ELEMTYPE(v2);
223 /* Check we have matching element types */
224 if (element_type1 != element_type2)
226 (errcode(ERRCODE_DATATYPE_MISMATCH),
227 errmsg("cannot concatenate incompatible arrays"),
228 errdetail("Arrays with element types %s and %s are not "
229 "compatible for concatenation.",
230 format_type_be(element_type1),
231 format_type_be(element_type2))));
234 element_type = element_type1;
237 * We must have one of the following combinations of inputs:
238 * 1) one empty array, and one non-empty array
239 * 2) both arrays empty
240 * 3) two arrays with ndims1 == ndims2
241 * 4) ndims1 == ndims2 - 1
242 * 5) ndims1 == ndims2 + 1
245 ndims1 = ARR_NDIM(v1);
246 ndims2 = ARR_NDIM(v2);
249 * short circuit - if one input array is empty, and the other is not, we
250 * return the non-empty one as the result
252 * if both are empty, return the first one
254 if (ndims1 == 0 && ndims2 > 0)
255 PG_RETURN_ARRAYTYPE_P(v2);
258 PG_RETURN_ARRAYTYPE_P(v1);
260 /* the rest fall under rule 3, 4, or 5 */
261 if (ndims1 != ndims2 &&
262 ndims1 != ndims2 - 1 &&
263 ndims1 != ndims2 + 1)
265 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
266 errmsg("cannot concatenate incompatible arrays"),
267 errdetail("Arrays of %d and %d dimensions are not "
268 "compatible for concatenation.",
271 /* get argument array details */
272 lbs1 = ARR_LBOUND(v1);
273 lbs2 = ARR_LBOUND(v2);
274 dims1 = ARR_DIMS(v1);
275 dims2 = ARR_DIMS(v2);
276 dat1 = ARR_DATA_PTR(v1);
277 dat2 = ARR_DATA_PTR(v2);
278 bitmap1 = ARR_NULLBITMAP(v1);
279 bitmap2 = ARR_NULLBITMAP(v2);
280 nitems1 = ArrayGetNItems(ndims1, dims1);
281 nitems2 = ArrayGetNItems(ndims2, dims2);
282 ndatabytes1 = ARR_SIZE(v1) - ARR_DATA_OFFSET(v1);
283 ndatabytes2 = ARR_SIZE(v2) - ARR_DATA_OFFSET(v2);
285 if (ndims1 == ndims2)
288 * resulting array is made up of the elements (possibly arrays
289 * themselves) of the input argument arrays
292 dims = (int *) palloc(ndims * sizeof(int));
293 lbs = (int *) palloc(ndims * sizeof(int));
295 dims[0] = dims1[0] + dims2[0];
298 for (i = 1; i < ndims; i++)
300 if (dims1[i] != dims2[i] || lbs1[i] != lbs2[i])
302 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
303 errmsg("cannot concatenate incompatible arrays"),
304 errdetail("Arrays with differing element dimensions are "
305 "not compatible for concatenation.")));
311 else if (ndims1 == ndims2 - 1)
314 * resulting array has the second argument as the outer array, with
315 * the first argument inserted at the front of the outer dimension
318 dims = (int *) palloc(ndims * sizeof(int));
319 lbs = (int *) palloc(ndims * sizeof(int));
320 memcpy(dims, dims2, ndims * sizeof(int));
321 memcpy(lbs, lbs2, ndims * sizeof(int));
323 /* increment number of elements in outer array */
326 /* make sure the added element matches our existing elements */
327 for (i = 0; i < ndims1; i++)
329 if (dims1[i] != dims[i + 1] || lbs1[i] != lbs[i + 1])
331 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
332 errmsg("cannot concatenate incompatible arrays"),
333 errdetail("Arrays with differing dimensions are not "
334 "compatible for concatenation.")));
340 * (ndims1 == ndims2 + 1)
342 * resulting array has the first argument as the outer array, with the
343 * second argument appended to the end of the outer dimension
346 dims = (int *) palloc(ndims * sizeof(int));
347 lbs = (int *) palloc(ndims * sizeof(int));
348 memcpy(dims, dims1, ndims * sizeof(int));
349 memcpy(lbs, lbs1, ndims * sizeof(int));
351 /* increment number of elements in outer array */
354 /* make sure the added element matches our existing elements */
355 for (i = 0; i < ndims2; i++)
357 if (dims2[i] != dims[i + 1] || lbs2[i] != lbs[i + 1])
359 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
360 errmsg("cannot concatenate incompatible arrays"),
361 errdetail("Arrays with differing dimensions are not "
362 "compatible for concatenation.")));
366 /* Do this mainly for overflow checking */
367 nitems = ArrayGetNItems(ndims, dims);
369 /* build the result array */
370 ndatabytes = ndatabytes1 + ndatabytes2;
371 if (ARR_HASNULL(v1) || ARR_HASNULL(v2))
373 dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
374 nbytes = ndatabytes + dataoffset;
378 dataoffset = 0; /* marker for no null bitmap */
379 nbytes = ndatabytes + ARR_OVERHEAD_NONULLS(ndims);
381 result = (ArrayType *) palloc(nbytes);
382 result->size = nbytes;
383 result->ndim = ndims;
384 result->dataoffset = dataoffset;
385 result->elemtype = element_type;
386 memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
387 memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
388 /* data area is arg1 then arg2 */
389 memcpy(ARR_DATA_PTR(result), dat1, ndatabytes1);
390 memcpy(ARR_DATA_PTR(result) + ndatabytes1, dat2, ndatabytes2);
391 /* handle the null bitmap if needed */
392 if (ARR_HASNULL(result))
394 array_bitmap_copy(ARR_NULLBITMAP(result), 0,
397 array_bitmap_copy(ARR_NULLBITMAP(result), nitems1,
402 PG_RETURN_ARRAYTYPE_P(result);
407 * used by text_to_array() in varlena.c
410 create_singleton_array(FunctionCallInfo fcinfo,
422 ArrayMetaState *my_extra;
426 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
427 errmsg("invalid number of dimensions: %d", ndims)));
430 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
431 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
434 dvalues[0] = element;
436 for (i = 0; i < ndims; i++)
443 * We arrange to look up info about element type only once per series of
444 * calls, assuming the element type doesn't change underneath us.
446 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
447 if (my_extra == NULL)
449 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
450 sizeof(ArrayMetaState));
451 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
452 my_extra->element_type = ~element_type;
455 if (my_extra->element_type != element_type)
457 /* Get info about element type */
458 get_typlenbyvalalign(element_type,
461 &my_extra->typalign);
462 my_extra->element_type = element_type;
464 typlen = my_extra->typlen;
465 typbyval = my_extra->typbyval;
466 typalign = my_extra->typalign;
468 return construct_md_array(dvalues, NULL, ndims, dims, lbs, element_type,
469 typlen, typbyval, typalign);