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); \
92 int *dim,
int *lBound,
93 const char *origStr,
Node *escontext);
95 const char *origStr,
Node *escontext);
99 int typlen,
bool typbyval,
char typalign,
100 int *ndim_p,
int *dim,
102 Datum **values_p,
bool **nulls_p,
103 const char *origStr,
Node *escontext);
105 const char *origStr,
Node *escontext);
108 int typlen,
bool typbyval,
char typalign,
110 bool *hasnulls,
int32 *nbytes);
112 int nSubscripts,
int *indx,
114 int elmlen,
bool elmbyval,
char elmalign,
117 int nSubscripts,
int *indx,
118 Datum dataValue,
bool isNull,
120 int elmlen,
bool elmbyval,
char elmalign);
125 int typlen,
bool typbyval,
char typalign,
128 int typlen,
bool typbyval,
char typalign);
132 char *srcptr,
int offset,
bits8 *nullbitmap,
133 int typlen,
bool typbyval,
char typalign);
135 int ndim,
int *dim,
int *lb,
137 int typlen,
bool typbyval,
char typalign);
139 int ndim,
int *dim,
int *lb,
140 char *arraydataptr,
bits8 *arraynullsptr,
142 int typlen,
bool typbyval,
char typalign);
145 int ndim,
int *dim,
int *lb,
147 int typlen,
bool typbyval,
char typalign);
150 Oid elmtype,
int dataoffset);
155 Datum search,
bool search_isnull,
156 Datum replace,
bool replace_isnull,
157 bool remove,
Oid collation,
185 Node *escontext = fcinfo->context;
210 if (my_extra == NULL)
231 typlen = my_extra->
typlen;
263 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
264 errmsg(
"malformed array literal: \"%s\"",
string),
265 errdetail(
"Array value must start with \"{\" or dimension information.")));
272 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
273 errmsg(
"malformed array literal: \"%s\"",
string),
274 errdetail(
"Missing \"%s\" after array dimensions.",
283 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
284 errmsg(
"malformed array literal: \"%s\"",
string),
285 errdetail(
"Array contents must start with \"{\".")));
290 &my_extra->
proc, typioparam, typmod,
306 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
307 errmsg(
"malformed array literal: \"%s\"",
string),
308 errdetail(
"Junk after closing right brace.")));
334 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
335 errmsg(
"array size exceeds the maximum allowed (%d)",
342 nbytes += dataoffset;
364 memcpy(
ARR_DIMS(retval), dim, ndim *
sizeof(
int));
365 memcpy(
ARR_LBOUND(retval), lBound, ndim *
sizeof(
int));
403 const char *origStr,
Node *escontext)
430 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
431 errmsg(
"number of array dimensions exceeds the maximum allowed (%d)",
439 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
440 errmsg(
"malformed array literal: \"%s\"", origStr),
441 errdetail(
"\"[\" must introduce explicitly-specified array dimensions.")));
453 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
454 errmsg(
"malformed array literal: \"%s\"", origStr),
455 errdetail(
"Missing array dimension value.")));
465 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
466 errmsg(
"malformed array literal: \"%s\"", origStr),
467 errdetail(
"Missing \"%s\" after array dimensions.",
478 if (ub < lBound[ndim])
480 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
481 errmsg(
"upper bound cannot be less than lower bound")));
486 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
487 errmsg(
"array upper bound is too large: %d", ub)));
493 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
494 errmsg(
"array size exceeds the maximum allowed (%d)",
520 const char *origStr,
Node *escontext)
526 if (!isdigit((
unsigned char) *p) && *p !=
'-' && *p !=
'+')
533 l = strtol(p, srcptr, 10);
537 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
538 errmsg(
"array bound is out of integer range")));
596 bool dimensions_specified = (ndim != 0);
621 ndim_frozen = dimensions_specified;
622 expect_delim =
false;
627 tok =
ReadArrayToken(srcptr, &elembuf, typdelim, origStr, escontext);
635 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
636 errmsg(
"malformed array literal: \"%s\"", origStr),
637 errdetail(
"Unexpected \"%c\" character.",
'{')));
642 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
643 errmsg(
"number of array dimensions exceeds the maximum allowed (%d)",
646 nelems[nest_level] = 0;
648 if (nest_level > ndim)
652 goto dimension_error;
665 if (nelems[nest_level - 1] > 0 && !expect_delim)
667 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
668 errmsg(
"malformed array literal: \"%s\"", origStr),
669 errdetail(
"Unexpected \"%c\" character.",
674 nelems[nest_level - 1]++;
681 if (dim[nest_level] < 0)
684 dim[nest_level] = nelems[nest_level];
686 else if (nelems[nest_level] != dim[nest_level])
689 goto dimension_error;
702 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
703 errmsg(
"malformed array literal: \"%s\"", origStr),
704 errdetail(
"Unexpected \"%c\" character.",
706 expect_delim =
false;
717 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
718 errmsg(
"malformed array literal: \"%s\"", origStr),
719 errdetail(
"Unexpected array element.")));
726 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
727 errmsg(
"array size exceeds the maximum allowed (%d)",
750 if (nest_level != ndim)
751 goto dimension_error;
753 nelems[nest_level - 1]++;
762 }
while (nest_level > 0);
774 if (dimensions_specified)
776 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
777 errmsg(
"malformed array literal: \"%s\"", origStr),
778 errdetail(
"Specified array dimensions do not match array contents.")));
781 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
782 errmsg(
"malformed array literal: \"%s\"", origStr),
783 errdetail(
"Multidimensional arrays must have sub-arrays with matching dimensions.")));
797 const char *origStr,
Node *escontext)
832 goto unquoted_element;
860 while (*(++p) !=
'\0')
862 if (*p == typdelim || *p ==
'}' || *p ==
'{')
869 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
870 errmsg(
"malformed array literal: \"%s\"", origStr),
871 errdetail(
"Incorrectly quoted array element.")));
896 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
897 errmsg(
"malformed array literal: \"%s\"", origStr),
898 errdetail(
"Unexpected \"%c\" character.",
903 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
904 errmsg(
"malformed array literal: \"%s\"", origStr),
905 errdetail(
"Incorrectly quoted array element.")));
912 dstlen = elembuf->
len;
917 if (*p == typdelim || *p ==
'}')
920 elembuf->
data[dstlen] =
'\0';
921 elembuf->
len = dstlen;
932 dstlen = elembuf->
len;
940 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
941 errmsg(
"malformed array literal: \"%s\"", origStr),
981 if (nulls && nulls[
i])
984 elog(
ERROR,
"null array element where not supported");
997 if (bitmask == 0x100)
1006 if (bitmap && bitmask != 1)
1028 dims_str[(
MAXDIM * 33) + 2];
1037 size_t overall_length;
1055 if (my_extra == NULL)
1076 typlen = my_extra->
typlen;
1096 for (
i = 0;
i < ndim;
i++)
1129 overall_length += 4;
1144 for (tmp =
values[
i]; *tmp !=
'\0'; tmp++)
1148 overall_length += 1;
1149 if (ch ==
'"' || ch ==
'\\')
1152 overall_length += 1;
1154 else if (ch ==
'{' || ch ==
'}' || ch == typdelim ||
1160 needquotes[
i] = needquote;
1164 overall_length += 2;
1166 overall_length += 1;
1175 for (
i =
j = 0, k = 1;
i < ndim;
i++)
1177 j += k, k *= dims[
i];
1179 overall_length += 2 *
j;
1185 char *ptr = dims_str;
1187 for (
i = 0;
i < ndim;
i++)
1189 sprintf(ptr,
"[%d:%d]", lb[
i], lb[
i] + dims[
i] - 1);
1194 overall_length += ptr - dims_str;
1198 retval = (
char *)
palloc(overall_length);
1201 #define APPENDSTR(str) (strcpy(p, (str)), p += strlen(p))
1202 #define APPENDCHAR(ch) (*p++ = (ch), *p = '\0')
1207 for (
i = 0;
i < ndim;
i++)
1213 for (
i =
j;
i < ndim - 1;
i++)
1219 for (tmp =
values[k]; *tmp; tmp++)
1223 if (ch ==
'"' || ch ==
'\\')
1234 for (
i = ndim - 1;
i >= 0;
i--)
1236 if (++(indx[
i]) < dims[
i])
1254 Assert(overall_length == (p - retval + 1));
1300 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1301 errmsg(
"invalid number of dimensions: %d", ndim)));
1304 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1305 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
1309 if (flags != 0 && flags != 1)
1311 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1312 errmsg(
"invalid array flags")));
1328 if (element_type != spec_element_type)
1333 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1334 errmsg(
"binary data has array element type %u (%s) instead of expected %u (%s)",
1341 element_type = spec_element_type;
1344 for (
i = 0;
i < ndim;
i++)
1360 if (my_extra == NULL)
1377 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
1378 errmsg(
"no binary input function available for type %s",
1391 typlen = my_extra->
typlen;
1399 &my_extra->
proc, typioparam, typmod,
1402 &hasnulls, &nbytes);
1406 nbytes += dataoffset;
1415 retval->
ndim = ndim;
1418 memcpy(
ARR_DIMS(retval), dim, ndim *
sizeof(
int));
1419 memcpy(
ARR_LBOUND(retval), lBound, ndim *
sizeof(
int));
1422 dataPtr, nullsPtr,
nitems,
1478 if (itemlen < -1 || itemlen > (
buf->len -
buf->cursor))
1480 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1481 errmsg(
"insufficient data left in message")));
1487 typioparam, typmod);
1498 buf->cursor += itemlen;
1502 typioparam, typmod);
1506 if (elem_buf.
cursor != itemlen)
1508 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1509 errmsg(
"improper binary format in array element %d",
1532 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1533 errmsg(
"array size exceeds the maximum allowed (%d)",
1537 *hasnulls = hasnull;
1570 if (my_extra == NULL)
1587 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
1588 errmsg(
"no binary output function available for type %s",
1594 typlen = my_extra->
typlen;
1609 for (
i = 0;
i < ndim;
i++)
1693 sprintf(p,
"[%d:%d]", lb[
i], dimv[
i] + lb[
i] - 1);
1718 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
1722 result = lb[reqdim - 1];
1746 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
1752 result = dimv[reqdim - 1] + lb[reqdim - 1] - 1;
1775 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
1780 result = dimv[reqdim - 1];
1838 bits8 *arraynullsptr;
1840 if (arraytyplen > 0)
1846 fixedDim[0] = arraytyplen / elmlen;
1851 arraynullsptr = NULL;
1880 if (ndim != nSubscripts || ndim <= 0 || ndim >
MAXDIM)
1885 for (
i = 0;
i < ndim;
i++)
1887 if (indx[
i] < lb[
i] || indx[
i] >= (dim[
i] + lb[
i]))
1912 retptr =
array_seek(arraydataptr, 0, arraynullsptr, offset,
1913 elmlen, elmbyval, elmalign);
1914 return ArrayCast(retptr, elmbyval, elmlen);
1922 int nSubscripts,
int *indx,
1924 int elmlen,
bool elmbyval,
char elmalign,
1940 Assert(arraytyplen == -1);
1952 if (ndim != nSubscripts || ndim <= 0 || ndim >
MAXDIM)
1957 for (
i = 0;
i < ndim;
i++)
1959 if (indx[
i] < lb[
i] || indx[
i] >= (dim[
i] + lb[
i]))
1983 if (dnulls && dnulls[offset])
1996 return dvalues[offset];
2034 bool *upperProvided,
2035 bool *lowerProvided,
2052 bits8 *arraynullsptr;
2057 if (arraytyplen > 0)
2066 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2067 errmsg(
"slices of fixed-length arrays not implemented")));
2075 fixedDim[0] = arraytyplen / elmlen;
2081 arraynullsptr = NULL;
2101 if (ndim < nSubscripts || ndim <= 0 || ndim >
MAXDIM)
2104 for (
i = 0;
i < nSubscripts;
i++)
2106 if (!lowerProvided[
i] || lowerIndx[
i] < lb[
i])
2107 lowerIndx[
i] = lb[
i];
2108 if (!upperProvided[
i] || upperIndx[
i] >= (dim[
i] + lb[
i]))
2109 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2110 if (lowerIndx[
i] > upperIndx[
i])
2114 for (;
i < ndim;
i++)
2116 lowerIndx[
i] = lb[
i];
2117 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2118 if (lowerIndx[
i] > upperIndx[
i])
2126 lowerIndx, upperIndx,
2127 elmlen, elmbyval, elmalign);
2136 bytes += dataoffset;
2146 newarray->
ndim = ndim;
2149 memcpy(
ARR_DIMS(newarray), span, ndim *
sizeof(
int));
2156 for (
i = 0;
i < ndim;
i++)
2161 arraydataptr, arraynullsptr,
2162 lowerIndx, upperIndx,
2163 elmlen, elmbyval, elmalign);
2220 bits8 *oldnullbitmap;
2234 if (arraytyplen > 0)
2242 if (nSubscripts != 1)
2244 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2245 errmsg(
"wrong number of array subscripts")));
2247 if (indx[0] < 0 || indx[0] >= arraytyplen / elmlen)
2249 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2250 errmsg(
"array subscript out of range")));
2254 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2255 errmsg(
"cannot assign null value to an element of a fixed-length array")));
2257 resultarray = (
char *)
palloc(arraytyplen);
2259 elt_ptr = (
char *) resultarray + indx[0] * elmlen;
2264 if (nSubscripts <= 0 || nSubscripts >
MAXDIM)
2266 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2267 errmsg(
"wrong number of array subscripts")));
2270 if (elmlen == -1 && !isNull)
2301 for (
i = 0;
i < nSubscripts;
i++)
2308 nSubscripts, dim, lb,
2310 elmlen, elmbyval, elmalign));
2313 if (ndim != nSubscripts)
2315 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2316 errmsg(
"wrong number of array subscripts")));
2319 memcpy(dim,
ARR_DIMS(array), ndim *
sizeof(
int));
2320 memcpy(lb,
ARR_LBOUND(array), ndim *
sizeof(
int));
2323 addedbefore = addedafter = 0;
2333 if (indx[0] < lb[0])
2340 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2341 errmsg(
"array size exceeds the maximum allowed (%d)",
2344 if (addedbefore > 1)
2347 if (indx[0] >= (dim[0] + lb[0]))
2355 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2356 errmsg(
"array size exceeds the maximum allowed (%d)",
2368 for (
i = 0;
i < ndim;
i++)
2370 if (indx[
i] < lb[
i] ||
2371 indx[
i] >= (dim[
i] + lb[
i]))
2373 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2374 errmsg(
"array subscript out of range")));
2392 olddatasize =
ARR_SIZE(array) - oldoverheadlen;
2398 lenafter = olddatasize;
2400 else if (addedafter)
2403 lenbefore = olddatasize;
2411 elmlen, elmbyval, elmalign);
2420 lenafter = (int) (olddatasize - lenbefore - olditemlen);
2431 newsize = overheadlen + lenbefore + newitemlen + lenafter;
2438 newarray->
ndim = ndim;
2439 newarray->
dataoffset = newhasnulls ? overheadlen : 0;
2441 memcpy(
ARR_DIMS(newarray), dim, ndim *
sizeof(
int));
2442 memcpy(
ARR_LBOUND(newarray), lb, ndim *
sizeof(
int));
2447 memcpy((
char *) newarray + overheadlen,
2448 (
char *) array + oldoverheadlen,
2452 (
char *) newarray + overheadlen + lenbefore);
2453 memcpy((
char *) newarray + overheadlen + lenbefore + newitemlen,
2454 (
char *) array + oldoverheadlen + lenbefore + olditemlen,
2483 if (addedafter == 0)
2485 oldnullbitmap, offset + 1,
2486 oldnitems - offset - 1);
2502 int nSubscripts,
int *indx,
2503 Datum dataValue,
bool isNull,
2505 int elmlen,
bool elmbyval,
char elmalign)
2525 Assert(arraytyplen == -1);
2537 memcpy(dim, eah->
dims, ndim *
sizeof(
int));
2538 memcpy(lb, eah->
lbound, ndim *
sizeof(
int));
2539 dimschanged =
false;
2554 nSubscripts *
sizeof(
int));
2556 nSubscripts *
sizeof(
int));
2560 for (
i = 0;
i < nSubscripts;
i++)
2567 else if (ndim != nSubscripts)
2569 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2570 errmsg(
"wrong number of array subscripts")));
2599 newhasnulls = ((dnulls != NULL) || isNull);
2600 addedbefore = addedafter = 0;
2610 if (indx[0] < lb[0])
2617 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2618 errmsg(
"array size exceeds the maximum allowed (%d)",
2622 if (addedbefore > 1)
2625 if (indx[0] >= (dim[0] + lb[0]))
2633 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2634 errmsg(
"array size exceeds the maximum allowed (%d)",
2647 for (
i = 0;
i < ndim;
i++)
2649 if (indx[
i] < lb[
i] ||
2650 indx[
i] >= (dim[
i] + lb[
i]))
2652 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2653 errmsg(
"array subscript out of range")));
2671 int newlen = dim[0] + dim[0] / 8;
2673 newlen =
Max(newlen, dim[0]);
2677 eah->
dnulls = dnulls = (
bool *)
2678 repalloc(dnulls, newlen *
sizeof(
bool));
2686 if (newhasnulls && dnulls == NULL)
2687 eah->
dnulls = dnulls = (
bool *)
2705 memcpy(eah->
dims, dim, ndim *
sizeof(
int));
2706 memcpy(eah->
lbound, lb, ndim *
sizeof(
int));
2710 if (addedbefore > 0)
2712 memmove(dvalues + addedbefore, dvalues, eah->
nelems *
sizeof(
Datum));
2713 for (
i = 0;
i < addedbefore;
i++)
2717 memmove(dnulls + addedbefore, dnulls, eah->
nelems *
sizeof(
bool));
2718 for (
i = 0;
i < addedbefore;
i++)
2721 eah->
nelems += addedbefore;
2727 for (
i = 0;
i < addedafter;
i++)
2731 for (
i = 0;
i < addedafter;
i++)
2732 dnulls[eah->
nelems +
i] =
true;
2734 eah->
nelems += addedafter;
2738 if (!eah->
typbyval && (dnulls == NULL || !dnulls[offset]))
2744 dvalues[offset] = dataValue;
2746 dnulls[offset] = isNull;
2756 if (oldValue < eah->fstartptr || oldValue >= eah->
fendptr)
2810 bool *upperProvided,
2811 bool *lowerProvided,
2812 Datum srcArrayDatum,
2848 if (arraytyplen > 0)
2854 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2855 errmsg(
"updates on slices of fixed-length arrays not implemented")));
2879 &dvalues, &dnulls, &nelems);
2881 for (
i = 0;
i < nSubscripts;
i++)
2883 if (!upperProvided[
i] || !lowerProvided[
i])
2885 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2886 errmsg(
"array slice subscript must provide both boundaries"),
2887 errdetail(
"When assigning to a slice of an empty array value,"
2888 " slice boundaries must be fully specified.")));
2894 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2895 errmsg(
"array size exceeds the maximum allowed (%d)",
2898 lb[
i] = lowerIndx[
i];
2904 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2905 errmsg(
"source array too small")));
2909 elmlen, elmbyval, elmalign));
2912 if (ndim < nSubscripts || ndim <= 0 || ndim >
MAXDIM)
2914 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2915 errmsg(
"wrong number of array subscripts")));
2918 memcpy(dim,
ARR_DIMS(array), ndim *
sizeof(
int));
2919 memcpy(lb,
ARR_LBOUND(array), ndim *
sizeof(
int));
2922 addedbefore = addedafter = 0;
2932 Assert(nSubscripts == 1);
2933 if (!lowerProvided[0])
2934 lowerIndx[0] = lb[0];
2935 if (!upperProvided[0])
2936 upperIndx[0] = dim[0] + lb[0] - 1;
2937 if (lowerIndx[0] > upperIndx[0])
2939 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2940 errmsg(
"upper bound cannot be less than lower bound")));
2941 if (lowerIndx[0] < lb[0])
2948 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2949 errmsg(
"array size exceeds the maximum allowed (%d)",
2951 lb[0] = lowerIndx[0];
2952 if (addedbefore > 1)
2955 if (upperIndx[0] >= (dim[0] + lb[0]))
2963 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2964 errmsg(
"array size exceeds the maximum allowed (%d)",
2976 for (
i = 0;
i < nSubscripts;
i++)
2978 if (!lowerProvided[
i])
2979 lowerIndx[
i] = lb[
i];
2980 if (!upperProvided[
i])
2981 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2982 if (lowerIndx[
i] > upperIndx[
i])
2984 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2985 errmsg(
"upper bound cannot be less than lower bound")));
2986 if (lowerIndx[
i] < lb[
i] ||
2987 upperIndx[
i] >= (dim[
i] + lb[
i]))
2989 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2990 errmsg(
"array subscript out of range")));
2993 for (;
i < ndim;
i++)
2995 lowerIndx[
i] = lb[
i];
2996 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2997 if (lowerIndx[
i] > upperIndx[
i])
2999 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3000 errmsg(
"upper bound cannot be less than lower bound")));
3016 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3017 errmsg(
"source array too small")));
3029 elmlen, elmbyval, elmalign);
3031 olddatasize =
ARR_SIZE(array) - oldoverheadlen;
3041 lowerIndx, upperIndx,
3042 elmlen, elmbyval, elmalign);
3043 lenbefore = lenafter = 0;
3044 itemsbefore = itemsafter = nolditems = 0;
3053 int oldub = oldlb +
ARR_DIMS(array)[0] - 1;
3054 int slicelb =
Max(oldlb, lowerIndx[0]);
3055 int sliceub =
Min(oldub, upperIndx[0]);
3060 itemsbefore =
Min(slicelb, oldub + 1) - oldlb;
3063 elmlen, elmbyval, elmalign);
3065 if (slicelb > sliceub)
3072 nolditems = sliceub - slicelb + 1;
3074 itemsbefore, oldarraybitmap,
3076 elmlen, elmbyval, elmalign);
3079 itemsafter = oldub + 1 -
Max(sliceub + 1, oldlb);
3080 lenafter = olddatasize - lenbefore - olditemsize;
3083 newsize = overheadlen + olddatasize - olditemsize + newitemsize;
3087 newarray->
ndim = ndim;
3088 newarray->
dataoffset = newhasnulls ? overheadlen : 0;
3090 memcpy(
ARR_DIMS(newarray), dim, ndim *
sizeof(
int));
3091 memcpy(
ARR_LBOUND(newarray), lb, ndim *
sizeof(
int));
3101 lowerIndx, upperIndx,
3102 elmlen, elmbyval, elmalign);
3107 memcpy((
char *) newarray + overheadlen,
3108 (
char *) array + oldoverheadlen,
3110 memcpy((
char *) newarray + overheadlen + lenbefore,
3113 memcpy((
char *) newarray + overheadlen + lenbefore + newitemsize,
3114 (
char *) array + oldoverheadlen + lenbefore + olditemsize,
3130 oldnullbitmap, itemsbefore + nolditems,
3147 int arraytyplen,
int elmlen,
bool elmbyval,
char elmalign,
3151 arraytyplen, elmlen, elmbyval, elmalign,
3164 Datum dataValue,
bool isNull,
3165 int arraytyplen,
int elmlen,
bool elmbyval,
char elmalign)
3171 elmlen, elmbyval, elmalign));
3257 inp_typlen = inp_extra->
typlen;
3258 inp_typbyval = inp_extra->
typbyval;
3259 inp_typalign = inp_extra->
typalign;
3269 typlen = ret_extra->
typlen;
3286 inp_typlen, inp_typbyval, inp_typalign);
3304 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3305 errmsg(
"array size exceeds the maximum allowed (%d)",
3314 nbytes += dataoffset;
3323 result->
ndim = ndim;
3363 int elmlen,
bool elmbyval,
char elmalign)
3372 elmtype, elmlen, elmbyval, elmalign);
3392 elmalign = TYPALIGN_CHAR;
3398 elmalign = TYPALIGN_CHAR;
3404 elmalign = TYPALIGN_INT;
3410 elmalign = TYPALIGN_DOUBLE;
3414 elmlen =
sizeof(
int16);
3416 elmalign = TYPALIGN_SHORT;
3420 elmlen =
sizeof(
int32);
3422 elmalign = TYPALIGN_INT;
3426 elmlen =
sizeof(
int64);
3428 elmalign = TYPALIGN_DOUBLE;
3434 elmalign = TYPALIGN_CHAR;
3439 elmlen =
sizeof(
Oid);
3441 elmalign = TYPALIGN_INT;
3447 elmalign = TYPALIGN_INT;
3453 elmalign = TYPALIGN_SHORT;
3459 elmalign = TYPALIGN_INT;
3463 elog(
ERROR,
"type %u not supported by construct_array_builtin()", elmtype);
3470 return construct_array(elems, nelems, elmtype, elmlen, elmbyval, elmalign);
3499 Oid elmtype,
int elmlen,
bool elmbyval,
char elmalign)
3510 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3511 errmsg(
"invalid number of dimensions: %d", ndims)));
3514 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3515 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
3529 for (
i = 0;
i < nelems;
i++)
3531 if (nulls && nulls[
i])
3544 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3545 errmsg(
"array size exceeds the maximum allowed (%d)",
3553 nbytes += dataoffset;
3562 result->
ndim = ndims;
3565 memcpy(
ARR_DIMS(result), dims, ndims *
sizeof(
int));
3566 memcpy(
ARR_LBOUND(result), lbs, ndims *
sizeof(
int));
3569 elems, nulls, nelems,
3570 elmlen, elmbyval, elmalign,
3633 int elmlen,
bool elmbyval,
char elmalign,
3634 Datum **elemsp,
bool **nullsp,
int *nelemsp)
3649 *nullsp = nulls = (
bool *)
palloc0(nelems *
sizeof(
bool));
3658 for (
i = 0;
i < nelems;
i++)
3661 if (bitmap && (*bitmap & bitmask) == 0)
3668 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3669 errmsg(
"null array element not allowed in this context")));
3682 if (bitmask == 0x100)
3699 Datum **elemsp,
bool **nullsp,
int *nelemsp)
3710 elmalign = TYPALIGN_CHAR;
3716 elmalign = TYPALIGN_CHAR;
3722 elmalign = TYPALIGN_DOUBLE;
3726 elmlen =
sizeof(
int16);
3728 elmalign = TYPALIGN_SHORT;
3732 elmlen =
sizeof(
Oid);
3734 elmalign = TYPALIGN_INT;
3740 elmalign = TYPALIGN_INT;
3746 elmalign = TYPALIGN_SHORT;
3750 elog(
ERROR,
"type %u not supported by deconstruct_array_builtin()", elmtype);
3757 deconstruct_array(array, elmtype, elmlen, elmbyval, elmalign, elemsp, nullsp, nelemsp);
3784 if (*bitmap != 0xFF)
3794 if ((*bitmap & bitmask) == 0)
3839 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3840 errmsg(
"cannot compare arrays of different element types")));
3843 if (ndims1 != ndims2 ||
3844 memcmp(dims1, dims2, ndims1 *
sizeof(
int)) != 0 ||
3845 memcmp(lbs1, lbs2, ndims1 *
sizeof(
int)) != 0)
3856 if (typentry == NULL ||
3857 typentry->
type_id != element_type)
3863 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
3864 errmsg(
"could not identify an equality operator for type %s",
3866 fcinfo->flinfo->fn_extra = typentry;
3868 typlen = typentry->
typlen;
3876 collation, NULL, NULL);
3900 if (isnull1 && isnull2)
3902 if (isnull1 || isnull2)
3911 locfcinfo->args[0].value = elt1;
3912 locfcinfo->args[0].isnull =
false;
3913 locfcinfo->args[1].value = elt2;
3914 locfcinfo->args[1].isnull =
false;
3915 locfcinfo->isnull =
false;
3917 if (locfcinfo->isnull || !oprresult)
4010 (
errcode(ERRCODE_DATATYPE_MISMATCH),
4011 errmsg(
"cannot compare arrays of different element types")));
4020 if (typentry == NULL ||
4021 typentry->
type_id != element_type)
4027 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4028 errmsg(
"could not identify a comparison function for type %s",
4032 typlen = typentry->
typlen;
4040 collation, NULL, NULL);
4043 min_nitems =
Min(nitems1, nitems2);
4047 for (
i = 0;
i < min_nitems;
i++)
4062 if (isnull1 && isnull2)
4078 locfcinfo->args[0].value = elt1;
4079 locfcinfo->args[0].isnull =
false;
4080 locfcinfo->args[1].value = elt2;
4081 locfcinfo->args[1].isnull =
false;
4085 Assert(!locfcinfo->isnull);
4112 if (nitems1 != nitems2)
4113 result = (nitems1 < nitems2) ? -1 : 1;
4114 else if (ndims1 != ndims2)
4115 result = (ndims1 < ndims2) ? -1 : 1;
4118 for (
i = 0;
i < ndims1;
i++)
4120 if (dims1[
i] != dims2[
i])
4122 result = (dims1[
i] < dims2[
i]) ? -1 : 1;
4131 for (
i = 0;
i < ndims1;
i++)
4133 if (lbound1[
i] != lbound2[
i])
4135 result = (lbound1[
i] < lbound2[
i]) ? -1 : 1;
4181 if (typentry == NULL ||
4182 typentry->
type_id != element_type)
4188 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4189 errmsg(
"could not identify a hash function for type %s",
4199 if (element_type == RECORDOID)
4211 record_typentry =
palloc0(
sizeof(*record_typentry));
4212 record_typentry->
type_id = element_type;
4222 typentry = record_typentry;
4225 fcinfo->flinfo->fn_extra = typentry;
4228 typlen = typentry->
typlen;
4259 locfcinfo->args[0].value = elt;
4260 locfcinfo->args[0].isnull =
false;
4263 Assert(!locfcinfo->isnull);
4277 result = (result << 5) - result + elthash;
4309 if (typentry == NULL ||
4310 typentry->
type_id != element_type)
4316 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4317 errmsg(
"could not identify an extended hash function for type %s",
4319 fcinfo->flinfo->fn_extra = typentry;
4321 typlen = typentry->
typlen;
4348 locfcinfo->args[0].value = elt;
4349 locfcinfo->args[0].isnull =
false;
4351 locfcinfo->args[1].isnull =
false;
4354 Assert(!locfcinfo->isnull);
4357 result = (result << 5) - result + elthash;
4382 bool matchall,
void **fn_extra)
4385 bool result = matchall;
4401 (
errcode(ERRCODE_DATATYPE_MISMATCH),
4402 errmsg(
"cannot compare arrays of different element types")));
4411 if (typentry == NULL ||
4412 typentry->
type_id != element_type)
4418 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4419 errmsg(
"could not identify an equality operator for type %s",
4421 *fn_extra = typentry;
4423 typlen = typentry->
typlen;
4442 element_type, typlen, typbyval,
typalign,
4443 &values2, &nulls2, &nelems2);
4449 collation, NULL, NULL);
4455 for (
i = 0;
i < nelems1;
i++)
4478 for (
j = 0;
j < nelems2;
j++)
4481 bool isnull2 = nulls2 ? nulls2[
j] :
false;
4490 locfcinfo->args[0].value = elt1;
4491 locfcinfo->args[0].isnull =
false;
4492 locfcinfo->args[1].value = elt2;
4493 locfcinfo->args[1].isnull =
false;
4494 locfcinfo->isnull =
false;
4496 if (!locfcinfo->isnull && oprresult)
4532 &fcinfo->flinfo->fn_extra);
4550 &fcinfo->flinfo->fn_extra);
4568 &fcinfo->flinfo->fn_extra);
4605 if (slice_ndim < 0 || slice_ndim >
ARR_NDIM(arr))
4606 elog(
ERROR,
"invalid arguments to array_create_iterator");
4611 iterator->
arr = arr;
4783 if (nullbitmap == NULL)
4785 if (nullbitmap[offset / 8] & (1 << (offset % 8)))
4802 nullbitmap += offset / 8;
4803 bitmask = 1 << (offset % 8);
4805 *nullbitmap &= ~bitmask;
4807 *nullbitmap |= bitmask;
4867 int typlen,
bool typbyval,
char typalign)
4873 if (typlen > 0 && !nullbitmap)
4879 nullbitmap += offset / 8;
4880 bitmask = 1 << (offset % 8);
4884 if (*nullbitmap & bitmask)
4890 if (bitmask == 0x100)
4915 int typlen,
bool typbyval,
char typalign)
4937 char *srcptr,
int offset,
bits8 *nullbitmap,
4938 int typlen,
bool typbyval,
char typalign)
4944 memcpy(destptr, srcptr, numbytes);
4967 const bits8 *srcbitmap,
int srcoffset,
4978 destbitmap += destoffset / 8;
4979 destbitmask = 1 << (destoffset % 8);
4980 destbitval = *destbitmap;
4983 srcbitmap += srcoffset / 8;
4984 srcbitmask = 1 << (srcoffset % 8);
4985 srcbitval = *srcbitmap;
4988 if (srcbitval & srcbitmask)
4989 destbitval |= destbitmask;
4991 destbitval &= ~destbitmask;
4993 if (destbitmask == 0x100)
4995 *destbitmap++ = destbitval;
4998 destbitval = *destbitmap;
5001 if (srcbitmask == 0x100)
5006 srcbitval = *srcbitmap;
5009 if (destbitmask != 1)
5010 *destbitmap = destbitval;
5016 destbitval |= destbitmask;
5018 if (destbitmask == 0x100)
5020 *destbitmap++ = destbitval;
5023 destbitval = *destbitmap;
5026 if (destbitmask != 1)
5027 *destbitmap = destbitval;
5038 int ndim,
int *dim,
int *lb,
5040 int typlen,
bool typbyval,
char typalign)
5056 if (typlen > 0 && !arraynullsptr)
5061 ptr =
array_seek(arraydataptr, 0, arraynullsptr, src_offset,
5065 for (
i = 0;
i < ndim;
i++)
5072 ptr =
array_seek(ptr, src_offset, arraynullsptr, dist[
j],
5074 src_offset += dist[
j];
5102 bits8 *arraynullsptr,
5123 srcdataptr =
array_seek(arraydataptr, 0, arraynullsptr, src_offset,
5128 for (
i = 0;
i < ndim;
i++)
5137 srcdataptr =
array_seek(srcdataptr, src_offset, arraynullsptr,
5140 src_offset += dist[
j];
5143 srcdataptr, src_offset, arraynullsptr,
5147 arraynullsptr, src_offset,
5204 origPtr, 0, origBitmap,
5210 orig_offset = dest_offset;
5214 for (
i = 0;
i < ndim;
i++)
5224 origPtr, orig_offset, origBitmap,
5230 origBitmap, orig_offset,
5232 dest_offset += dist[
j];
5233 orig_offset += dist[
j];
5237 srcPtr, src_offset, srcBitmap,
5241 srcBitmap, src_offset,
5248 origPtr =
array_seek(origPtr, orig_offset, origBitmap, 1,
5254 array_copy(destPtr, orignitems - orig_offset,
5255 origPtr, orig_offset, origBitmap,
5259 origBitmap, orig_offset,
5260 orignitems - orig_offset);
5301 subcontext ? 64 : 8);
5311 bool subcontext,
int initsize)
5326 astate->
alen = initsize;
5329 astate->
dnulls = (
bool *)
5351 Datum dvalue,
bool disnull,
5376 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5377 errmsg(
"array size exceeds the maximum allowed (%d)",
5381 astate->
dnulls = (
bool *)
5395 if (astate->
typlen == -1)
5428 ndims = (astate->
nelems > 0) ? 1 : 0;
5429 dims[0] = astate->
nelems;
5517 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5518 errmsg(
"data type %s is not an array type",
5525 "accumArrayResultArr",
5551 Datum dvalue,
bool disnull,
5572 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5573 errmsg(
"cannot accumulate null arrays")));
5593 if (astate->
ndims == 0)
5600 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
5601 errmsg(
"cannot accumulate empty arrays")));
5604 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5605 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
5612 astate->
ndims = ndims + 1;
5613 astate->
dims[0] = 0;
5614 memcpy(&astate->
dims[1], dims, ndims *
sizeof(
int));
5616 memcpy(&astate->
lbs[1], lbs, ndims *
sizeof(
int));
5625 if (astate->
ndims != ndims + 1)
5627 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
5628 errmsg(
"cannot accumulate arrays of different dimensionality")));
5629 for (
i = 0;
i < ndims;
i++)
5631 if (astate->
dims[
i + 1] != dims[
i] || astate->
lbs[
i + 1] != lbs[
i])
5633 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
5634 errmsg(
"cannot accumulate arrays of different dimensionality")));
5641 astate->
nbytes + ndatabytes);
5653 astate->
nbytes += ndatabytes;
5672 else if (newnitems > astate->
aitems)
5684 astate->
dims[0] += 1;
5713 if (astate->
ndims == 0)
5732 nbytes += dataoffset;
5828 Datum dvalue,
bool disnull,
5838 input_type, rcontext);
5842 input_type, rcontext);
5937 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
5949 fctx->
lower = lb[reqdim - 1];
5950 fctx->
upper = dimv[reqdim - 1] + lb[reqdim - 1] - 1;