mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
First phase of work on array improvements. ARRAY[x,y,z] constructor
expressions, ARRAY(sub-SELECT) expressions, some array functions. Polymorphic functions using ANYARRAY/ANYELEMENT argument and return types. Some regression tests in place, documentation is lacking. Joe Conway, with some kibitzing from Tom Lane.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#
|
||||
# Makefile for utils/adt
|
||||
#
|
||||
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.53 2002/08/22 00:01:43 tgl Exp $
|
||||
# $Header: /cvsroot/pgsql/src/backend/utils/adt/Makefile,v 1.54 2003/04/08 23:20:02 tgl Exp $
|
||||
#
|
||||
|
||||
subdir = src/backend/utils/adt
|
||||
@@ -15,8 +15,8 @@ override CFLAGS+= -mieee
|
||||
endif
|
||||
endif
|
||||
|
||||
OBJS = acl.o arrayfuncs.o arrayutils.o bool.o cash.o char.o \
|
||||
date.o datetime.o datum.o float.o format_type.o \
|
||||
OBJS = acl.o arrayfuncs.o array_userfuncs.o arrayutils.o bool.o \
|
||||
cash.o char.o date.o datetime.o datum.o float.o format_type.o \
|
||||
geo_ops.o geo_selfuncs.o int.o int8.o like.o lockfuncs.o \
|
||||
misc.o nabstime.o name.o not_in.o numeric.o numutils.o \
|
||||
oid.o oracle_compat.o pseudotypes.o \
|
||||
|
||||
436
src/backend/utils/adt/array_userfuncs.c
Normal file
436
src/backend/utils/adt/array_userfuncs.c
Normal file
@@ -0,0 +1,436 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* array_userfuncs.c
|
||||
* Misc user-visible array support functions
|
||||
*
|
||||
* Copyright (c) 2003, PostgreSQL Global Development Group
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/array_userfuncs.c,v 1.1 2003/04/08 23:20:02 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include "catalog/pg_proc.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "utils/array.h"
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/syscache.h"
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* singleton_array :
|
||||
* Form a multi-dimensional array given one starting element.
|
||||
*
|
||||
* - first argument is the datum with which to build the array
|
||||
* - second argument is the number of dimensions the array should have;
|
||||
* defaults to 1 if no second argument is provided
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
Datum
|
||||
singleton_array(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid elem_type = get_fn_expr_argtype(fcinfo, 0);
|
||||
int ndims;
|
||||
|
||||
if (elem_type == InvalidOid)
|
||||
elog(ERROR, "Cannot determine input datatype");
|
||||
|
||||
if (PG_NARGS() == 2)
|
||||
ndims = PG_GETARG_INT32(1);
|
||||
else
|
||||
ndims = 1;
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(create_singleton_array(elem_type,
|
||||
PG_GETARG_DATUM(0),
|
||||
ndims));
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* array_push :
|
||||
* push an element onto either end of a one-dimensional array
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
Datum
|
||||
array_push(PG_FUNCTION_ARGS)
|
||||
{
|
||||
ArrayType *v;
|
||||
Datum newelem;
|
||||
int *dimv,
|
||||
*lb;
|
||||
ArrayType *result;
|
||||
int indx;
|
||||
bool isNull;
|
||||
Oid element_type;
|
||||
int16 typlen;
|
||||
bool typbyval;
|
||||
char typalign;
|
||||
Oid arg0_typeid = get_fn_expr_argtype(fcinfo, 0);
|
||||
Oid arg1_typeid = get_fn_expr_argtype(fcinfo, 1);
|
||||
Oid arg0_elemid;
|
||||
Oid arg1_elemid;
|
||||
|
||||
if (arg0_typeid == InvalidOid || arg1_typeid == InvalidOid)
|
||||
elog(ERROR, "array_push: cannot determine input data types");
|
||||
arg0_elemid = get_element_type(arg0_typeid);
|
||||
arg1_elemid = get_element_type(arg1_typeid);
|
||||
|
||||
if (arg0_elemid != InvalidOid)
|
||||
{
|
||||
v = PG_GETARG_ARRAYTYPE_P(0);
|
||||
element_type = ARR_ELEMTYPE(v);
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Shouldn't get here given proper type checking in parser */
|
||||
elog(ERROR, "array_push: neither input type is an array");
|
||||
PG_RETURN_NULL(); /* keep compiler quiet */
|
||||
}
|
||||
|
||||
/* Sanity check: do we have a one-dimensional array */
|
||||
if (ARR_NDIM(v) != 1)
|
||||
elog(ERROR, "Arrays greater than one-dimension are not supported");
|
||||
|
||||
lb = ARR_LBOUND(v);
|
||||
dimv = ARR_DIMS(v);
|
||||
if (arg0_elemid != InvalidOid)
|
||||
{
|
||||
/* append newelem */
|
||||
int ub = dimv[0] + lb[0] - 1;
|
||||
indx = ub + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* prepend newelem */
|
||||
indx = lb[0] - 1;
|
||||
}
|
||||
|
||||
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
|
||||
|
||||
result = array_set(v, 1, &indx, newelem, -1,
|
||||
typlen, typbyval, typalign, &isNull);
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* array_cat :
|
||||
* concatenate two nD arrays to form an (n+1)D array, or
|
||||
* push an (n-1)D array onto the end of an nD array
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
Datum
|
||||
array_cat(PG_FUNCTION_ARGS)
|
||||
{
|
||||
ArrayType *v1, *v2;
|
||||
int *dims, *lbs, ndims, ndatabytes, nbytes;
|
||||
int *dims1, *lbs1, ndims1, ndatabytes1;
|
||||
int *dims2, *lbs2, ndims2, ndatabytes2;
|
||||
char *dat1, *dat2;
|
||||
Oid element_type;
|
||||
Oid element_type1;
|
||||
Oid element_type2;
|
||||
ArrayType *result;
|
||||
|
||||
v1 = PG_GETARG_ARRAYTYPE_P(0);
|
||||
v2 = PG_GETARG_ARRAYTYPE_P(1);
|
||||
|
||||
/*
|
||||
* We must have one of the following combinations of inputs:
|
||||
* 1) two arrays with ndims1 == ndims2
|
||||
* 2) ndims1 == ndims2 - 1
|
||||
* 3) ndims1 == ndims2 + 1
|
||||
*/
|
||||
ndims1 = ARR_NDIM(v1);
|
||||
ndims2 = ARR_NDIM(v2);
|
||||
|
||||
if (ndims1 != ndims2 && ndims1 != ndims2 - 1 && ndims1 != ndims2 + 1)
|
||||
elog(ERROR, "Cannot concatenate incompatible arrays of %d and "
|
||||
"%d dimensions", ndims1, ndims2);
|
||||
|
||||
element_type1 = ARR_ELEMTYPE(v1);
|
||||
element_type2 = ARR_ELEMTYPE(v2);
|
||||
|
||||
/* Do we have a matching element types */
|
||||
if (element_type1 != element_type2)
|
||||
elog(ERROR, "Cannot concatenate incompatible arrays with element "
|
||||
"type %u and %u", element_type1, element_type2);
|
||||
|
||||
/* OK, use it */
|
||||
element_type = element_type1;
|
||||
|
||||
/* get argument array details */
|
||||
lbs1 = ARR_LBOUND(v1);
|
||||
lbs2 = ARR_LBOUND(v2);
|
||||
dims1 = ARR_DIMS(v1);
|
||||
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);
|
||||
|
||||
if (ndims1 == ndims2)
|
||||
{
|
||||
/*
|
||||
* resulting array has two element outer array made up of input
|
||||
* argument arrays
|
||||
*/
|
||||
int i;
|
||||
|
||||
ndims = ndims1 + 1;
|
||||
dims = (int *) palloc(ndims * sizeof(int));
|
||||
lbs = (int *) palloc(ndims * sizeof(int));
|
||||
|
||||
dims[0] = 2; /* outer array made up of two input arrays */
|
||||
lbs[0] = 1; /* start lower bound at 1 */
|
||||
|
||||
for (i = 0; i < ndims1; i++)
|
||||
{
|
||||
if (dims1[i] != dims2[i] || lbs1[i] != lbs2[i])
|
||||
elog(ERROR, "Cannot concatenate arrays with differing dimensions");
|
||||
|
||||
dims[i + 1] = dims1[i];
|
||||
lbs[i + 1] = lbs1[i];
|
||||
}
|
||||
}
|
||||
else if (ndims1 == ndims2 - 1)
|
||||
{
|
||||
/*
|
||||
* resulting array has the second argument as the outer array,
|
||||
* with the first argument appended to the front of the outer
|
||||
* dimension
|
||||
*/
|
||||
int i;
|
||||
|
||||
ndims = ndims2;
|
||||
dims = dims2;
|
||||
lbs = lbs2;
|
||||
|
||||
/* increment number of elements in outer array */
|
||||
dims[0] += 1;
|
||||
|
||||
/* make sure the added element matches our existing elements */
|
||||
for (i = 0; i < ndims1; i++)
|
||||
{
|
||||
if (dims1[i] != dims[i + 1] || lbs1[i] != lbs[i + 1])
|
||||
elog(ERROR, "Cannot concatenate arrays with differing dimensions");
|
||||
}
|
||||
}
|
||||
else /* (ndims1 == ndims2 + 1) */
|
||||
{
|
||||
/*
|
||||
* resulting array has the first argument as the outer array,
|
||||
* with the second argument appended to the end of the outer
|
||||
* dimension
|
||||
*/
|
||||
int i;
|
||||
|
||||
ndims = ndims1;
|
||||
dims = dims1;
|
||||
lbs = lbs1;
|
||||
|
||||
/* increment number of elements in outer array */
|
||||
dims[0] += 1;
|
||||
|
||||
/* make sure the added element matches our existing elements */
|
||||
for (i = 0; i < ndims2; i++)
|
||||
{
|
||||
if (dims2[i] != dims[i + 1] || lbs2[i] != lbs[i + 1])
|
||||
elog(ERROR, "Cannot concatenate arrays with differing dimensions");
|
||||
}
|
||||
}
|
||||
|
||||
/* build the result array */
|
||||
ndatabytes = ndatabytes1 + ndatabytes2;
|
||||
nbytes = ndatabytes + ARR_OVERHEAD(ndims);
|
||||
result = (ArrayType *) palloc(nbytes);
|
||||
|
||||
result->size = nbytes;
|
||||
result->ndim = ndims;
|
||||
result->flags = 0;
|
||||
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);
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* array_accum :
|
||||
* accumulator to build a 1-D array from input values -- this can be used
|
||||
* to create custom aggregates.
|
||||
*
|
||||
* This function is not marked strict, so we have to be careful about nulls.
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
Datum
|
||||
array_accum(PG_FUNCTION_ARGS)
|
||||
{
|
||||
/* return NULL if both arguments are NULL */
|
||||
if (PG_ARGISNULL(0) && PG_ARGISNULL(1))
|
||||
PG_RETURN_NULL();
|
||||
|
||||
/* create a new 1-D array from the new element if the array is NULL */
|
||||
if (PG_ARGISNULL(0))
|
||||
{
|
||||
Oid tgt_type = get_fn_expr_rettype(fcinfo);
|
||||
Oid tgt_elem_type;
|
||||
|
||||
if (tgt_type == InvalidOid)
|
||||
elog(ERROR, "Cannot determine target array type");
|
||||
tgt_elem_type = get_element_type(tgt_type);
|
||||
if (tgt_elem_type == InvalidOid)
|
||||
elog(ERROR, "Target type is not an array");
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(create_singleton_array(tgt_elem_type,
|
||||
PG_GETARG_DATUM(1),
|
||||
1));
|
||||
}
|
||||
|
||||
/* return the array if the new element is NULL */
|
||||
if (PG_ARGISNULL(1))
|
||||
PG_RETURN_ARRAYTYPE_P(PG_GETARG_ARRAYTYPE_P_COPY(0));
|
||||
|
||||
/*
|
||||
* Otherwise this is equivalent to array_push. We hack the call a little
|
||||
* so that array_push can see the fn_expr information.
|
||||
*/
|
||||
return array_push(fcinfo);
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* array_assign :
|
||||
* assign an element of an array to a new value and return the
|
||||
* redefined array
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
Datum
|
||||
array_assign(PG_FUNCTION_ARGS)
|
||||
{
|
||||
ArrayType *v;
|
||||
int idx_to_chg;
|
||||
Datum newelem;
|
||||
int *dimv,
|
||||
*lb, ub;
|
||||
ArrayType *result;
|
||||
bool isNull;
|
||||
Oid element_type;
|
||||
int16 typlen;
|
||||
bool typbyval;
|
||||
char typalign;
|
||||
|
||||
v = PG_GETARG_ARRAYTYPE_P(0);
|
||||
idx_to_chg = PG_GETARG_INT32(1);
|
||||
newelem = PG_GETARG_DATUM(2);
|
||||
|
||||
/* Sanity check: do we have a one-dimensional array */
|
||||
if (ARR_NDIM(v) != 1)
|
||||
elog(ERROR, "Arrays greater than one-dimension are not supported");
|
||||
|
||||
lb = ARR_LBOUND(v);
|
||||
dimv = ARR_DIMS(v);
|
||||
ub = dimv[0] + lb[0] - 1;
|
||||
if (idx_to_chg < lb[0] || idx_to_chg > ub)
|
||||
elog(ERROR, "Cannot alter nonexistent array element: %d", idx_to_chg);
|
||||
|
||||
element_type = ARR_ELEMTYPE(v);
|
||||
/* Sanity check: do we have a non-zero element type */
|
||||
if (element_type == 0)
|
||||
elog(ERROR, "Invalid array element type: %u", element_type);
|
||||
|
||||
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
|
||||
|
||||
result = array_set(v, 1, &idx_to_chg, newelem, -1,
|
||||
typlen, typbyval, typalign, &isNull);
|
||||
|
||||
PG_RETURN_ARRAYTYPE_P(result);
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* array_subscript :
|
||||
* return specific element of an array
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
Datum
|
||||
array_subscript(PG_FUNCTION_ARGS)
|
||||
{
|
||||
ArrayType *v;
|
||||
int idx;
|
||||
int *dimv,
|
||||
*lb, ub;
|
||||
Datum result;
|
||||
bool isNull;
|
||||
Oid element_type;
|
||||
int16 typlen;
|
||||
bool typbyval;
|
||||
char typalign;
|
||||
|
||||
v = PG_GETARG_ARRAYTYPE_P(0);
|
||||
idx = PG_GETARG_INT32(1);
|
||||
|
||||
/* Sanity check: do we have a one-dimensional array */
|
||||
if (ARR_NDIM(v) != 1)
|
||||
elog(ERROR, "Arrays greater than one-dimension are not supported");
|
||||
|
||||
lb = ARR_LBOUND(v);
|
||||
dimv = ARR_DIMS(v);
|
||||
ub = dimv[0] + lb[0] - 1;
|
||||
if (idx < lb[0] || idx > ub)
|
||||
elog(ERROR, "Cannot return nonexistent array element: %d", idx);
|
||||
|
||||
element_type = ARR_ELEMTYPE(v);
|
||||
/* Sanity check: do we have a non-zero element type */
|
||||
if (element_type == 0)
|
||||
elog(ERROR, "Invalid array element type: %u", element_type);
|
||||
|
||||
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
|
||||
|
||||
result = array_ref(v, 1, &idx, -1, typlen, typbyval, typalign, &isNull);
|
||||
|
||||
PG_RETURN_DATUM(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* actually does the work for singleton_array(), and array_accum() if it is
|
||||
* given a null input array.
|
||||
*/
|
||||
ArrayType *
|
||||
create_singleton_array(Oid element_type, Datum element, int ndims)
|
||||
{
|
||||
Datum dvalues[1];
|
||||
int16 typlen;
|
||||
bool typbyval;
|
||||
char typalign;
|
||||
int dims[MAXDIM];
|
||||
int lbs[MAXDIM];
|
||||
int i;
|
||||
|
||||
if (element_type == 0)
|
||||
elog(ERROR, "Invalid array element type: %u", element_type);
|
||||
if (ndims < 1 || ndims > MAXDIM)
|
||||
elog(ERROR, "Invalid number of dimensions %d", ndims);
|
||||
|
||||
dvalues[0] = element;
|
||||
|
||||
for (i = 0; i < ndims; i++)
|
||||
{
|
||||
dims[i] = 1;
|
||||
lbs[i] = 1;
|
||||
}
|
||||
|
||||
get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign);
|
||||
|
||||
return construct_md_array(dvalues, ndims, dims, lbs, element_type,
|
||||
typlen, typbyval, typalign);
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.86 2003/01/29 01:28:33 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.87 2003/04/08 23:20:02 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -21,7 +21,9 @@
|
||||
#include "catalog/pg_type.h"
|
||||
#include "parser/parse_coerce.h"
|
||||
#include "utils/array.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/lsyscache.h"
|
||||
#include "utils/syscache.h"
|
||||
|
||||
|
||||
@@ -763,7 +765,11 @@ array_length_coerce(PG_FUNCTION_ARGS)
|
||||
int32 len = PG_GETARG_INT32(1);
|
||||
bool isExplicit = PG_GETARG_BOOL(2);
|
||||
FmgrInfo *fmgr_info = fcinfo->flinfo;
|
||||
FmgrInfo *element_finfo;
|
||||
typedef struct {
|
||||
Oid elemtype;
|
||||
FmgrInfo coerce_finfo;
|
||||
} alc_extra;
|
||||
alc_extra *my_extra;
|
||||
FunctionCallInfoData locfcinfo;
|
||||
|
||||
/* If no typmod is provided, shortcircuit the whole thing */
|
||||
@@ -772,33 +778,38 @@ array_length_coerce(PG_FUNCTION_ARGS)
|
||||
|
||||
/*
|
||||
* We arrange to look up the element type's coercion function only
|
||||
* once per series of calls.
|
||||
* once per series of calls, assuming the element type doesn't change
|
||||
* underneath us.
|
||||
*/
|
||||
if (fmgr_info->fn_extra == NULL)
|
||||
my_extra = (alc_extra *) fmgr_info->fn_extra;
|
||||
if (my_extra == NULL)
|
||||
{
|
||||
fmgr_info->fn_extra = MemoryContextAlloc(fmgr_info->fn_mcxt,
|
||||
sizeof(alc_extra));
|
||||
my_extra = (alc_extra *) fmgr_info->fn_extra;
|
||||
my_extra->elemtype = InvalidOid;
|
||||
}
|
||||
|
||||
if (my_extra->elemtype != ARR_ELEMTYPE(v))
|
||||
{
|
||||
Oid funcId;
|
||||
int nargs;
|
||||
|
||||
fmgr_info->fn_extra = MemoryContextAlloc(fmgr_info->fn_mcxt,
|
||||
sizeof(FmgrInfo));
|
||||
element_finfo = (FmgrInfo *) fmgr_info->fn_extra;
|
||||
|
||||
funcId = find_typmod_coercion_function(ARR_ELEMTYPE(v), &nargs);
|
||||
|
||||
if (OidIsValid(funcId))
|
||||
fmgr_info_cxt(funcId, element_finfo, fmgr_info->fn_mcxt);
|
||||
fmgr_info_cxt(funcId, &my_extra->coerce_finfo, fmgr_info->fn_mcxt);
|
||||
else
|
||||
element_finfo->fn_oid = InvalidOid;
|
||||
my_extra->coerce_finfo.fn_oid = InvalidOid;
|
||||
my_extra->elemtype = ARR_ELEMTYPE(v);
|
||||
}
|
||||
else
|
||||
element_finfo = (FmgrInfo *) fmgr_info->fn_extra;
|
||||
|
||||
/*
|
||||
* If we didn't find a coercion function, return the array unmodified
|
||||
* (this should not happen in the normal course of things, but might
|
||||
* happen if this function is called manually).
|
||||
*/
|
||||
if (element_finfo->fn_oid == InvalidOid)
|
||||
if (my_extra->coerce_finfo.fn_oid == InvalidOid)
|
||||
PG_RETURN_ARRAYTYPE_P(v);
|
||||
|
||||
/*
|
||||
@@ -807,7 +818,7 @@ array_length_coerce(PG_FUNCTION_ARGS)
|
||||
* Note: we pass isExplicit whether or not the function wants it ...
|
||||
*/
|
||||
MemSet(&locfcinfo, 0, sizeof(locfcinfo));
|
||||
locfcinfo.flinfo = element_finfo;
|
||||
locfcinfo.flinfo = &my_extra->coerce_finfo;
|
||||
locfcinfo.nargs = 3;
|
||||
locfcinfo.arg[0] = PointerGetDatum(v);
|
||||
locfcinfo.arg[1] = Int32GetDatum(len);
|
||||
@@ -1617,19 +1628,63 @@ array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType)
|
||||
* NULL element values are not supported.
|
||||
*
|
||||
* NOTE: it would be cleaner to look up the elmlen/elmbval/elmalign info
|
||||
* from the system catalogs, given the elmtype. However, in most current
|
||||
* uses the type is hard-wired into the caller and so we can save a lookup
|
||||
* cycle by hard-wiring the type info as well.
|
||||
* from the system catalogs, given the elmtype. However, the caller is
|
||||
* in a better position to cache this info across multiple uses, or even
|
||||
* to hard-wire values if the element type is hard-wired.
|
||||
*----------
|
||||
*/
|
||||
ArrayType *
|
||||
construct_array(Datum *elems, int nelems,
|
||||
Oid elmtype,
|
||||
int elmlen, bool elmbyval, char elmalign)
|
||||
{
|
||||
int dims[1];
|
||||
int lbs[1];
|
||||
|
||||
dims[0] = nelems;
|
||||
lbs[0] = 1;
|
||||
|
||||
return construct_md_array(elems, 1, dims, lbs,
|
||||
elmtype, elmlen, elmbyval, elmalign);
|
||||
}
|
||||
|
||||
/*----------
|
||||
* construct_md_array --- simple method for constructing an array object
|
||||
* with arbitrary dimensions
|
||||
*
|
||||
* elems: array of Datum items to become the array contents
|
||||
* ndims: number of dimensions
|
||||
* dims: integer array with size of each dimension
|
||||
* lbs: integer array with lower bound of each dimension
|
||||
* elmtype, elmlen, elmbyval, elmalign: info for the datatype of the items
|
||||
*
|
||||
* A palloc'd ndims-D array object is constructed and returned. Note that
|
||||
* elem values will be copied into the object even if pass-by-ref type.
|
||||
* NULL element values are not supported.
|
||||
*
|
||||
* NOTE: it would be cleaner to look up the elmlen/elmbval/elmalign info
|
||||
* from the system catalogs, given the elmtype. However, the caller is
|
||||
* in a better position to cache this info across multiple uses, or even
|
||||
* to hard-wire values if the element type is hard-wired.
|
||||
*----------
|
||||
*/
|
||||
ArrayType *
|
||||
construct_md_array(Datum *elems,
|
||||
int ndims,
|
||||
int *dims,
|
||||
int *lbs,
|
||||
Oid elmtype, int elmlen, bool elmbyval, char elmalign)
|
||||
{
|
||||
ArrayType *result;
|
||||
int nbytes;
|
||||
int i;
|
||||
int nelems;
|
||||
|
||||
if (ndims < 1 || ndims > MAXDIM)
|
||||
elog(ERROR, "Number of array dimensions, %d, exceeds the maximum allowed (%d)",
|
||||
ndims, MAXDIM);
|
||||
|
||||
nelems = ArrayGetNItems(ndims, dims);
|
||||
|
||||
/* compute required space */
|
||||
if (elmlen > 0)
|
||||
@@ -1648,17 +1703,16 @@ construct_array(Datum *elems, int nelems,
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate and initialize 1-D result array */
|
||||
nbytes += ARR_OVERHEAD(1);
|
||||
/* Allocate and initialize ndims-D result array */
|
||||
nbytes += ARR_OVERHEAD(ndims);
|
||||
result = (ArrayType *) palloc(nbytes);
|
||||
|
||||
result->size = nbytes;
|
||||
result->ndim = 1;
|
||||
result->ndim = ndims;
|
||||
result->flags = 0;
|
||||
result->elemtype = elmtype;
|
||||
ARR_DIMS(result)[0] = nelems;
|
||||
ARR_LBOUND(result)[0] = 1;
|
||||
|
||||
memcpy((char *) ARR_DIMS(result), (char *) dims, ndims * sizeof(int));
|
||||
memcpy((char *) ARR_LBOUND(result), (char *) lbs, ndims * sizeof(int));
|
||||
CopyArrayEls(ARR_DATA_PTR(result), elems, nelems,
|
||||
elmlen, elmbyval, elmalign, false);
|
||||
|
||||
@@ -2035,3 +2089,82 @@ array_insert_slice(int ndim,
|
||||
/* don't miss any data at the end */
|
||||
memcpy(destPtr, origPtr, origEndpoint - origPtr);
|
||||
}
|
||||
|
||||
/*
|
||||
* array_type_coerce -- allow explicit or assignment coercion from
|
||||
* one array type to another.
|
||||
*
|
||||
* Caller should have already verified that the source element type can be
|
||||
* coerced into the target element type.
|
||||
*/
|
||||
Datum
|
||||
array_type_coerce(PG_FUNCTION_ARGS)
|
||||
{
|
||||
ArrayType *src = PG_GETARG_ARRAYTYPE_P(0);
|
||||
Oid src_elem_type = ARR_ELEMTYPE(src);
|
||||
FmgrInfo *fmgr_info = fcinfo->flinfo;
|
||||
typedef struct {
|
||||
Oid srctype;
|
||||
Oid desttype;
|
||||
FmgrInfo coerce_finfo;
|
||||
} atc_extra;
|
||||
atc_extra *my_extra;
|
||||
FunctionCallInfoData locfcinfo;
|
||||
|
||||
/*
|
||||
* We arrange to look up the coercion function only once per series of
|
||||
* calls, assuming the input data type doesn't change underneath us.
|
||||
* (Output type can't change.)
|
||||
*/
|
||||
my_extra = (atc_extra *) fmgr_info->fn_extra;
|
||||
if (my_extra == NULL)
|
||||
{
|
||||
fmgr_info->fn_extra = MemoryContextAlloc(fmgr_info->fn_mcxt,
|
||||
sizeof(atc_extra));
|
||||
my_extra = (atc_extra *) fmgr_info->fn_extra;
|
||||
my_extra->srctype = InvalidOid;
|
||||
}
|
||||
|
||||
if (my_extra->srctype != src_elem_type)
|
||||
{
|
||||
Oid tgt_type = get_fn_expr_rettype(fcinfo);
|
||||
Oid tgt_elem_type;
|
||||
Oid funcId;
|
||||
|
||||
if (tgt_type == InvalidOid)
|
||||
elog(ERROR, "Cannot determine target array type");
|
||||
tgt_elem_type = get_element_type(tgt_type);
|
||||
if (tgt_elem_type == InvalidOid)
|
||||
elog(ERROR, "Target type is not an array");
|
||||
|
||||
if (!find_coercion_pathway(tgt_elem_type, src_elem_type,
|
||||
COERCION_EXPLICIT, &funcId))
|
||||
{
|
||||
/* should never happen, but check anyway */
|
||||
elog(ERROR, "no conversion function from %s to %s",
|
||||
format_type_be(src_elem_type), format_type_be(tgt_elem_type));
|
||||
}
|
||||
if (OidIsValid(funcId))
|
||||
fmgr_info_cxt(funcId, &my_extra->coerce_finfo, fmgr_info->fn_mcxt);
|
||||
else
|
||||
my_extra->coerce_finfo.fn_oid = InvalidOid;
|
||||
my_extra->srctype = src_elem_type;
|
||||
my_extra->desttype = tgt_elem_type;
|
||||
}
|
||||
|
||||
/*
|
||||
* If it's binary-compatible, return the array unmodified.
|
||||
*/
|
||||
if (my_extra->coerce_finfo.fn_oid == InvalidOid)
|
||||
PG_RETURN_ARRAYTYPE_P(src);
|
||||
|
||||
/*
|
||||
* Use array_map to apply the function to each array element.
|
||||
*/
|
||||
MemSet(&locfcinfo, 0, sizeof(locfcinfo));
|
||||
locfcinfo.flinfo = &my_extra->coerce_finfo;
|
||||
locfcinfo.nargs = 1;
|
||||
locfcinfo.arg[0] = PointerGetDatum(src);
|
||||
|
||||
return array_map(&locfcinfo, my_extra->srctype, my_extra->desttype);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/pseudotypes.c,v 1.4 2002/09/04 20:31:28 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/pseudotypes.c,v 1.5 2003/04/08 23:20:02 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -238,3 +238,26 @@ opaque_out(PG_FUNCTION_ARGS)
|
||||
|
||||
PG_RETURN_VOID(); /* keep compiler quiet */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* anyelement_in - input routine for pseudo-type ANYELEMENT.
|
||||
*/
|
||||
Datum
|
||||
anyelement_in(PG_FUNCTION_ARGS)
|
||||
{
|
||||
elog(ERROR, "Cannot accept a constant of type %s", "ANYELEMENT");
|
||||
|
||||
PG_RETURN_VOID(); /* keep compiler quiet */
|
||||
}
|
||||
|
||||
/*
|
||||
* anyelement_out - output routine for pseudo-type ANYELEMENT.
|
||||
*/
|
||||
Datum
|
||||
anyelement_out(PG_FUNCTION_ARGS)
|
||||
{
|
||||
elog(ERROR, "Cannot display a value of type %s", "ANYELEMENT");
|
||||
|
||||
PG_RETURN_VOID(); /* keep compiler quiet */
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* back to source text
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.137 2003/03/20 18:58:02 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.138 2003/04/08 23:20:02 tgl Exp $
|
||||
*
|
||||
* This software is copyrighted by Jan Wieck - Hamburg.
|
||||
*
|
||||
@@ -2217,6 +2217,7 @@ get_rule_expr(Node *node, deparse_context *context,
|
||||
{
|
||||
ArrayRef *aref = (ArrayRef *) node;
|
||||
bool savevarprefix = context->varprefix;
|
||||
bool need_parens;
|
||||
List *lowlist;
|
||||
List *uplist;
|
||||
|
||||
@@ -2229,7 +2230,16 @@ get_rule_expr(Node *node, deparse_context *context,
|
||||
*/
|
||||
if (aref->refassgnexpr)
|
||||
context->varprefix = false;
|
||||
/*
|
||||
* Parenthesize the argument unless it's a simple Var.
|
||||
*/
|
||||
need_parens = (aref->refassgnexpr == NULL) &&
|
||||
!IsA(aref->refexpr, Var);
|
||||
if (need_parens)
|
||||
appendStringInfoChar(buf, '(');
|
||||
get_rule_expr((Node *) aref->refexpr, context, showimplicit);
|
||||
if (need_parens)
|
||||
appendStringInfoChar(buf, ')');
|
||||
context->varprefix = savevarprefix;
|
||||
lowlist = aref->reflowerindexpr;
|
||||
foreach(uplist, aref->refupperindexpr)
|
||||
@@ -2421,6 +2431,26 @@ get_rule_expr(Node *node, deparse_context *context,
|
||||
}
|
||||
break;
|
||||
|
||||
case T_ArrayExpr:
|
||||
{
|
||||
ArrayExpr *arrayexpr = (ArrayExpr *) node;
|
||||
List *element;
|
||||
char *sep;
|
||||
|
||||
appendStringInfo(buf, "ARRAY[");
|
||||
sep = "";
|
||||
foreach(element, arrayexpr->elements)
|
||||
{
|
||||
Node *e = (Node *) lfirst(element);
|
||||
|
||||
appendStringInfo(buf, sep);
|
||||
get_rule_expr(e, context, true);
|
||||
sep = ", ";
|
||||
}
|
||||
appendStringInfo(buf, "]");
|
||||
}
|
||||
break;
|
||||
|
||||
case T_CoalesceExpr:
|
||||
{
|
||||
CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
|
||||
@@ -2906,7 +2936,10 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
|
||||
char *sep;
|
||||
bool need_paren;
|
||||
|
||||
appendStringInfoChar(buf, '(');
|
||||
if (sublink->subLinkType == ARRAY_SUBLINK)
|
||||
appendStringInfo(buf, "ARRAY(");
|
||||
else
|
||||
appendStringInfoChar(buf, '(');
|
||||
|
||||
if (sublink->lefthand != NIL)
|
||||
{
|
||||
@@ -2967,6 +3000,7 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
|
||||
break;
|
||||
|
||||
case EXPR_SUBLINK:
|
||||
case ARRAY_SUBLINK:
|
||||
need_paren = false;
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user