1996-08-28 03:59:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* array.h
|
2005-11-17 23:14:56 +01:00
|
|
|
* Declarations for Postgres arrays.
|
|
|
|
*
|
|
|
|
* A standard varlena array has the following internal structure:
|
|
|
|
* <size> - total number of bytes (also, TOAST info flags)
|
|
|
|
* <ndim> - number of dimensions of the array
|
|
|
|
* <dataoffset> - offset to stored data, or 0 if no nulls bitmap
|
|
|
|
* <elemtype> - element type OID
|
|
|
|
* <dimensions> - length of each array axis (C array of int)
|
|
|
|
* <lower bnds> - lower boundary of each dimension (C array of int)
|
2005-11-22 19:17:34 +01:00
|
|
|
* <null bitmap> - bitmap showing locations of nulls (OPTIONAL)
|
2005-11-17 23:14:56 +01:00
|
|
|
* <actual data> - whatever is the stored data
|
|
|
|
*
|
|
|
|
* The <dimensions> and <lower bnds> arrays each have ndim elements.
|
|
|
|
*
|
|
|
|
* The <null bitmap> may be omitted if the array contains no NULL elements.
|
|
|
|
* If it is absent, the <dataoffset> field is zero and the offset to the
|
|
|
|
* stored data must be computed on-the-fly. If the bitmap is present,
|
|
|
|
* <dataoffset> is nonzero and is equal to the offset from the array start
|
|
|
|
* to the first data element (including any alignment padding). The bitmap
|
|
|
|
* follows the same conventions as tuple null bitmaps, ie, a 1 indicates
|
|
|
|
* a non-null entry and the LSB of each bitmap byte is used first.
|
|
|
|
*
|
|
|
|
* The actual data starts on a MAXALIGN boundary. Individual items in the
|
|
|
|
* array are aligned as specified by the array element type. They are
|
|
|
|
* stored in row-major order (last subscript varies most rapidly).
|
|
|
|
*
|
|
|
|
* NOTE: it is important that array elements of toastable datatypes NOT be
|
|
|
|
* toasted, since the tupletoaster won't know they are there. (We could
|
|
|
|
* support compressed toasted items; only out-of-line items are dangerous.
|
|
|
|
* However, it seems preferable to store such items uncompressed and allow
|
|
|
|
* the toaster to compress the whole array as one input.)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* The OIDVECTOR and INT2VECTOR datatypes are storage-compatible with
|
|
|
|
* generic arrays, but they support only one-dimensional arrays with no
|
|
|
|
* nulls (and no null bitmap).
|
|
|
|
*
|
|
|
|
* There are also some "fixed-length array" datatypes, such as NAME and
|
|
|
|
* POINT. These are simply a sequence of a fixed number of items each
|
|
|
|
* of a fixed-length datatype, with no overhead; the item size must be
|
|
|
|
* a multiple of its alignment requirement, because we do no padding.
|
|
|
|
* We support subscripting on these types, but array_in() and array_out()
|
|
|
|
* only work with varlena arrays.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2006-11-08 20:24:38 +01:00
|
|
|
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.60 2006/11/08 19:24:38 tgl Exp $
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef ARRAY_H
|
|
|
|
#define ARRAY_H
|
|
|
|
|
2000-05-29 23:02:32 +02:00
|
|
|
#include "fmgr.h"
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2000-07-17 05:05:41 +02:00
|
|
|
/*
|
|
|
|
* Arrays are varlena objects, so must meet the varlena convention that
|
|
|
|
* the first int32 of the object contains the total object size in bytes.
|
2005-03-29 02:17:27 +02:00
|
|
|
*
|
|
|
|
* CAUTION: if you change the header for ordinary arrays you will also
|
|
|
|
* need to change the headers for oidvector and int2vector!
|
2000-07-17 05:05:41 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2000-07-17 05:05:41 +02:00
|
|
|
int32 size; /* total array size (varlena requirement) */
|
1997-09-08 04:41:22 +02:00
|
|
|
int ndim; /* # of dimensions */
|
2005-11-17 23:14:56 +01:00
|
|
|
int32 dataoffset; /* offset to data, or 0 if no bitmap */
|
2002-08-26 19:54:02 +02:00
|
|
|
Oid elemtype; /* element type OID */
|
1997-09-08 22:59:27 +02:00
|
|
|
} ArrayType;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2003-06-27 02:33:26 +02:00
|
|
|
typedef struct ArrayBuildState
|
|
|
|
{
|
|
|
|
MemoryContext mcontext; /* where all the temp stuff is kept */
|
|
|
|
Datum *dvalues; /* array of accumulated Datums */
|
2005-11-17 23:14:56 +01:00
|
|
|
bool *dnulls; /* array of is-null flags for Datums */
|
2006-11-08 20:24:38 +01:00
|
|
|
int alen; /* allocated length of above arrays */
|
|
|
|
int nelems; /* number of valid entries in above arrays */
|
2003-06-27 02:33:26 +02:00
|
|
|
Oid element_type; /* data type of the Datums */
|
|
|
|
int16 typlen; /* needed info about datatype */
|
|
|
|
bool typbyval;
|
|
|
|
char typalign;
|
2003-08-08 23:42:59 +02:00
|
|
|
} ArrayBuildState;
|
2003-06-27 02:33:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* structure to cache type metadata needed for array manipulation
|
|
|
|
*/
|
|
|
|
typedef struct ArrayMetaState
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
Oid element_type;
|
|
|
|
int16 typlen;
|
|
|
|
bool typbyval;
|
|
|
|
char typalign;
|
|
|
|
char typdelim;
|
2004-06-06 02:41:28 +02:00
|
|
|
Oid typioparam;
|
2003-08-04 02:43:34 +02:00
|
|
|
Oid typiofunc;
|
|
|
|
FmgrInfo proc;
|
2003-08-08 23:42:59 +02:00
|
|
|
} ArrayMetaState;
|
2003-06-27 02:33:26 +02:00
|
|
|
|
2005-03-24 22:50:38 +01:00
|
|
|
/*
|
|
|
|
* private state needed by array_map (here because caller must provide it)
|
|
|
|
*/
|
|
|
|
typedef struct ArrayMapState
|
|
|
|
{
|
|
|
|
ArrayMetaState inp_extra;
|
|
|
|
ArrayMetaState ret_extra;
|
|
|
|
} ArrayMapState;
|
|
|
|
|
2000-07-17 05:05:41 +02:00
|
|
|
/*
|
|
|
|
* fmgr macros for array objects
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
#define DatumGetArrayTypeP(X) ((ArrayType *) PG_DETOAST_DATUM(X))
|
|
|
|
#define DatumGetArrayTypePCopy(X) ((ArrayType *) PG_DETOAST_DATUM_COPY(X))
|
|
|
|
#define PG_GETARG_ARRAYTYPE_P(n) DatumGetArrayTypeP(PG_GETARG_DATUM(n))
|
2000-07-17 05:05:41 +02:00
|
|
|
#define PG_GETARG_ARRAYTYPE_P_COPY(n) DatumGetArrayTypePCopy(PG_GETARG_DATUM(n))
|
2001-03-22 05:01:46 +01:00
|
|
|
#define PG_RETURN_ARRAYTYPE_P(x) PG_RETURN_POINTER(x)
|
2000-07-17 05:05:41 +02:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
2000-07-22 05:34:43 +02:00
|
|
|
* Access macros for array header fields.
|
|
|
|
*
|
1996-08-28 03:59:28 +02:00
|
|
|
* ARR_DIMS returns a pointer to an array of array dimensions (number of
|
|
|
|
* elements along the various array axes).
|
|
|
|
*
|
|
|
|
* ARR_LBOUND returns a pointer to an array of array lower bounds.
|
|
|
|
*
|
2004-09-16 05:15:54 +02:00
|
|
|
* That is: if the third axis of an array has elements 5 through 8, then
|
|
|
|
* ARR_DIMS(a)[2] == 4 and ARR_LBOUND(a)[2] == 5.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* Unlike C, the default lower bound is 1.
|
|
|
|
*/
|
2005-11-17 23:14:56 +01:00
|
|
|
#define ARR_SIZE(a) ((a)->size)
|
|
|
|
#define ARR_NDIM(a) ((a)->ndim)
|
|
|
|
#define ARR_HASNULL(a) ((a)->dataoffset != 0)
|
|
|
|
#define ARR_ELEMTYPE(a) ((a)->elemtype)
|
2000-07-22 05:34:43 +02:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
#define ARR_DIMS(a) \
|
2000-07-22 05:34:43 +02:00
|
|
|
((int *) (((char *) (a)) + sizeof(ArrayType)))
|
1996-08-28 03:59:28 +02:00
|
|
|
#define ARR_LBOUND(a) \
|
2000-07-22 05:34:43 +02:00
|
|
|
((int *) (((char *) (a)) + sizeof(ArrayType) + \
|
2005-11-17 23:14:56 +01:00
|
|
|
sizeof(int) * ARR_NDIM(a)))
|
|
|
|
|
|
|
|
#define ARR_NULLBITMAP(a) \
|
|
|
|
(ARR_HASNULL(a) ? \
|
|
|
|
(bits8 *) (((char *) (a)) + sizeof(ArrayType) + \
|
|
|
|
2 * sizeof(int) * ARR_NDIM(a)) \
|
|
|
|
: (bits8 *) NULL)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/*
|
2005-11-17 23:14:56 +01:00
|
|
|
* The total array header size (in bytes) for an array with the specified
|
|
|
|
* number of dimensions and total number of items.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
2005-11-17 23:14:56 +01:00
|
|
|
#define ARR_OVERHEAD_NONULLS(ndims) \
|
|
|
|
MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims))
|
|
|
|
#define ARR_OVERHEAD_WITHNULLS(ndims, nitems) \
|
|
|
|
MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims) + \
|
|
|
|
((nitems) + 7) / 8)
|
|
|
|
|
|
|
|
#define ARR_DATA_OFFSET(a) \
|
|
|
|
(ARR_HASNULL(a) ? (a)->dataoffset : ARR_OVERHEAD_NONULLS(ARR_NDIM(a)))
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2000-07-22 05:34:43 +02:00
|
|
|
/*
|
|
|
|
* Returns a pointer to the actual array data.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
2000-07-22 05:34:43 +02:00
|
|
|
#define ARR_DATA_PTR(a) \
|
2005-11-17 23:14:56 +01:00
|
|
|
(((char *) (a)) + ARR_DATA_OFFSET(a))
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
/*
|
|
|
|
* GUC parameter
|
|
|
|
*/
|
|
|
|
extern bool Array_nulls;
|
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
|
|
|
* prototypes for functions defined in arrayfuncs.c
|
|
|
|
*/
|
2000-06-13 09:35:40 +02:00
|
|
|
extern Datum array_in(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_out(PG_FUNCTION_ARGS);
|
2003-05-09 00:19:58 +02:00
|
|
|
extern Datum array_recv(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_send(PG_FUNCTION_ARGS);
|
2000-06-13 09:35:40 +02:00
|
|
|
extern Datum array_eq(PG_FUNCTION_ARGS);
|
2003-06-27 02:33:26 +02:00
|
|
|
extern Datum array_ne(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_lt(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_gt(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_le(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_ge(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum btarraycmp(PG_FUNCTION_ARGS);
|
2006-09-10 22:14:20 +02:00
|
|
|
extern Datum arrayoverlap(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum arraycontains(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum arraycontained(PG_FUNCTION_ARGS);
|
2000-06-13 09:35:40 +02:00
|
|
|
extern Datum array_dims(PG_FUNCTION_ARGS);
|
2002-11-08 18:27:03 +01:00
|
|
|
extern Datum array_lower(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_upper(PG_FUNCTION_ARGS);
|
2003-04-09 01:20:04 +02:00
|
|
|
extern Datum array_type_coerce(PG_FUNCTION_ARGS);
|
2004-06-16 03:27:00 +02:00
|
|
|
extern Datum array_type_length_coerce(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_length_coerce(PG_FUNCTION_ARGS);
|
2005-02-28 04:45:24 +01:00
|
|
|
extern Datum array_larger(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_smaller(PG_FUNCTION_ARGS);
|
2000-06-13 09:35:40 +02:00
|
|
|
|
|
|
|
extern Datum array_ref(ArrayType *array, int nSubscripts, int *indx,
|
2005-11-17 23:14:56 +01:00
|
|
|
int arraytyplen, int elmlen, bool elmbyval, char elmalign,
|
2002-08-26 19:54:02 +02:00
|
|
|
bool *isNull);
|
2000-06-13 09:35:40 +02:00
|
|
|
extern ArrayType *array_set(ArrayType *array, int nSubscripts, int *indx,
|
2005-11-17 23:14:56 +01:00
|
|
|
Datum dataValue, bool isNull,
|
|
|
|
int arraytyplen, int elmlen, bool elmbyval, char elmalign);
|
2000-07-22 05:34:43 +02:00
|
|
|
extern ArrayType *array_get_slice(ArrayType *array, int nSubscripts,
|
2001-03-22 05:01:46 +01:00
|
|
|
int *upperIndx, int *lowerIndx,
|
2005-11-17 23:14:56 +01:00
|
|
|
int arraytyplen, int elmlen, bool elmbyval, char elmalign);
|
2000-07-22 05:34:43 +02:00
|
|
|
extern ArrayType *array_set_slice(ArrayType *array, int nSubscripts,
|
2001-03-22 05:01:46 +01:00
|
|
|
int *upperIndx, int *lowerIndx,
|
2005-11-17 23:14:56 +01:00
|
|
|
ArrayType *srcArray, bool isNull,
|
|
|
|
int arraytyplen, int elmlen, bool elmbyval, char elmalign);
|
2000-07-22 05:34:43 +02:00
|
|
|
|
2005-03-24 22:50:38 +01:00
|
|
|
extern Datum array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType,
|
2005-10-15 04:49:52 +02:00
|
|
|
ArrayMapState *amstate);
|
2000-06-13 09:35:40 +02:00
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
extern void array_bitmap_copy(bits8 *destbitmap, int destoffset,
|
2005-11-22 19:17:34 +01:00
|
|
|
const bits8 *srcbitmap, int srcoffset,
|
|
|
|
int nitems);
|
2005-11-17 23:14:56 +01:00
|
|
|
|
2000-07-17 05:05:41 +02:00
|
|
|
extern ArrayType *construct_array(Datum *elems, int nelems,
|
2002-09-04 22:31:48 +02:00
|
|
|
Oid elmtype,
|
|
|
|
int elmlen, bool elmbyval, char elmalign);
|
2003-04-09 01:20:04 +02:00
|
|
|
extern ArrayType *construct_md_array(Datum *elems,
|
2005-11-17 23:14:56 +01:00
|
|
|
bool *nulls,
|
2003-08-04 02:43:34 +02:00
|
|
|
int ndims,
|
|
|
|
int *dims,
|
|
|
|
int *lbs,
|
|
|
|
Oid elmtype, int elmlen, bool elmbyval, char elmalign);
|
2005-11-17 23:14:56 +01:00
|
|
|
extern ArrayType *construct_empty_array(Oid elmtype);
|
2000-07-17 05:05:41 +02:00
|
|
|
extern void deconstruct_array(ArrayType *array,
|
2002-09-04 22:31:48 +02:00
|
|
|
Oid elmtype,
|
|
|
|
int elmlen, bool elmbyval, char elmalign,
|
2005-11-17 23:14:56 +01:00
|
|
|
Datum **elemsp, bool **nullsp, int *nelemsp);
|
2003-08-08 23:42:59 +02:00
|
|
|
extern ArrayBuildState *accumArrayResult(ArrayBuildState *astate,
|
2003-08-04 02:43:34 +02:00
|
|
|
Datum dvalue, bool disnull,
|
|
|
|
Oid element_type,
|
|
|
|
MemoryContext rcontext);
|
2003-08-08 23:42:59 +02:00
|
|
|
extern Datum makeArrayResult(ArrayBuildState *astate,
|
2003-08-04 02:43:34 +02:00
|
|
|
MemoryContext rcontext);
|
2003-08-08 23:42:59 +02:00
|
|
|
extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims,
|
2003-08-04 02:43:34 +02:00
|
|
|
int *dims, int *lbs, MemoryContext rcontext);
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* prototypes for functions defined in arrayutils.c
|
|
|
|
*/
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
extern int ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx);
|
|
|
|
extern int ArrayGetOffset0(int n, const int *tup, const int *scale);
|
|
|
|
extern int ArrayGetNItems(int ndim, const int *dims);
|
|
|
|
extern void mda_get_range(int n, int *span, const int *st, const int *endp);
|
|
|
|
extern void mda_get_prod(int n, const int *range, int *prod);
|
|
|
|
extern void mda_get_offset_values(int n, int *dist, const int *prod, const int *span);
|
|
|
|
extern int mda_next_tuple(int n, int *curr, const int *span);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
/*
|
|
|
|
* prototypes for functions defined in array_userfuncs.c
|
|
|
|
*/
|
|
|
|
extern Datum array_push(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum array_cat(PG_FUNCTION_ARGS);
|
|
|
|
|
2003-06-27 02:33:26 +02:00
|
|
|
extern ArrayType *create_singleton_array(FunctionCallInfo fcinfo,
|
2003-08-04 02:43:34 +02:00
|
|
|
Oid element_type,
|
|
|
|
Datum element,
|
|
|
|
int ndims);
|
2003-04-09 01:20:04 +02:00
|
|
|
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* ARRAY_H */
|