00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #ifndef ARRAY_H
00057 #define ARRAY_H
00058
00059 #include "fmgr.h"
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 typedef struct
00070 {
00071 int32 vl_len_;
00072 int ndim;
00073 int32 dataoffset;
00074 Oid elemtype;
00075 } ArrayType;
00076
00077
00078
00079
00080 typedef struct ArrayBuildState
00081 {
00082 MemoryContext mcontext;
00083 Datum *dvalues;
00084 bool *dnulls;
00085 int alen;
00086 int nelems;
00087 Oid element_type;
00088 int16 typlen;
00089 bool typbyval;
00090 char typalign;
00091 } ArrayBuildState;
00092
00093
00094
00095
00096 typedef struct ArrayMetaState
00097 {
00098 Oid element_type;
00099 int16 typlen;
00100 bool typbyval;
00101 char typalign;
00102 char typdelim;
00103 Oid typioparam;
00104 Oid typiofunc;
00105 FmgrInfo proc;
00106 } ArrayMetaState;
00107
00108
00109
00110
00111 typedef struct ArrayMapState
00112 {
00113 ArrayMetaState inp_extra;
00114 ArrayMetaState ret_extra;
00115 } ArrayMapState;
00116
00117
00118 typedef struct ArrayIteratorData *ArrayIterator;
00119
00120
00121
00122
00123 #define DatumGetArrayTypeP(X) ((ArrayType *) PG_DETOAST_DATUM(X))
00124 #define DatumGetArrayTypePCopy(X) ((ArrayType *) PG_DETOAST_DATUM_COPY(X))
00125 #define PG_GETARG_ARRAYTYPE_P(n) DatumGetArrayTypeP(PG_GETARG_DATUM(n))
00126 #define PG_GETARG_ARRAYTYPE_P_COPY(n) DatumGetArrayTypePCopy(PG_GETARG_DATUM(n))
00127 #define PG_RETURN_ARRAYTYPE_P(x) PG_RETURN_POINTER(x)
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 #define ARR_SIZE(a) VARSIZE(a)
00143 #define ARR_NDIM(a) ((a)->ndim)
00144 #define ARR_HASNULL(a) ((a)->dataoffset != 0)
00145 #define ARR_ELEMTYPE(a) ((a)->elemtype)
00146
00147 #define ARR_DIMS(a) \
00148 ((int *) (((char *) (a)) + sizeof(ArrayType)))
00149 #define ARR_LBOUND(a) \
00150 ((int *) (((char *) (a)) + sizeof(ArrayType) + \
00151 sizeof(int) * ARR_NDIM(a)))
00152
00153 #define ARR_NULLBITMAP(a) \
00154 (ARR_HASNULL(a) ? \
00155 (bits8 *) (((char *) (a)) + sizeof(ArrayType) + \
00156 2 * sizeof(int) * ARR_NDIM(a)) \
00157 : (bits8 *) NULL)
00158
00159
00160
00161
00162
00163 #define ARR_OVERHEAD_NONULLS(ndims) \
00164 MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims))
00165 #define ARR_OVERHEAD_WITHNULLS(ndims, nitems) \
00166 MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims) + \
00167 ((nitems) + 7) / 8)
00168
00169 #define ARR_DATA_OFFSET(a) \
00170 (ARR_HASNULL(a) ? (a)->dataoffset : ARR_OVERHEAD_NONULLS(ARR_NDIM(a)))
00171
00172
00173
00174
00175 #define ARR_DATA_PTR(a) \
00176 (((char *) (a)) + ARR_DATA_OFFSET(a))
00177
00178
00179
00180
00181
00182 extern bool Array_nulls;
00183
00184
00185
00186
00187 extern Datum array_in(PG_FUNCTION_ARGS);
00188 extern Datum array_out(PG_FUNCTION_ARGS);
00189 extern Datum array_recv(PG_FUNCTION_ARGS);
00190 extern Datum array_send(PG_FUNCTION_ARGS);
00191 extern Datum array_eq(PG_FUNCTION_ARGS);
00192 extern Datum array_ne(PG_FUNCTION_ARGS);
00193 extern Datum array_lt(PG_FUNCTION_ARGS);
00194 extern Datum array_gt(PG_FUNCTION_ARGS);
00195 extern Datum array_le(PG_FUNCTION_ARGS);
00196 extern Datum array_ge(PG_FUNCTION_ARGS);
00197 extern Datum btarraycmp(PG_FUNCTION_ARGS);
00198 extern Datum hash_array(PG_FUNCTION_ARGS);
00199 extern Datum arrayoverlap(PG_FUNCTION_ARGS);
00200 extern Datum arraycontains(PG_FUNCTION_ARGS);
00201 extern Datum arraycontained(PG_FUNCTION_ARGS);
00202 extern Datum array_ndims(PG_FUNCTION_ARGS);
00203 extern Datum array_dims(PG_FUNCTION_ARGS);
00204 extern Datum array_lower(PG_FUNCTION_ARGS);
00205 extern Datum array_upper(PG_FUNCTION_ARGS);
00206 extern Datum array_length(PG_FUNCTION_ARGS);
00207 extern Datum array_larger(PG_FUNCTION_ARGS);
00208 extern Datum array_smaller(PG_FUNCTION_ARGS);
00209 extern Datum generate_subscripts(PG_FUNCTION_ARGS);
00210 extern Datum generate_subscripts_nodir(PG_FUNCTION_ARGS);
00211 extern Datum array_fill(PG_FUNCTION_ARGS);
00212 extern Datum array_fill_with_lower_bounds(PG_FUNCTION_ARGS);
00213 extern Datum array_unnest(PG_FUNCTION_ARGS);
00214
00215 extern Datum array_ref(ArrayType *array, int nSubscripts, int *indx,
00216 int arraytyplen, int elmlen, bool elmbyval, char elmalign,
00217 bool *isNull);
00218 extern ArrayType *array_set(ArrayType *array, int nSubscripts, int *indx,
00219 Datum dataValue, bool isNull,
00220 int arraytyplen, int elmlen, bool elmbyval, char elmalign);
00221 extern ArrayType *array_get_slice(ArrayType *array, int nSubscripts,
00222 int *upperIndx, int *lowerIndx,
00223 int arraytyplen, int elmlen, bool elmbyval, char elmalign);
00224 extern ArrayType *array_set_slice(ArrayType *array, int nSubscripts,
00225 int *upperIndx, int *lowerIndx,
00226 ArrayType *srcArray, bool isNull,
00227 int arraytyplen, int elmlen, bool elmbyval, char elmalign);
00228
00229 extern Datum array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType,
00230 ArrayMapState *amstate);
00231
00232 extern void array_bitmap_copy(bits8 *destbitmap, int destoffset,
00233 const bits8 *srcbitmap, int srcoffset,
00234 int nitems);
00235
00236 extern ArrayType *construct_array(Datum *elems, int nelems,
00237 Oid elmtype,
00238 int elmlen, bool elmbyval, char elmalign);
00239 extern ArrayType *construct_md_array(Datum *elems,
00240 bool *nulls,
00241 int ndims,
00242 int *dims,
00243 int *lbs,
00244 Oid elmtype, int elmlen, bool elmbyval, char elmalign);
00245 extern ArrayType *construct_empty_array(Oid elmtype);
00246 extern void deconstruct_array(ArrayType *array,
00247 Oid elmtype,
00248 int elmlen, bool elmbyval, char elmalign,
00249 Datum **elemsp, bool **nullsp, int *nelemsp);
00250 extern bool array_contains_nulls(ArrayType *array);
00251 extern ArrayBuildState *accumArrayResult(ArrayBuildState *astate,
00252 Datum dvalue, bool disnull,
00253 Oid element_type,
00254 MemoryContext rcontext);
00255 extern Datum makeArrayResult(ArrayBuildState *astate,
00256 MemoryContext rcontext);
00257 extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims,
00258 int *dims, int *lbs, MemoryContext rcontext, bool release);
00259
00260 extern ArrayIterator array_create_iterator(ArrayType *arr, int slice_ndim);
00261 extern bool array_iterate(ArrayIterator iterator, Datum *value, bool *isnull);
00262 extern void array_free_iterator(ArrayIterator iterator);
00263
00264
00265
00266
00267
00268 extern int ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx);
00269 extern int ArrayGetOffset0(int n, const int *tup, const int *scale);
00270 extern int ArrayGetNItems(int ndim, const int *dims);
00271 extern void mda_get_range(int n, int *span, const int *st, const int *endp);
00272 extern void mda_get_prod(int n, const int *range, int *prod);
00273 extern void mda_get_offset_values(int n, int *dist, const int *prod, const int *span);
00274 extern int mda_next_tuple(int n, int *curr, const int *span);
00275 extern int32 *ArrayGetIntegerTypmods(ArrayType *arr, int *n);
00276
00277
00278
00279
00280 extern Datum array_push(PG_FUNCTION_ARGS);
00281 extern Datum array_cat(PG_FUNCTION_ARGS);
00282
00283 extern ArrayType *create_singleton_array(FunctionCallInfo fcinfo,
00284 Oid element_type,
00285 Datum element,
00286 bool isNull,
00287 int ndims);
00288
00289 extern Datum array_agg_transfn(PG_FUNCTION_ARGS);
00290 extern Datum array_agg_finalfn(PG_FUNCTION_ARGS);
00291
00292
00293
00294
00295 extern Datum array_typanalyze(PG_FUNCTION_ARGS);
00296
00297 #endif