33 #include "utils/fmgroids.h"
50 #define AARR_FREE_IF_COPY(array,n) \
52 if (!VARATT_IS_EXPANDED_HEADER(array)) \
53 PG_FREE_IF_COPY(array, n); \
93 static int ArrayCount(
const char *
str,
int *dim,
char typdelim,
95 static bool ReadArrayStr(
char *arrayStr,
const char *origStr,
96 int nitems,
int ndim,
int *dim,
99 int typlen,
bool typbyval,
char typalign,
101 bool *hasnulls,
int32 *nbytes,
Node *escontext);
104 int typlen,
bool typbyval,
char typalign,
106 bool *hasnulls,
int32 *nbytes);
108 int nSubscripts,
int *indx,
110 int elmlen,
bool elmbyval,
char elmalign,
113 int nSubscripts,
int *indx,
114 Datum dataValue,
bool isNull,
116 int elmlen,
bool elmbyval,
char elmalign);
121 int typlen,
bool typbyval,
char typalign,
124 int typlen,
bool typbyval,
char typalign);
128 char *srcptr,
int offset,
bits8 *nullbitmap,
129 int typlen,
bool typbyval,
char typalign);
131 int ndim,
int *dim,
int *lb,
133 int typlen,
bool typbyval,
char typalign);
135 int ndim,
int *dim,
int *lb,
136 char *arraydataptr,
bits8 *arraynullsptr,
138 int typlen,
bool typbyval,
char typalign);
141 int ndim,
int *dim,
int *lb,
143 int typlen,
bool typbyval,
char typalign);
146 Oid elmtype,
int dataoffset);
151 Datum search,
bool search_isnull,
152 Datum replace,
bool replace_isnull,
153 bool remove,
Oid collation,
181 Node *escontext = fcinfo->context;
208 if (my_extra == NULL)
229 typlen = my_extra->
typlen;
264 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
265 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
268 for (q = p; isdigit((
unsigned char) *q) || (*q ==
'-') || (*q ==
'+'); q++)
272 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
273 errmsg(
"malformed array literal: \"%s\"",
string),
274 errdetail(
"\"[\" must introduce explicitly-specified array dimensions.")));
280 lBound[ndim] = atoi(p);
282 for (q = p; isdigit((
unsigned char) *q) || (*q ==
'-') || (*q ==
'+'); q++)
286 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
287 errmsg(
"malformed array literal: \"%s\"",
string),
288 errdetail(
"Missing array dimension value.")));
297 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
298 errmsg(
"malformed array literal: \"%s\"",
string),
299 errdetail(
"Missing \"%s\" after array dimensions.",
305 if (ub < lBound[ndim])
307 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
308 errmsg(
"upper bound cannot be less than lower bound")));
310 dim[ndim] = ub - lBound[ndim] + 1;
319 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
320 errmsg(
"malformed array literal: \"%s\"",
string),
321 errdetail(
"Array value must start with \"{\" or dimension information.")));
322 ndim =
ArrayCount(p, dim, typdelim, escontext);
325 for (
i = 0;
i < ndim;
i++)
336 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
337 errmsg(
"malformed array literal: \"%s\"",
string),
338 errdetail(
"Missing \"%s\" after array dimensions.",
350 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
351 errmsg(
"malformed array literal: \"%s\"",
string),
352 errdetail(
"Array contents must start with \"{\".")));
353 ndim_braces =
ArrayCount(p, dim_braces, typdelim, escontext);
356 if (ndim_braces != ndim)
358 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
359 errmsg(
"malformed array literal: \"%s\"",
string),
360 errdetail(
"Specified array dimensions do not match array contents.")));
361 for (
i = 0;
i < ndim; ++
i)
363 if (dim[
i] != dim_braces[
i])
365 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
366 errmsg(
"malformed array literal: \"%s\"",
string),
367 errdetail(
"Specified array dimensions do not match array contents.")));
372 printf(
"array_in- ndim %d (", ndim);
373 for (
i = 0;
i < ndim;
i++)
377 printf(
") for %s\n",
string);
395 &my_extra->
proc, typioparam, typmod,
399 &hasnulls, &nbytes, escontext))
404 nbytes += dataoffset;
422 memcpy(
ARR_DIMS(retval), dim, ndim *
sizeof(
int));
423 memcpy(
ARR_LBOUND(retval), lBound, ndim *
sizeof(
int));
426 dataPtr, nullsPtr,
nitems,
456 bool in_quotes =
false;
457 bool eoArray =
false;
458 bool empty_array =
true;
464 temp[
i] = dim[
i] = nelems_last[
i] = 0;
471 bool itemdone =
false;
484 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
485 errmsg(
"malformed array literal: \"%s\"",
str),
499 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
500 errmsg(
"malformed array literal: \"%s\"",
str),
501 errdetail(
"Unexpected \"%c\" character.",
510 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
511 errmsg(
"malformed array literal: \"%s\"",
str),
525 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
526 errmsg(
"malformed array literal: \"%s\"",
str),
527 errdetail(
"Unexpected array element.")));
528 in_quotes = !in_quotes;
546 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
547 errmsg(
"malformed array literal: \"%s\"",
str),
548 errdetail(
"Unexpected \"%c\" character.",
553 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
554 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
555 nest_level + 1,
MAXDIM)));
556 temp[nest_level] = 0;
558 if (ndim < nest_level)
576 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
577 errmsg(
"malformed array literal: \"%s\"",
str),
578 errdetail(
"Unexpected \"%c\" character.",
583 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
584 errmsg(
"malformed array literal: \"%s\"",
str),
585 errdetail(
"Unmatched \"%c\" character.",
'}')));
588 if (nelems_last[nest_level] != 0 &&
589 nelems[nest_level] != nelems_last[nest_level])
591 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
592 errmsg(
"malformed array literal: \"%s\"",
str),
593 errdetail(
"Multidimensional arrays must have "
594 "sub-arrays with matching "
596 nelems_last[nest_level] = nelems[nest_level];
597 nelems[nest_level] = 1;
599 eoArray = itemdone =
true;
606 temp[nest_level - 1]++;
613 if (*ptr == typdelim)
625 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
626 errmsg(
"malformed array literal: \"%s\"",
str),
627 errdetail(
"Unexpected \"%c\" character.",
634 nelems[nest_level - 1]++;
648 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
649 errmsg(
"malformed array literal: \"%s\"",
str),
650 errdetail(
"Unexpected array element.")));
668 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
669 errmsg(
"malformed array literal: \"%s\"",
str),
670 errdetail(
"Junk after closing right brace.")));
677 for (
i = 0;
i < ndim; ++
i)
738 bool in_quotes =
false;
739 bool eoArray =
false;
748 memset(nulls,
true,
nitems *
sizeof(
bool));
768 bool itemdone =
false;
769 bool leadingspace =
true;
770 bool hasquoting =
false;
776 itemstart = dstptr = dstendptr = srcptr;
785 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
786 errmsg(
"malformed array literal: \"%s\"",
794 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
795 errmsg(
"malformed array literal: \"%s\"",
797 *dstptr++ = *srcptr++;
799 leadingspace =
false;
804 in_quotes = !in_quotes;
806 leadingspace =
false;
822 if (nest_level >= ndim)
824 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
825 errmsg(
"malformed array literal: \"%s\"",
828 indx[nest_level - 1] = 0;
832 *dstptr++ = *srcptr++;
839 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
840 errmsg(
"malformed array literal: \"%s\"",
844 indx[nest_level - 1] = 0;
847 eoArray = itemdone =
true;
849 indx[nest_level - 1]++;
853 *dstptr++ = *srcptr++;
857 *dstptr++ = *srcptr++;
858 else if (*srcptr == typdelim)
875 *dstptr++ = *srcptr++;
879 *dstptr++ = *srcptr++;
880 leadingspace =
false;
892 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
893 errmsg(
"malformed array literal: \"%s\"",
937 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
938 errmsg(
"array size exceeds the maximum allowed (%d)",
984 if (nulls && nulls[
i])
987 elog(
ERROR,
"null array element where not supported");
1000 if (bitmask == 0x100)
1009 if (bitmap && bitmask != 1)
1031 dims_str[(
MAXDIM * 33) + 2];
1040 size_t overall_length;
1058 if (my_extra == NULL)
1079 typlen = my_extra->
typlen;
1099 for (
i = 0;
i < ndim;
i++)
1132 overall_length += 4;
1147 for (tmp =
values[
i]; *tmp !=
'\0'; tmp++)
1151 overall_length += 1;
1152 if (ch ==
'"' || ch ==
'\\')
1155 overall_length += 1;
1157 else if (ch ==
'{' || ch ==
'}' || ch == typdelim ||
1163 needquotes[
i] = needquote;
1167 overall_length += 2;
1169 overall_length += 1;
1178 for (
i =
j = 0, k = 1;
i < ndim;
i++)
1180 j += k, k *= dims[
i];
1182 overall_length += 2 *
j;
1188 char *ptr = dims_str;
1190 for (
i = 0;
i < ndim;
i++)
1192 sprintf(ptr,
"[%d:%d]", lb[
i], lb[
i] + dims[
i] - 1);
1197 overall_length += ptr - dims_str;
1201 retval = (
char *)
palloc(overall_length);
1204 #define APPENDSTR(str) (strcpy(p, (str)), p += strlen(p))
1205 #define APPENDCHAR(ch) (*p++ = (ch), *p = '\0')
1210 for (
i = 0;
i < ndim;
i++)
1216 for (
i =
j;
i < ndim - 1;
i++)
1222 for (tmp =
values[k]; *tmp; tmp++)
1226 if (ch ==
'"' || ch ==
'\\')
1237 for (
i = ndim - 1;
i >= 0;
i--)
1239 if (++(indx[
i]) < dims[
i])
1257 Assert(overall_length == (p - retval + 1));
1303 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1304 errmsg(
"invalid number of dimensions: %d", ndim)));
1307 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1308 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
1312 if (flags != 0 && flags != 1)
1314 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1315 errmsg(
"invalid array flags")));
1331 if (element_type != spec_element_type)
1336 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1337 errmsg(
"binary data has array element type %u (%s) instead of expected %u (%s)",
1344 element_type = spec_element_type;
1347 for (
i = 0;
i < ndim;
i++)
1363 if (my_extra == NULL)
1380 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
1381 errmsg(
"no binary input function available for type %s",
1394 typlen = my_extra->
typlen;
1402 &my_extra->
proc, typioparam, typmod,
1405 &hasnulls, &nbytes);
1409 nbytes += dataoffset;
1418 retval->
ndim = ndim;
1421 memcpy(
ARR_DIMS(retval), dim, ndim *
sizeof(
int));
1422 memcpy(
ARR_LBOUND(retval), lBound, ndim *
sizeof(
int));
1425 dataPtr, nullsPtr,
nitems,
1482 if (itemlen < -1 || itemlen > (
buf->len -
buf->cursor))
1484 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1485 errmsg(
"insufficient data left in message")));
1491 typioparam, typmod);
1503 elem_buf.
maxlen = itemlen + 1;
1504 elem_buf.
len = itemlen;
1507 buf->cursor += itemlen;
1509 csave =
buf->data[
buf->cursor];
1510 buf->data[
buf->cursor] =
'\0';
1514 typioparam, typmod);
1518 if (elem_buf.
cursor != itemlen)
1520 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1521 errmsg(
"improper binary format in array element %d",
1524 buf->data[
buf->cursor] = csave;
1546 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1547 errmsg(
"array size exceeds the maximum allowed (%d)",
1551 *hasnulls = hasnull;
1584 if (my_extra == NULL)
1601 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
1602 errmsg(
"no binary output function available for type %s",
1608 typlen = my_extra->
typlen;
1623 for (
i = 0;
i < ndim;
i++)
1707 sprintf(p,
"[%d:%d]", lb[
i], dimv[
i] + lb[
i] - 1);
1732 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
1736 result = lb[reqdim - 1];
1760 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
1766 result = dimv[reqdim - 1] + lb[reqdim - 1] - 1;
1789 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
1794 result = dimv[reqdim - 1];
1852 bits8 *arraynullsptr;
1854 if (arraytyplen > 0)
1860 fixedDim[0] = arraytyplen / elmlen;
1865 arraynullsptr = NULL;
1894 if (ndim != nSubscripts || ndim <= 0 || ndim >
MAXDIM)
1899 for (
i = 0;
i < ndim;
i++)
1901 if (indx[
i] < lb[
i] || indx[
i] >= (dim[
i] + lb[
i]))
1926 retptr =
array_seek(arraydataptr, 0, arraynullsptr, offset,
1927 elmlen, elmbyval, elmalign);
1928 return ArrayCast(retptr, elmbyval, elmlen);
1936 int nSubscripts,
int *indx,
1938 int elmlen,
bool elmbyval,
char elmalign,
1954 Assert(arraytyplen == -1);
1966 if (ndim != nSubscripts || ndim <= 0 || ndim >
MAXDIM)
1971 for (
i = 0;
i < ndim;
i++)
1973 if (indx[
i] < lb[
i] || indx[
i] >= (dim[
i] + lb[
i]))
1997 if (dnulls && dnulls[offset])
2010 return dvalues[offset];
2048 bool *upperProvided,
2049 bool *lowerProvided,
2066 bits8 *arraynullsptr;
2071 if (arraytyplen > 0)
2080 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2081 errmsg(
"slices of fixed-length arrays not implemented")));
2089 fixedDim[0] = arraytyplen / elmlen;
2095 arraynullsptr = NULL;
2115 if (ndim < nSubscripts || ndim <= 0 || ndim >
MAXDIM)
2118 for (
i = 0;
i < nSubscripts;
i++)
2120 if (!lowerProvided[
i] || lowerIndx[
i] < lb[
i])
2121 lowerIndx[
i] = lb[
i];
2122 if (!upperProvided[
i] || upperIndx[
i] >= (dim[
i] + lb[
i]))
2123 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2124 if (lowerIndx[
i] > upperIndx[
i])
2128 for (;
i < ndim;
i++)
2130 lowerIndx[
i] = lb[
i];
2131 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2132 if (lowerIndx[
i] > upperIndx[
i])
2140 lowerIndx, upperIndx,
2141 elmlen, elmbyval, elmalign);
2150 bytes += dataoffset;
2160 newarray->
ndim = ndim;
2163 memcpy(
ARR_DIMS(newarray), span, ndim *
sizeof(
int));
2170 for (
i = 0;
i < ndim;
i++)
2175 arraydataptr, arraynullsptr,
2176 lowerIndx, upperIndx,
2177 elmlen, elmbyval, elmalign);
2234 bits8 *oldnullbitmap;
2248 if (arraytyplen > 0)
2256 if (nSubscripts != 1)
2258 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2259 errmsg(
"wrong number of array subscripts")));
2261 if (indx[0] < 0 || indx[0] >= arraytyplen / elmlen)
2263 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2264 errmsg(
"array subscript out of range")));
2268 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2269 errmsg(
"cannot assign null value to an element of a fixed-length array")));
2271 resultarray = (
char *)
palloc(arraytyplen);
2273 elt_ptr = (
char *) resultarray + indx[0] * elmlen;
2278 if (nSubscripts <= 0 || nSubscripts >
MAXDIM)
2280 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2281 errmsg(
"wrong number of array subscripts")));
2284 if (elmlen == -1 && !isNull)
2315 for (
i = 0;
i < nSubscripts;
i++)
2322 nSubscripts, dim, lb,
2324 elmlen, elmbyval, elmalign));
2327 if (ndim != nSubscripts)
2329 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2330 errmsg(
"wrong number of array subscripts")));
2333 memcpy(dim,
ARR_DIMS(array), ndim *
sizeof(
int));
2334 memcpy(lb,
ARR_LBOUND(array), ndim *
sizeof(
int));
2337 addedbefore = addedafter = 0;
2344 if (indx[0] < lb[0])
2346 addedbefore = lb[0] - indx[0];
2347 dim[0] += addedbefore;
2349 if (addedbefore > 1)
2352 if (indx[0] >= (dim[0] + lb[0]))
2354 addedafter = indx[0] - (dim[0] + lb[0]) + 1;
2355 dim[0] += addedafter;
2366 for (
i = 0;
i < ndim;
i++)
2368 if (indx[
i] < lb[
i] ||
2369 indx[
i] >= (dim[
i] + lb[
i]))
2371 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2372 errmsg(
"array subscript out of range")));
2390 olddatasize =
ARR_SIZE(array) - oldoverheadlen;
2396 lenafter = olddatasize;
2398 else if (addedafter)
2401 lenbefore = olddatasize;
2409 elmlen, elmbyval, elmalign);
2418 lenafter = (int) (olddatasize - lenbefore - olditemlen);
2429 newsize = overheadlen + lenbefore + newitemlen + lenafter;
2436 newarray->
ndim = ndim;
2437 newarray->
dataoffset = newhasnulls ? overheadlen : 0;
2439 memcpy(
ARR_DIMS(newarray), dim, ndim *
sizeof(
int));
2440 memcpy(
ARR_LBOUND(newarray), lb, ndim *
sizeof(
int));
2445 memcpy((
char *) newarray + overheadlen,
2446 (
char *) array + oldoverheadlen,
2450 (
char *) newarray + overheadlen + lenbefore);
2451 memcpy((
char *) newarray + overheadlen + lenbefore + newitemlen,
2452 (
char *) array + oldoverheadlen + lenbefore + olditemlen,
2481 if (addedafter == 0)
2483 oldnullbitmap, offset + 1,
2484 oldnitems - offset - 1);
2500 int nSubscripts,
int *indx,
2501 Datum dataValue,
bool isNull,
2503 int elmlen,
bool elmbyval,
char elmalign)
2523 Assert(arraytyplen == -1);
2535 memcpy(dim, eah->
dims, ndim *
sizeof(
int));
2536 memcpy(lb, eah->
lbound, ndim *
sizeof(
int));
2537 dimschanged =
false;
2552 nSubscripts *
sizeof(
int));
2554 nSubscripts *
sizeof(
int));
2558 for (
i = 0;
i < nSubscripts;
i++)
2565 else if (ndim != nSubscripts)
2567 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2568 errmsg(
"wrong number of array subscripts")));
2597 newhasnulls = ((dnulls != NULL) || isNull);
2598 addedbefore = addedafter = 0;
2605 if (indx[0] < lb[0])
2607 addedbefore = lb[0] - indx[0];
2608 dim[0] += addedbefore;
2611 if (addedbefore > 1)
2614 if (indx[0] >= (dim[0] + lb[0]))
2616 addedafter = indx[0] - (dim[0] + lb[0]) + 1;
2617 dim[0] += addedafter;
2629 for (
i = 0;
i < ndim;
i++)
2631 if (indx[
i] < lb[
i] ||
2632 indx[
i] >= (dim[
i] + lb[
i]))
2634 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2635 errmsg(
"array subscript out of range")));
2653 int newlen = dim[0] + dim[0] / 8;
2655 newlen =
Max(newlen, dim[0]);
2659 eah->
dnulls = dnulls = (
bool *)
2660 repalloc(dnulls, newlen *
sizeof(
bool));
2668 if (newhasnulls && dnulls == NULL)
2669 eah->
dnulls = dnulls = (
bool *)
2687 memcpy(eah->
dims, dim, ndim *
sizeof(
int));
2688 memcpy(eah->
lbound, lb, ndim *
sizeof(
int));
2692 if (addedbefore > 0)
2694 memmove(dvalues + addedbefore, dvalues, eah->
nelems *
sizeof(
Datum));
2695 for (
i = 0;
i < addedbefore;
i++)
2699 memmove(dnulls + addedbefore, dnulls, eah->
nelems *
sizeof(
bool));
2700 for (
i = 0;
i < addedbefore;
i++)
2703 eah->
nelems += addedbefore;
2709 for (
i = 0;
i < addedafter;
i++)
2713 for (
i = 0;
i < addedafter;
i++)
2714 dnulls[eah->
nelems +
i] =
true;
2716 eah->
nelems += addedafter;
2720 if (!eah->
typbyval && (dnulls == NULL || !dnulls[offset]))
2726 dvalues[offset] = dataValue;
2728 dnulls[offset] = isNull;
2738 if (oldValue < eah->fstartptr || oldValue >= eah->
fendptr)
2792 bool *upperProvided,
2793 bool *lowerProvided,
2794 Datum srcArrayDatum,
2830 if (arraytyplen > 0)
2836 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2837 errmsg(
"updates on slices of fixed-length arrays not implemented")));
2861 &dvalues, &dnulls, &nelems);
2863 for (
i = 0;
i < nSubscripts;
i++)
2865 if (!upperProvided[
i] || !lowerProvided[
i])
2867 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2868 errmsg(
"array slice subscript must provide both boundaries"),
2869 errdetail(
"When assigning to a slice of an empty array value,"
2870 " slice boundaries must be fully specified.")));
2872 dim[
i] = 1 + upperIndx[
i] - lowerIndx[
i];
2873 lb[
i] = lowerIndx[
i];
2879 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2880 errmsg(
"source array too small")));
2884 elmlen, elmbyval, elmalign));
2887 if (ndim < nSubscripts || ndim <= 0 || ndim >
MAXDIM)
2889 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2890 errmsg(
"wrong number of array subscripts")));
2893 memcpy(dim,
ARR_DIMS(array), ndim *
sizeof(
int));
2894 memcpy(lb,
ARR_LBOUND(array), ndim *
sizeof(
int));
2897 addedbefore = addedafter = 0;
2904 Assert(nSubscripts == 1);
2905 if (!lowerProvided[0])
2906 lowerIndx[0] = lb[0];
2907 if (!upperProvided[0])
2908 upperIndx[0] = dim[0] + lb[0] - 1;
2909 if (lowerIndx[0] > upperIndx[0])
2911 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2912 errmsg(
"upper bound cannot be less than lower bound")));
2913 if (lowerIndx[0] < lb[0])
2915 if (upperIndx[0] < lb[0] - 1)
2917 addedbefore = lb[0] - lowerIndx[0];
2918 dim[0] += addedbefore;
2919 lb[0] = lowerIndx[0];
2921 if (upperIndx[0] >= (dim[0] + lb[0]))
2923 if (lowerIndx[0] > (dim[0] + lb[0]))
2925 addedafter = upperIndx[0] - (dim[0] + lb[0]) + 1;
2926 dim[0] += addedafter;
2935 for (
i = 0;
i < nSubscripts;
i++)
2937 if (!lowerProvided[
i])
2938 lowerIndx[
i] = lb[
i];
2939 if (!upperProvided[
i])
2940 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2941 if (lowerIndx[
i] > upperIndx[
i])
2943 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2944 errmsg(
"upper bound cannot be less than lower bound")));
2945 if (lowerIndx[
i] < lb[
i] ||
2946 upperIndx[
i] >= (dim[
i] + lb[
i]))
2948 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2949 errmsg(
"array subscript out of range")));
2952 for (;
i < ndim;
i++)
2954 lowerIndx[
i] = lb[
i];
2955 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2956 if (lowerIndx[
i] > upperIndx[
i])
2958 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2959 errmsg(
"upper bound cannot be less than lower bound")));
2975 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2976 errmsg(
"source array too small")));
2988 elmlen, elmbyval, elmalign);
2990 olddatasize =
ARR_SIZE(array) - oldoverheadlen;
3000 lowerIndx, upperIndx,
3001 elmlen, elmbyval, elmalign);
3002 lenbefore = lenafter = 0;
3003 itemsbefore = itemsafter = nolditems = 0;
3012 int oldub = oldlb +
ARR_DIMS(array)[0] - 1;
3013 int slicelb =
Max(oldlb, lowerIndx[0]);
3014 int sliceub =
Min(oldub, upperIndx[0]);
3019 itemsbefore =
Min(slicelb, oldub + 1) - oldlb;
3022 elmlen, elmbyval, elmalign);
3024 if (slicelb > sliceub)
3031 nolditems = sliceub - slicelb + 1;
3033 itemsbefore, oldarraybitmap,
3035 elmlen, elmbyval, elmalign);
3038 itemsafter = oldub + 1 -
Max(sliceub + 1, oldlb);
3039 lenafter = olddatasize - lenbefore - olditemsize;
3042 newsize = overheadlen + olddatasize - olditemsize + newitemsize;
3046 newarray->
ndim = ndim;
3047 newarray->
dataoffset = newhasnulls ? overheadlen : 0;
3049 memcpy(
ARR_DIMS(newarray), dim, ndim *
sizeof(
int));
3050 memcpy(
ARR_LBOUND(newarray), lb, ndim *
sizeof(
int));
3060 lowerIndx, upperIndx,
3061 elmlen, elmbyval, elmalign);
3066 memcpy((
char *) newarray + overheadlen,
3067 (
char *) array + oldoverheadlen,
3069 memcpy((
char *) newarray + overheadlen + lenbefore,
3072 memcpy((
char *) newarray + overheadlen + lenbefore + newitemsize,
3073 (
char *) array + oldoverheadlen + lenbefore + olditemsize,
3089 oldnullbitmap, itemsbefore + nolditems,
3106 int arraytyplen,
int elmlen,
bool elmbyval,
char elmalign,
3110 arraytyplen, elmlen, elmbyval, elmalign,
3123 Datum dataValue,
bool isNull,
3124 int arraytyplen,
int elmlen,
bool elmbyval,
char elmalign)
3130 elmlen, elmbyval, elmalign));
3216 inp_typlen = inp_extra->
typlen;
3217 inp_typbyval = inp_extra->
typbyval;
3218 inp_typalign = inp_extra->
typalign;
3228 typlen = ret_extra->
typlen;
3245 inp_typlen, inp_typbyval, inp_typalign);
3263 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3264 errmsg(
"array size exceeds the maximum allowed (%d)",
3273 nbytes += dataoffset;
3282 result->
ndim = ndim;
3322 int elmlen,
bool elmbyval,
char elmalign)
3331 elmtype, elmlen, elmbyval, elmalign);
3351 elmalign = TYPALIGN_CHAR;
3357 elmalign = TYPALIGN_CHAR;
3363 elmalign = TYPALIGN_INT;
3367 elmlen =
sizeof(
int16);
3369 elmalign = TYPALIGN_SHORT;
3373 elmlen =
sizeof(
int32);
3375 elmalign = TYPALIGN_INT;
3379 elmlen =
sizeof(int64);
3381 elmalign = TYPALIGN_DOUBLE;
3387 elmalign = TYPALIGN_CHAR;
3392 elmlen =
sizeof(
Oid);
3394 elmalign = TYPALIGN_INT;
3400 elmalign = TYPALIGN_INT;
3406 elmalign = TYPALIGN_SHORT;
3410 elog(
ERROR,
"type %u not supported by construct_array_builtin()", elmtype);
3417 return construct_array(elems, nelems, elmtype, elmlen, elmbyval, elmalign);
3446 Oid elmtype,
int elmlen,
bool elmbyval,
char elmalign)
3457 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3458 errmsg(
"invalid number of dimensions: %d", ndims)));
3461 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3462 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
3476 for (
i = 0;
i < nelems;
i++)
3478 if (nulls && nulls[
i])
3491 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3492 errmsg(
"array size exceeds the maximum allowed (%d)",
3500 nbytes += dataoffset;
3509 result->
ndim = ndims;
3512 memcpy(
ARR_DIMS(result), dims, ndims *
sizeof(
int));
3513 memcpy(
ARR_LBOUND(result), lbs, ndims *
sizeof(
int));
3516 elems, nulls, nelems,
3517 elmlen, elmbyval, elmalign,
3580 int elmlen,
bool elmbyval,
char elmalign,
3581 Datum **elemsp,
bool **nullsp,
int *nelemsp)
3596 *nullsp = nulls = (
bool *)
palloc0(nelems *
sizeof(
bool));
3605 for (
i = 0;
i < nelems;
i++)
3608 if (bitmap && (*bitmap & bitmask) == 0)
3615 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3616 errmsg(
"null array element not allowed in this context")));
3629 if (bitmask == 0x100)
3646 Datum **elemsp,
bool **nullsp,
int *nelemsp)
3657 elmalign = TYPALIGN_CHAR;
3663 elmalign = TYPALIGN_CHAR;
3669 elmalign = TYPALIGN_DOUBLE;
3673 elmlen =
sizeof(
int16);
3675 elmalign = TYPALIGN_SHORT;
3679 elmlen =
sizeof(
Oid);
3681 elmalign = TYPALIGN_INT;
3687 elmalign = TYPALIGN_INT;
3693 elmalign = TYPALIGN_SHORT;
3697 elog(
ERROR,
"type %u not supported by deconstruct_array_builtin()", elmtype);
3704 deconstruct_array(array, elmtype, elmlen, elmbyval, elmalign, elemsp, nullsp, nelemsp);
3731 if (*bitmap != 0xFF)
3741 if ((*bitmap & bitmask) == 0)
3786 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3787 errmsg(
"cannot compare arrays of different element types")));
3790 if (ndims1 != ndims2 ||
3791 memcmp(dims1, dims2, ndims1 *
sizeof(
int)) != 0 ||
3792 memcmp(lbs1, lbs2, ndims1 *
sizeof(
int)) != 0)
3803 if (typentry == NULL ||
3804 typentry->
type_id != element_type)
3810 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
3811 errmsg(
"could not identify an equality operator for type %s",
3813 fcinfo->flinfo->fn_extra = (
void *) typentry;
3815 typlen = typentry->
typlen;
3823 collation, NULL, NULL);
3847 if (isnull1 && isnull2)
3849 if (isnull1 || isnull2)
3858 locfcinfo->args[0].value = elt1;
3859 locfcinfo->args[0].isnull =
false;
3860 locfcinfo->args[1].value = elt2;
3861 locfcinfo->args[1].isnull =
false;
3862 locfcinfo->isnull =
false;
3864 if (locfcinfo->isnull || !oprresult)
3957 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3958 errmsg(
"cannot compare arrays of different element types")));
3967 if (typentry == NULL ||
3968 typentry->
type_id != element_type)
3974 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
3975 errmsg(
"could not identify a comparison function for type %s",
3979 typlen = typentry->
typlen;
3987 collation, NULL, NULL);
3990 min_nitems =
Min(nitems1, nitems2);
3994 for (
i = 0;
i < min_nitems;
i++)
4009 if (isnull1 && isnull2)
4025 locfcinfo->args[0].value = elt1;
4026 locfcinfo->args[0].isnull =
false;
4027 locfcinfo->args[1].value = elt2;
4028 locfcinfo->args[1].isnull =
false;
4032 Assert(!locfcinfo->isnull);
4059 if (nitems1 != nitems2)
4060 result = (nitems1 < nitems2) ? -1 : 1;
4061 else if (ndims1 != ndims2)
4062 result = (ndims1 < ndims2) ? -1 : 1;
4065 for (
i = 0;
i < ndims1;
i++)
4067 if (dims1[
i] != dims2[
i])
4069 result = (dims1[
i] < dims2[
i]) ? -1 : 1;
4078 for (
i = 0;
i < ndims1;
i++)
4080 if (lbound1[
i] != lbound2[
i])
4082 result = (lbound1[
i] < lbound2[
i]) ? -1 : 1;
4128 if (typentry == NULL ||
4129 typentry->
type_id != element_type)
4135 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4136 errmsg(
"could not identify a hash function for type %s",
4146 if (element_type == RECORDOID)
4158 record_typentry =
palloc0(
sizeof(*record_typentry));
4159 record_typentry->
type_id = element_type;
4169 typentry = record_typentry;
4172 fcinfo->flinfo->fn_extra = (
void *) typentry;
4175 typlen = typentry->
typlen;
4206 locfcinfo->args[0].value = elt;
4207 locfcinfo->args[0].isnull =
false;
4210 Assert(!locfcinfo->isnull);
4224 result = (result << 5) - result + elthash;
4256 if (typentry == NULL ||
4257 typentry->
type_id != element_type)
4263 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4264 errmsg(
"could not identify an extended hash function for type %s",
4266 fcinfo->flinfo->fn_extra = (
void *) typentry;
4268 typlen = typentry->
typlen;
4295 locfcinfo->args[0].value = elt;
4296 locfcinfo->args[0].isnull =
false;
4298 locfcinfo->args[1].isnull =
false;
4301 Assert(!locfcinfo->isnull);
4304 result = (result << 5) - result + elthash;
4329 bool matchall,
void **fn_extra)
4332 bool result = matchall;
4348 (
errcode(ERRCODE_DATATYPE_MISMATCH),
4349 errmsg(
"cannot compare arrays of different element types")));
4358 if (typentry == NULL ||
4359 typentry->
type_id != element_type)
4365 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4366 errmsg(
"could not identify an equality operator for type %s",
4368 *fn_extra = (
void *) typentry;
4370 typlen = typentry->
typlen;
4389 element_type, typlen, typbyval,
typalign,
4390 &values2, &nulls2, &nelems2);
4396 collation, NULL, NULL);
4402 for (
i = 0;
i < nelems1;
i++)
4425 for (
j = 0;
j < nelems2;
j++)
4428 bool isnull2 = nulls2 ? nulls2[
j] :
false;
4437 locfcinfo->args[0].value = elt1;
4438 locfcinfo->args[0].isnull =
false;
4439 locfcinfo->args[1].value = elt2;
4440 locfcinfo->args[1].isnull =
false;
4441 locfcinfo->isnull =
false;
4443 if (!locfcinfo->isnull && oprresult)
4479 &fcinfo->flinfo->fn_extra);
4497 &fcinfo->flinfo->fn_extra);
4515 &fcinfo->flinfo->fn_extra);
4552 if (slice_ndim < 0 || slice_ndim >
ARR_NDIM(arr))
4553 elog(
ERROR,
"invalid arguments to array_create_iterator");
4558 iterator->
arr = arr;
4730 if (nullbitmap == NULL)
4732 if (nullbitmap[offset / 8] & (1 << (offset % 8)))
4749 nullbitmap += offset / 8;
4750 bitmask = 1 << (offset % 8);
4752 *nullbitmap &= ~bitmask;
4754 *nullbitmap |= bitmask;
4814 int typlen,
bool typbyval,
char typalign)
4820 if (typlen > 0 && !nullbitmap)
4826 nullbitmap += offset / 8;
4827 bitmask = 1 << (offset % 8);
4831 if (*nullbitmap & bitmask)
4837 if (bitmask == 0x100)
4862 int typlen,
bool typbyval,
char typalign)
4884 char *srcptr,
int offset,
bits8 *nullbitmap,
4885 int typlen,
bool typbyval,
char typalign)
4891 memcpy(destptr, srcptr, numbytes);
4914 const bits8 *srcbitmap,
int srcoffset,
4925 destbitmap += destoffset / 8;
4926 destbitmask = 1 << (destoffset % 8);
4927 destbitval = *destbitmap;
4930 srcbitmap += srcoffset / 8;
4931 srcbitmask = 1 << (srcoffset % 8);
4932 srcbitval = *srcbitmap;
4935 if (srcbitval & srcbitmask)
4936 destbitval |= destbitmask;
4938 destbitval &= ~destbitmask;
4940 if (destbitmask == 0x100)
4942 *destbitmap++ = destbitval;
4945 destbitval = *destbitmap;
4948 if (srcbitmask == 0x100)
4953 srcbitval = *srcbitmap;
4956 if (destbitmask != 1)
4957 *destbitmap = destbitval;
4963 destbitval |= destbitmask;
4965 if (destbitmask == 0x100)
4967 *destbitmap++ = destbitval;
4970 destbitval = *destbitmap;
4973 if (destbitmask != 1)
4974 *destbitmap = destbitval;
4985 int ndim,
int *dim,
int *lb,
4987 int typlen,
bool typbyval,
char typalign)
5003 if (typlen > 0 && !arraynullsptr)
5008 ptr =
array_seek(arraydataptr, 0, arraynullsptr, src_offset,
5012 for (
i = 0;
i < ndim;
i++)
5019 ptr =
array_seek(ptr, src_offset, arraynullsptr, dist[
j],
5021 src_offset += dist[
j];
5049 bits8 *arraynullsptr,
5070 srcdataptr =
array_seek(arraydataptr, 0, arraynullsptr, src_offset,
5075 for (
i = 0;
i < ndim;
i++)
5084 srcdataptr =
array_seek(srcdataptr, src_offset, arraynullsptr,
5087 src_offset += dist[
j];
5090 srcdataptr, src_offset, arraynullsptr,
5094 arraynullsptr, src_offset,
5151 origPtr, 0, origBitmap,
5157 orig_offset = dest_offset;
5161 for (
i = 0;
i < ndim;
i++)
5171 origPtr, orig_offset, origBitmap,
5177 origBitmap, orig_offset,
5179 dest_offset += dist[
j];
5180 orig_offset += dist[
j];
5184 srcPtr, src_offset, srcBitmap,
5188 srcBitmap, src_offset,
5195 origPtr =
array_seek(origPtr, orig_offset, origBitmap, 1,
5201 array_copy(destPtr, orignitems - orig_offset,
5202 origPtr, orig_offset, origBitmap,
5206 origBitmap, orig_offset,
5207 orignitems - orig_offset);
5248 subcontext ? 64 : 8);
5258 bool subcontext,
int initsize)
5273 astate->
alen = initsize;
5276 astate->
dnulls = (
bool *)
5298 Datum dvalue,
bool disnull,
5323 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5324 errmsg(
"array size exceeds the maximum allowed (%d)",
5328 astate->
dnulls = (
bool *)
5342 if (astate->
typlen == -1)
5375 ndims = (astate->
nelems > 0) ? 1 : 0;
5376 dims[0] = astate->
nelems;
5464 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5465 errmsg(
"data type %s is not an array type",
5472 "accumArrayResultArr",
5498 Datum dvalue,
bool disnull,
5519 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5520 errmsg(
"cannot accumulate null arrays")));
5540 if (astate->
ndims == 0)
5547 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
5548 errmsg(
"cannot accumulate empty arrays")));
5551 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5552 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
5559 astate->
ndims = ndims + 1;
5560 astate->
dims[0] = 0;
5561 memcpy(&astate->
dims[1], dims, ndims *
sizeof(
int));
5563 memcpy(&astate->
lbs[1], lbs, ndims *
sizeof(
int));
5572 if (astate->
ndims != ndims + 1)
5574 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
5575 errmsg(
"cannot accumulate arrays of different dimensionality")));
5576 for (
i = 0;
i < ndims;
i++)
5578 if (astate->
dims[
i + 1] != dims[
i] || astate->
lbs[
i + 1] != lbs[
i])
5580 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
5581 errmsg(
"cannot accumulate arrays of different dimensionality")));
5588 astate->
nbytes + ndatabytes);
5600 astate->
nbytes += ndatabytes;
5619 else if (newnitems > astate->
aitems)
5631 astate->
dims[0] += 1;
5660 if (astate->
ndims == 0)
5679 nbytes += dataoffset;
5775 Datum dvalue,
bool disnull,
5785 input_type, rcontext);
5789 input_type, rcontext);
5884 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
5896 fctx->
lower = lb[reqdim - 1];
5897 fctx->
upper = dimv[reqdim - 1] + lb[reqdim - 1] - 1;