1 /*-------------------------------------------------------------------------
4 * Misc user-visible array support functions
6 * Copyright (c) 2003, PostgreSQL Global Development Group
9 * $Header: /cvsroot/pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.3 2003/06/25 21:30:32 momjian Exp $
11 *-------------------------------------------------------------------------
15 #include "catalog/pg_proc.h"
16 #include "catalog/pg_type.h"
17 #include "utils/array.h"
18 #include "utils/lsyscache.h"
19 #include "utils/syscache.h"
22 /*-----------------------------------------------------------------------------
24 * Form a multi-dimensional array given one starting element.
26 * - first argument is the datum with which to build the array
27 * - second argument is the number of dimensions the array should have;
28 * defaults to 1 if no second argument is provided
29 *----------------------------------------------------------------------------
32 singleton_array(PG_FUNCTION_ARGS)
34 Oid elem_type = get_fn_expr_argtype(fcinfo, 0);
37 if (elem_type == InvalidOid)
38 elog(ERROR, "Cannot determine input datatype");
41 ndims = PG_GETARG_INT32(1);
45 PG_RETURN_ARRAYTYPE_P(create_singleton_array(elem_type,
50 /*-----------------------------------------------------------------------------
52 * push an element onto either end of a one-dimensional array
53 *----------------------------------------------------------------------------
56 array_push(PG_FUNCTION_ARGS)
69 Oid arg0_typeid = get_fn_expr_argtype(fcinfo, 0);
70 Oid arg1_typeid = get_fn_expr_argtype(fcinfo, 1);
74 if (arg0_typeid == InvalidOid || arg1_typeid == InvalidOid)
75 elog(ERROR, "array_push: cannot determine input data types");
76 arg0_elemid = get_element_type(arg0_typeid);
77 arg1_elemid = get_element_type(arg1_typeid);
79 if (arg0_elemid != InvalidOid)
81 v = PG_GETARG_ARRAYTYPE_P(0);
82 element_type = ARR_ELEMTYPE(v);
83 newelem = PG_GETARG_DATUM(1);
85 else if (arg1_elemid != InvalidOid)
87 v = PG_GETARG_ARRAYTYPE_P(1);
88 element_type = ARR_ELEMTYPE(v);
89 newelem = PG_GETARG_DATUM(0);
93 /* Shouldn't get here given proper type checking in parser */
94 elog(ERROR, "array_push: neither input type is an array");
95 PG_RETURN_NULL(); /* keep compiler quiet */
98 /* Sanity check: do we have a one-dimensional array */
100 elog(ERROR, "Arrays greater than one-dimension are not supported");
104 if (arg0_elemid != InvalidOid)
107 int ub = dimv[0] + lb[0] - 1;
112 /* prepend newelem */
116 get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
118 result = array_set(v, 1, &indx, newelem, -1,
119 typlen, typbyval, typalign, &isNull);
121 PG_RETURN_ARRAYTYPE_P(result);
124 /*-----------------------------------------------------------------------------
126 * concatenate two nD arrays to form an (n+1)D array, or
127 * push an (n-1)D array onto the end of an nD array
128 *----------------------------------------------------------------------------
131 array_cat(PG_FUNCTION_ARGS)
134 int *dims, *lbs, ndims, ndatabytes, nbytes;
135 int *dims1, *lbs1, ndims1, ndatabytes1;
136 int *dims2, *lbs2, ndims2, ndatabytes2;
143 v1 = PG_GETARG_ARRAYTYPE_P(0);
144 v2 = PG_GETARG_ARRAYTYPE_P(1);
147 * We must have one of the following combinations of inputs:
148 * 1) two arrays with ndims1 == ndims2
149 * 2) ndims1 == ndims2 - 1
150 * 3) ndims1 == ndims2 + 1
152 ndims1 = ARR_NDIM(v1);
153 ndims2 = ARR_NDIM(v2);
155 if (ndims1 != ndims2 && ndims1 != ndims2 - 1 && ndims1 != ndims2 + 1)
156 elog(ERROR, "Cannot concatenate incompatible arrays of %d and "
157 "%d dimensions", ndims1, ndims2);
159 element_type1 = ARR_ELEMTYPE(v1);
160 element_type2 = ARR_ELEMTYPE(v2);
162 /* Do we have a matching element types */
163 if (element_type1 != element_type2)
164 elog(ERROR, "Cannot concatenate incompatible arrays with element "
165 "type %u and %u", element_type1, element_type2);
168 element_type = element_type1;
170 /* get argument array details */
171 lbs1 = ARR_LBOUND(v1);
172 lbs2 = ARR_LBOUND(v2);
173 dims1 = ARR_DIMS(v1);
174 dims2 = ARR_DIMS(v2);
175 dat1 = ARR_DATA_PTR(v1);
176 dat2 = ARR_DATA_PTR(v2);
177 ndatabytes1 = ARR_SIZE(v1) - ARR_OVERHEAD(ndims1);
178 ndatabytes2 = ARR_SIZE(v2) - ARR_OVERHEAD(ndims2);
180 if (ndims1 == ndims2)
183 * resulting array has two element outer array made up of input
189 dims = (int *) palloc(ndims * sizeof(int));
190 lbs = (int *) palloc(ndims * sizeof(int));
192 dims[0] = 2; /* outer array made up of two input arrays */
193 lbs[0] = 1; /* start lower bound at 1 */
195 for (i = 0; i < ndims1; i++)
197 if (dims1[i] != dims2[i] || lbs1[i] != lbs2[i])
198 elog(ERROR, "Cannot concatenate arrays with differing dimensions");
200 dims[i + 1] = dims1[i];
201 lbs[i + 1] = lbs1[i];
204 else if (ndims1 == ndims2 - 1)
207 * resulting array has the second argument as the outer array,
208 * with the first argument appended to the front of the outer
217 /* increment number of elements in outer array */
220 /* make sure the added element matches our existing elements */
221 for (i = 0; i < ndims1; i++)
223 if (dims1[i] != dims[i + 1] || lbs1[i] != lbs[i + 1])
224 elog(ERROR, "Cannot concatenate arrays with differing dimensions");
227 else /* (ndims1 == ndims2 + 1) */
230 * resulting array has the first argument as the outer array,
231 * with the second argument appended to the end of the outer
240 /* increment number of elements in outer array */
243 /* make sure the added element matches our existing elements */
244 for (i = 0; i < ndims2; i++)
246 if (dims2[i] != dims[i + 1] || lbs2[i] != lbs[i + 1])
247 elog(ERROR, "Cannot concatenate arrays with differing dimensions");
251 /* build the result array */
252 ndatabytes = ndatabytes1 + ndatabytes2;
253 nbytes = ndatabytes + ARR_OVERHEAD(ndims);
254 result = (ArrayType *) palloc(nbytes);
256 result->size = nbytes;
257 result->ndim = ndims;
259 result->elemtype = element_type;
260 memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
261 memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
262 /* data area is arg1 then arg2 */
263 memcpy(ARR_DATA_PTR(result), dat1, ndatabytes1);
264 memcpy(ARR_DATA_PTR(result) + ndatabytes1, dat2, ndatabytes2);
266 PG_RETURN_ARRAYTYPE_P(result);
269 /*----------------------------------------------------------------------------
271 * accumulator to build a 1-D array from input values -- this can be used
272 * to create custom aggregates.
274 * This function is not marked strict, so we have to be careful about nulls.
275 *----------------------------------------------------------------------------
278 array_accum(PG_FUNCTION_ARGS)
280 /* return NULL if both arguments are NULL */
281 if (PG_ARGISNULL(0) && PG_ARGISNULL(1))
284 /* create a new 1-D array from the new element if the array is NULL */
287 Oid tgt_type = get_fn_expr_rettype(fcinfo);
290 if (tgt_type == InvalidOid)
291 elog(ERROR, "Cannot determine target array type");
292 tgt_elem_type = get_element_type(tgt_type);
293 if (tgt_elem_type == InvalidOid)
294 elog(ERROR, "Target type is not an array");
296 PG_RETURN_ARRAYTYPE_P(create_singleton_array(tgt_elem_type,
301 /* return the array if the new element is NULL */
303 PG_RETURN_ARRAYTYPE_P(PG_GETARG_ARRAYTYPE_P_COPY(0));
306 * Otherwise this is equivalent to array_push. We hack the call a little
307 * so that array_push can see the fn_expr information.
309 return array_push(fcinfo);
312 /*-----------------------------------------------------------------------------
314 * assign an element of an array to a new value and return the
316 *----------------------------------------------------------------------------
319 array_assign(PG_FUNCTION_ARGS)
333 v = PG_GETARG_ARRAYTYPE_P(0);
334 idx_to_chg = PG_GETARG_INT32(1);
335 newelem = PG_GETARG_DATUM(2);
337 /* Sanity check: do we have a one-dimensional array */
338 if (ARR_NDIM(v) != 1)
339 elog(ERROR, "Arrays greater than one-dimension are not supported");
343 ub = dimv[0] + lb[0] - 1;
344 if (idx_to_chg < lb[0] || idx_to_chg > ub)
345 elog(ERROR, "Cannot alter nonexistent array element: %d", idx_to_chg);
347 element_type = ARR_ELEMTYPE(v);
348 /* Sanity check: do we have a non-zero element type */
349 if (element_type == 0)
350 elog(ERROR, "Invalid array element type: %u", element_type);
352 get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
354 result = array_set(v, 1, &idx_to_chg, newelem, -1,
355 typlen, typbyval, typalign, &isNull);
357 PG_RETURN_ARRAYTYPE_P(result);
360 /*-----------------------------------------------------------------------------
362 * return specific element of an array
363 *----------------------------------------------------------------------------
366 array_subscript(PG_FUNCTION_ARGS)
379 v = PG_GETARG_ARRAYTYPE_P(0);
380 idx = PG_GETARG_INT32(1);
382 /* Sanity check: do we have a one-dimensional array */
383 if (ARR_NDIM(v) != 1)
384 elog(ERROR, "Arrays greater than one-dimension are not supported");
388 ub = dimv[0] + lb[0] - 1;
389 if (idx < lb[0] || idx > ub)
390 elog(ERROR, "Cannot return nonexistent array element: %d", idx);
392 element_type = ARR_ELEMTYPE(v);
393 /* Sanity check: do we have a non-zero element type */
394 if (element_type == 0)
395 elog(ERROR, "Invalid array element type: %u", element_type);
397 get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
399 result = array_ref(v, 1, &idx, -1, typlen, typbyval, typalign, &isNull);
401 PG_RETURN_DATUM(result);
405 * actually does the work for singleton_array(), and array_accum() if it is
406 * given a null input array.
409 create_singleton_array(Oid element_type, Datum element, int ndims)
419 if (element_type == 0)
420 elog(ERROR, "Invalid array element type: %u", element_type);
421 if (ndims < 1 || ndims > MAXDIM)
422 elog(ERROR, "Invalid number of dimensions %d", ndims);
424 dvalues[0] = element;
426 for (i = 0; i < ndims; i++)
432 get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
434 return construct_md_array(dvalues, ndims, dims, lbs, element_type,
435 typlen, typbyval, typalign);