📄 arrayfuncs.c
字号:
VARSIZE(outputbytes) - VARHDRSZ); pfree(outputbytes); p = att_addlength(p, typlen, PointerGetDatum(p)); p = (char *) att_align(p, typalign); } PG_RETURN_BYTEA_P(pq_endtypsend(&buf));}/*----------------------------------------------------------------------------- * array_dims : * returns the dimensions of the array pointed to by "v", as a "text" *---------------------------------------------------------------------------- */Datumarray_dims(PG_FUNCTION_ARGS){ ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); text *result; char *p; int nbytes, i; int *dimv, *lb; /* Sanity check: does it look like an array at all? */ if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM) PG_RETURN_NULL(); nbytes = ARR_NDIM(v) * 33 + 1; /* * 33 since we assume 15 digits per number + ':' +'[]' * * +1 allows for temp trailing null */ result = (text *) palloc(nbytes + VARHDRSZ); p = VARDATA(result); dimv = ARR_DIMS(v); lb = ARR_LBOUND(v); for (i = 0; i < ARR_NDIM(v); i++) { sprintf(p, "[%d:%d]", lb[i], dimv[i] + lb[i] - 1); p += strlen(p); } VARATT_SIZEP(result) = strlen(VARDATA(result)) + VARHDRSZ; PG_RETURN_TEXT_P(result);}/*----------------------------------------------------------------------------- * array_lower : * returns the lower dimension, of the DIM requested, for * the array pointed to by "v", as an int4 *---------------------------------------------------------------------------- */Datumarray_lower(PG_FUNCTION_ARGS){ ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); int reqdim = PG_GETARG_INT32(1); int *lb; int result; /* Sanity check: does it look like an array at all? */ if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM) PG_RETURN_NULL(); /* Sanity check: was the requested dim valid */ if (reqdim <= 0 || reqdim > ARR_NDIM(v)) PG_RETURN_NULL(); lb = ARR_LBOUND(v); result = lb[reqdim - 1]; PG_RETURN_INT32(result);}/*----------------------------------------------------------------------------- * array_upper : * returns the upper dimension, of the DIM requested, for * the array pointed to by "v", as an int4 *---------------------------------------------------------------------------- */Datumarray_upper(PG_FUNCTION_ARGS){ ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); int reqdim = PG_GETARG_INT32(1); int *dimv, *lb; int result; /* Sanity check: does it look like an array at all? */ if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM) PG_RETURN_NULL(); /* Sanity check: was the requested dim valid */ if (reqdim <= 0 || reqdim > ARR_NDIM(v)) PG_RETURN_NULL(); lb = ARR_LBOUND(v); dimv = ARR_DIMS(v); result = dimv[reqdim - 1] + lb[reqdim - 1] - 1; PG_RETURN_INT32(result);}/*--------------------------------------------------------------------------- * array_ref : * This routine takes an array pointer and an index array and returns * the referenced item as a Datum. Note that for a pass-by-reference * datatype, the returned Datum is a pointer into the array object. *--------------------------------------------------------------------------- */Datumarray_ref(ArrayType *array, int nSubscripts, int *indx, int arraylen, int elmlen, bool elmbyval, char elmalign, bool *isNull){ int i, ndim, *dim, *lb, offset, fixedDim[1], fixedLb[1]; char *arraydataptr, *retptr; if (array == NULL) RETURN_NULL(Datum); if (arraylen > 0) { /* * fixed-length arrays -- these are assumed to be 1-d, 0-based */ ndim = 1; fixedDim[0] = arraylen / elmlen; fixedLb[0] = 0; dim = fixedDim; lb = fixedLb; arraydataptr = (char *) array; } else { /* detoast input array if necessary */ array = DatumGetArrayTypeP(PointerGetDatum(array)); ndim = ARR_NDIM(array); dim = ARR_DIMS(array); lb = ARR_LBOUND(array); arraydataptr = ARR_DATA_PTR(array); } /* * Return NULL for invalid subscript */ if (ndim != nSubscripts || ndim <= 0 || ndim > MAXDIM) RETURN_NULL(Datum); for (i = 0; i < ndim; i++) if (indx[i] < lb[i] || indx[i] >= (dim[i] + lb[i])) RETURN_NULL(Datum); /* * OK, get the element */ offset = ArrayGetOffset(nSubscripts, dim, lb, indx); retptr = array_seek(arraydataptr, offset, elmlen, elmbyval, elmalign); *isNull = false; return ArrayCast(retptr, elmbyval, elmlen);}/*----------------------------------------------------------------------------- * array_get_slice : * This routine takes an array and a range of indices (upperIndex and * lowerIndx), creates a new array structure for the referred elements * and returns a pointer to it. * * NOTE: we assume it is OK to scribble on the provided index arrays * lowerIndx[] and upperIndx[]. These are generally just temporaries. *----------------------------------------------------------------------------- */ArrayType *array_get_slice(ArrayType *array, int nSubscripts, int *upperIndx, int *lowerIndx, int arraylen, int elmlen, bool elmbyval, char elmalign, bool *isNull){ int i, ndim, *dim, *lb, *newlb; int fixedDim[1], fixedLb[1]; char *arraydataptr; ArrayType *newarray; int bytes, span[MAXDIM]; if (array == NULL) RETURN_NULL(ArrayType *); if (arraylen > 0) { /* * fixed-length arrays -- currently, cannot slice these because parser * labels output as being of the fixed-length array type! Code below * shows how we could support it if the parser were changed to label * output as a suitable varlena array type. */ ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("slices of fixed-length arrays not implemented"))); /* * fixed-length arrays -- these are assumed to be 1-d, 0-based XXX * where would we get the correct ELEMTYPE from? */ ndim = 1; fixedDim[0] = arraylen / elmlen; fixedLb[0] = 0; dim = fixedDim; lb = fixedLb; arraydataptr = (char *) array; } else { /* detoast input array if necessary */ array = DatumGetArrayTypeP(PointerGetDatum(array)); ndim = ARR_NDIM(array); dim = ARR_DIMS(array); lb = ARR_LBOUND(array); arraydataptr = ARR_DATA_PTR(array); } /* * Check provided subscripts. A slice exceeding the current array limits * is silently truncated to the array limits. If we end up with an empty * slice, return NULL (should it be an empty array instead?) */ if (ndim < nSubscripts || ndim <= 0 || ndim > MAXDIM) RETURN_NULL(ArrayType *); for (i = 0; i < nSubscripts; i++) { if (lowerIndx[i] < lb[i]) lowerIndx[i] = lb[i]; if (upperIndx[i] >= (dim[i] + lb[i])) upperIndx[i] = dim[i] + lb[i] - 1; if (lowerIndx[i] > upperIndx[i]) RETURN_NULL(ArrayType *); } /* fill any missing subscript positions with full array range */ for (; i < ndim; i++) { lowerIndx[i] = lb[i]; upperIndx[i] = dim[i] + lb[i] - 1; if (lowerIndx[i] > upperIndx[i]) RETURN_NULL(ArrayType *); } mda_get_range(ndim, span, lowerIndx, upperIndx); bytes = array_slice_size(ndim, dim, lb, arraydataptr, lowerIndx, upperIndx, elmlen, elmbyval, elmalign); bytes += ARR_OVERHEAD(ndim); newarray = (ArrayType *) palloc(bytes); newarray->size = bytes; newarray->ndim = ndim; newarray->flags = 0; newarray->elemtype = ARR_ELEMTYPE(array); memcpy(ARR_DIMS(newarray), span, ndim * sizeof(int)); /* * Lower bounds of the new array are set to 1. Formerly (before 7.3) we * copied the given lowerIndx values ... but that seems confusing. */ newlb = ARR_LBOUND(newarray); for (i = 0; i < ndim; i++) newlb[i] = 1; array_extract_slice(ndim, dim, lb, arraydataptr, lowerIndx, upperIndx, ARR_DATA_PTR(newarray), elmlen, elmbyval, elmalign); return newarray;}/*----------------------------------------------------------------------------- * array_set : * This routine sets the value of an array location (specified by * an index array) to a new value specified by "dataValue". * result : * A new array is returned, just like the old except for the one * modified entry. * * For one-dimensional arrays only, we allow the array to be extended * by assigning to the position one above or one below the existing range. * (We could be more flexible if we had a way to represent NULL elements.) * * NOTE: For assignments, we throw an error for invalid subscripts etc, * rather than returning a NULL as the fetch operations do. The reasoning * is that returning a NULL would cause the user's whole array to be replaced * with NULL, which will probably not make him happy. *----------------------------------------------------------------------------- */ArrayType *array_set(ArrayType *array, int nSubscripts, int *indx, Datum dataValue, int arraylen, int elmlen, bool elmbyval, char elmalign, bool *isNull){ int i, ndim, dim[MAXDIM], lb[MAXDIM], offset; ArrayType *newarray; char *elt_ptr; bool extendbefore = false; bool extendafter = false; int olddatasize, newsize, olditemlen, newitemlen, overheadlen, lenbefore, lenafter; if (array == NULL) RETURN_NULL(ArrayType *); if (arraylen > 0) { /* * fixed-length arrays -- these are assumed to be 1-d, 0-based. We * cannot extend them, either. */ if (nSubscripts != 1) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("invalid array subscripts"))); if (indx[0] < 0 || indx[0] * elmlen >= arraylen) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("invalid array subscripts"))); newarray = (ArrayType *) palloc(arraylen); memcpy(newarray, array, arraylen); elt_ptr = (char *) newarray + indx[0] * elmlen; ArrayCastAndSet(dataValue, elmlen, elmbyval, elmalign, elt_ptr); return newarray; } /* make sure item to be inserted is not toasted */ if (elmlen == -1) dataValue = PointerGetDatum(PG_DETOAST_DATUM(dataValue)); /* detoast input array if necessary */ array = DatumGetArrayTypeP(PointerGetDatum(array)); ndim = ARR_NDIM(array); /* * if number of dims is zero, i.e. an empty array, create an array with * nSubscripts dimensions, and set the lower bounds to the supplied * subscripts */ if (ndim == 0) { Oid elmtype = ARR_ELEMTYPE(array); for (i = 0; i < nSubscripts; i++) { dim[i] = 1; lb[i] = indx[i]; } return construct_md_array(&dataValue, nSubscripts, dim, lb, elmtype, elmlen, elmbyval, elmalign); } if (ndim != nSubscripts || ndim <= 0 || ndim > MAXDIM) ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("invalid array subscripts"))); /* copy dim/lb since we may modify them */ memcpy(dim, ARR_DIMS(array), ndim * sizeof(int)); memcpy(lb, ARR_LBOUND(array), ndim * sizeof(int)); /* * Check subscripts */ for (i = 0; i < ndim; i++) { if (indx[i] < lb[i]) { if (ndim == 1 && indx[i] == lb[i] - 1) { dim[i]++; lb[i]--; extendbefore = true; } else ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("invalid array subscripts"))); } if (indx[i] >= (dim[i] + lb[i])) { if (ndim == 1 && indx[i] == (dim[i] + lb[i])) { dim[i]++; extendafter = true; } else ereport(ERROR, (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("invalid array subscripts"))); } } /* * Compute sizes of items and areas to copy */ overheadlen = ARR_OVERHEAD(ndim); olddatasize = ARR_SIZE(array) - overheadlen; if (extendbefore) { lenbefore = 0; olditemlen = 0; lenafter = olddatasize; } else if (extendafter) { lenbefore = olddatasize; olditemlen = 0; lenafter = 0; } else { offset = ArrayGetOffset(nSubscripts, dim, lb, indx); elt_ptr = array_seek(ARR_DATA_PTR(array), offset,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -