From 210055ad614ae845686fdf9f8fc6b60301689cc8 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Mon, 3 May 1999 19:10:48 +0000 Subject: here are some patches for 6.5.0 which I already submitted but have never been applied. The patches are in the .tar.gz attachment at the end: varchar-array.patch this patch adds support for arrays of bpchar() and varchar(), which where always missing from postgres. These datatypes can be used to replace the _char4, _char8, etc., which were dropped some time ago. block-size.patch this patch fixes many errors in the parser and other program which happen with very large query statements (> 8K) when using a page size larger than 8192. This patch is needed if you want to submit queries larger than 8K. Postgres supports tuples up to 32K but you can't insert them because you can't submit queries larger than 8K. My patch fixes this problem. The patch also replaces all the occurrences of `8192' and `1<<13' in the sources with the proper constants defined in include files. You should now never find 8192 hardwired in C code, just to make code clearer. -- Massimo Dal Zotto --- src/backend/utils/adt/arrayfuncs.c | 156 ++++++++++++++++++++++++++++++++++++- 1 file changed, 155 insertions(+), 1 deletion(-) (limited to 'src/backend/utils/adt/arrayfuncs.c') diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index a4f1842e5f9..f8fe7e42229 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.38 1999/02/13 23:19:00 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.39 1999/05/03 19:09:59 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -15,6 +15,7 @@ #include #include #include +#include #include "postgres.h" @@ -26,6 +27,7 @@ #include "storage/fd.h" #include "fmgr.h" #include "utils/array.h" +#include "utils/elog.h" #include "libpq/libpq-fs.h" #include "libpq/be-fsstubs.h" @@ -614,7 +616,9 @@ array_out(ArrayType *v, Oid element_type) i, j, k, +#ifndef TCL_ARRAYS l, +#endif indx[MAXDIM]; bool dummy_bool; int ndim, @@ -1274,6 +1278,156 @@ array_assgn(ArrayType *array, return (char *) array; } +/* + * array_map() + * + * Map an arbitrary function to an array and return a new array with + * same dimensions and the source elements transformed by fn(). + */ +ArrayType * +array_map(ArrayType *v, + Oid type, + char *(fn)(char *p, ...), + Oid retType, + int nargs, + ...) +{ + ArrayType *result; + void *args[4]; + char **values; + char *elt; + int *dim; + int ndim; + int nitems; + int i; + int nbytes = 0; + int inp_typlen; + bool inp_typbyval; + int typlen; + bool typbyval; + char typdelim; + Oid typelem; + Oid proc; + char typalign; + char *s; + char *p; + va_list ap; + + /* Large objects not yet supported */ + if (ARR_IS_LO(v) == true) { + elog(ERROR, "array_map: large objects not supported"); + } + + /* Check nargs */ + if ((nargs < 0) || (nargs > 4)) { + elog(ERROR, "array_map: invalid nargs: %d", nargs); + } + + /* Copy extra args to local variable */ + va_start(ap, nargs); + for (i=0; i 0) { + s += inp_typlen; + } else { + s += INTALIGN(*(int32 *) s); + } + } + + /* + * Apply the given function to source elt and extra args. + * nargs is the number of extra args taken by fn(). + */ + switch (nargs) { + case 0: + p = (char *) (*fn) (elt); + break; + case 1: + p = (char *) (*fn) (elt, args[0]); + break; + case 2: + p = (char *) (*fn) (elt, args[0], args[1]); + break; + case 3: + p = (char *) (*fn) (elt, args[0], args[1], args[2]); + break; + case 4: + default: + p = (char *) (*fn) (elt, args[0], args[1], args[2], args[3]); + break; + } + + /* Update values and total result size */ + if (typbyval) { + values[i] = (char *) p; + nbytes += typlen; + } else { + int len; + len = ((typlen > 0) ? typlen : INTALIGN(*(int32 *) p)); + /* Needed because _CopyArrayEls tries to pfree items */ + if (p == elt) { + p = (char *) palloc(len); + memcpy(p, elt, len); + } + values[i] = (char *) p; + nbytes += len; + } + } + + /* Allocate and initialize the result array */ + nbytes += ARR_OVERHEAD(ndim); + result = (ArrayType *) palloc(nbytes); + MemSet(result, 0, nbytes); + + memcpy((char *) result, (char *) &nbytes, sizeof(int)); + memcpy((char *) ARR_NDIM_PTR(result), (char *) &ndim, sizeof(int)); + memcpy((char *) ARR_DIMS(result), ARR_DIMS(v), 2 * ndim * sizeof(int)); + + /* Copy new values into the result array. values is pfreed. */ + _CopyArrayEls((char **) values, + ARR_DATA_PTR(result), nitems, + typlen, typalign, typbyval); + + return result; +} + /*----------------------------------------------------------------------------- * array_eq : * compares two arrays for equality -- cgit v1.2.3