1 /*-------------------------------------------------------------------------
4 * Special functions for arrays.
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.58 2000/06/14 05:24:48 tgl Exp $
13 *-------------------------------------------------------------------------
20 #include "catalog/catalog.h"
21 #include "catalog/pg_type.h"
23 #include "libpq/be-fsstubs.h"
24 #include "libpq/libpq-fs.h"
25 #include "storage/fd.h"
26 #include "utils/array.h"
27 #include "utils/memutils.h"
28 #include "utils/syscache.h"
32 /* An array has the following internal structure:
33 * <nbytes> - total number of bytes
34 * <ndim> - number of dimensions of the array
35 * <flags> - bit mask of flags
36 * <dim> - size of each array axis
37 * <dim_lower> - lower boundary of each dimension
38 * <actual data> - whatever is the stored data
41 /*-=-=--=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-*/
42 static int _ArrayCount(char *str, int *dim, int typdelim);
43 static char *_ReadArrayStr(char *arrayStr, int nitems, int ndim, int *dim,
44 FmgrInfo *inputproc, Oid typelem, int32 typmod,
45 char typdelim, int typlen, bool typbyval,
46 char typalign, int *nbytes);
49 static char *_ReadLOArray(char *str, int *nbytes, int *fd, bool *chunkFlag,
50 int ndim, int *dim, int baseSize);
53 static void _CopyArrayEls(char **values, char *p, int nitems, int typlen,
54 char typalign, bool typbyval);
55 static void system_cache_lookup(Oid element_type, bool input, int *typlen,
56 bool *typbyval, char *typdelim, Oid *typelem, Oid *proc,
58 static Datum _ArrayCast(char *value, bool byval, int len);
61 static char *_AdvanceBy1word(char *str, char **word);
64 static void _ArrayRange(int *st, int *endp, int bsize, char *destPtr,
65 ArrayType *array, int from);
66 static int _ArrayClipCount(int *stI, int *endpI, ArrayType *array);
67 static void _LOArrayRange(int *st, int *endp, int bsize, int srcfd,
68 int destfd, ArrayType *array, int isSrcLO, bool *isNull);
69 static void _ReadArray(int *st, int *endp, int bsize, int srcfd, int destfd,
70 ArrayType *array, int isDestLO, bool *isNull);
71 static int ArrayCastAndSet(Datum src, bool typbyval, int typlen, char *dest);
72 static int SanityCheckInput(int ndim, int n, int *dim, int *lb, int *indx);
73 static int array_read(char *destptr, int eltsize, int nitems, char *srcptr);
74 static char *array_seek(char *ptr, int eltsize, int nitems);
76 /*---------------------------------------------------------------------
78 * converts an array from the external format in "string" to
79 * its internal format.
81 * the internal representation of the input array
82 *--------------------------------------------------------------------
85 array_in(PG_FUNCTION_ARGS)
87 char *string = PG_GETARG_CSTRING(0); /* external form */
88 Oid element_type = PG_GETARG_OID(1); /* type of an array element */
89 int32 typmod = PG_GETARG_INT32(2); /* typmod for array elements */
111 system_cache_lookup(element_type, true, &typlen, &typbyval, &typdelim,
112 &typelem, &typinput, &typalign);
114 fmgr_info(typinput, &inputproc);
116 string_save = (char *) palloc(strlen(string) + 3);
117 strcpy(string_save, string);
119 /* --- read array dimensions ---------- */
122 for (ndim = 0; !done;)
129 if ((r = (char *) strchr(p, ':')) == (char *) NULL)
134 lBound[ndim] = atoi(p);
137 for (q = p; isdigit(*q); q++);
139 elog(ERROR, "array_in: missing ']' in array declaration");
142 if ((dim[ndim] < 0) || (lBound[ndim] < 0))
143 elog(ERROR, "array_in: array dimensions need to be positive");
144 dim[ndim] = dim[ndim] - lBound[ndim] + 1;
146 elog(ERROR, "array_in: upper_bound cannot be < lower_bound");
158 ndim = _ArrayCount(p, dim, typdelim);
159 for (i = 0; i < ndim; lBound[i++] = 1);
162 elog(ERROR, "array_in: Need to specify dimension");
168 if (strncmp(p, ASSGN, strlen(ASSGN)))
169 elog(ERROR, "array_in: missing assignment operator");
176 printf("array_in- ndim %d (", ndim);
177 for (i = 0; i < ndim; i++)
179 printf(" %d", dim[i]);
181 printf(") for %s\n", string);
184 nitems = getNitems(ndim, dim);
187 retval = (ArrayType *) palloc(sizeof(ArrayType));
188 MemSet(retval, 0, sizeof(ArrayType));
189 *(int32 *) retval = sizeof(ArrayType);
190 PG_RETURN_POINTER(retval);
195 /* array not a large object */
196 dataPtr = (char *) _ReadArrayStr(p, nitems, ndim, dim, &inputproc, typelem,
197 typmod, typdelim, typlen, typbyval, typalign,
199 nbytes += ARR_OVERHEAD(ndim);
200 retval = (ArrayType *) palloc(nbytes);
201 MemSet(retval, 0, nbytes);
202 memmove(retval, (char *) &nbytes, sizeof(int));
203 memmove((char *) ARR_NDIM_PTR(retval), (char *) &ndim, sizeof(int));
204 SET_LO_FLAG(false, retval);
205 memmove((char *) ARR_DIMS(retval), (char *) dim, ndim * sizeof(int));
206 memmove((char *) ARR_LBOUND(retval), (char *) lBound,
210 * dataPtr is an array of arbitraystuff even though its type is
211 * char* cast to char** to pass to _CopyArrayEls for now - jolly
213 _CopyArrayEls((char **) dataPtr,
214 ARR_DATA_PTR(retval), nitems,
215 typlen, typalign, typbyval);
222 bool chunked = false;
224 dataPtr = _ReadLOArray(p, &bytes, &dummy, &chunked, ndim,
226 nbytes = bytes + ARR_OVERHEAD(ndim);
227 retval = (ArrayType *) palloc(nbytes);
228 MemSet(retval, 0, nbytes);
229 memmove(retval, (char *) &nbytes, sizeof(int));
230 memmove((char *) ARR_NDIM_PTR(retval), (char *) &ndim, sizeof(int));
231 SET_LO_FLAG(true, retval);
232 SET_CHUNK_FLAG(chunked, retval);
233 memmove((char *) ARR_DIMS(retval), (char *) dim, ndim * sizeof(int));
234 memmove((char *) ARR_LBOUND(retval), (char *) lBound, ndim * sizeof(int));
235 memmove(ARR_DATA_PTR(retval), dataPtr, bytes);
237 elog(ERROR, "large object arrays not supported");
241 PG_RETURN_POINTER(retval);
244 /*-----------------------------------------------------------------------------
246 * Counts the number of dimensions and the *dim array for an array string.
247 * The syntax for array input is C-like nested curly braces
248 *-----------------------------------------------------------------------------
251 _ArrayCount(char *str, int *dim, int typdelim)
257 bool scanning_string = false;
258 bool eoArray = false;
261 for (i = 0; i < MAXDIM; ++i)
262 temp[i] = dim[i] = 0;
264 if (strncmp(str, "{}", 2) == 0)
268 while (eoArray != true)
277 /* skip escaped characters (\ and ") inside strings */
278 if (scanning_string && *(q + 1))
284 * Signal a premature end of the string. DZ -
287 elog(ERROR, "malformed array constant: %s", str);
290 scanning_string = !scanning_string;
293 if (!scanning_string)
295 temp[nest_level] = 0;
300 if (!scanning_string)
306 temp[nest_level - 1]++;
308 eoArray = done = true;
314 if (*q == typdelim && !scanning_string)
327 for (i = 0; i < ndim; ++i)
333 /*---------------------------------------------------------------------------
335 * parses the array string pointed by "arrayStr" and converts it in the
336 * internal format. The external format expected is like C array
337 * declaration. Unspecified elements are initialized to zero for fixed length
338 * base types and to empty varlena structures for variable length base
341 * returns the internal representation of the array elements
342 * nbytes is set to the size of the array in its internal representation.
343 *---------------------------------------------------------------------------
346 _ReadArrayStr(char *arrayStr,
350 FmgrInfo *inputproc, /* function used for the
366 bool scanning_string = false;
369 bool eoArray = false;
371 mda_get_prod(ndim, dim, prod);
372 for (i = 0; i < ndim; indx[i++] = 0);
373 /* read array enclosed within {} */
374 values = (char **) palloc(nitems * sizeof(char *));
375 MemSet(values, 0, nitems * sizeof(char *));
388 /* Crunch the string on top of the backslash. */
389 for (r = q; *r != '\0'; r++)
393 if (!scanning_string)
397 p++; /* get p past first doublequote */
401 scanning_string = !scanning_string;
404 if (!scanning_string)
408 if (nest_level > ndim)
409 elog(ERROR, "array_in: illformed array constant");
410 indx[nest_level - 1] = 0;
415 if (!scanning_string)
418 i = tuple2linear(ndim, indx, prod);
421 eoArray = done = true;
425 indx[nest_level - 1]++;
430 if (*q == typdelim && !scanning_string)
433 i = tuple2linear(ndim, indx, prod);
444 elog(ERROR, "array_in: illformed array constant");
445 values[i] = (char *) FunctionCall3(inputproc,
447 ObjectIdGetDatum(typelem),
448 Int32GetDatum(typmod));
453 * if not at the end of the array skip white space
463 *nbytes = nitems * typlen;
465 for (i = 0; i < nitems; i++)
468 values[i] = palloc(typlen);
469 MemSet(values[i], 0, typlen);
474 for (i = 0, *nbytes = 0; i < nitems; i++)
479 *nbytes += MAXALIGN(*(int32 *) values[i]);
481 *nbytes += INTALIGN(*(int32 *) values[i]);
485 *nbytes += sizeof(int32);
486 values[i] = palloc(sizeof(int32));
487 *(int32 *) values[i] = sizeof(int32);
491 return (char *) values;
495 /*----------------------------------------------------------------------------
496 * Read data about an array to be stored as a large object
497 *----------------------------------------------------------------------------
501 _ReadLOArray(char *str,
516 str = _AdvanceBy1word(str, &inputfile);
522 str = _AdvanceBy1word(str, &word);
524 if (!strcmp(word, "-chunk"))
527 elog(ERROR, "array_in: access pattern file required");
528 str = _AdvanceBy1word(str, &accessfile);
530 else if (!strcmp(word, "-noreorg"))
533 elog(ERROR, "array_in: chunk file required");
534 str = _AdvanceBy1word(str, &chunkfile);
537 elog(ERROR, "usage: <input file> -chunk DEFAULT/<access pattern file> -invert/-native [-noreorg <chunk file>]");
540 if (inputfile == NULL)
541 elog(ERROR, "array_in: missing file name");
542 lobjId = DatumGetObjectId(DirectFunctionCall1(lo_creat,
544 *fd = DatumGetInt32(DirectFunctionCall2(lo_open,
545 ObjectIdGetDatum(lobjId),
546 Int32GetDatum(INV_READ)));
548 elog(ERROR, "Large object create failed");
550 *nbytes = strlen(retStr) + 2;
556 if ((afd = AllocateFile(accessfile, PG_BINARY_R)) == NULL)
557 elog(ERROR, "unable to open access pattern file");
559 retStr = _ChunkArray(*fd, afd, ndim, dim, baseSize, nbytes,
569 _CopyArrayEls(char **values,
578 for (i = 0; i < nitems; i++)
582 inc = ArrayCastAndSet((Datum) values[i], typbyval, typlen, p);
590 /*-------------------------------------------------------------------------
592 * takes the internal representation of an array and returns a string
593 * containing the array in its external format.
594 *-------------------------------------------------------------------------
597 array_out(PG_FUNCTION_ARGS)
599 ArrayType *v = (ArrayType *) PG_GETARG_VARLENA_P(0);
600 Oid element_type = PG_GETARG_OID(1);
625 if (ARR_IS_LO(v) == true)
631 p = (text *) DatumGetPointer(DirectFunctionCall1(array_dims,
632 PointerGetDatum(v)));
633 plen = VARSIZE(p) - VARHDRSZ;
635 /* get a wide string to print to */
636 nbytes = strlen(ARR_DATA_PTR(v)) + strlen(ASSGN) + plen + 1;
637 retval = (char *) palloc(nbytes);
639 memcpy(retval, VARDATA(p), plen);
640 strcpy(retval + plen, ASSGN);
641 strcat(retval, ARR_DATA_PTR(v));
643 PG_RETURN_CSTRING(retval);
646 system_cache_lookup(element_type, false, &typlen, &typbyval,
647 &typdelim, &typelem, &typoutput, &typalign);
648 fmgr_info(typoutput, &outputproc);
649 sprintf(delim, "%c", typdelim);
652 nitems = getNitems(ndim, dim);
656 retval = (char *) palloc(3);
660 PG_RETURN_CSTRING(retval);
664 overall_length = 1; /* [TRH] don't forget to count \0 at end. */
665 values = (char **) palloc(nitems * sizeof(char *));
666 for (i = 0; i < nitems; i++)
673 values[i] = DatumGetCString(FunctionCall3(&outputproc,
675 ObjectIdGetDatum(typelem),
679 values[i] = DatumGetCString(FunctionCall3(&outputproc,
680 Int16GetDatum(*(int16 *) p),
681 ObjectIdGetDatum(typelem),
686 values[i] = DatumGetCString(FunctionCall3(&outputproc,
687 Int32GetDatum(*(int32 *) p),
688 ObjectIdGetDatum(typelem),
696 values[i] = DatumGetCString(FunctionCall3(&outputproc,
698 ObjectIdGetDatum(typelem),
703 p += INTALIGN(*(int32 *) p);
706 * For the pair of double quotes
710 for (tmp = values[i]; *tmp; tmp++)
722 * count total number of curly braces in output string
724 for (i = j = 0, k = 1; i < ndim; k *= dim[i++], j += k);
726 p = (char *) palloc(overall_length + 2 * j);
730 for (i = 0; i < ndim; indx[i++] = 0);
735 for (i = j; i < ndim - 1; i++)
739 * Surround anything that is not passed by value in double quotes.
740 * See above for more details.
747 for (tmp = values[k]; *tmp; tmp++)
755 strcat(p, values[k]);
760 strcat(p, values[k]);
763 for (i = ndim - 1; i >= 0; i--)
765 indx[i] = (indx[i] + 1) % dim[i];
778 PG_RETURN_CSTRING(retval);
781 /*-----------------------------------------------------------------------------
783 * returns the dimensions of the array pointed to by "v", as a "text"
784 *----------------------------------------------------------------------------
787 array_dims(PG_FUNCTION_ARGS)
789 ArrayType *v = (ArrayType *) PG_GETARG_VARLENA_P(0);
797 nbytes = ARR_NDIM(v) * 33 + 1;
799 * 33 since we assume 15 digits per number + ':' +'[]'
801 * +1 allows for temp trailing null
804 result = (text *) palloc(nbytes + VARHDRSZ);
805 MemSet(result, 0, nbytes + VARHDRSZ);
811 for (i = 0; i < ARR_NDIM(v); i++)
813 sprintf(p, "[%d:%d]", lb[i], dimv[i] + lb[i] - 1);
816 VARSIZE(result) = strlen(VARDATA(result)) + VARHDRSZ;
818 PG_RETURN_TEXT_P(result);
821 /*---------------------------------------------------------------------------
823 * This routine takes an array pointer and an index array and returns
824 * a pointer to the referred element if element is passed by
825 * reference otherwise returns the value of the referred element.
826 *---------------------------------------------------------------------------
829 array_ref(ArrayType *array,
843 struct varlena *v = NULL;
847 if (array == (ArrayType *) NULL)
853 * fixed length arrays -- these are assumed to be 1-d
855 if (indx[0] * elmlen > arraylen)
856 elog(ERROR, "array_ref: array bound exceeded");
857 retval = (char *) array + indx[0] * elmlen;
858 return _ArrayCast(retval, elmbyval, elmlen);
860 dim = ARR_DIMS(array);
861 lb = ARR_LBOUND(array);
862 ndim = ARR_NDIM(array);
863 nbytes = (*(int32 *) array) - ARR_OVERHEAD(ndim);
865 if (!SanityCheckInput(ndim, nSubscripts, dim, lb, indx))
868 offset = GetOffset(nSubscripts, dim, lb, indx);
870 if (ARR_IS_LO(array))
875 /* We are assuming fixed element lengths here */
877 lo_name = (char *) ARR_DATA_PTR(array);
879 if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_READ : O_RDONLY)) < 0)
882 if (ARR_IS_CHUNKED(array))
883 v = _ReadChunkArray1El(indx, elmlen, fd, array, isNull);
886 if (DatumGetInt32(DirectFunctionCall3(lo_lseek,
888 Int32GetDatum(offset),
889 Int32GetDatum(SEEK_SET))) < 0)
892 v = (struct varlena *)
893 DatumGetPointer(DirectFunctionCall2(loread,
895 Int32GetDatum(elmlen)));
900 if (VARSIZE(v) - VARHDRSZ < elmlen)
902 DirectFunctionCall1(lo_close, Int32GetDatum(fd));
903 result = _ArrayCast((char *) VARDATA(v), elmbyval, elmlen);
906 char *tempdata = palloc(elmlen);
908 memmove(tempdata, DatumGetPointer(result), elmlen);
909 result = PointerGetDatum(tempdata);
917 offset = offset * elmlen;
918 /* off the end of the array */
919 if (nbytes - offset < 1)
921 retval = ARR_DATA_PTR(array) + offset;
922 return _ArrayCast(retval, elmbyval, elmlen);
928 retval = ARR_DATA_PTR(array);
933 return PointerGetDatum(retval);
934 bytes -= INTALIGN(*(int32 *) retval);
935 retval += INTALIGN(*(int32 *) retval);
942 /*-----------------------------------------------------------------------------
944 * This routine takes an array and a range of indices (upperIndex and
945 * lowerIndx), creates a new array structure for the referred elements
946 * and returns a pointer to it.
947 *-----------------------------------------------------------------------------
950 array_clip(ArrayType *array,
968 if (array == (ArrayType *) NULL)
969 RETURN_NULL(ArrayType *);
970 dim = ARR_DIMS(array);
971 lb = ARR_LBOUND(array);
972 ndim = ARR_NDIM(array);
973 nbytes = (*(int32 *) array) - ARR_OVERHEAD(ndim);
975 if (!SanityCheckInput(ndim, nSubscripts, dim, lb, upperIndx) ||
976 !SanityCheckInput(ndim, nSubscripts, dim, lb, lowerIndx))
977 RETURN_NULL(ArrayType *);
979 for (i = 0; i < nSubscripts; i++)
980 if (lowerIndx[i] > upperIndx[i])
981 elog(ERROR, "lowerIndex cannot be larger than upperIndx");
982 mda_get_range(nSubscripts, span, lowerIndx, upperIndx);
984 if (ARR_IS_LO(array))
990 char *newname = NULL;
997 elog(ERROR, "array_clip: array of variable length objects not implemented");
999 lo_name = (char *) ARR_DATA_PTR(array);
1000 if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_READ : O_RDONLY)) < 0)
1001 RETURN_NULL(ArrayType *);
1002 newname = _array_newLO(&newfd, Unix);
1004 bytes = strlen(newname) + 1 + ARR_OVERHEAD(nSubscripts);
1005 newArr = (ArrayType *) palloc(bytes);
1006 memmove(newArr, array, sizeof(ArrayType));
1007 memmove(newArr, &bytes, sizeof(int));
1008 memmove(ARR_DIMS(newArr), span, nSubscripts * sizeof(int));
1009 memmove(ARR_LBOUND(newArr), lowerIndx, nSubscripts * sizeof(int));
1010 strcpy(ARR_DATA_PTR(newArr), newname);
1012 rsize = compute_size(lowerIndx, upperIndx, nSubscripts, elmlen);
1013 if (rsize < MAX_BUFF_SIZE)
1018 buff = palloc(rsize);
1021 if (ARR_IS_CHUNKED(array))
1023 _ReadChunkArray(lowerIndx, upperIndx, elmlen, fd, &(buff[VARHDRSZ]),
1028 _ReadArray(lowerIndx, upperIndx, elmlen, fd, (int) &(buff[VARHDRSZ]),
1032 memmove(buff, &rsize, VARHDRSZ);
1035 bytes = DatumGetInt32(DirectFunctionCall2(lowrite,
1036 Int32GetDatum(newfd),
1037 PointerGetDatum(buff)));
1043 if (ARR_IS_CHUNKED(array))
1045 _ReadChunkArray(lowerIndx, upperIndx, elmlen, fd, (char *) newfd, array,
1049 _ReadArray(lowerIndx, upperIndx, elmlen, fd, newfd, array, 1, isNull);
1066 bytes = getNitems(nSubscripts, span);
1067 bytes = bytes * elmlen + ARR_OVERHEAD(nSubscripts);
1071 bytes = _ArrayClipCount(lowerIndx, upperIndx, array);
1072 bytes += ARR_OVERHEAD(nSubscripts);
1074 newArr = (ArrayType *) palloc(bytes);
1075 memmove(newArr, array, sizeof(ArrayType));
1076 memmove(newArr, &bytes, sizeof(int));
1077 memmove(ARR_DIMS(newArr), span, nSubscripts * sizeof(int));
1078 memmove(ARR_LBOUND(newArr), lowerIndx, nSubscripts * sizeof(int));
1079 _ArrayRange(lowerIndx, upperIndx, elmlen, ARR_DATA_PTR(newArr), array, 1);
1083 /*-----------------------------------------------------------------------------
1085 * This routine sets the value of an array location (specified by
1086 * an index array) to a new value specified by "dataValue".
1088 * returns a pointer to the modified array.
1089 *-----------------------------------------------------------------------------
1092 array_set(ArrayType *array,
1108 if (array == (ArrayType *) NULL)
1109 RETURN_NULL(ArrayType *);
1114 * fixed length arrays -- these are assumed to be 1-d
1116 if (indx[0] * elmlen > arraylen)
1117 elog(ERROR, "array_ref: array bound exceeded");
1118 pos = (char *) array + indx[0] * elmlen;
1119 ArrayCastAndSet(dataValue, elmbyval, elmlen, pos);
1122 dim = ARR_DIMS(array);
1123 lb = ARR_LBOUND(array);
1124 ndim = ARR_NDIM(array);
1125 nbytes = (*(int32 *) array) - ARR_OVERHEAD(ndim);
1127 if (!SanityCheckInput(ndim, nSubscripts, dim, lb, indx))
1129 elog(ERROR, "array_set: array bound exceeded");
1132 offset = GetOffset(nSubscripts, dim, lb, indx);
1134 if (ARR_IS_LO(array))
1139 /* We are assuming fixed element lengths here */
1144 lo_name = ARR_DATA_PTR(array);
1145 if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_WRITE : O_WRONLY)) < 0)
1148 if (DatumGetInt32(DirectFunctionCall3(lo_lseek,
1150 Int32GetDatum(offset),
1151 Int32GetDatum(SEEK_SET))) < 0)
1153 v = (struct varlena *) palloc(elmlen + VARHDRSZ);
1154 VARSIZE(v) = elmlen + VARHDRSZ;
1155 ArrayCastAndSet(dataValue, elmbyval, elmlen, VARDATA(v));
1157 if (DatumGetInt32(DirectFunctionCall2(lowrite,
1159 PointerGetDatum(v)))
1161 RETURN_NULL(ArrayType *);
1164 DirectFunctionCall1(lo_close, Int32GetDatum(fd));
1169 offset = offset * elmlen;
1170 /* off the end of the array */
1171 if (nbytes - offset < 1)
1173 pos = ARR_DATA_PTR(array) + offset;
1177 ArrayType *newarray;
1187 elt_ptr = array_seek(ARR_DATA_PTR(array), -1, offset);
1188 oldlen = INTALIGN(*(int32 *) elt_ptr);
1189 newlen = INTALIGN(*(int32 *) DatumGetPointer(dataValue));
1191 if (oldlen == newlen)
1193 /* new element with same size, overwrite old data */
1194 ArrayCastAndSet(dataValue, elmbyval, elmlen, elt_ptr);
1198 /* new element with different size, reallocate the array */
1199 oldsize = array->size;
1200 lth0 = ARR_OVERHEAD(nSubscripts);
1201 lth1 = (int) (elt_ptr - ARR_DATA_PTR(array));
1202 lth2 = (int) (oldsize - lth0 - lth1 - oldlen);
1203 newsize = lth0 + lth1 + newlen + lth2;
1205 newarray = (ArrayType *) palloc(newsize);
1206 memmove((char *) newarray, (char *) array, lth0 + lth1);
1207 newarray->size = newsize;
1208 newlen = ArrayCastAndSet(dataValue, elmbyval, elmlen,
1209 (char *) newarray + lth0 + lth1);
1210 memmove((char *) newarray + lth0 + lth1 + newlen,
1211 (char *) array + lth0 + lth1 + oldlen, lth2);
1213 /* ??? who should free this storage ??? */
1216 ArrayCastAndSet(dataValue, elmbyval, elmlen, pos);
1220 /*----------------------------------------------------------------------------
1222 * This routine sets the value of a range of array locations (specified
1223 * by upper and lower index values ) to new values passed as
1226 * returns a pointer to the modified array.
1227 *----------------------------------------------------------------------------
1230 array_assgn(ArrayType *array,
1244 if (array == (ArrayType *) NULL)
1245 RETURN_NULL(ArrayType *);
1247 elog(ERROR, "array_assgn: updates on arrays of variable length elements not implemented");
1249 dim = ARR_DIMS(array);
1250 lb = ARR_LBOUND(array);
1251 ndim = ARR_NDIM(array);
1253 if (!SanityCheckInput(ndim, nSubscripts, dim, lb, upperIndx) ||
1254 !SanityCheckInput(ndim, nSubscripts, dim, lb, lowerIndx))
1255 RETURN_NULL(ArrayType *);
1257 for (i = 0; i < nSubscripts; i++)
1258 if (lowerIndx[i] > upperIndx[i])
1259 elog(ERROR, "lowerIndex larger than upperIndx");
1261 if (ARR_IS_LO(array))
1269 lo_name = (char *) ARR_DATA_PTR(array);
1270 if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_WRITE : O_WRONLY)) < 0)
1273 if (ARR_IS_LO(newArr))
1276 lo_name = (char *) ARR_DATA_PTR(newArr);
1277 if ((newfd = LOopen(lo_name, ARR_IS_INV(newArr) ? INV_READ : O_RDONLY)) < 0)
1280 _LOArrayRange(lowerIndx, upperIndx, elmlen, fd, newfd, array, 1, isNull);
1281 DirectFunctionCall1(lo_close, Int32GetDatum(newfd));
1285 _LOArrayRange(lowerIndx, upperIndx, elmlen, fd, (int) ARR_DATA_PTR(newArr),
1288 DirectFunctionCall1(lo_close, Int32GetDatum(fd));
1291 _ArrayRange(lowerIndx, upperIndx, elmlen, ARR_DATA_PTR(newArr), array, 0);
1298 * Map an array through an arbitrary function. Return a new array with
1299 * same dimensions and each source element transformed by fn(). Each
1300 * source element is passed as the first argument to fn(); additional
1301 * arguments to be passed to fn() can be specified by the caller.
1302 * The output array can have a different element type than the input.
1305 * * fcinfo: a function-call data structure pre-constructed by the caller
1306 * to be ready to call the desired function, with everything except the
1307 * first argument position filled in. In particular, flinfo identifies
1308 * the function fn(), and if nargs > 1 then argument positions after the
1309 * first must be preset to the additional values to be passed. The
1310 * first argument position initially holds the input array value.
1311 * * inpType: OID of element type of input array. This must be the same as,
1312 * or binary-compatible with, the first argument type of fn().
1313 * * retType: OID of element type of output array. This must be the same as,
1314 * or binary-compatible with, the result type of fn().
1316 * NB: caller must assure that input array is not NULL. Currently,
1317 * any additional parameters passed to fn() may not be specified as NULL
1321 array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType)
1343 /* Get input array */
1344 if (fcinfo->nargs < 1)
1345 elog(ERROR, "array_map: invalid nargs: %d", fcinfo->nargs);
1346 if (PG_ARGISNULL(0))
1347 elog(ERROR, "array_map: null input array");
1348 v = (ArrayType *) PG_GETARG_VARLENA_P(0);
1350 /* Large objects not yet supported */
1351 if (ARR_IS_LO(v) == true)
1352 elog(ERROR, "array_map: large objects not supported");
1356 nitems = getNitems(ndim, dim);
1358 /* Check for empty array */
1360 PG_RETURN_POINTER(v);
1362 /* Lookup source and result types. Unneeded variables are reused. */
1363 system_cache_lookup(inpType, false, &inp_typlen, &inp_typbyval,
1364 &typdelim, &typelem, &proc, &typalign);
1365 system_cache_lookup(retType, false, &typlen, &typbyval,
1366 &typdelim, &typelem, &proc, &typalign);
1368 /* Allocate temporary array for new values */
1369 values = (char **) palloc(nitems * sizeof(char *));
1370 MemSet(values, 0, nitems * sizeof(char *));
1372 /* Loop over source data */
1373 s = (char *) ARR_DATA_PTR(v);
1374 for (i = 0; i < nitems; i++)
1376 /* Get source element */
1382 elt = (char *) ((int) (*(char *) s));
1385 elt = (char *) ((int) (*(int16 *) s));
1390 elt = (char *) (*(int32 *) s);
1401 s += INTALIGN(*(int32 *) s);
1405 * Apply the given function to source elt and extra args.
1407 * We assume the extra args are non-NULL, so need not check
1408 * whether fn() is strict. Would need to do more work here
1409 * to support arrays containing nulls, too.
1411 fcinfo->arg[0] = (Datum) elt;
1412 fcinfo->argnull[0] = false;
1413 fcinfo->isnull = false;
1414 p = (char *) FunctionCallInvoke(fcinfo);
1416 elog(ERROR, "array_map: cannot handle NULL in array");
1418 /* Update values and total result size */
1428 len = ((typlen > 0) ? typlen : INTALIGN(*(int32 *) p));
1429 /* Needed because _CopyArrayEls tries to pfree items */
1432 p = (char *) palloc(len);
1433 memcpy(p, elt, len);
1440 /* Allocate and initialize the result array */
1441 nbytes += ARR_OVERHEAD(ndim);
1442 result = (ArrayType *) palloc(nbytes);
1443 MemSet(result, 0, nbytes);
1445 memcpy((char *) result, (char *) &nbytes, sizeof(int));
1446 memcpy((char *) ARR_NDIM_PTR(result), (char *) &ndim, sizeof(int));
1447 memcpy((char *) ARR_DIMS(result), ARR_DIMS(v), 2 * ndim * sizeof(int));
1449 /* Copy new values into the result array. values is pfreed. */
1450 _CopyArrayEls((char **) values,
1451 ARR_DATA_PTR(result), nitems,
1452 typlen, typalign, typbyval);
1454 PG_RETURN_POINTER(result);
1457 /*-----------------------------------------------------------------------------
1459 * compares two arrays for equality
1461 * returns true if the arrays are equal, false otherwise.
1462 *-----------------------------------------------------------------------------
1465 array_eq(PG_FUNCTION_ARGS)
1467 ArrayType *array1 = (ArrayType *) PG_GETARG_VARLENA_P(0);
1468 ArrayType *array2 = (ArrayType *) PG_GETARG_VARLENA_P(1);
1470 if (*(int32 *) array1 != *(int32 *) array2)
1471 PG_RETURN_BOOL(false);
1472 if (memcmp(array1, array2, *(int32 *) array1) != 0)
1473 PG_RETURN_BOOL(false);
1474 PG_RETURN_BOOL(true);
1477 /***************************************************************************/
1478 /******************| Support Routines |*****************/
1479 /***************************************************************************/
1481 system_cache_lookup(Oid element_type,
1490 HeapTuple typeTuple;
1491 Form_pg_type typeStruct;
1493 typeTuple = SearchSysCacheTuple(TYPEOID,
1494 ObjectIdGetDatum(element_type),
1497 if (!HeapTupleIsValid(typeTuple))
1499 elog(ERROR, "array_out: Cache lookup failed for type %u\n",
1503 typeStruct = (Form_pg_type) GETSTRUCT(typeTuple);
1504 *typlen = typeStruct->typlen;
1505 *typbyval = typeStruct->typbyval;
1506 *typdelim = typeStruct->typdelim;
1507 *typelem = typeStruct->typelem;
1508 *typalign = typeStruct->typalign;
1510 *proc = typeStruct->typinput;
1512 *proc = typeStruct->typoutput;
1516 _ArrayCast(char *value, bool byval, int len)
1523 return (Datum) *value;
1525 return (Datum) *(int16 *) value;
1528 return (Datum) *(int32 *) value;
1530 elog(ERROR, "array_ref: byval and elt len > 4!");
1535 return (Datum) value;
1541 ArrayCastAndSet(Datum src,
1555 *dest = DatumGetChar(src);
1558 *(int16 *) dest = DatumGetInt16(src);
1561 *(int32 *) dest = DatumGetInt32(src);
1566 memmove(dest, DatumGetPointer(src), typlen);
1571 memmove(dest, DatumGetPointer(src), *(int32 *) DatumGetPointer(src));
1572 inc = (INTALIGN(*(int32 *) DatumGetPointer(src)));
1579 _AdvanceBy1word(char *str, char **word)
1587 while (isspace(*str))
1590 if ((space = (char *) strchr(str, ' ')) != (char *) NULL)
1603 SanityCheckInput(int ndim, int n, int *dim, int *lb, int *indx)
1607 /* Do Sanity check on input */
1610 for (i = 0; i < ndim; i++)
1611 if ((lb[i] > indx[i]) || (indx[i] >= (dim[i] + lb[i])))
1617 _ArrayRange(int *st,
1637 n = ARR_NDIM(array);
1638 dim = ARR_DIMS(array);
1639 lb = ARR_LBOUND(array);
1640 srcPtr = ARR_DATA_PTR(array);
1641 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++);
1642 mda_get_prod(n, dim, prod);
1643 st_pos = tuple2linear(n, st, prod);
1644 srcPtr = array_seek(srcPtr, bsize, st_pos);
1645 mda_get_range(n, span, st, endp);
1646 mda_get_offset_values(n, dist, prod, span);
1647 for (i = 0; i < n; indx[i++] = 0);
1652 srcPtr = array_seek(srcPtr, bsize, dist[j]);
1654 inc = array_read(destPtr, bsize, 1, srcPtr);
1656 inc = array_read(srcPtr, bsize, 1, destPtr);
1659 } while ((j = next_tuple(i + 1, indx, span)) != -1);
1663 _ArrayClipCount(int *stI, int *endpI, ArrayType *array)
1681 n = ARR_NDIM(array);
1682 dim = ARR_DIMS(array);
1683 lb = ARR_LBOUND(array);
1684 ptr = ARR_DATA_PTR(array);
1685 for (i = 0; i < n; st[i] = stI[i] - lb[i], endp[i] = endpI[i] - lb[i], i++);
1686 mda_get_prod(n, dim, prod);
1687 st_pos = tuple2linear(n, st, prod);
1688 ptr = array_seek(ptr, -1, st_pos);
1689 mda_get_range(n, span, st, endp);
1690 mda_get_offset_values(n, dist, prod, span);
1691 for (i = 0; i < n; indx[i++] = 0);
1695 ptr = array_seek(ptr, -1, dist[j]);
1696 inc = INTALIGN(*(int32 *) ptr);
1699 } while ((j = next_tuple(i + 1, indx, span)) != -1);
1704 array_seek(char *ptr, int eltsize, int nitems)
1709 return ptr + eltsize * nitems;
1710 for (i = 0; i < nitems; i++)
1711 ptr += INTALIGN(*(int32 *) ptr);
1716 array_read(char *destptr, int eltsize, int nitems, char *srcptr)
1724 memmove(destptr, srcptr, eltsize * nitems);
1725 return eltsize * nitems;
1727 for (i = inc = 0; i < nitems; i++)
1729 tmp = (INTALIGN(*(int32 *) srcptr));
1730 memmove(destptr, srcptr, tmp);
1739 _LOArrayRange(int *st,
1762 n = ARR_NDIM(array);
1763 dim = ARR_DIMS(array);
1764 lb = ARR_LBOUND(array);
1765 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++);
1767 mda_get_prod(n, dim, prod);
1768 st_pos = tuple2linear(n, st, prod);
1769 offset = st_pos * bsize;
1770 if (DatumGetInt32(DirectFunctionCall3(lo_lseek,
1771 Int32GetDatum(srcfd),
1772 Int32GetDatum(offset),
1773 Int32GetDatum(SEEK_SET))) < 0)
1775 mda_get_range(n, span, st, endp);
1776 mda_get_offset_values(n, dist, prod, span);
1777 for (i = 0; i < n; indx[i++] = 0);
1778 for (i = n - 1, inc = bsize; i >= 0; inc *= span[i--])
1784 offset += (dist[j] * bsize);
1785 if (DatumGetInt32(DirectFunctionCall3(lo_lseek,
1786 Int32GetDatum(srcfd),
1787 Int32GetDatum(offset),
1788 Int32GetDatum(SEEK_SET))) < 0)
1790 tmp = _LOtransfer((char **) &srcfd, inc, 1, (char **) &destfd, isSrcLO, 1);
1794 } while ((j = next_tuple(i + 1, indx, span)) != -1);
1822 n = ARR_NDIM(array);
1823 dim = ARR_DIMS(array);
1824 lb = ARR_LBOUND(array);
1825 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++);
1827 mda_get_prod(n, dim, prod);
1828 st_pos = tuple2linear(n, st, prod);
1829 offset = st_pos * bsize;
1830 if (DatumGetInt32(DirectFunctionCall3(lo_lseek,
1831 Int32GetDatum(srcfd),
1832 Int32GetDatum(offset),
1833 Int32GetDatum(SEEK_SET))) < 0)
1835 mda_get_range(n, span, st, endp);
1836 mda_get_offset_values(n, dist, prod, span);
1837 for (i = 0; i < n; indx[i++] = 0);
1838 for (i = n - 1, inc = bsize; i >= 0; inc *= span[i--])
1844 offset += (dist[j] * bsize);
1845 if (DatumGetInt32(DirectFunctionCall3(lo_lseek,
1846 Int32GetDatum(srcfd),
1847 Int32GetDatum(offset),
1848 Int32GetDatum(SEEK_SET))) < 0)
1850 tmp = _LOtransfer((char **) &destfd, inc, 1, (char **) &srcfd, 1, isDestLO);
1854 } while ((j = next_tuple(i + 1, indx, span)) != -1);
1859 _LOtransfer(char **destfd,
1866 #define MAX_READ (512 * 1024)
1868 #define min(a, b) (a < b ? a : b)
1870 struct varlena *v = NULL;
1875 inc = nitems * size;
1876 if (isSrcLO && isDestLO && inc > 0)
1877 for (tmp = 0, resid = inc;
1878 resid > 0 && (inc = min(resid, MAX_READ)) > 0; resid -= inc)
1881 v = (struct varlena *)
1882 DatumGetPointer(DirectFunctionCall2(loread,
1883 Int32GetDatum((int32) *srcfd),
1884 Int32GetDatum(inc)));
1885 if (VARSIZE(v) - VARHDRSZ < inc)
1890 tmp += DatumGetInt32(DirectFunctionCall2(lowrite,
1891 Int32GetDatum((int32) *destfd),
1892 PointerGetDatum(v)));
1897 else if (!isSrcLO && isDestLO)
1899 tmp = lo_write((int) *destfd, *srcfd, inc);
1900 *srcfd = *srcfd + tmp;
1902 else if (isSrcLO && !isDestLO)
1904 tmp = lo_read((int) *srcfd, *destfd, inc);
1905 *destfd = *destfd + tmp;
1909 memmove(*destfd, *srcfd, inc);
1919 _array_newLO(int *fd, int flag)
1922 char saveName[NAME_LEN];
1924 p = (char *) palloc(NAME_LEN);
1925 sprintf(p, "/Arry.%u", newoid());
1926 strcpy(saveName, p);
1928 if ((*fd = LOcreat(saveName, 0600, flag)) < 0)
1929 elog(ERROR, "Large object create failed");