78 static int ArrayCount(
const char *str,
int *dim,
char typdelim);
79 static void ReadArrayStr(
char *arrayStr,
const char *origStr,
80 int nitems,
int ndim,
int *dim,
83 int typlen,
bool typbyval,
char typalign,
85 bool *hasnulls,
int32 *nbytes);
88 int typlen,
bool typbyval,
char typalign,
90 bool *hasnulls,
int32 *nbytes);
93 int typlen,
bool typbyval,
char typalign,
99 int typlen,
bool typbyval,
char typalign,
101 static char *
array_seek(
char *ptr,
int offset,
bits8 *nullbitmap,
int nitems,
102 int typlen,
bool typbyval,
char typalign);
104 int nitems,
int typlen,
bool typbyval,
char typalign);
105 static int array_copy(
char *destptr,
int nitems,
106 char *srcptr,
int offset,
bits8 *nullbitmap,
107 int typlen,
bool typbyval,
char typalign);
109 int ndim,
int *dim,
int *lb,
111 int typlen,
bool typbyval,
char typalign);
113 int ndim,
int *dim,
int *lb,
114 char *arraydataptr,
bits8 *arraynullsptr,
116 int typlen,
bool typbyval,
char typalign);
119 int ndim,
int *dim,
int *lb,
121 int typlen,
bool typbyval,
char typalign);
124 Oid elmtype,
int dataoffset);
129 Datum search,
bool search_isnull,
130 Datum replace,
bool replace_isnull,
131 bool remove,
Oid collation,
176 if (my_extra ==
NULL)
197 typlen = my_extra->
typlen;
232 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
233 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
236 for (q = p; isdigit((
unsigned char) *q) || (*q ==
'-') || (*q ==
'+'); q++);
239 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
240 errmsg(
"missing dimension value")));
246 lBound[ndim] = atoi(p);
248 for (q = p; isdigit((
unsigned char) *q) || (*q ==
'-') || (*q ==
'+'); q++);
251 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
252 errmsg(
"missing dimension value")));
261 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
262 errmsg(
"missing \"]\" in array dimensions")));
267 if (ub < lBound[ndim])
269 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
270 errmsg(
"upper bound cannot be less than lower bound")));
272 dim[ndim] = ub - lBound[ndim] + 1;
281 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
282 errmsg(
"array value must start with \"{\" or dimension information")));
284 for (i = 0; i < ndim; i++)
295 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
296 errmsg(
"missing assignment operator")));
307 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
308 errmsg(
"array value must start with \"{\" or dimension information")));
309 ndim_braces =
ArrayCount(p, dim_braces, typdelim);
310 if (ndim_braces != ndim)
312 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
313 errmsg(
"array dimensions incompatible with array literal")));
314 for (i = 0; i < ndim; ++
i)
316 if (dim[i] != dim_braces[i])
318 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
319 errmsg(
"array dimensions incompatible with array literal")));
324 printf(
"array_in- ndim %d (", ndim);
325 for (i = 0; i < ndim; i++)
327 printf(
" %d", dim[i]);
329 printf(
") for %s\n",
string);
339 nullsPtr = (
bool *)
palloc(nitems *
sizeof(
bool));
342 &my_extra->
proc, typioparam, typmod,
344 typlen, typbyval, typalign,
350 nbytes += dataoffset;
368 memcpy(
ARR_DIMS(retval), dim, ndim *
sizeof(
int));
369 memcpy(
ARR_LBOUND(retval), lBound, ndim *
sizeof(
int));
372 dataPtr, nullsPtr, nitems,
373 typlen, typbyval, typalign,
420 bool in_quotes =
false;
421 bool eoArray =
false;
422 bool empty_array =
true;
428 temp[
i] = dim[
i] = 0;
429 nelems_last[
i] = nelems[
i] = 1;
435 bool itemdone =
false;
448 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
449 errmsg(
"malformed array literal: \"%s\"", str)));
463 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
464 errmsg(
"malformed array literal: \"%s\"", str)));
472 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
473 errmsg(
"malformed array literal: \"%s\"", str)));
486 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
487 errmsg(
"malformed array literal: \"%s\"", str)));
488 in_quotes = !in_quotes;
506 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
507 errmsg(
"malformed array literal: \"%s\"", str)));
509 if (nest_level >= MAXDIM)
511 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
512 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
513 nest_level + 1, MAXDIM)));
514 temp[nest_level] = 0;
516 if (ndim < nest_level)
534 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
535 errmsg(
"malformed array literal: \"%s\"", str)));
539 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
540 errmsg(
"malformed array literal: \"%s\"", str)));
543 if ((nelems_last[nest_level] != 1) &&
544 (nelems[nest_level] != nelems_last[nest_level]))
546 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
547 errmsg(
"multidimensional arrays must have "
548 "array expressions with matching "
550 nelems_last[nest_level] = nelems[nest_level];
551 nelems[nest_level] = 1;
553 eoArray = itemdone =
true;
560 temp[nest_level - 1]++;
567 if (*ptr == typdelim)
579 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
580 errmsg(
"malformed array literal: \"%s\"", str)));
586 nelems[nest_level - 1]++;
600 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
601 errmsg(
"malformed array literal: \"%s\"", str)));
619 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
620 errmsg(
"malformed array literal: \"%s\"", str)));
627 for (
i = 0;
i < ndim; ++
i)
682 bool in_quotes =
false;
683 bool eoArray =
false;
690 MemSet(indx, 0,
sizeof(indx));
693 memset(nulls,
true, nitems *
sizeof(
bool));
712 bool itemdone =
false;
713 bool leadingspace =
true;
714 bool hasquoting =
false;
720 itemstart = dstptr = dstendptr = srcptr;
729 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
730 errmsg(
"malformed array literal: \"%s\"",
738 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
739 errmsg(
"malformed array literal: \"%s\"",
741 *dstptr++ = *srcptr++;
743 leadingspace =
false;
748 in_quotes = !in_quotes;
750 leadingspace =
false;
766 if (nest_level >= ndim)
768 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
769 errmsg(
"malformed array literal: \"%s\"",
772 indx[nest_level - 1] = 0;
776 *dstptr++ = *srcptr++;
783 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
784 errmsg(
"malformed array literal: \"%s\"",
788 indx[nest_level - 1] = 0;
791 eoArray = itemdone =
true;
793 indx[nest_level - 1]++;
797 *dstptr++ = *srcptr++;
801 *dstptr++ = *srcptr++;
802 else if (*srcptr == typdelim)
819 *dstptr++ = *srcptr++;
823 *dstptr++ = *srcptr++;
824 leadingspace =
false;
834 if (i < 0 || i >= nitems)
836 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
837 errmsg(
"malformed array literal: \"%s\"",
861 for (i = 0; i < nitems; i++)
875 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
876 errmsg(
"array size exceeds the maximum allowed (%d)",
919 for (i = 0; i < nitems; i++)
921 if (nulls && nulls[i])
924 elog(
ERROR,
"null array element where not supported");
937 if (bitmask == 0x100)
946 if (bitmap && bitmask != 1)
968 dims_str[(
MAXDIM * 33) + 2];
996 if (my_extra ==
NULL)
1017 typlen = my_extra->
typlen;
1037 for (i = 0; i < ndim; i++)
1051 values = (
char **)
palloc(nitems *
sizeof(
char *));
1052 needquotes = (
bool *)
palloc(nitems *
sizeof(
bool));
1059 for (i = 0; i < nitems; i++)
1064 if (bitmap && (*bitmap & bitmask) == 0)
1067 overall_length += 4;
1074 itemvalue =
fetch_att(p, typbyval, typlen);
1080 if (values[i][0] ==
'\0')
1087 for (tmp = values[i]; *tmp !=
'\0'; tmp++)
1091 overall_length += 1;
1092 if (ch ==
'"' || ch ==
'\\')
1095 overall_length += 1;
1097 else if (ch ==
'{' || ch ==
'}' || ch == typdelim ||
1103 needquotes[
i] = needquote;
1107 overall_length += 2;
1109 overall_length += 1;
1115 if (bitmask == 0x100)
1126 for (i = j = 0, k = 1; i < ndim; i++)
1127 k *= dims[i], j += k;
1134 char *ptr = dims_str;
1136 for (i = 0; i < ndim; i++)
1138 sprintf(ptr,
"[%d:%d]", lb[i], lb[i] + dims[i] - 1);
1145 retval = (
char *)
palloc(strlen(dims_str) + overall_length + 2 * j);
1148 #define APPENDSTR(str) (strcpy(p, (str)), p += strlen(p))
1149 #define APPENDCHAR(ch) (*p++ = (ch), *p = '\0')
1154 for (i = 0; i < ndim; i++)
1160 for (i = j; i < ndim - 1; i++)
1166 for (tmp = values[k]; *tmp; tmp++)
1170 if (ch ==
'"' || ch ==
'\\')
1181 for (i = ndim - 1; i >= 0; i--)
1183 indx[
i] = (indx[
i] + 1) % dims[i];
1242 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1243 errmsg(
"invalid number of dimensions: %d", ndim)));
1246 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1247 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
1251 if (flags != 0 && flags != 1)
1253 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1254 errmsg(
"invalid array flags")));
1257 if (element_type != spec_element_type)
1261 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1262 errmsg(
"wrong element type")));
1265 for (i = 0; i < ndim; i++)
1276 int ub = lBound[
i] + dim[
i] - 1;
1280 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1281 errmsg(
"integer out of range")));
1294 if (my_extra ==
NULL)
1311 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
1312 errmsg(
"no binary input function available for type %s",
1325 typlen = my_extra->
typlen;
1331 nullsPtr = (
bool *)
palloc(nitems *
sizeof(
bool));
1333 &my_extra->
proc, typioparam, typmod,
1334 typlen, typbyval, typalign,
1336 &hasnulls, &nbytes);
1340 nbytes += dataoffset;
1349 retval->
ndim = ndim;
1352 memcpy(
ARR_DIMS(retval), dim, ndim *
sizeof(
int));
1353 memcpy(
ARR_LBOUND(retval), lBound, ndim *
sizeof(
int));
1356 dataPtr, nullsPtr, nitems,
1357 typlen, typbyval, typalign,
1405 for (i = 0; i < nitems; i++)
1413 if (itemlen < -1 || itemlen > (buf->
len - buf->
cursor))
1415 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1416 errmsg(
"insufficient data left in message")));
1422 typioparam, typmod);
1434 elem_buf.
maxlen = itemlen + 1;
1435 elem_buf.
len = itemlen;
1445 typioparam, typmod);
1449 if (elem_buf.
cursor != itemlen)
1451 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1452 errmsg(
"improper binary format in array element %d",
1463 for (i = 0; i < nitems; i++)
1477 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1478 errmsg(
"array size exceeds the maximum allowed (%d)",
1482 *hasnulls = hasnull;
1516 if (my_extra ==
NULL)
1533 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
1534 errmsg(
"no binary output function available for type %s",
1540 typlen = my_extra->
typlen;
1554 for (i = 0; i < ndim; i++)
1565 for (i = 0; i < nitems; i++)
1568 if (bitmap && (*bitmap & bitmask) == 0)
1578 itemvalue =
fetch_att(p, typbyval, typlen);
1593 if (bitmask == 0x100)
1650 sprintf(p,
"[%d:%d]", lb[i], dimv[i] + lb[i] - 1);
1675 if (reqdim <= 0 || reqdim >
ARR_NDIM(v))
1679 result = lb[reqdim - 1];
1703 if (reqdim <= 0 || reqdim >
ARR_NDIM(v))
1709 result = dimv[reqdim - 1] + lb[reqdim - 1] - 1;
1732 if (reqdim <= 0 || reqdim >
ARR_NDIM(v))
1737 result = dimv[reqdim - 1];
1794 bits8 *arraynullsptr;
1796 if (arraytyplen > 0)
1802 fixedDim[0] = arraytyplen / elmlen;
1806 arraydataptr = (
char *) array;
1807 arraynullsptr =
NULL;
1824 if (ndim != nSubscripts || ndim <= 0 || ndim >
MAXDIM)
1829 for (i = 0; i < ndim; i++)
1831 if (indx[i] < lb[i] || indx[i] >= (dim[i] + lb[i]))
1856 retptr =
array_seek(arraydataptr, 0, arraynullsptr, offset,
1857 elmlen, elmbyval, elmalign);
1858 return ArrayCast(retptr, elmbyval, elmlen);
1905 bits8 *arraynullsptr;
1910 if (arraytyplen > 0)
1919 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1920 errmsg(
"slices of fixed-length arrays not implemented")));
1928 fixedDim[0] = arraytyplen / elmlen;
1933 arraydataptr = (
char *) array;
1934 arraynullsptr =
NULL;
1954 if (ndim < nSubscripts || ndim <= 0 || ndim >
MAXDIM)
1957 for (i = 0; i < nSubscripts; i++)
1959 if (lowerIndx[i] < lb[i])
1960 lowerIndx[
i] = lb[
i];
1961 if (upperIndx[i] >= (dim[i] + lb[i]))
1962 upperIndx[
i] = dim[
i] + lb[
i] - 1;
1963 if (lowerIndx[i] > upperIndx[i])
1967 for (; i < ndim; i++)
1969 lowerIndx[
i] = lb[
i];
1970 upperIndx[
i] = dim[
i] + lb[
i] - 1;
1971 if (lowerIndx[i] > upperIndx[i])
1979 lowerIndx, upperIndx,
1980 elmlen, elmbyval, elmalign);
1989 bytes += dataoffset;
1999 newarray->
ndim = ndim;
2002 memcpy(
ARR_DIMS(newarray), span, ndim *
sizeof(
int));
2009 for (i = 0; i < ndim; i++)
2014 arraydataptr, arraynullsptr,
2015 lowerIndx, upperIndx,
2016 elmlen, elmbyval, elmalign);
2070 bits8 *oldnullbitmap;
2084 if (arraytyplen > 0)
2090 if (nSubscripts != 1)
2092 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2093 errmsg(
"wrong number of array subscripts")));
2095 if (indx[0] < 0 || indx[0] * elmlen >= arraytyplen)
2097 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2098 errmsg(
"array subscript out of range")));
2102 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2103 errmsg(
"cannot assign null value to an element of a fixed-length array")));
2106 memcpy(newarray, array, arraytyplen);
2107 elt_ptr = (
char *) newarray + indx[0] * elmlen;
2112 if (nSubscripts <= 0 || nSubscripts >
MAXDIM)
2114 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2115 errmsg(
"wrong number of array subscripts")));
2118 if (elmlen == -1 && !isNull)
2135 for (i = 0; i < nSubscripts; i++)
2143 elmlen, elmbyval, elmalign);
2146 if (ndim != nSubscripts)
2148 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2149 errmsg(
"wrong number of array subscripts")));
2152 memcpy(dim,
ARR_DIMS(array), ndim *
sizeof(
int));
2153 memcpy(lb,
ARR_LBOUND(array), ndim *
sizeof(
int));
2156 addedbefore = addedafter = 0;
2163 if (indx[0] < lb[0])
2165 addedbefore = lb[0] - indx[0];
2166 dim[0] += addedbefore;
2168 if (addedbefore > 1)
2171 if (indx[0] >= (dim[0] + lb[0]))
2173 addedafter = indx[0] - (dim[0] + lb[0]) + 1;
2174 dim[0] += addedafter;
2185 for (i = 0; i < ndim; i++)
2187 if (indx[i] < lb[i] ||
2188 indx[i] >= (dim[i] + lb[i]))
2190 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2191 errmsg(
"array subscript out of range")));
2206 olddatasize =
ARR_SIZE(array) - oldoverheadlen;
2212 lenafter = olddatasize;
2214 else if (addedafter)
2217 lenbefore = olddatasize;
2225 elmlen, elmbyval, elmalign);
2234 lenafter = (int) (olddatasize - lenbefore - olditemlen);
2245 newsize = overheadlen + lenbefore + newitemlen + lenafter;
2252 newarray->
ndim = ndim;
2253 newarray->
dataoffset = newhasnulls ? overheadlen : 0;
2255 memcpy(
ARR_DIMS(newarray), dim, ndim *
sizeof(
int));
2256 memcpy(
ARR_LBOUND(newarray), lb, ndim *
sizeof(
int));
2261 memcpy((
char *) newarray + overheadlen,
2262 (
char *) array + oldoverheadlen,
2266 (
char *) newarray + overheadlen + lenbefore);
2267 memcpy((
char *) newarray + overheadlen + lenbefore + newitemlen,
2268 (
char *) array + oldoverheadlen + lenbefore + olditemlen,
2282 MemSet(newnullbitmap, 0, (newnitems + 7) / 8);
2298 if (addedafter == 0)
2300 oldnullbitmap, offset + 1,
2301 oldnitems - offset - 1);
2382 if (arraytyplen > 0)
2388 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2389 errmsg(
"updates on slices of fixed-length arrays not implemented")));
2413 &dvalues, &dnulls, &nelems);
2415 for (i = 0; i < nSubscripts; i++)
2417 dim[
i] = 1 + upperIndx[
i] - lowerIndx[
i];
2418 lb[
i] = lowerIndx[
i];
2424 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2425 errmsg(
"source array too small")));
2429 elmlen, elmbyval, elmalign);
2432 if (ndim < nSubscripts || ndim <= 0 || ndim >
MAXDIM)
2434 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2435 errmsg(
"wrong number of array subscripts")));
2438 memcpy(dim,
ARR_DIMS(array), ndim *
sizeof(
int));
2439 memcpy(lb,
ARR_LBOUND(array), ndim *
sizeof(
int));
2442 addedbefore = addedafter = 0;
2449 Assert(nSubscripts == 1);
2450 if (lowerIndx[0] > upperIndx[0])
2452 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2453 errmsg(
"upper bound cannot be less than lower bound")));
2454 if (lowerIndx[0] < lb[0])
2456 if (upperIndx[0] < lb[0] - 1)
2458 addedbefore = lb[0] - lowerIndx[0];
2459 dim[0] += addedbefore;
2460 lb[0] = lowerIndx[0];
2462 if (upperIndx[0] >= (dim[0] + lb[0]))
2464 if (lowerIndx[0] > (dim[0] + lb[0]))
2466 addedafter = upperIndx[0] - (dim[0] + lb[0]) + 1;
2467 dim[0] += addedafter;
2476 for (i = 0; i < nSubscripts; i++)
2478 if (lowerIndx[i] > upperIndx[i])
2480 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2481 errmsg(
"upper bound cannot be less than lower bound")));
2482 if (lowerIndx[i] < lb[i] ||
2483 upperIndx[i] >= (dim[i] + lb[i]))
2485 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2486 errmsg(
"array subscript out of range")));
2489 for (; i < ndim; i++)
2491 lowerIndx[
i] = lb[
i];
2492 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2493 if (lowerIndx[i] > upperIndx[i])
2495 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2496 errmsg(
"upper bound cannot be less than lower bound")));
2511 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2512 errmsg(
"source array too small")));
2524 elmlen, elmbyval, elmalign);
2526 olddatasize =
ARR_SIZE(array) - oldoverheadlen;
2536 lowerIndx, upperIndx,
2537 elmlen, elmbyval, elmalign);
2538 lenbefore = lenafter = 0;
2539 itemsbefore = itemsafter = nolditems = 0;
2548 int oldub = oldlb +
ARR_DIMS(array)[0] - 1;
2549 int slicelb =
Max(oldlb, lowerIndx[0]);
2550 int sliceub =
Min(oldub, upperIndx[0]);
2555 itemsbefore =
Min(slicelb, oldub + 1) - oldlb;
2558 elmlen, elmbyval, elmalign);
2560 if (slicelb > sliceub)
2567 nolditems = sliceub - slicelb + 1;
2569 itemsbefore, oldarraybitmap,
2571 elmlen, elmbyval, elmalign);
2574 itemsafter = oldub + 1 -
Max(sliceub + 1, oldlb);
2575 lenafter = olddatasize - lenbefore - olditemsize;
2578 newsize = overheadlen + olddatasize - olditemsize + newitemsize;
2582 newarray->
ndim = ndim;
2583 newarray->
dataoffset = newhasnulls ? overheadlen : 0;
2585 memcpy(
ARR_DIMS(newarray), dim, ndim *
sizeof(
int));
2586 memcpy(
ARR_LBOUND(newarray), lb, ndim *
sizeof(
int));
2596 lowerIndx, upperIndx,
2597 elmlen, elmbyval, elmalign);
2602 memcpy((
char *) newarray + overheadlen,
2603 (
char *) array + oldoverheadlen,
2605 memcpy((
char *) newarray + overheadlen + lenbefore,
2608 memcpy((
char *) newarray + overheadlen + lenbefore + newitemsize,
2609 (
char *) array + oldoverheadlen + lenbefore + olditemsize,
2618 MemSet(newnullbitmap, 0, (nitems + 7) / 8);
2626 oldnullbitmap, itemsbefore + nolditems,
2693 if (fcinfo->
nargs < 1)
2728 inp_typlen = inp_extra->
typlen;
2729 inp_typbyval = inp_extra->
typbyval;
2730 inp_typalign = inp_extra->
typalign;
2740 typlen = ret_extra->
typlen;
2746 nulls = (
bool *)
palloc(nitems *
sizeof(
bool));
2754 for (i = 0; i < nitems; i++)
2759 if (bitmap && (*bitmap & bitmask) == 0)
2765 elt =
fetch_att(s, inp_typbyval, inp_typlen);
2768 fcinfo->
arg[0] = elt;
2779 for (j = 0; j < fcinfo->
nargs; j++)
2811 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2812 errmsg(
"array size exceeds the maximum allowed (%d)",
2820 if (bitmask == 0x100)
2832 nbytes += dataoffset;
2841 result->
ndim = ndim;
2850 values, nulls, nitems,
2851 typlen, typbyval, typalign,
2879 int elmlen,
bool elmbyval,
char elmalign)
2888 elmtype, elmlen, elmbyval, elmalign);
2916 Oid elmtype,
int elmlen,
bool elmbyval,
char elmalign)
2927 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2928 errmsg(
"invalid number of dimensions: %d", ndims)));
2931 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2932 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
2944 for (i = 0; i < nelems; i++)
2946 if (nulls && nulls[i])
2959 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2960 errmsg(
"array size exceeds the maximum allowed (%d)",
2968 nbytes += dataoffset;
2977 result->
ndim = ndims;
2980 memcpy(
ARR_DIMS(result), dims, ndims *
sizeof(
int));
2981 memcpy(
ARR_LBOUND(result), lbs, ndims *
sizeof(
int));
2984 elems, nulls, nelems,
2985 elmlen, elmbyval, elmalign,
3031 int elmlen,
bool elmbyval,
char elmalign,
3032 Datum **elemsp,
bool **nullsp,
int *nelemsp)
3047 *nullsp = nulls = (
bool *)
palloc0(nelems *
sizeof(
bool));
3056 for (i = 0; i < nelems; i++)
3059 if (bitmap && (*bitmap & bitmask) == 0)
3066 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3067 errmsg(
"null array element not allowed in this context")));
3080 if (bitmask == 0x100)
3113 if (*bitmap != 0xFF)
3123 if ((*bitmap & bitmask) == 0)
3169 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3170 errmsg(
"cannot compare arrays of different element types")));
3173 if (ndims1 != ndims2 ||
3174 memcmp(dims1, dims2, 2 * ndims1 *
sizeof(
int)) != 0)
3185 if (typentry ==
NULL ||
3186 typentry->
type_id != element_type)
3192 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
3193 errmsg(
"could not identify an equality operator for type %s",
3195 fcinfo->flinfo->fn_extra = (
void *) typentry;
3197 typlen = typentry->
typlen;
3215 for (i = 0; i < nitems; i++)
3224 if (bitmap1 && (*bitmap1 & bitmask) == 0)
3232 elt1 =
fetch_att(ptr1, typbyval, typlen);
3237 if (bitmap2 && (*bitmap2 & bitmask) == 0)
3245 elt2 =
fetch_att(ptr2, typbyval, typlen);
3252 if (bitmask == 0x100)
3264 if (isnull1 && isnull2)
3266 if (isnull1 || isnull2)
3275 locfcinfo.
arg[0] = elt1;
3276 locfcinfo.
arg[1] = elt2;
3279 locfcinfo.
isnull =
false;
3377 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3378 errmsg(
"cannot compare arrays of different element types")));
3387 if (typentry ==
NULL ||
3388 typentry->
type_id != element_type)
3394 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
3395 errmsg(
"could not identify a comparison function for type %s",
3399 typlen = typentry->
typlen;
3410 min_nitems =
Min(nitems1, nitems2);
3417 for (i = 0; i < min_nitems; i++)
3426 if (bitmap1 && (*bitmap1 & bitmask) == 0)
3434 elt1 =
fetch_att(ptr1, typbyval, typlen);
3439 if (bitmap2 && (*bitmap2 & bitmask) == 0)
3447 elt2 =
fetch_att(ptr2, typbyval, typlen);
3454 if (bitmask == 0x100)
3466 if (isnull1 && isnull2)
3482 locfcinfo.
arg[0] = elt1;
3483 locfcinfo.
arg[1] = elt2;
3486 locfcinfo.
isnull =
false;
3514 if (nitems1 != nitems2)
3515 result = (nitems1 < nitems2) ? -1 : 1;
3516 else if (ndims1 != ndims2)
3517 result = (ndims1 < ndims2) ? -1 : 1;
3521 for (i = 0; i < ndims1 * 2; i++)
3523 if (dims1[i] != dims2[i])
3525 result = (dims1[
i] < dims2[
i]) ? -1 : 1;
3572 if (typentry ==
NULL ||
3573 typentry->
type_id != element_type)
3579 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
3580 errmsg(
"could not identify a hash function for type %s",
3582 fcinfo->flinfo->fn_extra = (
void *) typentry;
3584 typlen = typentry->
typlen;
3600 for (i = 0; i < nitems; i++)
3605 if (bitmap && (*bitmap & bitmask) == 0)
3619 locfcinfo.
arg[0] = elt;
3621 locfcinfo.
isnull =
false;
3629 if (bitmask == 0x100)
3647 result = (result << 5) - result + elthash;
3673 bool matchall,
void **fn_extra)
3675 bool result = matchall;
3694 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3695 errmsg(
"cannot compare arrays of different element types")));
3704 if (typentry ==
NULL ||
3705 typentry->
type_id != element_type)
3711 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
3712 errmsg(
"could not identify an equality operator for type %s",
3714 *fn_extra = (
void *) typentry;
3716 typlen = typentry->
typlen;
3726 &values2, &nulls2, &nelems2);
3740 for (i = 0; i < nelems1; i++)
3746 if (bitmap1 && (*bitmap1 & bitmask) == 0)
3754 elt1 =
fetch_att(ptr1, typbyval, typlen);
3761 if (bitmask == 0x100)
3783 for (j = 0; j < nelems2; j++)
3785 Datum elt2 = values2[j];
3786 bool isnull2 = nulls2[j];
3795 locfcinfo.
arg[0] = elt1;
3796 locfcinfo.
arg[1] = elt2;
3799 locfcinfo.
isnull =
false;
3840 &fcinfo->flinfo->fn_extra);
3858 &fcinfo->flinfo->fn_extra);
3876 &fcinfo->flinfo->fn_extra);
3913 if (slice_ndim < 0 || slice_ndim >
ARR_NDIM(arr))
3914 elog(
ERROR,
"invalid arguments to array_create_iterator");
3919 iterator->
arr = arr;
4015 for (i = 0; i < iterator->
slice_len; i++)
4081 if (nullbitmap ==
NULL)
4083 if (nullbitmap[offset / 8] & (1 << (offset % 8)))
4100 nullbitmap += offset / 8;
4101 bitmask = 1 << (offset % 8);
4103 *nullbitmap &= ~bitmask;
4105 *nullbitmap |= bitmask;
4165 int typlen,
bool typbyval,
char typalign)
4171 if (typlen > 0 && !nullbitmap)
4177 nullbitmap += offset / 8;
4178 bitmask = 1 << (offset % 8);
4180 for (i = 0; i < nitems; i++)
4182 if (*nullbitmap & bitmask)
4188 if (bitmask == 0x100)
4197 for (i = 0; i < nitems; i++)
4213 int typlen,
bool typbyval,
char typalign)
4215 return array_seek(ptr, offset, nullbitmap, nitems,
4216 typlen, typbyval, typalign) - ptr;
4235 char *srcptr,
int offset,
bits8 *nullbitmap,
4236 int typlen,
bool typbyval,
char typalign)
4241 typlen, typbyval, typalign);
4242 memcpy(destptr, srcptr, numbytes);
4265 const bits8 *srcbitmap,
int srcoffset,
4276 destbitmap += destoffset / 8;
4277 destbitmask = 1 << (destoffset % 8);
4278 destbitval = *destbitmap;
4281 srcbitmap += srcoffset / 8;
4282 srcbitmask = 1 << (srcoffset % 8);
4283 srcbitval = *srcbitmap;
4284 while (nitems-- > 0)
4286 if (srcbitval & srcbitmask)
4287 destbitval |= destbitmask;
4289 destbitval &= ~destbitmask;
4291 if (destbitmask == 0x100)
4293 *destbitmap++ = destbitval;
4296 destbitval = *destbitmap;
4299 if (srcbitmask == 0x100)
4304 srcbitval = *srcbitmap;
4307 if (destbitmask != 1)
4308 *destbitmap = destbitval;
4312 while (nitems-- > 0)
4314 destbitval |= destbitmask;
4316 if (destbitmask == 0x100)
4318 *destbitmap++ = destbitval;
4321 destbitval = *destbitmap;
4324 if (destbitmask != 1)
4325 *destbitmap = destbitval;
4336 int ndim,
int *dim,
int *lb,
4338 int typlen,
bool typbyval,
char typalign)
4354 if (typlen > 0 && !arraynullsptr)
4359 ptr =
array_seek(arraydataptr, 0, arraynullsptr, src_offset,
4360 typlen, typbyval, typalign);
4363 for (i = 0; i < ndim; i++)
4370 ptr =
array_seek(ptr, src_offset, arraynullsptr, dist[j],
4371 typlen, typbyval, typalign);
4372 src_offset += dist[j];
4400 bits8 *arraynullsptr,
4421 srcdataptr =
array_seek(arraydataptr, 0, arraynullsptr, src_offset,
4422 typlen, typbyval, typalign);
4426 for (i = 0; i < ndim; i++)
4435 srcdataptr =
array_seek(srcdataptr, src_offset, arraynullsptr,
4437 typlen, typbyval, typalign);
4438 src_offset += dist[j];
4441 srcdataptr, src_offset, arraynullsptr,
4442 typlen, typbyval, typalign);
4445 arraynullsptr, src_offset,
4502 origPtr, 0, origBitmap,
4503 typlen, typbyval, typalign);
4508 orig_offset = dest_offset;
4512 for (i = 0; i < ndim; i++)
4522 origPtr, orig_offset, origBitmap,
4523 typlen, typbyval, typalign);
4528 origBitmap, orig_offset,
4530 dest_offset += dist[j];
4531 orig_offset += dist[j];
4535 srcPtr, src_offset, srcBitmap,
4536 typlen, typbyval, typalign);
4539 srcBitmap, src_offset,
4546 origPtr =
array_seek(origPtr, orig_offset, origBitmap, 1,
4547 typlen, typbyval, typalign);
4552 array_copy(destPtr, orignitems - orig_offset,
4553 origPtr, orig_offset, origBitmap,
4554 typlen, typbyval, typalign);
4557 origBitmap, orig_offset,
4558 orignitems - orig_offset);
4569 Datum dvalue,
bool disnull,
4609 astate->
dnulls = (
bool *)
4624 if (astate->
typlen == -1)
4652 dims[0] = astate->
nelems;
4711 result = ((
array_cmp(fcinfo) > 0) ? v1 : v2);
4726 result = ((
array_cmp(fcinfo) < 0) ? v1 : v2);
4766 if (reqdim <= 0 || reqdim >
ARR_NDIM(v))
4778 fctx->
lower = lb[reqdim - 1];
4779 fctx->
upper = dimv[reqdim - 1] + lb[reqdim - 1] - 1;
4830 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4831 errmsg(
"dimension array or low bound array cannot be null")));
4849 elog(
ERROR,
"could not determine data type of input");
4870 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4871 errmsg(
"dimension array or low bound array cannot be null")));
4888 elog(
ERROR,
"could not determine data type of input");
4896 Oid elmtype,
int dataoffset)
4902 result->
ndim = ndims;
4905 memcpy(
ARR_DIMS(result), dimv, ndims *
sizeof(
int));
4906 memcpy(
ARR_LBOUND(result), lbsv, ndims *
sizeof(
int));
4932 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4933 errmsg(
"wrong number of array subscripts"),
4934 errdetail(
"Dimension array must be one dimensional.")));
4938 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4939 errmsg(
"wrong range of array subscripts"),
4940 errdetail(
"Lower bound of dimension array must be one.")));
4944 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4945 errmsg(
"dimension values cannot be null")));
4952 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4953 errmsg(
"invalid number of dimensions: %d", ndims)));
4956 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4957 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
4964 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4965 errmsg(
"wrong number of array subscripts"),
4966 errdetail(
"Dimension array must be one dimensional.")));
4970 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4971 errmsg(
"wrong range of array subscripts"),
4972 errdetail(
"Lower bound of dimension array must be one.")));
4976 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4977 errmsg(
"dimension values cannot be null")));
4981 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
4982 errmsg(
"wrong number of array subscripts"),
4983 errdetail(
"Low bound array has different size than dimensions array.")));
4991 for (i = 0; i <
MAXDIM; i++)
5008 if (my_extra ==
NULL)
5026 elmlen = my_extra->
typlen;
5046 totbytes = nbytes * nitems;
5049 if (totbytes / nbytes != nitems ||
5052 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5053 errmsg(
"array size exceeds the maximum allowed (%d)",
5066 for (i = 0; i < nitems; i++)
5075 nbytes = dataoffset;
5078 elmtype, dataoffset);
5099 bits8 *arraynullsptr;
5103 } array_unnest_fctx;
5106 array_unnest_fctx *fctx;
5132 fctx = (array_unnest_fctx *)
palloc(
sizeof(array_unnest_fctx));
5155 if (fctx->nextelem < fctx->numelems)
5157 int offset = fctx->nextelem++;
5165 fcinfo->isnull =
true;
5174 char *ptr = fctx->elemdataptr;
5176 fcinfo->isnull =
false;
5177 elem =
ArrayCast(ptr, fctx->elmbyval, fctx->elmlen);
5184 fctx->elemdataptr = ptr;
5207 Datum search,
bool search_isnull,
5208 Datum replace,
bool replace_isnull,
5209 bool remove,
Oid collation,
5230 bool changed =
false;
5247 if (
remove && ndim > 1)
5249 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5250 errmsg(
"removing elements from multidimensional arrays is not supported")));
5257 if (typentry ==
NULL ||
5258 typentry->
type_id != element_type)
5264 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
5265 errmsg(
"could not identify an equality operator for type %s",
5269 typlen = typentry->
typlen;
5282 if (!replace_isnull)
5292 nulls = (
bool *)
palloc(nitems *
sizeof(
bool));
5301 for (i = 0; i < nitems; i++)
5309 if (bitmap && (*bitmap & bitmask) == 0)
5320 else if (!replace_isnull)
5322 values[nresult] = replace;
5331 elt =
fetch_att(arraydataptr, typbyval, typlen);
5338 values[nresult] = elt;
5345 locfcinfo.
arg[0] = elt;
5346 locfcinfo.
arg[1] = search;
5349 locfcinfo.
isnull =
false;
5354 values[nresult] = elt;
5364 values[nresult] = replace;
5365 isNull = replace_isnull;
5373 nulls[nresult] = isNull;
5384 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5385 errmsg(
"array size exceeds the maximum allowed (%d)",
5395 if (bitmask == 0x100)
5425 nbytes += dataoffset;
5434 result->
ndim = ndim;
5447 values, nulls, nresult,
5448 typlen, typbyval, typalign,
5474 search, search_isnull,
5498 search, search_isnull,
5499 replace, replace_isnull,