mirror of
https://github.com/postgres/postgres.git
synced 2025-06-14 18:42:34 +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:
@ -1,16 +1,55 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* array.h
|
||||
* Utilities for the new array code. Contains prototypes from the
|
||||
* following files:
|
||||
* utils/adt/arrayfuncs.c
|
||||
* utils/adt/arrayutils.c
|
||||
* 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)
|
||||
* <null bitmap> - bitmap showing locations of nulls (OPTIONAL)
|
||||
* <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.
|
||||
*
|
||||
*
|
||||
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.55 2005/10/15 02:49:46 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/include/utils/array.h,v 1.56 2005/11/17 22:14:55 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -30,8 +69,7 @@ typedef struct
|
||||
{
|
||||
int32 size; /* total array size (varlena requirement) */
|
||||
int ndim; /* # of dimensions */
|
||||
int flags; /* implementation flags */
|
||||
/* flags field is currently unused, always zero. */
|
||||
int32 dataoffset; /* offset to data, or 0 if no bitmap */
|
||||
Oid elemtype; /* element type OID */
|
||||
} ArrayType;
|
||||
|
||||
@ -39,9 +77,10 @@ typedef struct ArrayBuildState
|
||||
{
|
||||
MemoryContext mcontext; /* where all the temp stuff is kept */
|
||||
Datum *dvalues; /* array of accumulated Datums */
|
||||
bool *dnulls; /* array of is-null flags for Datums */
|
||||
|
||||
/*
|
||||
* The allocated size of dvalues[] is always a multiple of
|
||||
* The allocated size of dvalues[] and dnulls[] is always a multiple of
|
||||
* ARRAY_ELEMS_CHUNKSIZE
|
||||
*/
|
||||
#define ARRAY_ELEMS_CHUNKSIZE 64
|
||||
@ -98,29 +137,48 @@ typedef struct ArrayMapState
|
||||
*
|
||||
* Unlike C, the default lower bound is 1.
|
||||
*/
|
||||
#define ARR_SIZE(a) (((ArrayType *) (a))->size)
|
||||
#define ARR_NDIM(a) (((ArrayType *) (a))->ndim)
|
||||
#define ARR_ELEMTYPE(a) (((ArrayType *) (a))->elemtype)
|
||||
#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)
|
||||
|
||||
#define ARR_DIMS(a) \
|
||||
((int *) (((char *) (a)) + sizeof(ArrayType)))
|
||||
#define ARR_LBOUND(a) \
|
||||
((int *) (((char *) (a)) + sizeof(ArrayType) + \
|
||||
(sizeof(int) * ARR_NDIM(a))))
|
||||
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)
|
||||
|
||||
/*
|
||||
* The total array header size for an array of dimension n (in bytes).
|
||||
* The total array header size (in bytes) for an array with the specified
|
||||
* number of dimensions and total number of items.
|
||||
*/
|
||||
#define ARR_OVERHEAD(n) \
|
||||
(MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (n)))
|
||||
#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)))
|
||||
|
||||
/*
|
||||
* Returns a pointer to the actual array data.
|
||||
*/
|
||||
#define ARR_DATA_PTR(a) \
|
||||
(((char *) (a)) + ARR_OVERHEAD(ARR_NDIM(a)))
|
||||
(((char *) (a)) + ARR_DATA_OFFSET(a))
|
||||
|
||||
|
||||
/*
|
||||
* GUC parameter
|
||||
*/
|
||||
extern bool Array_nulls;
|
||||
|
||||
/*
|
||||
* prototypes for functions defined in arrayfuncs.c
|
||||
*/
|
||||
@ -145,37 +203,40 @@ extern Datum array_larger(PG_FUNCTION_ARGS);
|
||||
extern Datum array_smaller(PG_FUNCTION_ARGS);
|
||||
|
||||
extern Datum array_ref(ArrayType *array, int nSubscripts, int *indx,
|
||||
int arraylen, int elmlen, bool elmbyval, char elmalign,
|
||||
int arraytyplen, int elmlen, bool elmbyval, char elmalign,
|
||||
bool *isNull);
|
||||
extern ArrayType *array_set(ArrayType *array, int nSubscripts, int *indx,
|
||||
Datum dataValue,
|
||||
int arraylen, int elmlen, bool elmbyval, char elmalign,
|
||||
bool *isNull);
|
||||
Datum dataValue, bool isNull,
|
||||
int arraytyplen, int elmlen, bool elmbyval, char elmalign);
|
||||
extern ArrayType *array_get_slice(ArrayType *array, int nSubscripts,
|
||||
int *upperIndx, int *lowerIndx,
|
||||
int arraylen, int elmlen, bool elmbyval, char elmalign,
|
||||
bool *isNull);
|
||||
int arraytyplen, int elmlen, bool elmbyval, char elmalign);
|
||||
extern ArrayType *array_set_slice(ArrayType *array, int nSubscripts,
|
||||
int *upperIndx, int *lowerIndx,
|
||||
ArrayType *srcArray,
|
||||
int arraylen, int elmlen, bool elmbyval, char elmalign,
|
||||
bool *isNull);
|
||||
ArrayType *srcArray, bool isNull,
|
||||
int arraytyplen, int elmlen, bool elmbyval, char elmalign);
|
||||
|
||||
extern Datum array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType,
|
||||
ArrayMapState *amstate);
|
||||
|
||||
extern void array_bitmap_copy(bits8 *destbitmap, int destoffset,
|
||||
const bits8 *srcbitmap, int srcoffset,
|
||||
int nitems);
|
||||
|
||||
extern ArrayType *construct_array(Datum *elems, int nelems,
|
||||
Oid elmtype,
|
||||
int elmlen, bool elmbyval, char elmalign);
|
||||
extern ArrayType *construct_md_array(Datum *elems,
|
||||
bool *nulls,
|
||||
int ndims,
|
||||
int *dims,
|
||||
int *lbs,
|
||||
Oid elmtype, int elmlen, bool elmbyval, char elmalign);
|
||||
extern ArrayType *construct_empty_array(Oid elmtype);
|
||||
extern void deconstruct_array(ArrayType *array,
|
||||
Oid elmtype,
|
||||
int elmlen, bool elmbyval, char elmalign,
|
||||
Datum **elemsp, int *nelemsp);
|
||||
Datum **elemsp, bool **nullsp, int *nelemsp);
|
||||
extern ArrayBuildState *accumArrayResult(ArrayBuildState *astate,
|
||||
Datum dvalue, bool disnull,
|
||||
Oid element_type,
|
||||
@ -189,13 +250,13 @@ extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims,
|
||||
* prototypes for functions defined in arrayutils.c
|
||||
*/
|
||||
|
||||
extern int ArrayGetOffset(int n, int *dim, int *lb, int *indx);
|
||||
extern int ArrayGetOffset0(int n, int *tup, int *scale);
|
||||
extern int ArrayGetNItems(int ndims, int *dims);
|
||||
extern void mda_get_range(int n, int *span, int *st, int *endp);
|
||||
extern void mda_get_prod(int n, int *range, int *prod);
|
||||
extern void mda_get_offset_values(int n, int *dist, int *prod, int *span);
|
||||
extern int mda_next_tuple(int n, int *curr, int *span);
|
||||
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);
|
||||
|
||||
/*
|
||||
* prototypes for functions defined in array_userfuncs.c
|
||||
|
Reference in New Issue
Block a user