mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Make SQL arrays support null elements. This commit fixes the core array
functionality, but I still need to make another pass looking at places that incidentally use arrays (such as ACL manipulation) to make sure they are null-safe. Contrib needs work too. I have not changed the behaviors that are still under discussion about array comparison and what to do with lower bounds.
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.135 2005/10/29 00:31:50 petere Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.136 2005/11/17 22:14:51 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -119,12 +119,15 @@ ProcedureCreate(const char *procedureName,
|
||||
* need to use deconstruct_array() since the array data is just going
|
||||
* to look like a C array of OID values.
|
||||
*/
|
||||
allParamCount = ARR_DIMS(DatumGetPointer(allParameterTypes))[0];
|
||||
if (ARR_NDIM(DatumGetPointer(allParameterTypes)) != 1 ||
|
||||
ArrayType *allParamArray = (ArrayType *) DatumGetPointer(allParameterTypes);
|
||||
|
||||
allParamCount = ARR_DIMS(allParamArray)[0];
|
||||
if (ARR_NDIM(allParamArray) != 1 ||
|
||||
allParamCount <= 0 ||
|
||||
ARR_ELEMTYPE(DatumGetPointer(allParameterTypes)) != OIDOID)
|
||||
ARR_HASNULL(allParamArray) ||
|
||||
ARR_ELEMTYPE(allParamArray) != OIDOID)
|
||||
elog(ERROR, "allParameterTypes is not a 1-D Oid array");
|
||||
allParams = (Oid *) ARR_DATA_PTR(DatumGetPointer(allParameterTypes));
|
||||
allParams = (Oid *) ARR_DATA_PTR(allParamArray);
|
||||
Assert(allParamCount >= parameterCount);
|
||||
/* we assume caller got the contents right */
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.183 2005/10/19 22:30:30 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.184 2005/11/17 22:14:51 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -202,16 +202,8 @@ static Datum ExecEvalRelabelType(GenericExprState *exprstate,
|
||||
* if it's a simple reference, or the modified array value if it's
|
||||
* an array assignment (i.e., array element or slice insertion).
|
||||
*
|
||||
* NOTE: if we get a NULL result from a subexpression, we return NULL when
|
||||
* it's an array reference, or the unmodified source array when it's an
|
||||
* array assignment. This may seem peculiar, but if we return NULL (as was
|
||||
* done in versions up through 7.0) then an assignment like
|
||||
* UPDATE table SET arrayfield[4] = NULL
|
||||
* will result in setting the whole array to NULL, which is certainly not
|
||||
* very desirable. By returning the source array we make the assignment
|
||||
* into a no-op, instead. (Eventually we need to redesign arrays so that
|
||||
* individual elements can be NULL, but for now, let's try to protect users
|
||||
* from shooting themselves in the foot.)
|
||||
* NOTE: if we get a NULL result from a subscript expression, we return NULL
|
||||
* when it's an array reference, or raise an error when it's an assignment.
|
||||
*
|
||||
* NOTE: we deliberately refrain from applying DatumGetArrayTypeP() here,
|
||||
* even though that might seem natural, because this code needs to support
|
||||
@@ -270,15 +262,15 @@ ExecEvalArrayRef(ArrayRefExprState *astate,
|
||||
econtext,
|
||||
&eisnull,
|
||||
NULL));
|
||||
/* If any index expr yields NULL, result is NULL or source array */
|
||||
/* If any index expr yields NULL, result is NULL or error */
|
||||
if (eisnull)
|
||||
{
|
||||
if (!isAssignment)
|
||||
{
|
||||
*isNull = true;
|
||||
return (Datum) NULL;
|
||||
}
|
||||
return PointerGetDatum(array_source);
|
||||
if (isAssignment)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
||||
errmsg("array subscript in assignment must not be NULL")));
|
||||
*isNull = true;
|
||||
return (Datum) NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,18 +290,15 @@ ExecEvalArrayRef(ArrayRefExprState *astate,
|
||||
econtext,
|
||||
&eisnull,
|
||||
NULL));
|
||||
|
||||
/*
|
||||
* If any index expr yields NULL, result is NULL or source array
|
||||
*/
|
||||
/* If any index expr yields NULL, result is NULL or error */
|
||||
if (eisnull)
|
||||
{
|
||||
if (!isAssignment)
|
||||
{
|
||||
*isNull = true;
|
||||
return (Datum) NULL;
|
||||
}
|
||||
return PointerGetDatum(array_source);
|
||||
if (isAssignment)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
||||
errmsg("array subscript in assignment must not be NULL")));
|
||||
*isNull = true;
|
||||
return (Datum) NULL;
|
||||
}
|
||||
}
|
||||
/* this can't happen unless parser messed up */
|
||||
@@ -327,8 +316,8 @@ ExecEvalArrayRef(ArrayRefExprState *astate,
|
||||
/*
|
||||
* Evaluate the value to be assigned into the array.
|
||||
*
|
||||
* XXX At some point we'll need to look into making the old value of the
|
||||
* array element available via CaseTestExpr, as is done by
|
||||
* XXX At some point we'll need to look into making the old value of
|
||||
* the array element available via CaseTestExpr, as is done by
|
||||
* ExecEvalFieldStore. This is not needed now but will be needed to
|
||||
* support arrays of composite types; in an assignment to a field of
|
||||
* an array member, the parser would generate a FieldStore that
|
||||
@@ -340,29 +329,23 @@ ExecEvalArrayRef(ArrayRefExprState *astate,
|
||||
NULL);
|
||||
|
||||
/*
|
||||
* For now, can't cope with inserting NULL into an array, so make it a
|
||||
* no-op per discussion above...
|
||||
* For an assignment to a fixed-length array type, both the original
|
||||
* array and the value to be assigned into it must be non-NULL, else
|
||||
* we punt and return the original array.
|
||||
*/
|
||||
if (eisnull)
|
||||
return PointerGetDatum(array_source);
|
||||
if (astate->refattrlength > 0) /* fixed-length array? */
|
||||
if (eisnull || *isNull)
|
||||
return PointerGetDatum(array_source);
|
||||
|
||||
/*
|
||||
* For an assignment, if all the subscripts and the input expression
|
||||
* are non-null but the original array is null, then substitute an
|
||||
* empty (zero-dimensional) array and proceed with the assignment.
|
||||
* This only works for varlena arrays, though; for fixed-length array
|
||||
* types we punt and return the null input array.
|
||||
* For assignment to varlena arrays, we handle a NULL original array
|
||||
* by substituting an empty (zero-dimensional) array; insertion of
|
||||
* the new element will result in a singleton array value. It does
|
||||
* not matter whether the new element is NULL.
|
||||
*/
|
||||
if (*isNull)
|
||||
{
|
||||
if (astate->refattrlength > 0) /* fixed-length array? */
|
||||
return PointerGetDatum(array_source);
|
||||
|
||||
array_source = construct_md_array(NULL, 0, NULL, NULL,
|
||||
arrayRef->refelemtype,
|
||||
astate->refelemlength,
|
||||
astate->refelembyval,
|
||||
astate->refelemalign);
|
||||
array_source = construct_empty_array(arrayRef->refelemtype);
|
||||
*isNull = false;
|
||||
}
|
||||
|
||||
@@ -370,20 +353,20 @@ ExecEvalArrayRef(ArrayRefExprState *astate,
|
||||
resultArray = array_set(array_source, i,
|
||||
upper.indx,
|
||||
sourceData,
|
||||
eisnull,
|
||||
astate->refattrlength,
|
||||
astate->refelemlength,
|
||||
astate->refelembyval,
|
||||
astate->refelemalign,
|
||||
isNull);
|
||||
astate->refelemalign);
|
||||
else
|
||||
resultArray = array_set_slice(array_source, i,
|
||||
upper.indx, lower.indx,
|
||||
(ArrayType *) DatumGetPointer(sourceData),
|
||||
eisnull,
|
||||
astate->refattrlength,
|
||||
astate->refelemlength,
|
||||
astate->refelembyval,
|
||||
astate->refelemalign,
|
||||
isNull);
|
||||
astate->refelemalign);
|
||||
return PointerGetDatum(resultArray);
|
||||
}
|
||||
|
||||
@@ -401,8 +384,7 @@ ExecEvalArrayRef(ArrayRefExprState *astate,
|
||||
astate->refattrlength,
|
||||
astate->refelemlength,
|
||||
astate->refelembyval,
|
||||
astate->refelemalign,
|
||||
isNull);
|
||||
astate->refelemalign);
|
||||
return PointerGetDatum(resultArray);
|
||||
}
|
||||
}
|
||||
@@ -1620,6 +1602,8 @@ ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
|
||||
bool typbyval;
|
||||
char typalign;
|
||||
char *s;
|
||||
bits8 *bitmap;
|
||||
int bitmask;
|
||||
|
||||
/* Set default values for result flags: non-null, not a set result */
|
||||
*isNull = false;
|
||||
@@ -1668,9 +1652,8 @@ ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
|
||||
return BoolGetDatum(!useOr);
|
||||
|
||||
/*
|
||||
* If the scalar is NULL, and the function is strict, return NULL. This is
|
||||
* just to avoid having to test for strictness inside the loop. (XXX but
|
||||
* if arrays could have null elements, we'd need a test anyway.)
|
||||
* If the scalar is NULL, and the function is strict, return NULL;
|
||||
* no point in iterating the loop.
|
||||
*/
|
||||
if (fcinfo.argnull[0] && sstate->fxprstate.func.fn_strict)
|
||||
{
|
||||
@@ -1699,22 +1682,40 @@ ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
|
||||
|
||||
/* Loop over the array elements */
|
||||
s = (char *) ARR_DATA_PTR(arr);
|
||||
bitmap = ARR_NULLBITMAP(arr);
|
||||
bitmask = 1;
|
||||
|
||||
for (i = 0; i < nitems; i++)
|
||||
{
|
||||
Datum elt;
|
||||
Datum thisresult;
|
||||
|
||||
/* Get array element */
|
||||
elt = fetch_att(s, typbyval, typlen);
|
||||
|
||||
s = att_addlength(s, typlen, PointerGetDatum(s));
|
||||
s = (char *) att_align(s, typalign);
|
||||
/* Get array element, checking for NULL */
|
||||
if (bitmap && (*bitmap & bitmask) == 0)
|
||||
{
|
||||
fcinfo.arg[1] = (Datum) 0;
|
||||
fcinfo.argnull[1] = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
elt = fetch_att(s, typbyval, typlen);
|
||||
s = att_addlength(s, typlen, PointerGetDatum(s));
|
||||
s = (char *) att_align(s, typalign);
|
||||
fcinfo.arg[1] = elt;
|
||||
fcinfo.argnull[1] = false;
|
||||
}
|
||||
|
||||
/* Call comparison function */
|
||||
fcinfo.arg[1] = elt;
|
||||
fcinfo.argnull[1] = false;
|
||||
fcinfo.isnull = false;
|
||||
thisresult = FunctionCallInvoke(&fcinfo);
|
||||
if (fcinfo.argnull[1] && sstate->fxprstate.func.fn_strict)
|
||||
{
|
||||
fcinfo.isnull = true;
|
||||
thisresult = (Datum) 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
fcinfo.isnull = false;
|
||||
thisresult = FunctionCallInvoke(&fcinfo);
|
||||
}
|
||||
|
||||
/* Combine results per OR or AND semantics */
|
||||
if (fcinfo.isnull)
|
||||
@@ -1737,6 +1738,17 @@ ExecEvalScalarArrayOp(ScalarArrayOpExprState *sstate,
|
||||
break; /* needn't look at any more elements */
|
||||
}
|
||||
}
|
||||
|
||||
/* advance bitmap pointer if any */
|
||||
if (bitmap)
|
||||
{
|
||||
bitmask <<= 1;
|
||||
if (bitmask == 0x100)
|
||||
{
|
||||
bitmap++;
|
||||
bitmask = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*isNull = resultnull;
|
||||
@@ -2053,10 +2065,6 @@ ExecEvalCaseTestExpr(ExprState *exprstate,
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* ExecEvalArray - ARRAY[] expressions
|
||||
*
|
||||
* NOTE: currently, if any input value is NULL then we return a NULL array,
|
||||
* so the ARRAY[] construct can be considered strict. Eventually this will
|
||||
* change; when it does, be sure to fix contain_nonstrict_functions().
|
||||
* ----------------------------------------------------------------
|
||||
*/
|
||||
static Datum
|
||||
@@ -2081,39 +2089,33 @@ ExecEvalArray(ArrayExprState *astate, ExprContext *econtext,
|
||||
/* Elements are presumably of scalar type */
|
||||
int nelems;
|
||||
Datum *dvalues;
|
||||
bool *dnulls;
|
||||
int i = 0;
|
||||
|
||||
ndims = 1;
|
||||
nelems = list_length(astate->elements);
|
||||
|
||||
/* Shouldn't happen here, but if length is 0, return NULL */
|
||||
/* Shouldn't happen here, but if length is 0, return empty array */
|
||||
if (nelems == 0)
|
||||
{
|
||||
*isNull = true;
|
||||
return (Datum) 0;
|
||||
}
|
||||
return PointerGetDatum(construct_empty_array(element_type));
|
||||
|
||||
dvalues = (Datum *) palloc(nelems * sizeof(Datum));
|
||||
dnulls = (bool *) palloc(nelems * sizeof(bool));
|
||||
|
||||
/* loop through and build array of datums */
|
||||
foreach(element, astate->elements)
|
||||
{
|
||||
ExprState *e = (ExprState *) lfirst(element);
|
||||
bool eisnull;
|
||||
|
||||
dvalues[i++] = ExecEvalExpr(e, econtext, &eisnull, NULL);
|
||||
if (eisnull)
|
||||
{
|
||||
*isNull = true;
|
||||
return (Datum) 0;
|
||||
}
|
||||
dvalues[i] = ExecEvalExpr(e, econtext, &dnulls[i], NULL);
|
||||
i++;
|
||||
}
|
||||
|
||||
/* setup for 1-D array of the given length */
|
||||
dims[0] = nelems;
|
||||
lbs[0] = 1;
|
||||
|
||||
result = construct_md_array(dvalues, ndims, dims, lbs,
|
||||
result = construct_md_array(dvalues, dnulls, ndims, dims, lbs,
|
||||
element_type,
|
||||
astate->elemlength,
|
||||
astate->elembyval,
|
||||
@@ -2122,15 +2124,28 @@ ExecEvalArray(ArrayExprState *astate, ExprContext *econtext,
|
||||
else
|
||||
{
|
||||
/* Must be nested array expressions */
|
||||
char *dat = NULL;
|
||||
Size ndatabytes = 0;
|
||||
int nbytes;
|
||||
int outer_nelems = list_length(astate->elements);
|
||||
int nbytes = 0;
|
||||
int nitems = 0;
|
||||
int outer_nelems = 0;
|
||||
int elem_ndims = 0;
|
||||
int *elem_dims = NULL;
|
||||
int *elem_lbs = NULL;
|
||||
bool firstone = true;
|
||||
bool havenulls = false;
|
||||
char **subdata;
|
||||
bits8 **subbitmaps;
|
||||
int *subbytes;
|
||||
int *subnitems;
|
||||
int i;
|
||||
int32 dataoffset;
|
||||
char *dat;
|
||||
int iitem;
|
||||
|
||||
i = list_length(astate->elements);
|
||||
subdata = (char **) palloc(i * sizeof(char *));
|
||||
subbitmaps = (bits8 **) palloc(i * sizeof(bits8 *));
|
||||
subbytes = (int *) palloc(i * sizeof(int));
|
||||
subnitems = (int *) palloc(i * sizeof(int));
|
||||
|
||||
/* loop through and get data area from each element */
|
||||
foreach(element, astate->elements)
|
||||
@@ -2139,14 +2154,11 @@ ExecEvalArray(ArrayExprState *astate, ExprContext *econtext,
|
||||
bool eisnull;
|
||||
Datum arraydatum;
|
||||
ArrayType *array;
|
||||
int elem_ndatabytes;
|
||||
|
||||
arraydatum = ExecEvalExpr(e, econtext, &eisnull, NULL);
|
||||
/* ignore null subarrays */
|
||||
if (eisnull)
|
||||
{
|
||||
*isNull = true;
|
||||
return (Datum) 0;
|
||||
}
|
||||
continue;
|
||||
|
||||
array = DatumGetArrayTypeP(arraydatum);
|
||||
|
||||
@@ -2192,16 +2204,15 @@ ExecEvalArray(ArrayExprState *astate, ExprContext *econtext,
|
||||
"expressions with matching dimensions")));
|
||||
}
|
||||
|
||||
elem_ndatabytes = ARR_SIZE(array) - ARR_OVERHEAD(elem_ndims);
|
||||
ndatabytes += elem_ndatabytes;
|
||||
if (dat == NULL)
|
||||
dat = (char *) palloc(ndatabytes);
|
||||
else
|
||||
dat = (char *) repalloc(dat, ndatabytes);
|
||||
|
||||
memcpy(dat + (ndatabytes - elem_ndatabytes),
|
||||
ARR_DATA_PTR(array),
|
||||
elem_ndatabytes);
|
||||
subdata[outer_nelems] = ARR_DATA_PTR(array);
|
||||
subbitmaps[outer_nelems] = ARR_NULLBITMAP(array);
|
||||
subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array);
|
||||
nbytes += subbytes[outer_nelems];
|
||||
subnitems[outer_nelems] = ArrayGetNItems(ARR_NDIM(array),
|
||||
ARR_DIMS(array));
|
||||
nitems += subnitems[outer_nelems];
|
||||
havenulls |= ARR_HASNULL(array);
|
||||
outer_nelems++;
|
||||
}
|
||||
|
||||
/* setup for multi-D array */
|
||||
@@ -2213,20 +2224,37 @@ ExecEvalArray(ArrayExprState *astate, ExprContext *econtext,
|
||||
lbs[i] = elem_lbs[i - 1];
|
||||
}
|
||||
|
||||
nbytes = ndatabytes + ARR_OVERHEAD(ndims);
|
||||
result = (ArrayType *) palloc(nbytes);
|
||||
if (havenulls)
|
||||
{
|
||||
dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
|
||||
nbytes += dataoffset;
|
||||
}
|
||||
else
|
||||
{
|
||||
dataoffset = 0; /* marker for no null bitmap */
|
||||
nbytes += ARR_OVERHEAD_NONULLS(ndims);
|
||||
}
|
||||
|
||||
result = (ArrayType *) palloc(nbytes);
|
||||
result->size = nbytes;
|
||||
result->ndim = ndims;
|
||||
result->flags = 0;
|
||||
result->dataoffset = dataoffset;
|
||||
result->elemtype = element_type;
|
||||
memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
|
||||
memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
|
||||
if (ndatabytes > 0)
|
||||
memcpy(ARR_DATA_PTR(result), dat, ndatabytes);
|
||||
|
||||
if (dat != NULL)
|
||||
pfree(dat);
|
||||
dat = ARR_DATA_PTR(result);
|
||||
iitem = 0;
|
||||
for (i = 0; i < outer_nelems; i++)
|
||||
{
|
||||
memcpy(dat, subdata[i], subbytes[i]);
|
||||
dat += subbytes[i];
|
||||
if (havenulls)
|
||||
array_bitmap_copy(ARR_NULLBITMAP(result), iitem,
|
||||
subbitmaps[i], 0,
|
||||
subnitems[i]);
|
||||
iitem += subnitems[i];
|
||||
}
|
||||
}
|
||||
|
||||
return PointerGetDatum(result);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.201 2005/10/15 02:49:21 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.202 2005/11/17 22:14:52 tgl Exp $
|
||||
*
|
||||
* HISTORY
|
||||
* AUTHOR DATE MAJOR EVENT
|
||||
@@ -784,7 +784,7 @@ contain_nonstrict_functions_walker(Node *node, void *context)
|
||||
}
|
||||
if (IsA(node, ArrayRef))
|
||||
{
|
||||
/* array assignment is nonstrict */
|
||||
/* array assignment is nonstrict, but subscripting is strict */
|
||||
if (((ArrayRef *) node)->refassgnexpr != NULL)
|
||||
return true;
|
||||
/* else fall through to check args */
|
||||
@@ -842,7 +842,8 @@ contain_nonstrict_functions_walker(Node *node, void *context)
|
||||
return true;
|
||||
if (IsA(node, CaseWhen))
|
||||
return true;
|
||||
/* NB: ArrayExpr might someday be nonstrict */
|
||||
if (IsA(node, ArrayExpr))
|
||||
return true;
|
||||
if (IsA(node, RowExpr))
|
||||
return true;
|
||||
if (IsA(node, CoalesceExpr))
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.127 2005/11/04 17:25:15 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.128 2005/11/17 22:14:52 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -352,7 +352,7 @@ allocacl(int n)
|
||||
new_acl = (Acl *) palloc0(size);
|
||||
new_acl->size = size;
|
||||
new_acl->ndim = 1;
|
||||
new_acl->flags = 0;
|
||||
new_acl->dataoffset = 0; /* we never put in any nulls */
|
||||
new_acl->elemtype = ACLITEMOID;
|
||||
ARR_LBOUND(new_acl)[0] = 1;
|
||||
ARR_DIMS(new_acl)[0] = n;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
* Copyright (c) 2003-2005, PostgreSQL Global Development Group
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.16 2005/10/15 02:49:27 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.17 2005/11/17 22:14:52 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/syscache.h"
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* array_push :
|
||||
* push an element onto either end of a one-dimensional array
|
||||
@@ -29,11 +30,11 @@ array_push(PG_FUNCTION_ARGS)
|
||||
{
|
||||
ArrayType *v;
|
||||
Datum newelem;
|
||||
bool isNull;
|
||||
int *dimv,
|
||||
*lb;
|
||||
ArrayType *result;
|
||||
int indx;
|
||||
bool isNull;
|
||||
Oid element_type;
|
||||
int16 typlen;
|
||||
bool typbyval;
|
||||
@@ -54,15 +55,27 @@ array_push(PG_FUNCTION_ARGS)
|
||||
|
||||
if (arg0_elemid != InvalidOid)
|
||||
{
|
||||
v = PG_GETARG_ARRAYTYPE_P(0);
|
||||
element_type = ARR_ELEMTYPE(v);
|
||||
newelem = PG_GETARG_DATUM(1);
|
||||
if (PG_ARGISNULL(0))
|
||||
v = construct_empty_array(arg0_elemid);
|
||||
else
|
||||
v = PG_GETARG_ARRAYTYPE_P(0);
|
||||
isNull = PG_ARGISNULL(1);
|
||||
if (isNull)
|
||||
newelem = (Datum) 0;
|
||||
else
|
||||
newelem = PG_GETARG_DATUM(1);
|
||||
}
|
||||
else if (arg1_elemid != InvalidOid)
|
||||
{
|
||||
v = PG_GETARG_ARRAYTYPE_P(1);
|
||||
element_type = ARR_ELEMTYPE(v);
|
||||
newelem = PG_GETARG_DATUM(0);
|
||||
if (PG_ARGISNULL(1))
|
||||
v = construct_empty_array(arg1_elemid);
|
||||
else
|
||||
v = PG_GETARG_ARRAYTYPE_P(1);
|
||||
isNull = PG_ARGISNULL(0);
|
||||
if (isNull)
|
||||
newelem = (Datum) 0;
|
||||
else
|
||||
newelem = PG_GETARG_DATUM(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -73,6 +86,8 @@ array_push(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_NULL(); /* keep compiler quiet */
|
||||
}
|
||||
|
||||
element_type = ARR_ELEMTYPE(v);
|
||||
|
||||
if (ARR_NDIM(v) == 1)
|
||||
{
|
||||
lb = ARR_LBOUND(v);
|
||||
@@ -84,11 +99,21 @@ array_push(PG_FUNCTION_ARGS)
|
||||
int ub = dimv[0] + lb[0] - 1;
|
||||
|
||||
indx = ub + 1;
|
||||
/* overflow? */
|
||||
if (indx < ub)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("integer out of range")));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* prepend newelem */
|
||||
indx = lb[0] - 1;
|
||||
/* overflow? */
|
||||
if (indx > lb[0])
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("integer out of range")));
|
||||
}
|
||||
}
|
||||
else if (ARR_NDIM(v) == 0)
|
||||
@@ -108,7 +133,7 @@ array_push(PG_FUNCTION_ARGS)
|
||||
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
|
||||
sizeof(ArrayMetaState));
|
||||
my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
|
||||
my_extra->element_type = InvalidOid;
|
||||
my_extra->element_type = ~element_type;
|
||||
}
|
||||
|
||||
if (my_extra->element_type != element_type)
|
||||
@@ -124,8 +149,8 @@ array_push(PG_FUNCTION_ARGS)
|
||||
typbyval = my_extra->typbyval;
|
||||
typalign = my_extra->typalign;
|
||||
|
||||
result = array_set(v, 1, &indx, newelem, -1,
|
||||
typlen, typbyval, typalign, &isNull);
|
||||
result = array_set(v, 1, &indx, newelem, isNull,
|
||||
-1, typlen, typbyval, typalign);
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
@@ -141,26 +166,46 @@ array_cat(PG_FUNCTION_ARGS)
|
||||
{
|
||||
ArrayType *v1,
|
||||
*v2;
|
||||
ArrayType *result;
|
||||
int *dims,
|
||||
*lbs,
|
||||
ndims,
|
||||
nitems,
|
||||
ndatabytes,
|
||||
nbytes;
|
||||
int *dims1,
|
||||
*lbs1,
|
||||
ndims1,
|
||||
nitems1,
|
||||
ndatabytes1;
|
||||
int *dims2,
|
||||
*lbs2,
|
||||
ndims2,
|
||||
nitems2,
|
||||
ndatabytes2;
|
||||
int i;
|
||||
char *dat1,
|
||||
*dat2;
|
||||
bits8 *bitmap1,
|
||||
*bitmap2;
|
||||
Oid element_type;
|
||||
Oid element_type1;
|
||||
Oid element_type2;
|
||||
ArrayType *result;
|
||||
int32 dataoffset;
|
||||
|
||||
/* Concatenating a null array is a no-op, just return the other input */
|
||||
if (PG_ARGISNULL(0))
|
||||
{
|
||||
if (PG_ARGISNULL(1))
|
||||
PG_RETURN_NULL();
|
||||
result = PG_GETARG_ARRAYTYPE_P(1);
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
if (PG_ARGISNULL(1))
|
||||
{
|
||||
result = PG_GETARG_ARRAYTYPE_P(0);
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
|
||||
v1 = PG_GETARG_ARRAYTYPE_P(0);
|
||||
v2 = PG_GETARG_ARRAYTYPE_P(1);
|
||||
@@ -223,8 +268,12 @@ array_cat(PG_FUNCTION_ARGS)
|
||||
dims2 = ARR_DIMS(v2);
|
||||
dat1 = ARR_DATA_PTR(v1);
|
||||
dat2 = ARR_DATA_PTR(v2);
|
||||
ndatabytes1 = ARR_SIZE(v1) - ARR_OVERHEAD(ndims1);
|
||||
ndatabytes2 = ARR_SIZE(v2) - ARR_OVERHEAD(ndims2);
|
||||
bitmap1 = ARR_NULLBITMAP(v1);
|
||||
bitmap2 = ARR_NULLBITMAP(v2);
|
||||
nitems1 = ArrayGetNItems(ndims1, dims1);
|
||||
nitems2 = ArrayGetNItems(ndims2, dims2);
|
||||
ndatabytes1 = ARR_SIZE(v1) - ARR_DATA_OFFSET(v1);
|
||||
ndatabytes2 = ARR_SIZE(v2) - ARR_DATA_OFFSET(v2);
|
||||
|
||||
if (ndims1 == ndims2)
|
||||
{
|
||||
@@ -310,20 +359,41 @@ array_cat(PG_FUNCTION_ARGS)
|
||||
}
|
||||
}
|
||||
|
||||
/* Do this mainly for overflow checking */
|
||||
nitems = ArrayGetNItems(ndims, dims);
|
||||
|
||||
/* build the result array */
|
||||
ndatabytes = ndatabytes1 + ndatabytes2;
|
||||
nbytes = ndatabytes + ARR_OVERHEAD(ndims);
|
||||
if (ARR_HASNULL(v1) || ARR_HASNULL(v2))
|
||||
{
|
||||
dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems);
|
||||
nbytes = ndatabytes + dataoffset;
|
||||
}
|
||||
else
|
||||
{
|
||||
dataoffset = 0; /* marker for no null bitmap */
|
||||
nbytes = ndatabytes + ARR_OVERHEAD_NONULLS(ndims);
|
||||
}
|
||||
result = (ArrayType *) palloc(nbytes);
|
||||
|
||||
result->size = nbytes;
|
||||
result->ndim = ndims;
|
||||
result->flags = 0;
|
||||
result->dataoffset = dataoffset;
|
||||
result->elemtype = element_type;
|
||||
memcpy(ARR_DIMS(result), dims, ndims * sizeof(int));
|
||||
memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int));
|
||||
/* data area is arg1 then arg2 */
|
||||
memcpy(ARR_DATA_PTR(result), dat1, ndatabytes1);
|
||||
memcpy(ARR_DATA_PTR(result) + ndatabytes1, dat2, ndatabytes2);
|
||||
/* handle the null bitmap if needed */
|
||||
if (ARR_HASNULL(result))
|
||||
{
|
||||
array_bitmap_copy(ARR_NULLBITMAP(result), 0,
|
||||
bitmap1, 0,
|
||||
nitems1);
|
||||
array_bitmap_copy(ARR_NULLBITMAP(result), nitems1,
|
||||
bitmap2, 0,
|
||||
nitems2);
|
||||
}
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
@@ -347,10 +417,6 @@ create_singleton_array(FunctionCallInfo fcinfo,
|
||||
int i;
|
||||
ArrayMetaState *my_extra;
|
||||
|
||||
if (element_type == 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("invalid array element type OID: %u", element_type)));
|
||||
if (ndims < 1)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
@@ -379,7 +445,7 @@ create_singleton_array(FunctionCallInfo fcinfo,
|
||||
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
|
||||
sizeof(ArrayMetaState));
|
||||
my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
|
||||
my_extra->element_type = InvalidOid;
|
||||
my_extra->element_type = ~element_type;
|
||||
}
|
||||
|
||||
if (my_extra->element_type != element_type)
|
||||
@@ -395,6 +461,6 @@ create_singleton_array(FunctionCallInfo fcinfo,
|
||||
typbyval = my_extra->typbyval;
|
||||
typalign = my_extra->typalign;
|
||||
|
||||
return construct_md_array(dvalues, ndims, dims, lbs, element_type,
|
||||
return construct_md_array(dvalues, NULL, ndims, dims, lbs, element_type,
|
||||
typlen, typbyval, typalign);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.18 2004/12/31 22:01:21 pgsql Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayutils.c,v 1.19 2005/11/17 22:14:52 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -16,11 +16,17 @@
|
||||
#include "postgres.h"
|
||||
|
||||
#include "utils/array.h"
|
||||
#include "utils/memutils.h"
|
||||
|
||||
|
||||
/* Convert subscript list into linear element number (from 0) */
|
||||
/*
|
||||
* Convert subscript list into linear element number (from 0)
|
||||
*
|
||||
* We assume caller has already range-checked the dimensions and subscripts,
|
||||
* so no overflow is possible.
|
||||
*/
|
||||
int
|
||||
ArrayGetOffset(int n, int *dim, int *lb, int *indx)
|
||||
ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx)
|
||||
{
|
||||
int i,
|
||||
scale = 1,
|
||||
@@ -34,11 +40,12 @@ ArrayGetOffset(int n, int *dim, int *lb, int *indx)
|
||||
return offset;
|
||||
}
|
||||
|
||||
/* Same, but subscripts are assumed 0-based, and use a scale array
|
||||
/*
|
||||
* Same, but subscripts are assumed 0-based, and use a scale array
|
||||
* instead of raw dimension data (see mda_get_prod to create scale array)
|
||||
*/
|
||||
int
|
||||
ArrayGetOffset0(int n, int *tup, int *scale)
|
||||
ArrayGetOffset0(int n, const int *tup, const int *scale)
|
||||
{
|
||||
int i,
|
||||
lin = 0;
|
||||
@@ -48,24 +55,66 @@ ArrayGetOffset0(int n, int *tup, int *scale)
|
||||
return lin;
|
||||
}
|
||||
|
||||
/* Convert array dimensions into number of elements */
|
||||
/*
|
||||
* Convert array dimensions into number of elements
|
||||
*
|
||||
* This must do overflow checking, since it is used to validate that a user
|
||||
* dimensionality request doesn't overflow what we can handle.
|
||||
*
|
||||
* We limit array sizes to at most about a quarter billion elements,
|
||||
* so that it's not necessary to check for overflow in quite so many
|
||||
* places --- for instance when palloc'ing Datum arrays.
|
||||
*
|
||||
* The multiplication overflow check only works on machines that have int64
|
||||
* arithmetic, but that is nearly all platforms these days, and doing check
|
||||
* divides for those that don't seems way too expensive.
|
||||
*/
|
||||
int
|
||||
ArrayGetNItems(int ndim, int *dims)
|
||||
ArrayGetNItems(int ndim, const int *dims)
|
||||
{
|
||||
int i,
|
||||
ret;
|
||||
int32 ret;
|
||||
int i;
|
||||
|
||||
#define MaxArraySize ((Size) (MaxAllocSize / sizeof(Datum)))
|
||||
|
||||
if (ndim <= 0)
|
||||
return 0;
|
||||
ret = 1;
|
||||
for (i = 0; i < ndim; i++)
|
||||
ret *= dims[i];
|
||||
return ret;
|
||||
{
|
||||
int64 prod;
|
||||
|
||||
/* A negative dimension implies that UB-LB overflowed ... */
|
||||
if (dims[i] < 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
||||
errmsg("array size exceeds the maximum allowed (%d)",
|
||||
(int) MaxArraySize)));
|
||||
|
||||
prod = (int64) ret * (int64) dims[i];
|
||||
ret = (int32) prod;
|
||||
if ((int64) ret != prod)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
||||
errmsg("array size exceeds the maximum allowed (%d)",
|
||||
(int) MaxArraySize)));
|
||||
}
|
||||
Assert(ret >= 0);
|
||||
if ((Size) ret > MaxArraySize)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
||||
errmsg("array size exceeds the maximum allowed (%d)",
|
||||
(int) MaxArraySize)));
|
||||
return (int) ret;
|
||||
}
|
||||
|
||||
/* Compute ranges (sub-array dimensions) for an array slice */
|
||||
/*
|
||||
* Compute ranges (sub-array dimensions) for an array slice
|
||||
*
|
||||
* We assume caller has validated slice endpoints, so overflow is impossible
|
||||
*/
|
||||
void
|
||||
mda_get_range(int n, int *span, int *st, int *endp)
|
||||
mda_get_range(int n, int *span, const int *st, const int *endp)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -73,9 +122,13 @@ mda_get_range(int n, int *span, int *st, int *endp)
|
||||
span[i] = endp[i] - st[i] + 1;
|
||||
}
|
||||
|
||||
/* Compute products of array dimensions, ie, scale factors for subscripts */
|
||||
/*
|
||||
* Compute products of array dimensions, ie, scale factors for subscripts
|
||||
*
|
||||
* We assume caller has validated dimensions, so overflow is impossible
|
||||
*/
|
||||
void
|
||||
mda_get_prod(int n, int *range, int *prod)
|
||||
mda_get_prod(int n, const int *range, int *prod)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -84,11 +137,14 @@ mda_get_prod(int n, int *range, int *prod)
|
||||
prod[i] = prod[i + 1] * range[i + 1];
|
||||
}
|
||||
|
||||
/* From products of whole-array dimensions and spans of a sub-array,
|
||||
/*
|
||||
* From products of whole-array dimensions and spans of a sub-array,
|
||||
* compute offset distances needed to step through subarray within array
|
||||
*
|
||||
* We assume caller has validated dimensions, so overflow is impossible
|
||||
*/
|
||||
void
|
||||
mda_get_offset_values(int n, int *dist, int *prod, int *span)
|
||||
mda_get_offset_values(int n, int *dist, const int *prod, const int *span)
|
||||
{
|
||||
int i,
|
||||
j;
|
||||
@@ -102,16 +158,18 @@ mda_get_offset_values(int n, int *dist, int *prod, int *span)
|
||||
}
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
generates the tuple that is lexicographically one greater than the current
|
||||
n-tuple in "curr", with the restriction that the i-th element of "curr" is
|
||||
less than the i-th element of "span".
|
||||
Returns -1 if no next tuple exists, else the subscript position (0..n-1)
|
||||
corresponding to the dimension to advance along.
|
||||
-----------------------------------------------------------------------------
|
||||
*/
|
||||
/*
|
||||
* Generates the tuple that is lexicographically one greater than the current
|
||||
* n-tuple in "curr", with the restriction that the i-th element of "curr" is
|
||||
* less than the i-th element of "span".
|
||||
*
|
||||
* Returns -1 if no next tuple exists, else the subscript position (0..n-1)
|
||||
* corresponding to the dimension to advance along.
|
||||
*
|
||||
* We assume caller has validated dimensions, so overflow is impossible
|
||||
*/
|
||||
int
|
||||
mda_next_tuple(int n, int *curr, int *span)
|
||||
mda_next_tuple(int n, int *curr, const int *span)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.115 2005/10/15 02:49:28 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.116 2005/11/17 22:14:52 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -1886,6 +1886,7 @@ check_float8_array(ArrayType *transarray, const char *caller)
|
||||
*/
|
||||
if (ARR_NDIM(transarray) != 1 ||
|
||||
ARR_DIMS(transarray)[0] != 3 ||
|
||||
ARR_HASNULL(transarray) ||
|
||||
ARR_ELEMTYPE(transarray) != FLOAT8OID)
|
||||
elog(ERROR, "%s: expected 3-element float8 array", caller);
|
||||
return (float8 *) ARR_DATA_PTR(transarray);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.68 2005/10/15 02:49:28 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.69 2005/11/17 22:14:53 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -133,7 +133,7 @@ buildint2vector(const int2 *int2s, int n)
|
||||
*/
|
||||
result->size = Int2VectorSize(n);
|
||||
result->ndim = 1;
|
||||
result->flags = 0;
|
||||
result->dataoffset = 0; /* never any nulls */
|
||||
result->elemtype = INT2OID;
|
||||
result->dim1 = n;
|
||||
result->lbound1 = 0;
|
||||
@@ -171,7 +171,7 @@ int2vectorin(PG_FUNCTION_ARGS)
|
||||
|
||||
result->size = Int2VectorSize(n);
|
||||
result->ndim = 1;
|
||||
result->flags = 0;
|
||||
result->dataoffset = 0; /* never any nulls */
|
||||
result->elemtype = INT2OID;
|
||||
result->dim1 = n;
|
||||
result->lbound1 = 0;
|
||||
@@ -220,9 +220,9 @@ int2vectorrecv(PG_FUNCTION_ARGS)
|
||||
ObjectIdGetDatum(INT2OID),
|
||||
Int32GetDatum(-1)));
|
||||
/* sanity checks: int2vector must be 1-D, no nulls */
|
||||
if (result->ndim != 1 ||
|
||||
result->flags != 0 ||
|
||||
result->elemtype != INT2OID)
|
||||
if (ARR_NDIM(result) != 1 ||
|
||||
ARR_HASNULL(result) ||
|
||||
ARR_ELEMTYPE(result) != INT2OID)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
|
||||
errmsg("invalid int2vector data")));
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* Copyright (c) 1998-2005, PostgreSQL Global Development Group
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.86 2005/10/15 02:49:29 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.87 2005/11/17 22:14:53 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -2070,7 +2070,7 @@ do_numeric_accum(ArrayType *transarray, Numeric newval)
|
||||
/* We assume the input is array of numeric */
|
||||
deconstruct_array(transarray,
|
||||
NUMERICOID, -1, false, 'i',
|
||||
&transdatums, &ndatums);
|
||||
&transdatums, NULL, &ndatums);
|
||||
if (ndatums != 3)
|
||||
elog(ERROR, "expected 3-element numeric array");
|
||||
N = transdatums[0];
|
||||
@@ -2161,7 +2161,7 @@ numeric_avg(PG_FUNCTION_ARGS)
|
||||
/* We assume the input is array of numeric */
|
||||
deconstruct_array(transarray,
|
||||
NUMERICOID, -1, false, 'i',
|
||||
&transdatums, &ndatums);
|
||||
&transdatums, NULL, &ndatums);
|
||||
if (ndatums != 3)
|
||||
elog(ERROR, "expected 3-element numeric array");
|
||||
N = DatumGetNumeric(transdatums[0]);
|
||||
@@ -2197,7 +2197,7 @@ numeric_variance(PG_FUNCTION_ARGS)
|
||||
/* We assume the input is array of numeric */
|
||||
deconstruct_array(transarray,
|
||||
NUMERICOID, -1, false, 'i',
|
||||
&transdatums, &ndatums);
|
||||
&transdatums, NULL, &ndatums);
|
||||
if (ndatums != 3)
|
||||
elog(ERROR, "expected 3-element numeric array");
|
||||
N = DatumGetNumeric(transdatums[0]);
|
||||
@@ -2273,7 +2273,7 @@ numeric_stddev(PG_FUNCTION_ARGS)
|
||||
/* We assume the input is array of numeric */
|
||||
deconstruct_array(transarray,
|
||||
NUMERICOID, -1, false, 'i',
|
||||
&transdatums, &ndatums);
|
||||
&transdatums, NULL, &ndatums);
|
||||
if (ndatums != 3)
|
||||
elog(ERROR, "expected 3-element numeric array");
|
||||
N = DatumGetNumeric(transdatums[0]);
|
||||
@@ -2511,7 +2511,8 @@ int2_avg_accum(PG_FUNCTION_ARGS)
|
||||
else
|
||||
transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
|
||||
|
||||
if (ARR_SIZE(transarray) != ARR_OVERHEAD(1) + sizeof(Int8TransTypeData))
|
||||
if (ARR_HASNULL(transarray) ||
|
||||
ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
|
||||
elog(ERROR, "expected 2-element int8 array");
|
||||
|
||||
transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
|
||||
@@ -2538,7 +2539,8 @@ int4_avg_accum(PG_FUNCTION_ARGS)
|
||||
else
|
||||
transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
|
||||
|
||||
if (ARR_SIZE(transarray) != ARR_OVERHEAD(1) + sizeof(Int8TransTypeData))
|
||||
if (ARR_HASNULL(transarray) ||
|
||||
ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
|
||||
elog(ERROR, "expected 2-element int8 array");
|
||||
|
||||
transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
|
||||
@@ -2556,7 +2558,8 @@ int8_avg(PG_FUNCTION_ARGS)
|
||||
Datum countd,
|
||||
sumd;
|
||||
|
||||
if (ARR_SIZE(transarray) != ARR_OVERHEAD(1) + sizeof(Int8TransTypeData))
|
||||
if (ARR_HASNULL(transarray) ||
|
||||
ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
|
||||
elog(ERROR, "expected 2-element int8 array");
|
||||
transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/oid.c,v 1.64 2005/10/15 02:49:29 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/oid.c,v 1.65 2005/11/17 22:14:53 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -176,7 +176,7 @@ buildoidvector(const Oid *oids, int n)
|
||||
*/
|
||||
result->size = OidVectorSize(n);
|
||||
result->ndim = 1;
|
||||
result->flags = 0;
|
||||
result->dataoffset = 0; /* never any nulls */
|
||||
result->elemtype = OIDOID;
|
||||
result->dim1 = n;
|
||||
result->lbound1 = 0;
|
||||
@@ -213,7 +213,7 @@ oidvectorin(PG_FUNCTION_ARGS)
|
||||
|
||||
result->size = OidVectorSize(n);
|
||||
result->ndim = 1;
|
||||
result->flags = 0;
|
||||
result->dataoffset = 0; /* never any nulls */
|
||||
result->elemtype = OIDOID;
|
||||
result->dim1 = n;
|
||||
result->lbound1 = 0;
|
||||
@@ -262,9 +262,9 @@ oidvectorrecv(PG_FUNCTION_ARGS)
|
||||
ObjectIdGetDatum(OIDOID),
|
||||
Int32GetDatum(-1)));
|
||||
/* sanity checks: oidvector must be 1-D, no nulls */
|
||||
if (result->ndim != 1 ||
|
||||
result->flags != 0 ||
|
||||
result->elemtype != OIDOID)
|
||||
if (ARR_NDIM(result) != 1 ||
|
||||
ARR_HASNULL(result) ||
|
||||
ARR_ELEMTYPE(result) != OIDOID)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
|
||||
errmsg("invalid oidvector data")));
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* back to source text
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.207 2005/10/15 02:49:29 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.208 2005/11/17 22:14:53 tgl Exp $
|
||||
*
|
||||
* This software is copyrighted by Jan Wieck - Hamburg.
|
||||
*
|
||||
@@ -1107,7 +1107,7 @@ decompile_column_index_array(Datum column_index_array, Oid relId,
|
||||
/* Extract data from array of int16 */
|
||||
deconstruct_array(DatumGetArrayTypeP(column_index_array),
|
||||
INT2OID, 2, true, 's',
|
||||
&keys, &nKeys);
|
||||
&keys, NULL, &nKeys);
|
||||
|
||||
for (j = 0; j < nKeys; j++)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.157 2005/10/27 02:45:22 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.158 2005/11/17 22:14:53 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -2434,7 +2434,7 @@ interval_accum(PG_FUNCTION_ARGS)
|
||||
|
||||
deconstruct_array(transarray,
|
||||
INTERVALOID, sizeof(Interval), false, 'd',
|
||||
&transdatums, &ndatums);
|
||||
&transdatums, NULL, &ndatums);
|
||||
if (ndatums != 2)
|
||||
elog(ERROR, "expected 2-element interval array");
|
||||
|
||||
@@ -2475,7 +2475,7 @@ interval_avg(PG_FUNCTION_ARGS)
|
||||
|
||||
deconstruct_array(transarray,
|
||||
INTERVALOID, sizeof(Interval), false, 'd',
|
||||
&transdatums, &ndatums);
|
||||
&transdatums, NULL, &ndatums);
|
||||
if (ndatums != 2)
|
||||
elog(ERROR, "expected 2-element interval array");
|
||||
|
||||
|
||||
7
src/backend/utils/cache/lsyscache.c
vendored
7
src/backend/utils/cache/lsyscache.c
vendored
@@ -7,7 +7,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/lsyscache.c,v 1.129 2005/10/15 02:49:31 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/lsyscache.c,v 1.130 2005/11/17 22:14:53 tgl Exp $
|
||||
*
|
||||
* NOTES
|
||||
* Eventually, the index information should go through here, too.
|
||||
@@ -1896,13 +1896,13 @@ get_attstatsslot(HeapTuple statstuple,
|
||||
elog(ERROR, "cache lookup failed for type %u", atttype);
|
||||
typeForm = (Form_pg_type) GETSTRUCT(typeTuple);
|
||||
|
||||
/* Deconstruct array into Datum elements */
|
||||
/* Deconstruct array into Datum elements; NULLs not expected */
|
||||
deconstruct_array(statarray,
|
||||
atttype,
|
||||
typeForm->typlen,
|
||||
typeForm->typbyval,
|
||||
typeForm->typalign,
|
||||
values, nvalues);
|
||||
values, NULL, nvalues);
|
||||
|
||||
/*
|
||||
* If the element type is pass-by-reference, we now have a bunch of
|
||||
@@ -1944,6 +1944,7 @@ get_attstatsslot(HeapTuple statstuple,
|
||||
*/
|
||||
narrayelem = ARR_DIMS(statarray)[0];
|
||||
if (ARR_NDIM(statarray) != 1 || narrayelem <= 0 ||
|
||||
ARR_HASNULL(statarray) ||
|
||||
ARR_ELEMTYPE(statarray) != FLOAT4OID)
|
||||
elog(ERROR, "stanumbers is not a 1-D float4 array");
|
||||
*numbers = (float4 *) palloc(narrayelem * sizeof(float4));
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* Copyright (c) 2002-2005, PostgreSQL Global Development Group
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/fmgr/funcapi.c,v 1.26 2005/10/15 02:49:32 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/fmgr/funcapi.c,v 1.27 2005/11/17 22:14:53 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -686,16 +686,18 @@ get_func_result_name(Oid functionId)
|
||||
numargs = ARR_DIMS(arr)[0];
|
||||
if (ARR_NDIM(arr) != 1 ||
|
||||
numargs < 0 ||
|
||||
ARR_HASNULL(arr) ||
|
||||
ARR_ELEMTYPE(arr) != CHAROID)
|
||||
elog(ERROR, "proargmodes is not a 1-D char array");
|
||||
argmodes = (char *) ARR_DATA_PTR(arr);
|
||||
arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
|
||||
if (ARR_NDIM(arr) != 1 ||
|
||||
ARR_DIMS(arr)[0] != numargs ||
|
||||
ARR_HASNULL(arr) ||
|
||||
ARR_ELEMTYPE(arr) != TEXTOID)
|
||||
elog(ERROR, "proargnames is not a 1-D text array");
|
||||
deconstruct_array(arr, TEXTOID, -1, false, 'i',
|
||||
&argnames, &nargnames);
|
||||
&argnames, NULL, &nargnames);
|
||||
Assert(nargnames == numargs);
|
||||
|
||||
/* scan for output argument(s) */
|
||||
@@ -818,12 +820,14 @@ build_function_result_tupdesc_d(Datum proallargtypes,
|
||||
numargs = ARR_DIMS(arr)[0];
|
||||
if (ARR_NDIM(arr) != 1 ||
|
||||
numargs < 0 ||
|
||||
ARR_HASNULL(arr) ||
|
||||
ARR_ELEMTYPE(arr) != OIDOID)
|
||||
elog(ERROR, "proallargtypes is not a 1-D Oid array");
|
||||
argtypes = (Oid *) ARR_DATA_PTR(arr);
|
||||
arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
|
||||
if (ARR_NDIM(arr) != 1 ||
|
||||
ARR_DIMS(arr)[0] != numargs ||
|
||||
ARR_HASNULL(arr) ||
|
||||
ARR_ELEMTYPE(arr) != CHAROID)
|
||||
elog(ERROR, "proargmodes is not a 1-D char array");
|
||||
argmodes = (char *) ARR_DATA_PTR(arr);
|
||||
@@ -832,10 +836,11 @@ build_function_result_tupdesc_d(Datum proallargtypes,
|
||||
arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
|
||||
if (ARR_NDIM(arr) != 1 ||
|
||||
ARR_DIMS(arr)[0] != numargs ||
|
||||
ARR_HASNULL(arr) ||
|
||||
ARR_ELEMTYPE(arr) != TEXTOID)
|
||||
elog(ERROR, "proargnames is not a 1-D text array");
|
||||
deconstruct_array(arr, TEXTOID, -1, false, 'i',
|
||||
&argnames, &nargnames);
|
||||
&argnames, NULL, &nargnames);
|
||||
Assert(nargnames == numargs);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
* Written by Peter Eisentraut <peter_e@gmx.net>.
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.299 2005/11/04 23:50:30 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.300 2005/11/17 22:14:54 tgl Exp $
|
||||
*
|
||||
*--------------------------------------------------------------------
|
||||
*/
|
||||
@@ -876,6 +876,16 @@ static struct config_bool ConfigureNamesBool[] =
|
||||
&check_function_bodies,
|
||||
true, NULL, NULL
|
||||
},
|
||||
{
|
||||
{"array_nulls", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
|
||||
gettext_noop("Enable input of NULL elements in arrays."),
|
||||
gettext_noop("When turned on, unquoted NULL in an array input "
|
||||
"value means a NULL value; "
|
||||
"otherwise it is taken literally.")
|
||||
},
|
||||
&Array_nulls,
|
||||
true, NULL, NULL
|
||||
},
|
||||
{
|
||||
{"default_with_oids", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
|
||||
gettext_noop("Create new tables with OIDs by default."),
|
||||
@@ -5383,14 +5393,13 @@ GUCArrayAdd(ArrayType *array, const char *name, const char *value)
|
||||
}
|
||||
}
|
||||
|
||||
isnull = false;
|
||||
a = array_set(array, 1, &index,
|
||||
datum,
|
||||
-1 /* varlenarray */ ,
|
||||
false,
|
||||
-1 /* varlena array */ ,
|
||||
-1 /* TEXT's typlen */ ,
|
||||
false /* TEXT's typbyval */ ,
|
||||
'i' /* TEXT's typalign */ ,
|
||||
&isnull);
|
||||
'i' /* TEXT's typalign */ );
|
||||
}
|
||||
else
|
||||
a = construct_array(&datum, 1,
|
||||
@@ -5456,14 +5465,13 @@ GUCArrayDelete(ArrayType *array, const char *name)
|
||||
/* else add it to the output array */
|
||||
if (newarray)
|
||||
{
|
||||
isnull = false;
|
||||
newarray = array_set(newarray, 1, &index,
|
||||
d,
|
||||
false,
|
||||
-1 /* varlenarray */ ,
|
||||
-1 /* TEXT's typlen */ ,
|
||||
false /* TEXT's typbyval */ ,
|
||||
'i' /* TEXT's typalign */ ,
|
||||
&isnull);
|
||||
'i' /* TEXT's typalign */ );
|
||||
}
|
||||
else
|
||||
newarray = construct_array(&d, 1,
|
||||
|
||||
@@ -413,10 +413,11 @@
|
||||
# - Previous Postgres Versions -
|
||||
|
||||
#add_missing_from = off
|
||||
#regex_flavor = advanced # advanced, extended, or basic
|
||||
#sql_inheritance = on
|
||||
#array_nulls = on
|
||||
#default_with_oids = off
|
||||
#escape_string_warning = off
|
||||
#regex_flavor = advanced # advanced, extended, or basic
|
||||
#sql_inheritance = on
|
||||
|
||||
# - Other Platforms & Clients -
|
||||
|
||||
|
||||
Reference in New Issue
Block a user