34#include "utils/fmgroids.h"
51#define AARR_FREE_IF_COPY(array,n) \
53 if (!VARATT_IS_EXPANDED_HEADER(array)) \
54 PG_FREE_IF_COPY(array, n); \
93 int *dim,
int *lBound,
94 const char *origStr,
Node *escontext);
96 const char *origStr,
Node *escontext);
100 int typlen,
bool typbyval,
char typalign,
101 int *ndim_p,
int *dim,
103 Datum **values_p,
bool **nulls_p,
104 const char *origStr,
Node *escontext);
106 const char *origStr,
Node *escontext);
109 int typlen,
bool typbyval,
char typalign,
111 bool *hasnulls,
int32 *nbytes);
113 int nSubscripts,
int *indx,
115 int elmlen,
bool elmbyval,
char elmalign,
118 int nSubscripts,
int *indx,
119 Datum dataValue,
bool isNull,
121 int elmlen,
bool elmbyval,
char elmalign);
126 int typlen,
bool typbyval,
char typalign,
129 int typlen,
bool typbyval,
char typalign);
133 char *srcptr,
int offset,
bits8 *nullbitmap,
134 int typlen,
bool typbyval,
char typalign);
136 int ndim,
int *dim,
int *lb,
138 int typlen,
bool typbyval,
char typalign);
140 int ndim,
int *dim,
int *lb,
141 char *arraydataptr,
bits8 *arraynullsptr,
143 int typlen,
bool typbyval,
char typalign);
146 int ndim,
int *dim,
int *lb,
148 int typlen,
bool typbyval,
char typalign);
151 Oid elmtype,
int dataoffset);
156 Datum search,
bool search_isnull,
157 Datum replace,
bool replace_isnull,
158 bool remove,
Oid collation,
186 Node *escontext = fcinfo->context;
211 if (my_extra == NULL)
232 typlen = my_extra->
typlen;
264 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
265 errmsg(
"malformed array literal: \"%s\"",
string),
266 errdetail(
"Array value must start with \"{\" or dimension information.")));
273 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
274 errmsg(
"malformed array literal: \"%s\"",
string),
275 errdetail(
"Missing \"%s\" after array dimensions.",
284 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
285 errmsg(
"malformed array literal: \"%s\"",
string),
286 errdetail(
"Array contents must start with \"{\".")));
291 &my_extra->
proc, typioparam, typmod,
307 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
308 errmsg(
"malformed array literal: \"%s\"",
string),
309 errdetail(
"Junk after closing right brace.")));
335 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
336 errmsg(
"array size exceeds the maximum allowed (%d)",
343 nbytes += dataoffset;
365 memcpy(
ARR_DIMS(retval), dim, ndim *
sizeof(
int));
366 memcpy(
ARR_LBOUND(retval), lBound, ndim *
sizeof(
int));
404 const char *origStr,
Node *escontext)
431 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
432 errmsg(
"number of array dimensions exceeds the maximum allowed (%d)",
440 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
441 errmsg(
"malformed array literal: \"%s\"", origStr),
442 errdetail(
"\"[\" must introduce explicitly-specified array dimensions.")));
454 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
455 errmsg(
"malformed array literal: \"%s\"", origStr),
456 errdetail(
"Missing array dimension value.")));
466 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
467 errmsg(
"malformed array literal: \"%s\"", origStr),
468 errdetail(
"Missing \"%s\" after array dimensions.",
479 if (ub < lBound[ndim])
481 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
482 errmsg(
"upper bound cannot be less than lower bound")));
487 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
488 errmsg(
"array upper bound is too large: %d", ub)));
494 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
495 errmsg(
"array size exceeds the maximum allowed (%d)",
521 const char *origStr,
Node *escontext)
527 if (!isdigit((
unsigned char) *p) && *p !=
'-' && *p !=
'+')
534 l = strtol(p, srcptr, 10);
538 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
539 errmsg(
"array bound is out of integer range")));
597 bool dimensions_specified = (ndim != 0);
622 ndim_frozen = dimensions_specified;
623 expect_delim =
false;
628 tok =
ReadArrayToken(srcptr, &elembuf, typdelim, origStr, escontext);
636 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
637 errmsg(
"malformed array literal: \"%s\"", origStr),
638 errdetail(
"Unexpected \"%c\" character.",
'{')));
643 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
644 errmsg(
"number of array dimensions exceeds the maximum allowed (%d)",
647 nelems[nest_level] = 0;
649 if (nest_level > ndim)
653 goto dimension_error;
666 if (nelems[nest_level - 1] > 0 && !expect_delim)
668 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
669 errmsg(
"malformed array literal: \"%s\"", origStr),
670 errdetail(
"Unexpected \"%c\" character.",
675 nelems[nest_level - 1]++;
682 if (dim[nest_level] < 0)
685 dim[nest_level] = nelems[nest_level];
687 else if (nelems[nest_level] != dim[nest_level])
690 goto dimension_error;
703 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
704 errmsg(
"malformed array literal: \"%s\"", origStr),
705 errdetail(
"Unexpected \"%c\" character.",
707 expect_delim =
false;
718 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
719 errmsg(
"malformed array literal: \"%s\"", origStr),
720 errdetail(
"Unexpected array element.")));
727 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
728 errmsg(
"array size exceeds the maximum allowed (%d)",
751 if (nest_level != ndim)
752 goto dimension_error;
754 nelems[nest_level - 1]++;
763 }
while (nest_level > 0);
775 if (dimensions_specified)
777 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
778 errmsg(
"malformed array literal: \"%s\"", origStr),
779 errdetail(
"Specified array dimensions do not match array contents.")));
782 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
783 errmsg(
"malformed array literal: \"%s\"", origStr),
784 errdetail(
"Multidimensional arrays must have sub-arrays with matching dimensions.")));
798 const char *origStr,
Node *escontext)
833 goto unquoted_element;
861 while (*(++p) !=
'\0')
863 if (*p == typdelim || *p ==
'}' || *p ==
'{')
870 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
871 errmsg(
"malformed array literal: \"%s\"", origStr),
872 errdetail(
"Incorrectly quoted array element.")));
897 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
898 errmsg(
"malformed array literal: \"%s\"", origStr),
899 errdetail(
"Unexpected \"%c\" character.",
904 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
905 errmsg(
"malformed array literal: \"%s\"", origStr),
906 errdetail(
"Incorrectly quoted array element.")));
913 dstlen = elembuf->
len;
918 if (*p == typdelim || *p ==
'}')
921 elembuf->
data[dstlen] =
'\0';
922 elembuf->
len = dstlen;
933 dstlen = elembuf->
len;
941 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
942 errmsg(
"malformed array literal: \"%s\"", origStr),
982 if (nulls && nulls[
i])
985 elog(
ERROR,
"null array element where not supported");
998 if (bitmask == 0x100)
1007 if (bitmap && bitmask != 1)
1029 dims_str[(
MAXDIM * 33) + 2];
1038 size_t overall_length;
1056 if (my_extra == NULL)
1077 typlen = my_extra->
typlen;
1097 for (
i = 0;
i < ndim;
i++)
1130 overall_length += 4;
1145 for (tmp =
values[
i]; *tmp !=
'\0'; tmp++)
1149 overall_length += 1;
1150 if (ch ==
'"' || ch ==
'\\')
1153 overall_length += 1;
1155 else if (ch ==
'{' || ch ==
'}' || ch == typdelim ||
1161 needquotes[
i] = needquote;
1165 overall_length += 2;
1167 overall_length += 1;
1176 for (
i =
j = 0, k = 1;
i < ndim;
i++)
1178 j += k, k *= dims[
i];
1180 overall_length += 2 *
j;
1186 char *ptr = dims_str;
1188 for (
i = 0;
i < ndim;
i++)
1190 sprintf(ptr,
"[%d:%d]", lb[
i], lb[
i] + dims[
i] - 1);
1195 overall_length += ptr - dims_str;
1199 retval = (
char *)
palloc(overall_length);
1202#define APPENDSTR(str) (strcpy(p, (str)), p += strlen(p))
1203#define APPENDCHAR(ch) (*p++ = (ch), *p = '\0')
1208 for (
i = 0;
i < ndim;
i++)
1214 for (
i =
j;
i < ndim - 1;
i++)
1220 for (tmp =
values[k]; *tmp; tmp++)
1224 if (ch ==
'"' || ch ==
'\\')
1235 for (
i = ndim - 1;
i >= 0;
i--)
1237 if (++(indx[
i]) < dims[
i])
1255 Assert(overall_length == (p - retval + 1));
1301 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1302 errmsg(
"invalid number of dimensions: %d", ndim)));
1305 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1306 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
1310 if (flags != 0 && flags != 1)
1312 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1313 errmsg(
"invalid array flags")));
1329 if (element_type != spec_element_type)
1334 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1335 errmsg(
"binary data has array element type %u (%s) instead of expected %u (%s)",
1342 element_type = spec_element_type;
1345 for (
i = 0;
i < ndim;
i++)
1361 if (my_extra == NULL)
1378 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
1379 errmsg(
"no binary input function available for type %s",
1392 typlen = my_extra->
typlen;
1400 &my_extra->
proc, typioparam, typmod,
1403 &hasnulls, &nbytes);
1407 nbytes += dataoffset;
1416 retval->
ndim = ndim;
1419 memcpy(
ARR_DIMS(retval), dim, ndim *
sizeof(
int));
1420 memcpy(
ARR_LBOUND(retval), lBound, ndim *
sizeof(
int));
1423 dataPtr, nullsPtr,
nitems,
1479 if (itemlen < -1 || itemlen > (
buf->len -
buf->cursor))
1481 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1482 errmsg(
"insufficient data left in message")));
1488 typioparam, typmod);
1499 buf->cursor += itemlen;
1503 typioparam, typmod);
1507 if (elem_buf.
cursor != itemlen)
1509 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1510 errmsg(
"improper binary format in array element %d",
1533 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1534 errmsg(
"array size exceeds the maximum allowed (%d)",
1538 *hasnulls = hasnull;
1571 if (my_extra == NULL)
1588 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
1589 errmsg(
"no binary output function available for type %s",
1595 typlen = my_extra->
typlen;
1610 for (
i = 0;
i < ndim;
i++)
1694 sprintf(p,
"[%d:%d]", lb[
i], dimv[
i] + lb[
i] - 1);
1719 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
1723 result = lb[reqdim - 1];
1747 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
1753 result = dimv[reqdim - 1] + lb[reqdim - 1] - 1;
1776 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
1781 result = dimv[reqdim - 1];
1839 bits8 *arraynullsptr;
1841 if (arraytyplen > 0)
1847 fixedDim[0] = arraytyplen / elmlen;
1852 arraynullsptr = NULL;
1881 if (ndim != nSubscripts || ndim <= 0 || ndim >
MAXDIM)
1886 for (
i = 0;
i < ndim;
i++)
1888 if (indx[
i] < lb[
i] || indx[
i] >= (dim[
i] + lb[
i]))
1913 retptr =
array_seek(arraydataptr, 0, arraynullsptr, offset,
1914 elmlen, elmbyval, elmalign);
1915 return ArrayCast(retptr, elmbyval, elmlen);
1923 int nSubscripts,
int *indx,
1925 int elmlen,
bool elmbyval,
char elmalign,
1941 Assert(arraytyplen == -1);
1953 if (ndim != nSubscripts || ndim <= 0 || ndim >
MAXDIM)
1958 for (
i = 0;
i < ndim;
i++)
1960 if (indx[
i] < lb[
i] || indx[
i] >= (dim[
i] + lb[
i]))
1984 if (dnulls && dnulls[offset])
1997 return dvalues[offset];
2035 bool *upperProvided,
2036 bool *lowerProvided,
2053 bits8 *arraynullsptr;
2058 if (arraytyplen > 0)
2067 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2068 errmsg(
"slices of fixed-length arrays not implemented")));
2076 fixedDim[0] = arraytyplen / elmlen;
2082 arraynullsptr = NULL;
2102 if (ndim < nSubscripts || ndim <= 0 || ndim >
MAXDIM)
2105 for (
i = 0;
i < nSubscripts;
i++)
2107 if (!lowerProvided[
i] || lowerIndx[
i] < lb[
i])
2108 lowerIndx[
i] = lb[
i];
2109 if (!upperProvided[
i] || upperIndx[
i] >= (dim[
i] + lb[
i]))
2110 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2111 if (lowerIndx[
i] > upperIndx[
i])
2115 for (;
i < ndim;
i++)
2117 lowerIndx[
i] = lb[
i];
2118 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2119 if (lowerIndx[
i] > upperIndx[
i])
2127 lowerIndx, upperIndx,
2128 elmlen, elmbyval, elmalign);
2137 bytes += dataoffset;
2147 newarray->
ndim = ndim;
2150 memcpy(
ARR_DIMS(newarray), span, ndim *
sizeof(
int));
2157 for (
i = 0;
i < ndim;
i++)
2162 arraydataptr, arraynullsptr,
2163 lowerIndx, upperIndx,
2164 elmlen, elmbyval, elmalign);
2221 bits8 *oldnullbitmap;
2235 if (arraytyplen > 0)
2243 if (nSubscripts != 1)
2245 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2246 errmsg(
"wrong number of array subscripts")));
2248 if (indx[0] < 0 || indx[0] >= arraytyplen / elmlen)
2250 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2251 errmsg(
"array subscript out of range")));
2255 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2256 errmsg(
"cannot assign null value to an element of a fixed-length array")));
2258 resultarray = (
char *)
palloc(arraytyplen);
2260 elt_ptr = (
char *) resultarray + indx[0] * elmlen;
2265 if (nSubscripts <= 0 || nSubscripts >
MAXDIM)
2267 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2268 errmsg(
"wrong number of array subscripts")));
2271 if (elmlen == -1 && !isNull)
2302 for (
i = 0;
i < nSubscripts;
i++)
2309 nSubscripts, dim, lb,
2311 elmlen, elmbyval, elmalign));
2314 if (ndim != nSubscripts)
2316 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2317 errmsg(
"wrong number of array subscripts")));
2320 memcpy(dim,
ARR_DIMS(array), ndim *
sizeof(
int));
2321 memcpy(lb,
ARR_LBOUND(array), ndim *
sizeof(
int));
2324 addedbefore = addedafter = 0;
2334 if (indx[0] < lb[0])
2341 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2342 errmsg(
"array size exceeds the maximum allowed (%d)",
2345 if (addedbefore > 1)
2348 if (indx[0] >= (dim[0] + lb[0]))
2356 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2357 errmsg(
"array size exceeds the maximum allowed (%d)",
2369 for (
i = 0;
i < ndim;
i++)
2371 if (indx[
i] < lb[
i] ||
2372 indx[
i] >= (dim[
i] + lb[
i]))
2374 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2375 errmsg(
"array subscript out of range")));
2393 olddatasize =
ARR_SIZE(array) - oldoverheadlen;
2399 lenafter = olddatasize;
2401 else if (addedafter)
2404 lenbefore = olddatasize;
2412 elmlen, elmbyval, elmalign);
2421 lenafter = (int) (olddatasize - lenbefore - olditemlen);
2432 newsize = overheadlen + lenbefore + newitemlen + lenafter;
2439 newarray->
ndim = ndim;
2440 newarray->
dataoffset = newhasnulls ? overheadlen : 0;
2442 memcpy(
ARR_DIMS(newarray), dim, ndim *
sizeof(
int));
2443 memcpy(
ARR_LBOUND(newarray), lb, ndim *
sizeof(
int));
2448 memcpy((
char *) newarray + overheadlen,
2449 (
char *) array + oldoverheadlen,
2453 (
char *) newarray + overheadlen + lenbefore);
2454 memcpy((
char *) newarray + overheadlen + lenbefore + newitemlen,
2455 (
char *) array + oldoverheadlen + lenbefore + olditemlen,
2484 if (addedafter == 0)
2486 oldnullbitmap, offset + 1,
2487 oldnitems - offset - 1);
2503 int nSubscripts,
int *indx,
2504 Datum dataValue,
bool isNull,
2506 int elmlen,
bool elmbyval,
char elmalign)
2526 Assert(arraytyplen == -1);
2538 memcpy(dim, eah->
dims, ndim *
sizeof(
int));
2539 memcpy(lb, eah->
lbound, ndim *
sizeof(
int));
2540 dimschanged =
false;
2555 nSubscripts *
sizeof(
int));
2557 nSubscripts *
sizeof(
int));
2561 for (
i = 0;
i < nSubscripts;
i++)
2568 else if (ndim != nSubscripts)
2570 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2571 errmsg(
"wrong number of array subscripts")));
2600 newhasnulls = ((dnulls != NULL) || isNull);
2601 addedbefore = addedafter = 0;
2611 if (indx[0] < lb[0])
2618 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2619 errmsg(
"array size exceeds the maximum allowed (%d)",
2623 if (addedbefore > 1)
2626 if (indx[0] >= (dim[0] + lb[0]))
2634 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2635 errmsg(
"array size exceeds the maximum allowed (%d)",
2648 for (
i = 0;
i < ndim;
i++)
2650 if (indx[
i] < lb[
i] ||
2651 indx[
i] >= (dim[
i] + lb[
i]))
2653 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2654 errmsg(
"array subscript out of range")));
2672 int newlen = dim[0] + dim[0] / 8;
2674 newlen =
Max(newlen, dim[0]);
2678 eah->
dnulls = dnulls = (
bool *)
2679 repalloc(dnulls, newlen *
sizeof(
bool));
2687 if (newhasnulls && dnulls == NULL)
2688 eah->
dnulls = dnulls = (
bool *)
2706 memcpy(eah->
dims, dim, ndim *
sizeof(
int));
2707 memcpy(eah->
lbound, lb, ndim *
sizeof(
int));
2711 if (addedbefore > 0)
2713 memmove(dvalues + addedbefore, dvalues, eah->
nelems *
sizeof(
Datum));
2714 for (
i = 0;
i < addedbefore;
i++)
2718 memmove(dnulls + addedbefore, dnulls, eah->
nelems *
sizeof(
bool));
2719 for (
i = 0;
i < addedbefore;
i++)
2722 eah->
nelems += addedbefore;
2728 for (
i = 0;
i < addedafter;
i++)
2732 for (
i = 0;
i < addedafter;
i++)
2733 dnulls[eah->
nelems +
i] =
true;
2735 eah->
nelems += addedafter;
2739 if (!eah->
typbyval && (dnulls == NULL || !dnulls[offset]))
2745 dvalues[offset] = dataValue;
2747 dnulls[offset] = isNull;
2757 if (oldValue < eah->fstartptr || oldValue >= eah->
fendptr)
2811 bool *upperProvided,
2812 bool *lowerProvided,
2813 Datum srcArrayDatum,
2849 if (arraytyplen > 0)
2855 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2856 errmsg(
"updates on slices of fixed-length arrays not implemented")));
2880 &dvalues, &dnulls, &nelems);
2882 for (
i = 0;
i < nSubscripts;
i++)
2884 if (!upperProvided[
i] || !lowerProvided[
i])
2886 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2887 errmsg(
"array slice subscript must provide both boundaries"),
2888 errdetail(
"When assigning to a slice of an empty array value,"
2889 " slice boundaries must be fully specified.")));
2895 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2896 errmsg(
"array size exceeds the maximum allowed (%d)",
2899 lb[
i] = lowerIndx[
i];
2905 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2906 errmsg(
"source array too small")));
2910 elmlen, elmbyval, elmalign));
2913 if (ndim < nSubscripts || ndim <= 0 || ndim >
MAXDIM)
2915 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2916 errmsg(
"wrong number of array subscripts")));
2919 memcpy(dim,
ARR_DIMS(array), ndim *
sizeof(
int));
2920 memcpy(lb,
ARR_LBOUND(array), ndim *
sizeof(
int));
2923 addedbefore = addedafter = 0;
2933 Assert(nSubscripts == 1);
2934 if (!lowerProvided[0])
2935 lowerIndx[0] = lb[0];
2936 if (!upperProvided[0])
2937 upperIndx[0] = dim[0] + lb[0] - 1;
2938 if (lowerIndx[0] > upperIndx[0])
2940 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2941 errmsg(
"upper bound cannot be less than lower bound")));
2942 if (lowerIndx[0] < lb[0])
2949 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2950 errmsg(
"array size exceeds the maximum allowed (%d)",
2952 lb[0] = lowerIndx[0];
2953 if (addedbefore > 1)
2956 if (upperIndx[0] >= (dim[0] + lb[0]))
2964 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2965 errmsg(
"array size exceeds the maximum allowed (%d)",
2977 for (
i = 0;
i < nSubscripts;
i++)
2979 if (!lowerProvided[
i])
2980 lowerIndx[
i] = lb[
i];
2981 if (!upperProvided[
i])
2982 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2983 if (lowerIndx[
i] > upperIndx[
i])
2985 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2986 errmsg(
"upper bound cannot be less than lower bound")));
2987 if (lowerIndx[
i] < lb[
i] ||
2988 upperIndx[
i] >= (dim[
i] + lb[
i]))
2990 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
2991 errmsg(
"array subscript out of range")));
2994 for (;
i < ndim;
i++)
2996 lowerIndx[
i] = lb[
i];
2997 upperIndx[
i] = dim[
i] + lb[
i] - 1;
2998 if (lowerIndx[
i] > upperIndx[
i])
3000 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3001 errmsg(
"upper bound cannot be less than lower bound")));
3017 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3018 errmsg(
"source array too small")));
3030 elmlen, elmbyval, elmalign);
3032 olddatasize =
ARR_SIZE(array) - oldoverheadlen;
3042 lowerIndx, upperIndx,
3043 elmlen, elmbyval, elmalign);
3044 lenbefore = lenafter = 0;
3045 itemsbefore = itemsafter = nolditems = 0;
3054 int oldub = oldlb +
ARR_DIMS(array)[0] - 1;
3055 int slicelb =
Max(oldlb, lowerIndx[0]);
3056 int sliceub =
Min(oldub, upperIndx[0]);
3061 itemsbefore =
Min(slicelb, oldub + 1) - oldlb;
3064 elmlen, elmbyval, elmalign);
3066 if (slicelb > sliceub)
3073 nolditems = sliceub - slicelb + 1;
3075 itemsbefore, oldarraybitmap,
3077 elmlen, elmbyval, elmalign);
3080 itemsafter = oldub + 1 -
Max(sliceub + 1, oldlb);
3081 lenafter = olddatasize - lenbefore - olditemsize;
3084 newsize = overheadlen + olddatasize - olditemsize + newitemsize;
3088 newarray->
ndim = ndim;
3089 newarray->
dataoffset = newhasnulls ? overheadlen : 0;
3091 memcpy(
ARR_DIMS(newarray), dim, ndim *
sizeof(
int));
3092 memcpy(
ARR_LBOUND(newarray), lb, ndim *
sizeof(
int));
3102 lowerIndx, upperIndx,
3103 elmlen, elmbyval, elmalign);
3108 memcpy((
char *) newarray + overheadlen,
3109 (
char *) array + oldoverheadlen,
3111 memcpy((
char *) newarray + overheadlen + lenbefore,
3114 memcpy((
char *) newarray + overheadlen + lenbefore + newitemsize,
3115 (
char *) array + oldoverheadlen + lenbefore + olditemsize,
3131 oldnullbitmap, itemsbefore + nolditems,
3148 int arraytyplen,
int elmlen,
bool elmbyval,
char elmalign,
3152 arraytyplen, elmlen, elmbyval, elmalign,
3165 Datum dataValue,
bool isNull,
3166 int arraytyplen,
int elmlen,
bool elmbyval,
char elmalign)
3172 elmlen, elmbyval, elmalign));
3258 inp_typlen = inp_extra->
typlen;
3259 inp_typbyval = inp_extra->
typbyval;
3260 inp_typalign = inp_extra->
typalign;
3270 typlen = ret_extra->
typlen;
3287 inp_typlen, inp_typbyval, inp_typalign);
3305 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3306 errmsg(
"array size exceeds the maximum allowed (%d)",
3315 nbytes += dataoffset;
3324 result->
ndim = ndim;
3364 int elmlen,
bool elmbyval,
char elmalign)
3373 elmtype, elmlen, elmbyval, elmalign);
3393 elmalign = TYPALIGN_CHAR;
3399 elmalign = TYPALIGN_CHAR;
3405 elmalign = TYPALIGN_INT;
3411 elmalign = TYPALIGN_DOUBLE;
3415 elmlen =
sizeof(
int16);
3417 elmalign = TYPALIGN_SHORT;
3421 elmlen =
sizeof(
int32);
3423 elmalign = TYPALIGN_INT;
3427 elmlen =
sizeof(
int64);
3429 elmalign = TYPALIGN_DOUBLE;
3435 elmalign = TYPALIGN_CHAR;
3440 elmlen =
sizeof(
Oid);
3442 elmalign = TYPALIGN_INT;
3448 elmalign = TYPALIGN_INT;
3454 elmalign = TYPALIGN_SHORT;
3460 elmalign = TYPALIGN_INT;
3464 elog(
ERROR,
"type %u not supported by construct_array_builtin()", elmtype);
3471 return construct_array(elems, nelems, elmtype, elmlen, elmbyval, elmalign);
3500 Oid elmtype,
int elmlen,
bool elmbyval,
char elmalign)
3511 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3512 errmsg(
"invalid number of dimensions: %d", ndims)));
3515 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3516 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
3530 for (
i = 0;
i < nelems;
i++)
3532 if (nulls && nulls[
i])
3545 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3546 errmsg(
"array size exceeds the maximum allowed (%d)",
3554 nbytes += dataoffset;
3563 result->
ndim = ndims;
3566 memcpy(
ARR_DIMS(result), dims, ndims *
sizeof(
int));
3567 memcpy(
ARR_LBOUND(result), lbs, ndims *
sizeof(
int));
3570 elems, nulls, nelems,
3571 elmlen, elmbyval, elmalign,
3634 int elmlen,
bool elmbyval,
char elmalign,
3635 Datum **elemsp,
bool **nullsp,
int *nelemsp)
3650 *nullsp = nulls = (
bool *)
palloc0(nelems *
sizeof(
bool));
3659 for (
i = 0;
i < nelems;
i++)
3662 if (bitmap && (*bitmap & bitmask) == 0)
3669 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3670 errmsg(
"null array element not allowed in this context")));
3683 if (bitmask == 0x100)
3700 Datum **elemsp,
bool **nullsp,
int *nelemsp)
3711 elmalign = TYPALIGN_CHAR;
3717 elmalign = TYPALIGN_CHAR;
3723 elmalign = TYPALIGN_DOUBLE;
3727 elmlen =
sizeof(
int16);
3729 elmalign = TYPALIGN_SHORT;
3733 elmlen =
sizeof(
Oid);
3735 elmalign = TYPALIGN_INT;
3741 elmalign = TYPALIGN_INT;
3747 elmalign = TYPALIGN_SHORT;
3751 elog(
ERROR,
"type %u not supported by deconstruct_array_builtin()", elmtype);
3758 deconstruct_array(array, elmtype, elmlen, elmbyval, elmalign, elemsp, nullsp, nelemsp);
3785 if (*bitmap != 0xFF)
3795 if ((*bitmap & bitmask) == 0)
3840 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3841 errmsg(
"cannot compare arrays of different element types")));
3844 if (ndims1 != ndims2 ||
3845 memcmp(dims1, dims2, ndims1 *
sizeof(
int)) != 0 ||
3846 memcmp(lbs1, lbs2, ndims1 *
sizeof(
int)) != 0)
3857 if (typentry == NULL ||
3858 typentry->
type_id != element_type)
3864 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
3865 errmsg(
"could not identify an equality operator for type %s",
3867 fcinfo->flinfo->fn_extra = typentry;
3869 typlen = typentry->
typlen;
3877 collation, NULL, NULL);
3901 if (isnull1 && isnull2)
3903 if (isnull1 || isnull2)
3912 locfcinfo->args[0].value = elt1;
3913 locfcinfo->args[0].isnull =
false;
3914 locfcinfo->args[1].value = elt2;
3915 locfcinfo->args[1].isnull =
false;
3916 locfcinfo->isnull =
false;
3918 if (locfcinfo->isnull || !oprresult)
4011 (
errcode(ERRCODE_DATATYPE_MISMATCH),
4012 errmsg(
"cannot compare arrays of different element types")));
4021 if (typentry == NULL ||
4022 typentry->
type_id != element_type)
4028 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4029 errmsg(
"could not identify a comparison function for type %s",
4033 typlen = typentry->
typlen;
4041 collation, NULL, NULL);
4044 min_nitems =
Min(nitems1, nitems2);
4048 for (
i = 0;
i < min_nitems;
i++)
4063 if (isnull1 && isnull2)
4079 locfcinfo->args[0].value = elt1;
4080 locfcinfo->args[0].isnull =
false;
4081 locfcinfo->args[1].value = elt2;
4082 locfcinfo->args[1].isnull =
false;
4086 Assert(!locfcinfo->isnull);
4113 if (nitems1 != nitems2)
4114 result = (nitems1 < nitems2) ? -1 : 1;
4115 else if (ndims1 != ndims2)
4116 result = (ndims1 < ndims2) ? -1 : 1;
4119 for (
i = 0;
i < ndims1;
i++)
4121 if (dims1[
i] != dims2[
i])
4123 result = (dims1[
i] < dims2[
i]) ? -1 : 1;
4132 for (
i = 0;
i < ndims1;
i++)
4134 if (lbound1[
i] != lbound2[
i])
4136 result = (lbound1[
i] < lbound2[
i]) ? -1 : 1;
4182 if (typentry == NULL ||
4183 typentry->
type_id != element_type)
4189 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4190 errmsg(
"could not identify a hash function for type %s",
4200 if (element_type == RECORDOID)
4212 record_typentry =
palloc0(
sizeof(*record_typentry));
4213 record_typentry->
type_id = element_type;
4223 typentry = record_typentry;
4226 fcinfo->flinfo->fn_extra = typentry;
4229 typlen = typentry->
typlen;
4260 locfcinfo->args[0].value = elt;
4261 locfcinfo->args[0].isnull =
false;
4264 Assert(!locfcinfo->isnull);
4278 result = (result << 5) - result + elthash;
4310 if (typentry == NULL ||
4311 typentry->
type_id != element_type)
4317 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4318 errmsg(
"could not identify an extended hash function for type %s",
4320 fcinfo->flinfo->fn_extra = typentry;
4322 typlen = typentry->
typlen;
4349 locfcinfo->args[0].value = elt;
4350 locfcinfo->args[0].isnull =
false;
4352 locfcinfo->args[1].isnull =
false;
4355 Assert(!locfcinfo->isnull);
4358 result = (result << 5) - result + elthash;
4383 bool matchall,
void **fn_extra)
4386 bool result = matchall;
4402 (
errcode(ERRCODE_DATATYPE_MISMATCH),
4403 errmsg(
"cannot compare arrays of different element types")));
4412 if (typentry == NULL ||
4413 typentry->
type_id != element_type)
4419 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
4420 errmsg(
"could not identify an equality operator for type %s",
4422 *fn_extra = typentry;
4424 typlen = typentry->
typlen;
4443 element_type, typlen, typbyval,
typalign,
4444 &values2, &nulls2, &nelems2);
4450 collation, NULL, NULL);
4456 for (
i = 0;
i < nelems1;
i++)
4479 for (
j = 0;
j < nelems2;
j++)
4482 bool isnull2 = nulls2 ? nulls2[
j] :
false;
4491 locfcinfo->args[0].value = elt1;
4492 locfcinfo->args[0].isnull =
false;
4493 locfcinfo->args[1].value = elt2;
4494 locfcinfo->args[1].isnull =
false;
4495 locfcinfo->isnull =
false;
4497 if (!locfcinfo->isnull && oprresult)
4533 &fcinfo->flinfo->fn_extra);
4551 &fcinfo->flinfo->fn_extra);
4569 &fcinfo->flinfo->fn_extra);
4606 if (slice_ndim < 0 || slice_ndim >
ARR_NDIM(arr))
4607 elog(
ERROR,
"invalid arguments to array_create_iterator");
4612 iterator->
arr = arr;
4784 if (nullbitmap == NULL)
4786 if (nullbitmap[offset / 8] & (1 << (offset % 8)))
4803 nullbitmap += offset / 8;
4804 bitmask = 1 << (offset % 8);
4806 *nullbitmap &= ~bitmask;
4808 *nullbitmap |= bitmask;
4868 int typlen,
bool typbyval,
char typalign)
4874 if (typlen > 0 && !nullbitmap)
4880 nullbitmap += offset / 8;
4881 bitmask = 1 << (offset % 8);
4885 if (*nullbitmap & bitmask)
4891 if (bitmask == 0x100)
4916 int typlen,
bool typbyval,
char typalign)
4938 char *srcptr,
int offset,
bits8 *nullbitmap,
4939 int typlen,
bool typbyval,
char typalign)
4945 memcpy(destptr, srcptr, numbytes);
4968 const bits8 *srcbitmap,
int srcoffset,
4979 destbitmap += destoffset / 8;
4980 destbitmask = 1 << (destoffset % 8);
4981 destbitval = *destbitmap;
4984 srcbitmap += srcoffset / 8;
4985 srcbitmask = 1 << (srcoffset % 8);
4986 srcbitval = *srcbitmap;
4989 if (srcbitval & srcbitmask)
4990 destbitval |= destbitmask;
4992 destbitval &= ~destbitmask;
4994 if (destbitmask == 0x100)
4996 *destbitmap++ = destbitval;
4999 destbitval = *destbitmap;
5002 if (srcbitmask == 0x100)
5007 srcbitval = *srcbitmap;
5010 if (destbitmask != 1)
5011 *destbitmap = destbitval;
5017 destbitval |= destbitmask;
5019 if (destbitmask == 0x100)
5021 *destbitmap++ = destbitval;
5024 destbitval = *destbitmap;
5027 if (destbitmask != 1)
5028 *destbitmap = destbitval;
5039 int ndim,
int *dim,
int *lb,
5041 int typlen,
bool typbyval,
char typalign)
5057 if (typlen > 0 && !arraynullsptr)
5062 ptr =
array_seek(arraydataptr, 0, arraynullsptr, src_offset,
5066 for (
i = 0;
i < ndim;
i++)
5073 ptr =
array_seek(ptr, src_offset, arraynullsptr, dist[
j],
5075 src_offset += dist[
j];
5103 bits8 *arraynullsptr,
5124 srcdataptr =
array_seek(arraydataptr, 0, arraynullsptr, src_offset,
5129 for (
i = 0;
i < ndim;
i++)
5138 srcdataptr =
array_seek(srcdataptr, src_offset, arraynullsptr,
5141 src_offset += dist[
j];
5144 srcdataptr, src_offset, arraynullsptr,
5148 arraynullsptr, src_offset,
5205 origPtr, 0, origBitmap,
5211 orig_offset = dest_offset;
5215 for (
i = 0;
i < ndim;
i++)
5225 origPtr, orig_offset, origBitmap,
5231 origBitmap, orig_offset,
5233 dest_offset += dist[
j];
5234 orig_offset += dist[
j];
5238 srcPtr, src_offset, srcBitmap,
5242 srcBitmap, src_offset,
5249 origPtr =
array_seek(origPtr, orig_offset, origBitmap, 1,
5255 array_copy(destPtr, orignitems - orig_offset,
5256 origPtr, orig_offset, origBitmap,
5260 origBitmap, orig_offset,
5261 orignitems - orig_offset);
5302 subcontext ? 64 : 8);
5312 bool subcontext,
int initsize)
5327 astate->
alen = initsize;
5330 astate->
dnulls = (
bool *)
5352 Datum dvalue,
bool disnull,
5377 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5378 errmsg(
"array size exceeds the maximum allowed (%d)",
5382 astate->
dnulls = (
bool *)
5396 if (astate->
typlen == -1)
5429 ndims = (astate->
nelems > 0) ? 1 : 0;
5430 dims[0] = astate->
nelems;
5518 (
errcode(ERRCODE_DATATYPE_MISMATCH),
5519 errmsg(
"data type %s is not an array type",
5526 "accumArrayResultArr",
5552 Datum dvalue,
bool disnull,
5573 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5574 errmsg(
"cannot accumulate null arrays")));
5594 if (astate->
ndims == 0)
5601 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
5602 errmsg(
"cannot accumulate empty arrays")));
5605 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5606 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
5613 astate->
ndims = ndims + 1;
5614 astate->
dims[0] = 0;
5615 memcpy(&astate->
dims[1], dims, ndims *
sizeof(
int));
5617 memcpy(&astate->
lbs[1], lbs, ndims *
sizeof(
int));
5626 if (astate->
ndims != ndims + 1)
5628 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
5629 errmsg(
"cannot accumulate arrays of different dimensionality")));
5630 for (
i = 0;
i < ndims;
i++)
5632 if (astate->
dims[
i + 1] != dims[
i] || astate->
lbs[
i + 1] != lbs[
i])
5634 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
5635 errmsg(
"cannot accumulate arrays of different dimensionality")));
5642 astate->
nbytes + ndatabytes);
5654 astate->
nbytes += ndatabytes;
5673 else if (newnitems > astate->
aitems)
5685 astate->
dims[0] += 1;
5714 if (astate->
ndims == 0)
5733 nbytes += dataoffset;
5831 Datum dvalue,
bool disnull,
5841 input_type, rcontext);
5845 input_type, rcontext);
5940 if (reqdim <= 0 || reqdim >
AARR_NDIM(v))
5952 fctx->
lower = lb[reqdim - 1];
5953 fctx->
upper = dimv[reqdim - 1] + lb[reqdim - 1] - 1;
6004 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6005 errmsg(
"dimension array or low bound array cannot be null")));
6023 elog(
ERROR,
"could not determine data type of input");
6044 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6045 errmsg(
"dimension array or low bound array cannot be null")));
6062 elog(
ERROR,
"could not determine data type of input");
6070 Oid elmtype,
int dataoffset)
6076 result->
ndim = ndims;
6079 memcpy(
ARR_DIMS(result), dimv, ndims *
sizeof(
int));
6080 memcpy(
ARR_LBOUND(result), lbsv, ndims *
sizeof(
int));
6106 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
6107 errmsg(
"wrong number of array subscripts"),
6108 errdetail(
"Dimension array must be one dimensional.")));
6112 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6113 errmsg(
"dimension values cannot be null")));
6120 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6121 errmsg(
"invalid number of dimensions: %d", ndims)));
6124 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
6125 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
6132 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
6133 errmsg(
"wrong number of array subscripts"),
6134 errdetail(
"Dimension array must be one dimensional.")));
6138 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6139 errmsg(
"dimension values cannot be null")));
6143 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
6144 errmsg(
"wrong number of array subscripts"),
6145 errdetail(
"Low bound array has different size than dimensions array.")));
6172 if (my_extra == NULL)
6190 elmlen = my_extra->
typlen;
6210 totbytes = nbytes *
nitems;
6213 if (totbytes / nbytes !=
nitems ||
6216 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
6217 errmsg(
"array size exceeds the maximum allowed (%d)",
6239 nbytes = dataoffset;
6242 elmtype, dataoffset);
6265 } array_unnest_fctx;
6268 array_unnest_fctx *fctx;
6294 fctx = (array_unnest_fctx *)
palloc(
sizeof(array_unnest_fctx));
6322 if (fctx->nextelem < fctx->numelems)
6324 int offset = fctx->nextelem++;
6328 fctx->elmlen, fctx->elmbyval, fctx->elmalign);
6383 Datum search,
bool search_isnull,
6384 Datum replace,
bool replace_isnull,
6385 bool remove,
Oid collation,
6407 bool changed =
false;
6423 if (remove && ndim > 1)
6425 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6426 errmsg(
"removing elements from multidimensional arrays is not supported")));
6433 if (typentry == NULL ||
6434 typentry->
type_id != element_type)
6440 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
6441 errmsg(
"could not identify an equality operator for type %s",
6445 typlen = typentry->
typlen;
6458 if (!replace_isnull)
6464 collation, NULL, NULL);
6485 if (bitmap && (*bitmap & bitmask) == 0)
6496 else if (!replace_isnull)
6498 values[nresult] = replace;
6507 elt =
fetch_att(arraydataptr, typbyval, typlen);
6521 locfcinfo->args[0].value = elt;
6522 locfcinfo->args[0].isnull =
false;
6523 locfcinfo->args[1].value = search;
6524 locfcinfo->args[1].isnull =
false;
6525 locfcinfo->isnull =
false;
6527 if (locfcinfo->isnull || !oprresult)
6540 values[nresult] = replace;
6541 isNull = replace_isnull;
6549 nulls[nresult] = isNull;
6560 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
6561 errmsg(
"array size exceeds the maximum allowed (%d)",
6571 if (bitmask == 0x100)
6601 nbytes += dataoffset;
6610 result->
ndim = ndim;
6651 search, search_isnull,
6675 search, search_isnull,
6676 replace, replace_isnull,
6702 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
6703 errmsg(
"thresholds must be one-dimensional array")));
6707 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6708 errmsg(
"thresholds array must not contain NULLs")));
6711 if (element_type == FLOAT8OID)
6719 if (typentry == NULL ||
6720 typentry->
type_id != element_type)
6726 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
6727 errmsg(
"could not identify a comparison function for type %s",
6729 fcinfo->flinfo->fn_extra = typentry;
6736 if (typentry->
typlen > 0)
6738 collation, typentry);
6741 collation, typentry);
6781 while (left < right)
6783 int mid = (left + right) / 2;
6785 if (isnan(thresholds_data[mid]) || op < thresholds_data[mid])
6804 char *thresholds_data;
6805 int typlen = typentry->
typlen;
6806 bool typbyval = typentry->
typbyval;
6817 collation, NULL, NULL);
6822 while (left < right)
6824 int mid = (left + right) / 2;
6828 ptr = thresholds_data + mid * typlen;
6830 locfcinfo->args[0].value = operand;
6831 locfcinfo->args[0].isnull =
false;
6832 locfcinfo->args[1].value =
fetch_att(ptr, typbyval, typlen);
6833 locfcinfo->args[1].isnull =
false;
6838 Assert(!locfcinfo->isnull);
6859 char *thresholds_data;
6860 int typlen = typentry->
typlen;
6861 bool typbyval = typentry->
typbyval;
6869 collation, NULL, NULL);
6874 while (left < right)
6876 int mid = (left + right) / 2;
6882 ptr = thresholds_data;
6883 for (
i = left;
i < mid;
i++)
6889 locfcinfo->args[0].value = operand;
6890 locfcinfo->args[0].isnull =
false;
6891 locfcinfo->args[1].value =
fetch_att(ptr, typbyval, typlen);
6892 locfcinfo->args[1].isnull =
false;
6897 Assert(!locfcinfo->isnull);
6933 bool lowerProvided[
MAXDIM];
6934 bool upperProvided[
MAXDIM];
6940 (
errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
6941 errmsg(
"number of elements to trim must be between 0 and %d",
6945 memset(lowerProvided,
false,
sizeof(lowerProvided));
6946 memset(upperProvided,
false,
sizeof(upperProvided));
6950 upperProvided[0] =
true;
6959 -1, elmlen, elmbyval, elmalign);
#define PG_GETARG_ANY_ARRAY_P(n)
#define PG_GETARG_ARRAYTYPE_P(n)
#define ARR_NULLBITMAP(a)
#define ARR_OVERHEAD_WITHNULLS(ndims, nitems)
#define DatumGetArrayTypeP(X)
#define PG_RETURN_ARRAYTYPE_P(x)
#define ARR_OVERHEAD_NONULLS(ndims)
#define ARR_DATA_OFFSET(a)
Datum expand_array(Datum arraydatum, MemoryContext parentcontext, ArrayMetaState *metacache)
void deconstruct_expanded_array(ExpandedArrayHeader *eah)
ExpandedArrayHeader * DatumGetExpandedArray(Datum d)
AnyArrayType * DatumGetAnyArrayP(Datum d)
static Datum array_iter_next(array_iter *it, bool *isnull, int i, int elmlen, bool elmbyval, char elmalign)
static void array_iter_setup(array_iter *it, AnyArrayType *a)
Datum array_eq(PG_FUNCTION_ARGS)
Datum arraycontained(PG_FUNCTION_ARGS)
Datum array_lt(PG_FUNCTION_ARGS)
ArrayType * array_set(ArrayType *array, int nSubscripts, int *indx, Datum dataValue, bool isNull, int arraytyplen, int elmlen, bool elmbyval, char elmalign)
static bool ReadArrayStr(char **srcptr, FmgrInfo *inputproc, Oid typioparam, int32 typmod, char typdelim, int typlen, bool typbyval, char typalign, int *ndim_p, int *dim, int *nitems_p, Datum **values_p, bool **nulls_p, const char *origStr, Node *escontext)
Datum array_fill(PG_FUNCTION_ARGS)
Datum array_replace(PG_FUNCTION_ARGS)
static ArrayType * array_replace_internal(ArrayType *array, Datum search, bool search_isnull, Datum replace, bool replace_isnull, bool remove, Oid collation, FunctionCallInfo fcinfo)
Datum array_smaller(PG_FUNCTION_ARGS)
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
static bool ReadArrayDimensions(char **srcptr, int *ndim_p, int *dim, int *lBound, const char *origStr, Node *escontext)
Datum array_cardinality(PG_FUNCTION_ARGS)
ExpandedArrayHeader * construct_empty_expanded_array(Oid element_type, MemoryContext parentcontext, ArrayMetaState *metacache)
bool array_iterate(ArrayIterator iterator, Datum *value, bool *isnull)
static Datum array_set_element_expanded(Datum arraydatum, int nSubscripts, int *indx, Datum dataValue, bool isNull, int arraytyplen, int elmlen, bool elmbyval, char elmalign)
void array_free_iterator(ArrayIterator iterator)
Datum array_recv(PG_FUNCTION_ARGS)
Datum generate_subscripts_nodir(PG_FUNCTION_ARGS)
Datum hash_array(PG_FUNCTION_ARGS)
static int width_bucket_array_float8(Datum operand, ArrayType *thresholds)
ArrayBuildStateAny * initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
ArrayBuildStateAny * accumArrayResultAny(ArrayBuildStateAny *astate, Datum dvalue, bool disnull, Oid input_type, MemoryContext rcontext)
ArrayType * construct_empty_array(Oid elmtype)
Datum array_dims(PG_FUNCTION_ARGS)
bool array_contains_nulls(const ArrayType *array)
static ArrayToken ReadArrayToken(char **srcptr, StringInfo elembuf, char typdelim, const char *origStr, Node *escontext)
Datum arraycontains(PG_FUNCTION_ARGS)
static int array_slice_size(char *arraydataptr, bits8 *arraynullsptr, int ndim, int *dim, int *lb, int *st, int *endp, int typlen, bool typbyval, char typalign)
static bool array_get_isnull(const bits8 *nullbitmap, int offset)
Datum makeArrayResultArr(ArrayBuildStateArr *astate, MemoryContext rcontext, bool release)
#define AARR_FREE_IF_COPY(array, n)
Datum array_upper(PG_FUNCTION_ARGS)
Datum makeArrayResultAny(ArrayBuildStateAny *astate, MemoryContext rcontext, bool release)
static ArrayType * array_fill_internal(ArrayType *dims, ArrayType *lbs, Datum value, bool isnull, Oid elmtype, FunctionCallInfo fcinfo)
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Datum array_map(Datum arrayd, ExprState *exprstate, ExprContext *econtext, Oid retType, ArrayMapState *amstate)
Datum array_set_element(Datum arraydatum, int nSubscripts, int *indx, Datum dataValue, bool isNull, int arraytyplen, int elmlen, bool elmbyval, char elmalign)
Datum makeMdArrayResult(ArrayBuildState *astate, int ndims, int *dims, int *lbs, MemoryContext rcontext, bool release)
struct ArrayIteratorData ArrayIteratorData
static bool ReadDimensionInt(char **srcptr, int *result, const char *origStr, Node *escontext)
ArrayBuildStateArr * initArrayResultArr(Oid array_type, Oid element_type, MemoryContext rcontext, bool subcontext)
static bool array_contain_compare(AnyArrayType *array1, AnyArrayType *array2, Oid collation, bool matchall, void **fn_extra)
Datum array_lower(PG_FUNCTION_ARGS)
static int width_bucket_array_fixed(Datum operand, ArrayType *thresholds, Oid collation, TypeCacheEntry *typentry)
ArrayBuildStateArr * accumArrayResultArr(ArrayBuildStateArr *astate, Datum dvalue, bool disnull, Oid array_type, MemoryContext rcontext)
static int array_cmp(FunctionCallInfo fcinfo)
ArrayIterator array_create_iterator(ArrayType *arr, int slice_ndim, ArrayMetaState *mstate)
Datum btarraycmp(PG_FUNCTION_ARGS)
Datum array_ref(ArrayType *array, int nSubscripts, int *indx, int arraytyplen, int elmlen, bool elmbyval, char elmalign, bool *isNull)
static void ReadArrayBinary(StringInfo buf, int nitems, FmgrInfo *receiveproc, Oid typioparam, int32 typmod, int typlen, bool typbyval, char typalign, Datum *values, bool *nulls, bool *hasnulls, int32 *nbytes)
static Datum array_get_element_expanded(Datum arraydatum, int nSubscripts, int *indx, int arraytyplen, int elmlen, bool elmbyval, char elmalign, bool *isNull)
ArrayBuildState * initArrayResultWithSize(Oid element_type, MemoryContext rcontext, bool subcontext, int initsize)
Datum hash_array_extended(PG_FUNCTION_ARGS)
Datum generate_subscripts(PG_FUNCTION_ARGS)
static void array_insert_slice(ArrayType *destArray, ArrayType *origArray, ArrayType *srcArray, int ndim, int *dim, int *lb, int *st, int *endp, int typlen, bool typbyval, char typalign)
Datum array_send(PG_FUNCTION_ARGS)
Datum array_le(PG_FUNCTION_ARGS)
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
static int array_copy(char *destptr, int nitems, char *srcptr, int offset, bits8 *nullbitmap, int typlen, bool typbyval, char typalign)
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Datum array_gt(PG_FUNCTION_ARGS)
Datum array_unnest(PG_FUNCTION_ARGS)
static ArrayType * create_array_envelope(int ndims, int *dimv, int *lbsv, int nbytes, Oid elmtype, int dataoffset)
Datum array_remove(PG_FUNCTION_ARGS)
Datum array_ne(PG_FUNCTION_ARGS)
Datum array_larger(PG_FUNCTION_ARGS)
Datum array_in(PG_FUNCTION_ARGS)
static void array_extract_slice(ArrayType *newarray, int ndim, int *dim, int *lb, char *arraydataptr, bits8 *arraynullsptr, int *st, int *endp, int typlen, bool typbyval, char typalign)
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
static int width_bucket_array_variable(Datum operand, ArrayType *thresholds, Oid collation, TypeCacheEntry *typentry)
Datum array_length(PG_FUNCTION_ARGS)
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
static int array_nelems_size(char *ptr, int offset, bits8 *nullbitmap, int nitems, int typlen, bool typbyval, char typalign)
static void array_set_isnull(bits8 *nullbitmap, int offset, bool isNull)
void deconstruct_array(const ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
void array_bitmap_copy(bits8 *destbitmap, int destoffset, const bits8 *srcbitmap, int srcoffset, int nitems)
static int ArrayCastAndSet(Datum src, int typlen, bool typbyval, char typalign, char *dest)
static Datum ArrayCast(char *value, bool byval, int len)
Datum array_get_element(Datum arraydatum, int nSubscripts, int *indx, int arraytyplen, int elmlen, bool elmbyval, char elmalign, bool *isNull)
Datum array_out(PG_FUNCTION_ARGS)
Datum array_fill_with_lower_bounds(PG_FUNCTION_ARGS)
Datum array_ndims(PG_FUNCTION_ARGS)
static char * array_seek(char *ptr, int offset, bits8 *nullbitmap, int nitems, int typlen, bool typbyval, char typalign)
Datum array_get_slice(Datum arraydatum, int nSubscripts, int *upperIndx, int *lowerIndx, bool *upperProvided, bool *lowerProvided, int arraytyplen, int elmlen, bool elmbyval, char elmalign)
Datum array_ge(PG_FUNCTION_ARGS)
Datum width_bucket_array(PG_FUNCTION_ARGS)
Datum arrayoverlap(PG_FUNCTION_ARGS)
Datum array_set_slice(Datum arraydatum, int nSubscripts, int *upperIndx, int *lowerIndx, bool *upperProvided, bool *lowerProvided, Datum srcArrayDatum, bool isNull, int arraytyplen, int elmlen, bool elmbyval, char elmalign)
void CopyArrayEls(ArrayType *array, const Datum *values, const bool *nulls, int nitems, int typlen, bool typbyval, char typalign, bool freedata)
Datum trim_array(PG_FUNCTION_ARGS)
Datum array_unnest_support(PG_FUNCTION_ARGS)
struct generate_subscripts_fctx generate_subscripts_fctx
void mda_get_offset_values(int n, int *dist, const int *prod, const int *span)
int ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx)
void mda_get_range(int n, int *span, const int *st, const int *endp)
int ArrayGetNItems(int ndim, const int *dims)
void mda_get_prod(int n, const int *range, int *prod)
void ArrayCheckBounds(int ndim, const int *dims, const int *lb)
int mda_next_tuple(int n, int *curr, const int *span)
static Datum values[MAXATTR]
#define FORMAT_TYPE_ALLOW_INVALID
#define OidIsValid(objectId)
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Datum datumCopy(Datum value, bool typByVal, int typLen)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereturn(context, dummy_value,...)
#define ereport(elevel,...)
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
ExpandedObjectHeader * DatumGetEOHP(Datum d)
#define VARATT_IS_EXPANDED_HEADER(PTR)
static Datum EOHPGetRWDatum(const struct ExpandedObjectHeader *eohptr)
#define repalloc_array(pointer, type, count)
#define palloc_array(type, count)
void fmgr_info(Oid functionId, FmgrInfo *finfo)
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
bool InputFunctionCallSafe(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod, Node *escontext, Datum *result)
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Datum ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, Oid typioparam, int32 typmod)
#define PG_FREE_IF_COPY(ptr, n)
#define PG_RETURN_UINT32(x)
#define PG_RETURN_BYTEA_P(x)
#define PG_DETOAST_DATUM_COPY(datum)
#define PG_GETARG_POINTER(n)
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
#define PG_RETURN_CSTRING(x)
#define PG_GETARG_DATUM(n)
#define LOCAL_FCINFO(name, nargs)
#define PG_GETARG_CSTRING(n)
#define PG_GETARG_INT64(n)
#define PG_RETURN_UINT64(x)
#define PG_DETOAST_DATUM(datum)
#define FunctionCallInvoke(fcinfo)
#define PG_RETURN_TEXT_P(x)
#define PG_RETURN_INT32(x)
#define PG_GETARG_INT32(n)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_DATUM(x)
#define PG_RETURN_POINTER(x)
#define PG_GET_COLLATION()
#define PG_RETURN_BOOL(x)
#define SRF_IS_FIRSTCALL()
#define SRF_PERCALL_SETUP()
#define SRF_RETURN_NEXT(_funcctx, _result)
#define SRF_FIRSTCALL_INIT()
#define SRF_RETURN_DONE(_funcctx)
Assert(PointerIsAligned(start, uint64))
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
if(TABLE==NULL||TABLE_index==NULL)
struct ItemPointerData ItemPointerData
Oid get_element_type(Oid typid)
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
void get_type_io_data(Oid typid, IOFuncSelector which_func, int16 *typlen, bool *typbyval, char *typalign, char *typdelim, Oid *typioparam, Oid *func)
Oid get_array_type(Oid typid)
void * MemoryContextAlloc(MemoryContext context, Size size)
void * MemoryContextAllocZero(MemoryContext context, Size size)
char * pstrdup(const char *in)
void * repalloc(void *pointer, Size size)
void pfree(void *pointer)
void * palloc0(Size size)
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define AllocSizeIsValid(size)
static bool is_funcclause(const void *clause)
#define IsA(nodeptr, _type_)
Datum lower(PG_FUNCTION_ARGS)
Datum upper(PG_FUNCTION_ARGS)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static uint32 pg_nextpower2_32(uint32 num)
static const struct exclude_list_item skip[]
int pg_strcasecmp(const char *s1, const char *s2)
static uint32 DatumGetUInt32(Datum X)
static Datum Int64GetDatum(int64 X)
static uint64 DatumGetUInt64(Datum X)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
static float8 DatumGetFloat8(Datum X)
static Pointer DatumGetPointer(Datum X)
static Datum Int32GetDatum(int32 X)
static int32 DatumGetInt32(Datum X)
bool scanner_isspace(char ch)
double estimate_array_length(PlannerInfo *root, Node *arrayexpr)
struct StringInfoData * StringInfo
void resetStringInfo(StringInfo str)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
static void initReadOnlyStringInfo(StringInfo str, char *data, int len)
ArrayBuildStateArr * arraystate
ArrayBuildState * scalarstate
MemoryContext eoh_context
bool * innermost_casenull
Datum * innermost_caseval
MemoryContext multi_call_memory_ctx
FmgrInfo hash_extended_proc_finfo
#define FirstGenbkiObjectId
#define att_align_nominal(cur_offset, attalign)
#define att_addlength_pointer(cur_offset, attlen, attptr)
static Datum fetch_att(const void *T, bool attbyval, int attlen)
#define att_addlength_datum(cur_offset, attlen, attdatum)
static void store_att_byval(void *T, Datum newdatum, int attlen)
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
#define TYPECACHE_HASH_PROC_FINFO
#define TYPECACHE_EQ_OPR_FINFO
#define TYPECACHE_HASH_EXTENDED_PROC_FINFO
#define TYPECACHE_CMP_PROC_FINFO
static Size VARSIZE(const void *PTR)
static char * VARDATA(const void *PTR)
static bool VARATT_IS_EXTERNAL_EXPANDED(const void *PTR)
static void SET_VARSIZE(void *PTR, Size len)
text * cstring_to_text(const char *s)