1 /*-------------------------------------------------------------------------
4 * Special functions for arrays.
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.24 1998/01/15 19:45:01 pgsql Exp $
12 *-------------------------------------------------------------------------
21 #include "catalog/catalog.h"
22 #include "catalog/pg_type.h"
24 #include "utils/syscache.h"
25 #include "utils/memutils.h"
26 #include "storage/fd.h"
28 #include "utils/array.h"
30 #include "libpq/libpq-fs.h"
31 #include "libpq/be-fsstubs.h"
35 /* An array has the following internal structure:
36 * <nbytes> - total number of bytes
37 * <ndim> - number of dimensions of the array
38 * <flags> - bit mask of flags
39 * <dim> - size of each array axis
40 * <dim_lower> - lower boundary of each dimension
41 * <actual data> - whatever is the stored data
44 /*-=-=--=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-*/
45 static int _ArrayCount(char *str, int dim[], int typdelim);
47 _ReadArrayStr(char *arrayStr, int nitems, int ndim, int dim[],
48 FmgrInfo *inputproc, Oid typelem, char typdelim,
49 int typlen, bool typbyval, char typalign,
54 _ReadLOArray(char *str, int *nbytes, int *fd, bool *chunkFlag,
55 int ndim, int dim[], int baseSize);
59 _CopyArrayEls(char **values, char *p, int nitems, int typlen,
60 char typalign, bool typbyval);
62 system_cache_lookup(Oid element_type, bool input, int *typlen,
63 bool *typbyval, char *typdelim, Oid *typelem, Oid *proc,
65 static Datum _ArrayCast(char *value, bool byval, int len);
68 static char *_AdvanceBy1word(char *str, char **word);
72 _ArrayRange(int st[], int endp[], int bsize, char *destPtr,
73 ArrayType *array, int from);
74 static int _ArrayClipCount(int stI[], int endpI[], ArrayType *array);
76 _LOArrayRange(int st[], int endp[], int bsize, int srcfd,
77 int destfd, ArrayType *array, int isSrcLO, bool *isNull);
79 _ReadArray(int st[], int endp[], int bsize, int srcfd, int destfd,
80 ArrayType *array, int isDestLO, bool *isNull);
81 static ArrayCastAndSet(char *src, bool typbyval, int typlen, char *dest);
82 static SanityCheckInput(int ndim, int n, int dim[], int lb[], int indx[]);
83 static int array_read(char *destptr, int eltsize, int nitems, char *srcptr);
84 static char *array_seek(char *ptr, int eltsize, int nitems);
86 /*---------------------------------------------------------------------
88 * converts an array from the external format in "string" to
91 * the internal representation of the input array
92 *--------------------------------------------------------------------
95 array_in(char *string, /* input array in external form */
96 Oid element_type) /* type OID of an array element */
113 ArrayType *retval = NULL;
119 system_cache_lookup(element_type, true, &typlen, &typbyval, &typdelim,
120 &typelem, &typinput, &typalign);
122 fmgr_info(typinput, &inputproc);
124 string_save = (char *) palloc(strlen(string) + 3);
125 strcpy(string_save, string);
127 /* --- read array dimensions ---------- */
130 for (ndim = 0; !done;)
137 if ((r = (char *) strchr(p, ':')) == (char *) NULL)
142 lBound[ndim] = atoi(p);
145 for (q = p; isdigit(*q); q++);
147 elog(ERROR, "array_in: missing ']' in array declaration");
150 if ((dim[ndim] < 0) || (lBound[ndim] < 0))
151 elog(ERROR, "array_in: array dimensions need to be positive");
152 dim[ndim] = dim[ndim] - lBound[ndim] + 1;
154 elog(ERROR, "array_in: upper_bound cannot be < lower_bound");
168 ndim = _ArrayCount(p, dim, typdelim);
169 for (i = 0; i < ndim; lBound[i++] = 1);
173 elog(ERROR, "array_in: Need to specify dimension");
180 if (strncmp(p, ASSGN, strlen(ASSGN)))
181 elog(ERROR, "array_in: missing assignment operator");
188 printf("array_in- ndim %d (", ndim);
189 for (i = 0; i < ndim; i++)
191 printf(" %d", dim[i]);
193 printf(") for %s\n", string);
196 nitems = getNitems(ndim, dim);
199 char *emptyArray = palloc(sizeof(ArrayType));
201 MemSet(emptyArray, 0, sizeof(ArrayType));
202 *(int32 *) emptyArray = sizeof(ArrayType);
208 /* array not a large object */
210 (char *) _ReadArrayStr(p, nitems, ndim, dim, &inputproc, typelem,
211 typdelim, typlen, typbyval, typalign,
213 nbytes += ARR_OVERHEAD(ndim);
214 retval = (ArrayType *) palloc(nbytes);
215 MemSet(retval, 0, nbytes);
216 memmove(retval, (char *) &nbytes, sizeof(int));
217 memmove((char *) ARR_NDIM_PTR(retval), (char *) &ndim, sizeof(int));
218 SET_LO_FLAG(false, retval);
219 memmove((char *) ARR_DIMS(retval), (char *) dim, ndim * sizeof(int));
220 memmove((char *) ARR_LBOUND(retval), (char *) lBound,
224 * dataPtr is an array of arbitraystuff even though its type is
225 * char* cast to char** to pass to _CopyArrayEls for now - jolly
227 _CopyArrayEls((char **) dataPtr,
228 ARR_DATA_PTR(retval), nitems,
229 typlen, typalign, typbyval);
236 bool chunked = false;
238 dataPtr = _ReadLOArray(p, &bytes, &dummy, &chunked, ndim,
240 nbytes = bytes + ARR_OVERHEAD(ndim);
241 retval = (ArrayType *) palloc(nbytes);
242 MemSet(retval, 0, nbytes);
243 memmove(retval, (char *) &nbytes, sizeof(int));
244 memmove((char *) ARR_NDIM_PTR(retval), (char *) &ndim, sizeof(int));
245 SET_LO_FLAG(true, retval);
246 SET_CHUNK_FLAG(chunked, retval);
247 memmove((char *) ARR_DIMS(retval), (char *) dim, ndim * sizeof(int));
248 memmove((char *) ARR_LBOUND(retval), (char *) lBound, ndim * sizeof(int));
249 memmove(ARR_DATA_PTR(retval), dataPtr, bytes);
251 elog(ERROR, "large object arrays not supported");
254 return ((char *) retval);
257 /*-----------------------------------------------------------------------------
259 * Counts the number of dimensions and the dim[] array for an array string.
260 * The syntax for array input is C-like nested curly braces
261 *-----------------------------------------------------------------------------
264 _ArrayCount(char *str, int dim[], int typdelim)
270 bool scanning_string = false;
271 bool eoArray = false;
274 for (i = 0; i < MAXDIM; ++i)
276 temp[i] = dim[i] = 0;
279 if (strncmp(str, "{}", 2) == 0)
283 while (eoArray != true)
292 /* skip escaped characters (\ and ") inside strings */
293 if (scanning_string && *(q + 1))
301 * Signal a premature end of the string. DZ -
304 elog(ERROR, "malformed array constant: %s", str);
307 scanning_string = !scanning_string;
310 if (!scanning_string)
312 temp[nest_level] = 0;
317 if (!scanning_string)
323 temp[nest_level - 1]++;
325 eoArray = done = true;
331 if (*q == typdelim && !scanning_string)
344 for (i = 0; i < ndim; ++i)
352 /*---------------------------------------------------------------------------
354 * parses the array string pointed by "arrayStr" and converts it in the
355 * internal format. The external format expected is like C array
356 * declaration. Unspecified elements are initialized to zero for fixed length
357 * base types and to empty varlena structures for variable length base
360 * returns the internal representation of the array elements
361 * nbytes is set to the size of the array in its internal representation.
362 *---------------------------------------------------------------------------
365 _ReadArrayStr(char *arrayStr,
369 FmgrInfo *inputproc, /* function used for the
384 bool scanning_string = false;
387 bool eoArray = false;
389 mda_get_prod(ndim, dim, prod);
390 for (i = 0; i < ndim; indx[i++] = 0);
391 /* read array enclosed within {} */
392 values = (char **) palloc(nitems * sizeof(char *));
393 MemSet(values, 0, nitems * sizeof(char *));
406 /* Crunch the string on top of the backslash. */
407 for (r = q; *r != '\0'; r++)
411 if (!scanning_string)
415 p++; /* get p past first doublequote */
419 scanning_string = !scanning_string;
422 if (!scanning_string)
426 if (nest_level > ndim)
427 elog(ERROR, "array_in: illformed array constant");
428 indx[nest_level - 1] = 0;
433 if (!scanning_string)
436 i = tuple2linear(ndim, indx, prod);
439 eoArray = done = true;
443 indx[nest_level - 1]++;
448 if (*q == typdelim && !scanning_string)
451 i = tuple2linear(ndim, indx, prod);
462 elog(ERROR, "array_in: illformed array constant");
463 values[i] = (*fmgr_faddr(inputproc)) (p, typelem);
468 * if not at the end of the array skip white space
478 *nbytes = nitems * typlen;
480 for (i = 0; i < nitems; i++)
483 values[i] = palloc(typlen);
484 MemSet(values[i], 0, typlen);
489 for (i = 0, *nbytes = 0; i < nitems; i++)
495 *nbytes += DOUBLEALIGN(*(int32 *) values[i]);
499 *nbytes += INTALIGN(*(int32 *) values[i]);
504 *nbytes += sizeof(int32);
505 values[i] = palloc(sizeof(int32));
506 *(int32 *) values[i] = sizeof(int32);
510 return ((char *) values);
514 /*----------------------------------------------------------------------------
515 * Read data about an array to be stored as a large object
516 *----------------------------------------------------------------------------
520 _ReadLOArray(char *str,
535 str = _AdvanceBy1word(str, &inputfile);
541 str = _AdvanceBy1word(str, &word);
543 if (!strcmp(word, "-chunk"))
546 elog(ERROR, "array_in: access pattern file required");
547 str = _AdvanceBy1word(str, &accessfile);
549 else if (!strcmp(word, "-noreorg"))
552 elog(ERROR, "array_in: chunk file required");
553 str = _AdvanceBy1word(str, &chunkfile);
557 elog(ERROR, "usage: <input file> -chunk DEFAULT/<access pattern file> -invert/-native [-noreorg <chunk file>]");
561 if (inputfile == NULL)
562 elog(ERROR, "array_in: missing file name");
563 lobjId = lo_creat(0);
564 *fd = lo_open(lobjId, INV_READ);
566 elog(ERROR, "Large object create failed");
568 *nbytes = strlen(retStr) + 2;
574 if ((afd = AllocateFile(accessfile, "r")) == NULL)
575 elog(ERROR, "unable to open access pattern file");
577 retStr = _ChunkArray(*fd, afd, ndim, dim, baseSize, nbytes,
587 _CopyArrayEls(char **values,
596 for (i = 0; i < nitems; i++)
600 inc = ArrayCastAndSet(values[i], typbyval, typlen, p);
608 /*-------------------------------------------------------------------------
610 * takes the internal representation of an array and returns a string
611 * containing the array in its external format.
612 *-------------------------------------------------------------------------
615 array_out(ArrayType *v, Oid element_type)
639 if (v == (ArrayType *) NULL)
640 return ((char *) NULL);
642 if (ARR_IS_LO(v) == true)
648 /* get a wide string to print to */
649 p = array_dims(v, &dummy_bool);
650 nbytes = strlen(ARR_DATA_PTR(v)) + VARHDRSZ + *(int *) p;
652 save_p = (char *) palloc(nbytes);
654 strcpy(save_p, p + sizeof(int));
655 strcat(save_p, ASSGN);
656 strcat(save_p, ARR_DATA_PTR(v));
661 system_cache_lookup(element_type, false, &typlen, &typbyval,
662 &typdelim, &typelem, &typoutput, &typalign);
663 fmgr_info(typoutput, &outputproc);
664 sprintf(delim, "%c", typdelim);
667 nitems = getNitems(ndim, dim);
671 char *emptyArray = palloc(3);
675 emptyArray[2] = '\0';
680 overall_length = 1; /* [TRH] don't forget to count \0 at end. */
681 values = (char **) palloc(nitems * sizeof(char *));
682 for (i = 0; i < nitems; i++)
689 values[i] = (*fmgr_faddr(&outputproc)) (*p, typelem);
692 values[i] = (*fmgr_faddr(&outputproc)) (*(int16 *) p, typelem);
696 values[i] = (*fmgr_faddr(&outputproc)) (*(int32 *) p, typelem);
703 values[i] = (*fmgr_faddr(&outputproc)) (p, typelem);
707 p += INTALIGN(*(int32 *) p);
710 * For the pair of double quotes
714 overall_length += (strlen(values[i]) + 1);
718 * count total number of curly braces in output string
720 for (i = j = 0, k = 1; i < ndim; k *= dim[i++], j += k);
722 p = (char *) palloc(overall_length + 2 * j);
726 for (i = 0; i < ndim; indx[i++] = 0);
731 for (i = j; i < ndim - 1; i++)
735 * Surround anything that is not passed by value in double quotes.
736 * See above for more details.
741 strcat(p, values[k]);
745 strcat(p, values[k]);
748 for (i = ndim - 1; i >= 0; i--)
750 indx[i] = (indx[i] + 1) % dim[i];
766 /*-----------------------------------------------------------------------------
768 * returns the dimension of the array pointed to by "v"
769 *----------------------------------------------------------------------------
772 array_dims(ArrayType *v, bool *isNull)
781 if (v == (ArrayType *) NULL)
783 nbytes = ARR_NDIM(v) * 33;
786 * 33 since we assume 15 digits per number + ':' +'[]'
788 save_p = p = (char *) palloc(nbytes + VARHDRSZ);
789 MemSet(save_p, 0, nbytes + VARHDRSZ);
793 for (i = 0; i < ARR_NDIM(v); i++)
795 sprintf(p, "[%d:%d]", lb[i], dimv[i] + lb[i] - 1);
798 nbytes = strlen(save_p + VARHDRSZ) + VARHDRSZ;
799 memmove(save_p, &nbytes, VARHDRSZ);
803 /*---------------------------------------------------------------------------
805 * This routing takes an array pointer and an index array and returns
806 * a pointer to the referred element if element is passed by
807 * reference otherwise returns the value of the referred element.
808 *---------------------------------------------------------------------------
811 array_ref(ArrayType *array,
825 struct varlena *v = NULL;
828 if (array == (ArrayType *) NULL)
834 * fixed length arrays -- these are assumed to be 1-d
836 if (indx[0] * elmlen > arraylen)
837 elog(ERROR, "array_ref: array bound exceeded");
838 retval = (char *) array + indx[0] * elmlen;
839 return _ArrayCast(retval, reftype, elmlen);
841 dim = ARR_DIMS(array);
842 lb = ARR_LBOUND(array);
843 ndim = ARR_NDIM(array);
844 nbytes = (*(int32 *) array) - ARR_OVERHEAD(ndim);
846 if (!SanityCheckInput(ndim, n, dim, lb, indx))
849 offset = GetOffset(n, dim, lb, indx);
851 if (ARR_IS_LO(array))
856 /* We are assuming fixed element lengths here */
858 lo_name = (char *) ARR_DATA_PTR(array);
860 if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_READ : O_RDONLY)) < 0)
863 if (ARR_IS_CHUNKED(array))
864 v = _ReadChunkArray1El(indx, elmlen, fd, array, isNull);
867 if (lo_lseek(fd, offset, SEEK_SET) < 0)
870 v = (struct varlena *) LOread(fd, elmlen);
875 if (VARSIZE(v) - VARHDRSZ < elmlen)
878 retval = (char *) _ArrayCast((char *) VARDATA(v), reftype, elmlen);
881 char *tempdata = palloc(elmlen);
883 memmove(tempdata, retval, elmlen);
887 return (Datum) retval;
892 offset = offset * elmlen;
893 /* off the end of the array */
894 if (nbytes - offset < 1)
896 retval = ARR_DATA_PTR(array) + offset;
897 return _ArrayCast(retval, reftype, elmlen);
905 temp = ARR_DATA_PTR(array);
907 while (bytes > 0 && !done)
914 bytes -= INTALIGN(*(int32 *) temp);
915 temp += INTALIGN(*(int32 *) temp);
920 return (Datum) retval;
924 /*-----------------------------------------------------------------------------
926 * This routine takes an array and a range of indices (upperIndex and
927 * lowerIndx), creates a new array structure for the referred elements
928 * and returns a pointer to it.
929 *-----------------------------------------------------------------------------
932 array_clip(ArrayType *array,
950 if (array == (ArrayType *) NULL)
952 dim = ARR_DIMS(array);
953 lb = ARR_LBOUND(array);
954 ndim = ARR_NDIM(array);
955 nbytes = (*(int32 *) array) - ARR_OVERHEAD(ndim);
957 if (!SanityCheckInput(ndim, n, dim, lb, upperIndx))
960 if (!SanityCheckInput(ndim, n, dim, lb, lowerIndx))
963 for (i = 0; i < n; i++)
964 if (lowerIndx[i] > upperIndx[i])
965 elog(ERROR, "lowerIndex cannot be larger than upperIndx");
966 mda_get_range(n, span, lowerIndx, upperIndx);
968 if (ARR_IS_LO(array))
974 char *newname = NULL;
981 elog(ERROR, "array_clip: array of variable length objects not supported");
983 lo_name = (char *) ARR_DATA_PTR(array);
984 if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_READ : O_RDONLY)) < 0)
986 newname = _array_newLO(&newfd, Unix);
988 bytes = strlen(newname) + 1 + ARR_OVERHEAD(n);
989 newArr = (ArrayType *) palloc(bytes);
990 memmove(newArr, array, sizeof(ArrayType));
991 memmove(newArr, &bytes, sizeof(int));
992 memmove(ARR_DIMS(newArr), span, n * sizeof(int));
993 memmove(ARR_LBOUND(newArr), lowerIndx, n * sizeof(int));
994 strcpy(ARR_DATA_PTR(newArr), newname);
996 rsize = compute_size(lowerIndx, upperIndx, n, len);
997 if (rsize < MAX_BUFF_SIZE)
1002 buff = palloc(rsize);
1005 if (ARR_IS_CHUNKED(array))
1007 _ReadChunkArray(lowerIndx, upperIndx, len, fd, &(buff[VARHDRSZ]),
1012 _ReadArray(lowerIndx, upperIndx, len, fd, (int) &(buff[VARHDRSZ]),
1016 memmove(buff, &rsize, VARHDRSZ);
1019 bytes = LOwrite(newfd, (struct varlena *) buff);
1024 if (ARR_IS_CHUNKED(array))
1026 _ReadChunkArray(lowerIndx, upperIndx, len, fd, (char *) newfd, array,
1031 _ReadArray(lowerIndx, upperIndx, len, fd, newfd, array, 1, isNull);
1043 return ((Datum) newArr);
1048 bytes = getNitems(n, span);
1049 bytes = bytes * len + ARR_OVERHEAD(n);
1053 bytes = _ArrayClipCount(lowerIndx, upperIndx, array);
1054 bytes += ARR_OVERHEAD(n);
1056 newArr = (ArrayType *) palloc(bytes);
1057 memmove(newArr, array, sizeof(ArrayType));
1058 memmove(newArr, &bytes, sizeof(int));
1059 memmove(ARR_DIMS(newArr), span, n * sizeof(int));
1060 memmove(ARR_LBOUND(newArr), lowerIndx, n * sizeof(int));
1061 _ArrayRange(lowerIndx, upperIndx, len, ARR_DATA_PTR(newArr), array, 1);
1062 return (Datum) newArr;
1065 /*-----------------------------------------------------------------------------
1067 * This routine sets the value of an array location (specified by an index array)
1068 * to a new value specified by "dataPtr".
1070 * returns a pointer to the modified array.
1071 *-----------------------------------------------------------------------------
1074 array_set(ArrayType *array,
1090 if (array == (ArrayType *) NULL)
1096 * fixed length arrays -- these are assumed to be 1-d
1098 if (indx[0] * elmlen > arraylen)
1099 elog(ERROR, "array_ref: array bound exceeded");
1100 pos = (char *) array + indx[0] * elmlen;
1101 ArrayCastAndSet(dataPtr, (bool) reftype, elmlen, pos);
1102 return ((char *) array);
1104 dim = ARR_DIMS(array);
1105 lb = ARR_LBOUND(array);
1106 ndim = ARR_NDIM(array);
1107 nbytes = (*(int32 *) array) - ARR_OVERHEAD(ndim);
1109 if (!SanityCheckInput(ndim, n, dim, lb, indx))
1111 elog(ERROR, "array_set: array bound exceeded");
1112 return ((char *) array);
1114 offset = GetOffset(n, dim, lb, indx);
1116 if (ARR_IS_LO(array))
1121 /* We are assuming fixed element lengths here */
1126 lo_name = ARR_DATA_PTR(array);
1127 if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_WRITE : O_WRONLY)) < 0)
1128 return ((char *) array);
1130 if (lo_lseek(fd, offset, SEEK_SET) < 0)
1131 return ((char *) array);
1132 v = (struct varlena *) palloc(elmlen + VARHDRSZ);
1133 VARSIZE(v) = elmlen + VARHDRSZ;
1134 ArrayCastAndSet(dataPtr, (bool) reftype, elmlen, VARDATA(v));
1140 * if (n < VARSIZE(v) - VARHDRSZ) RETURN_NULL;
1144 return ((char *) array);
1148 offset = offset * elmlen;
1149 /* off the end of the array */
1150 if (nbytes - offset < 1)
1151 return ((char *) array);
1152 pos = ARR_DATA_PTR(array) + offset;
1156 ArrayType *newarray;
1166 elt_ptr = array_seek(ARR_DATA_PTR(array), -1, offset);
1167 oldlen = INTALIGN(*(int32 *) elt_ptr);
1168 newlen = INTALIGN(*(int32 *) dataPtr);
1170 if (oldlen == newlen)
1172 /* new element with same size, overwrite old data */
1173 ArrayCastAndSet(dataPtr, (bool) reftype, elmlen, elt_ptr);
1174 return ((char *) array);
1177 /* new element with different size, reallocate the array */
1178 oldsize = array->size;
1179 lth0 = ARR_OVERHEAD(n);
1180 lth1 = (int) (elt_ptr - ARR_DATA_PTR(array));
1181 lth2 = (int) (oldsize - lth0 - lth1 - oldlen);
1182 newsize = lth0 + lth1 + newlen + lth2;
1184 newarray = (ArrayType *) palloc(newsize);
1185 memmove((char *) newarray, (char *) array, lth0 + lth1);
1186 newarray->size = newsize;
1187 newlen = ArrayCastAndSet(dataPtr, (bool) reftype, elmlen,
1188 (char *) newarray + lth0 + lth1);
1189 memmove((char *) newarray + lth0 + lth1 + newlen,
1190 (char *) array + lth0 + lth1 + oldlen, lth2);
1192 /* ??? who should free this storage ??? */
1193 return ((char *) newarray);
1195 ArrayCastAndSet(dataPtr, (bool) reftype, elmlen, pos);
1196 return ((char *) array);
1199 /*----------------------------------------------------------------------------
1201 * This routine sets the value of a range of array locations (specified
1202 * by upper and lower index values ) to new values passed as
1205 * returns a pointer to the modified array.
1206 *----------------------------------------------------------------------------
1209 array_assgn(ArrayType *array,
1223 if (array == (ArrayType *) NULL)
1226 elog(ERROR, "array_assgn:updates on arrays of variable length elements not allowed");
1228 dim = ARR_DIMS(array);
1229 lb = ARR_LBOUND(array);
1230 ndim = ARR_NDIM(array);
1232 if (!SanityCheckInput(ndim, n, dim, lb, upperIndx) ||
1233 !SanityCheckInput(ndim, n, dim, lb, lowerIndx))
1235 return ((char *) array);
1238 for (i = 0; i < n; i++)
1239 if (lowerIndx[i] > upperIndx[i])
1240 elog(ERROR, "lowerIndex larger than upperIndx");
1242 if (ARR_IS_LO(array))
1250 lo_name = (char *) ARR_DATA_PTR(array);
1251 if ((fd = LOopen(lo_name, ARR_IS_INV(array) ? INV_WRITE : O_WRONLY)) < 0)
1252 return ((char *) array);
1254 if (ARR_IS_LO(newArr))
1257 lo_name = (char *) ARR_DATA_PTR(newArr);
1258 if ((newfd = LOopen(lo_name, ARR_IS_INV(newArr) ? INV_READ : O_RDONLY)) < 0)
1259 return ((char *) array);
1261 _LOArrayRange(lowerIndx, upperIndx, len, fd, newfd, array, 1, isNull);
1266 _LOArrayRange(lowerIndx, upperIndx, len, fd, (int) ARR_DATA_PTR(newArr),
1270 return ((char *) array);
1272 _ArrayRange(lowerIndx, upperIndx, len, ARR_DATA_PTR(newArr), array, 0);
1273 return (char *) array;
1276 /*-----------------------------------------------------------------------------
1278 * compares two arrays for equality
1280 * returns 1 if the arrays are equal, 0 otherwise.
1281 *-----------------------------------------------------------------------------
1284 array_eq(ArrayType *array1, ArrayType *array2)
1286 if ((array1 == NULL) || (array2 == NULL))
1288 if (*(int *) array1 != *(int *) array2)
1290 if (memcmp(array1, array2, *(int *) array1))
1295 /***************************************************************************/
1296 /******************| Support Routines |*****************/
1297 /***************************************************************************/
1299 system_cache_lookup(Oid element_type,
1308 HeapTuple typeTuple;
1309 TypeTupleForm typeStruct;
1311 typeTuple = SearchSysCacheTuple(TYPOID, ObjectIdGetDatum(element_type),
1314 if (!HeapTupleIsValid(typeTuple))
1316 elog(ERROR, "array_out: Cache lookup failed for type %d\n",
1320 typeStruct = (TypeTupleForm) GETSTRUCT(typeTuple);
1321 *typlen = typeStruct->typlen;
1322 *typbyval = typeStruct->typbyval;
1323 *typdelim = typeStruct->typdelim;
1324 *typelem = typeStruct->typelem;
1325 *typalign = typeStruct->typalign;
1328 *proc = typeStruct->typinput;
1332 *proc = typeStruct->typoutput;
1337 _ArrayCast(char *value, bool byval, int len)
1344 return ((Datum) *value);
1346 return ((Datum) *(int16 *) value);
1349 return ((Datum) *(int32 *) value);
1351 elog(ERROR, "array_ref: byval and elt len > 4!");
1357 return (Datum) value;
1364 ArrayCastAndSet(char *src,
1378 *dest = DatumGetChar(src);
1381 *(int16 *) dest = DatumGetInt16(src);
1384 *(int32 *) dest = (int32) src;
1390 memmove(dest, src, typlen);
1396 memmove(dest, src, *(int32 *) src);
1397 inc = (INTALIGN(*(int32 *) src));
1404 _AdvanceBy1word(char *str, char **word)
1412 while (isspace(*str))
1415 if ((space = (char *) strchr(str, ' ')) != (char *) NULL)
1428 SanityCheckInput(int ndim, int n, int dim[], int lb[], int indx[])
1432 /* Do Sanity check on input */
1435 for (i = 0; i < ndim; i++)
1436 if ((lb[i] > indx[i]) || (indx[i] >= (dim[i] + lb[i])))
1442 _ArrayRange(int st[],
1462 n = ARR_NDIM(array);
1463 dim = ARR_DIMS(array);
1464 lb = ARR_LBOUND(array);
1465 srcPtr = ARR_DATA_PTR(array);
1466 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++);
1467 mda_get_prod(n, dim, prod);
1468 st_pos = tuple2linear(n, st, prod);
1469 srcPtr = array_seek(srcPtr, bsize, st_pos);
1470 mda_get_range(n, span, st, endp);
1471 mda_get_offset_values(n, dist, prod, span);
1472 for (i = 0; i < n; indx[i++] = 0);
1477 srcPtr = array_seek(srcPtr, bsize, dist[j]);
1479 inc = array_read(destPtr, bsize, 1, srcPtr);
1481 inc = array_read(srcPtr, bsize, 1, destPtr);
1484 } while ((j = next_tuple(i + 1, indx, span)) != -1);
1488 _ArrayClipCount(int stI[], int endpI[], ArrayType *array)
1506 n = ARR_NDIM(array);
1507 dim = ARR_DIMS(array);
1508 lb = ARR_LBOUND(array);
1509 ptr = ARR_DATA_PTR(array);
1510 for (i = 0; i < n; st[i] = stI[i] - lb[i], endp[i] = endpI[i] - lb[i], i++);
1511 mda_get_prod(n, dim, prod);
1512 st_pos = tuple2linear(n, st, prod);
1513 ptr = array_seek(ptr, -1, st_pos);
1514 mda_get_range(n, span, st, endp);
1515 mda_get_offset_values(n, dist, prod, span);
1516 for (i = 0; i < n; indx[i++] = 0);
1520 ptr = array_seek(ptr, -1, dist[j]);
1521 inc = INTALIGN(*(int32 *) ptr);
1524 } while ((j = next_tuple(i + 1, indx, span)) != -1);
1529 array_seek(char *ptr, int eltsize, int nitems)
1534 return (ptr + eltsize * nitems);
1535 for (i = 0; i < nitems; i++)
1536 ptr += INTALIGN(*(int32 *) ptr);
1541 array_read(char *destptr, int eltsize, int nitems, char *srcptr)
1549 memmove(destptr, srcptr, eltsize * nitems);
1550 return (eltsize * nitems);
1552 for (i = inc = 0; i < nitems; i++)
1554 tmp = (INTALIGN(*(int32 *) srcptr));
1555 memmove(destptr, srcptr, tmp);
1564 _LOArrayRange(int st[],
1587 n = ARR_NDIM(array);
1588 dim = ARR_DIMS(array);
1589 lb = ARR_LBOUND(array);
1590 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++);
1592 mda_get_prod(n, dim, prod);
1593 st_pos = tuple2linear(n, st, prod);
1594 offset = st_pos * bsize;
1595 if (lo_lseek(srcfd, offset, SEEK_SET) < 0)
1597 mda_get_range(n, span, st, endp);
1598 mda_get_offset_values(n, dist, prod, span);
1599 for (i = 0; i < n; indx[i++] = 0);
1600 for (i = n - 1, inc = bsize; i >= 0; inc *= span[i--])
1606 offset += (dist[j] * bsize);
1607 if (lo_lseek(srcfd, offset, SEEK_SET) < 0)
1609 tmp = _LOtransfer((char **) &srcfd, inc, 1, (char **) &destfd, isSrcLO, 1);
1613 } while ((j = next_tuple(i + 1, indx, span)) != -1);
1618 _ReadArray(int st[],
1641 n = ARR_NDIM(array);
1642 dim = ARR_DIMS(array);
1643 lb = ARR_LBOUND(array);
1644 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++);
1646 mda_get_prod(n, dim, prod);
1647 st_pos = tuple2linear(n, st, prod);
1648 offset = st_pos * bsize;
1649 if (lo_lseek(srcfd, offset, SEEK_SET) < 0)
1651 mda_get_range(n, span, st, endp);
1652 mda_get_offset_values(n, dist, prod, span);
1653 for (i = 0; i < n; indx[i++] = 0);
1654 for (i = n - 1, inc = bsize; i >= 0; inc *= span[i--])
1660 offset += (dist[j] * bsize);
1661 if (lo_lseek(srcfd, offset, SEEK_SET) < 0)
1663 tmp = _LOtransfer((char **) &destfd, inc, 1, (char **) &srcfd, 1, isDestLO);
1667 } while ((j = next_tuple(i + 1, indx, span)) != -1);
1672 _LOtransfer(char **destfd,
1679 #define MAX_READ (512 * 1024)
1680 #define min(a, b) (a < b ? a : b)
1681 struct varlena *v = NULL;
1686 inc = nitems * size;
1687 if (isSrcLO && isDestLO && inc > 0)
1688 for (tmp = 0, resid = inc;
1689 resid > 0 && (inc = min(resid, MAX_READ)) > 0; resid -= inc)
1692 v = (struct varlena *) LOread((int) *srcfd, inc);
1693 if (VARSIZE(v) - VARHDRSZ < inc)
1698 tmp += LOwrite((int) *destfd, v);
1703 else if (!isSrcLO && isDestLO)
1705 tmp = lo_write((int) *destfd, *srcfd, inc);
1706 *srcfd = *srcfd + tmp;
1708 else if (isSrcLO && !isDestLO)
1710 tmp = lo_read((int) *srcfd, *destfd, inc);
1711 *destfd = *destfd + tmp;
1715 memmove(*destfd, *srcfd, inc);
1725 _array_newLO(int *fd, int flag)
1728 char saveName[NAME_LEN];
1730 p = (char *) palloc(NAME_LEN);
1731 sprintf(p, "/Arry.%d", newoid());
1732 strcpy(saveName, p);
1734 if ((*fd = LOcreat(saveName, 0600, flag)) < 0)
1735 elog(ERROR, "Large object create failed");