gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

459 行
17KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * array.h
  4. * Declarations for Postgres arrays.
  5. *
  6. * A standard varlena array has the following internal structure:
  7. * <vl_len_> - standard varlena header word
  8. * <ndim> - number of dimensions of the array
  9. * <dataoffset> - offset to stored data, or 0 if no nulls bitmap
  10. * <elemtype> - element type OID
  11. * <dimensions> - length of each array axis (C array of int)
  12. * <lower bnds> - lower boundary of each dimension (C array of int)
  13. * <null bitmap> - bitmap showing locations of nulls (OPTIONAL)
  14. * <actual data> - whatever is the stored data
  15. *
  16. * The <dimensions> and <lower bnds> arrays each have ndim elements.
  17. *
  18. * The <null bitmap> may be omitted if the array contains no NULL elements.
  19. * If it is absent, the <dataoffset> field is zero and the offset to the
  20. * stored data must be computed on-the-fly. If the bitmap is present,
  21. * <dataoffset> is nonzero and is equal to the offset from the array start
  22. * to the first data element (including any alignment padding). The bitmap
  23. * follows the same conventions as tuple null bitmaps, ie, a 1 indicates
  24. * a non-null entry and the LSB of each bitmap byte is used first.
  25. *
  26. * The actual data starts on a MAXALIGN boundary. Individual items in the
  27. * array are aligned as specified by the array element type. They are
  28. * stored in row-major order (last subscript varies most rapidly).
  29. *
  30. * NOTE: it is important that array elements of toastable datatypes NOT be
  31. * toasted, since the tupletoaster won't know they are there. (We could
  32. * support compressed toasted items; only out-of-line items are dangerous.
  33. * However, it seems preferable to store such items uncompressed and allow
  34. * the toaster to compress the whole array as one input.)
  35. *
  36. *
  37. * The OIDVECTOR and INT2VECTOR datatypes are storage-compatible with
  38. * generic arrays, but they support only one-dimensional arrays with no
  39. * nulls (and no null bitmap). They don't support being toasted, either.
  40. *
  41. * There are also some "fixed-length array" datatypes, such as NAME and
  42. * POINT. These are simply a sequence of a fixed number of items each
  43. * of a fixed-length datatype, with no overhead; the item size must be
  44. * a multiple of its alignment requirement, because we do no padding.
  45. * We support subscripting on these types, but array_in() and array_out()
  46. * only work with varlena arrays.
  47. *
  48. * In addition, arrays are a major user of the "expanded object" TOAST
  49. * infrastructure. This allows a varlena array to be converted to a
  50. * separate representation that may include "deconstructed" Datum/isnull
  51. * arrays holding the elements.
  52. *
  53. *
  54. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  55. * Portions Copyright (c) 1994, Regents of the University of California
  56. *
  57. * src/include/utils/array.h
  58. *
  59. *-------------------------------------------------------------------------
  60. */
  61. #ifndef ARRAY_H
  62. #define ARRAY_H
  63. #include "fmgr.h"
  64. #include "utils/expandeddatum.h"
  65. /* avoid including execnodes.h here */
  66. struct ExprState;
  67. struct ExprContext;
  68. /*
  69. * Arrays are varlena objects, so must meet the varlena convention that
  70. * the first int32 of the object contains the total object size in bytes.
  71. * Be sure to use VARSIZE() and SET_VARSIZE() to access it, though!
  72. *
  73. * CAUTION: if you change the header for ordinary arrays you will also
  74. * need to change the headers for oidvector and int2vector!
  75. */
  76. typedef struct
  77. {
  78. int32 vl_len_; /* varlena header (do not touch directly!) */
  79. int ndim; /* # of dimensions */
  80. int32 dataoffset; /* offset to data, or 0 if no bitmap */
  81. Oid elemtype; /* element type OID */
  82. } ArrayType;
  83. /*
  84. * An expanded array is contained within a private memory context (as
  85. * all expanded objects must be) and has a control structure as below.
  86. *
  87. * The expanded array might contain a regular "flat" array if that was the
  88. * original input and we've not modified it significantly. Otherwise, the
  89. * contents are represented by Datum/isnull arrays plus dimensionality and
  90. * type information. We could also have both forms, if we've deconstructed
  91. * the original array for access purposes but not yet changed it. For pass-
  92. * by-reference element types, the Datums would point into the flat array in
  93. * this situation. Once we start modifying array elements, new pass-by-ref
  94. * elements are separately palloc'd within the memory context.
  95. */
  96. #define EA_MAGIC 689375833 /* ID for debugging crosschecks */
  97. typedef struct ExpandedArrayHeader
  98. {
  99. /* Standard header for expanded objects */
  100. ExpandedObjectHeader hdr;
  101. /* Magic value identifying an expanded array (for debugging only) */
  102. int ea_magic;
  103. /* Dimensionality info (always valid) */
  104. int ndims; /* # of dimensions */
  105. int *dims; /* array dimensions */
  106. int *lbound; /* index lower bounds for each dimension */
  107. /* Element type info (always valid) */
  108. Oid element_type; /* element type OID */
  109. int16 typlen; /* needed info about element datatype */
  110. bool typbyval;
  111. char typalign;
  112. /*
  113. * If we have a Datum-array representation of the array, it's kept here;
  114. * else dvalues/dnulls are NULL. The dvalues and dnulls arrays are always
  115. * palloc'd within the object private context, but may change size from
  116. * time to time. For pass-by-ref element types, dvalues entries might
  117. * point either into the fstartptr..fendptr area, or to separately
  118. * palloc'd chunks. Elements should always be fully detoasted, as they
  119. * are in the standard flat representation.
  120. *
  121. * Even when dvalues is valid, dnulls can be NULL if there are no null
  122. * elements.
  123. */
  124. Datum *dvalues; /* array of Datums */
  125. bool *dnulls; /* array of is-null flags for Datums */
  126. int dvalueslen; /* allocated length of above arrays */
  127. int nelems; /* number of valid entries in above arrays */
  128. /*
  129. * flat_size is the current space requirement for the flat equivalent of
  130. * the expanded array, if known; otherwise it's 0. We store this to make
  131. * consecutive calls of get_flat_size cheap.
  132. */
  133. Size flat_size;
  134. /*
  135. * fvalue points to the flat representation if it is valid, else it is
  136. * NULL. If we have or ever had a flat representation then
  137. * fstartptr/fendptr point to the start and end+1 of its data area; this
  138. * is so that we can tell which Datum pointers point into the flat
  139. * representation rather than being pointers to separately palloc'd data.
  140. */
  141. ArrayType *fvalue; /* must be a fully detoasted array */
  142. char *fstartptr; /* start of its data area */
  143. char *fendptr; /* end+1 of its data area */
  144. } ExpandedArrayHeader;
  145. /*
  146. * Functions that can handle either a "flat" varlena array or an expanded
  147. * array use this union to work with their input. Don't refer to "flt";
  148. * instead, cast to ArrayType. This struct nominally requires 8-byte
  149. * alignment on 64-bit, but it's often used for an ArrayType having 4-byte
  150. * alignment. UBSan complains about referencing "flt" in such cases.
  151. */
  152. typedef union AnyArrayType
  153. {
  154. ArrayType flt;
  155. ExpandedArrayHeader xpn;
  156. } AnyArrayType;
  157. /*
  158. * working state for accumArrayResult() and friends
  159. * note that the input must be scalars (legal array elements)
  160. */
  161. typedef struct ArrayBuildState
  162. {
  163. MemoryContext mcontext; /* where all the temp stuff is kept */
  164. Datum *dvalues; /* array of accumulated Datums */
  165. bool *dnulls; /* array of is-null flags for Datums */
  166. int alen; /* allocated length of above arrays */
  167. int nelems; /* number of valid entries in above arrays */
  168. Oid element_type; /* data type of the Datums */
  169. int16 typlen; /* needed info about datatype */
  170. bool typbyval;
  171. char typalign;
  172. bool private_cxt; /* use private memory context */
  173. } ArrayBuildState;
  174. /*
  175. * working state for accumArrayResultArr() and friends
  176. * note that the input must be arrays, and the same array type is returned
  177. */
  178. typedef struct ArrayBuildStateArr
  179. {
  180. MemoryContext mcontext; /* where all the temp stuff is kept */
  181. char *data; /* accumulated data */
  182. bits8 *nullbitmap; /* bitmap of is-null flags, or NULL if none */
  183. int abytes; /* allocated length of "data" */
  184. int nbytes; /* number of bytes used so far */
  185. int aitems; /* allocated length of bitmap (in elements) */
  186. int nitems; /* total number of elements in result */
  187. int ndims; /* current dimensions of result */
  188. int dims[MAXDIM];
  189. int lbs[MAXDIM];
  190. Oid array_type; /* data type of the arrays */
  191. Oid element_type; /* data type of the array elements */
  192. bool private_cxt; /* use private memory context */
  193. } ArrayBuildStateArr;
  194. /*
  195. * working state for accumArrayResultAny() and friends
  196. * these functions handle both cases
  197. */
  198. typedef struct ArrayBuildStateAny
  199. {
  200. /* Exactly one of these is not NULL: */
  201. ArrayBuildState *scalarstate;
  202. ArrayBuildStateArr *arraystate;
  203. } ArrayBuildStateAny;
  204. /*
  205. * structure to cache type metadata needed for array manipulation
  206. */
  207. typedef struct ArrayMetaState
  208. {
  209. Oid element_type;
  210. int16 typlen;
  211. bool typbyval;
  212. char typalign;
  213. char typdelim;
  214. Oid typioparam;
  215. Oid typiofunc;
  216. FmgrInfo proc;
  217. } ArrayMetaState;
  218. /*
  219. * private state needed by array_map (here because caller must provide it)
  220. */
  221. typedef struct ArrayMapState
  222. {
  223. ArrayMetaState inp_extra;
  224. ArrayMetaState ret_extra;
  225. } ArrayMapState;
  226. /* ArrayIteratorData is private in arrayfuncs.c */
  227. typedef struct ArrayIteratorData *ArrayIterator;
  228. /* fmgr macros for regular varlena array objects */
  229. #define DatumGetArrayTypeP(X) ((ArrayType *) PG_DETOAST_DATUM(X))
  230. #define DatumGetArrayTypePCopy(X) ((ArrayType *) PG_DETOAST_DATUM_COPY(X))
  231. #define PG_GETARG_ARRAYTYPE_P(n) DatumGetArrayTypeP(PG_GETARG_DATUM(n))
  232. #define PG_GETARG_ARRAYTYPE_P_COPY(n) DatumGetArrayTypePCopy(PG_GETARG_DATUM(n))
  233. #define PG_RETURN_ARRAYTYPE_P(x) PG_RETURN_POINTER(x)
  234. /* fmgr macros for expanded array objects */
  235. #define PG_GETARG_EXPANDED_ARRAY(n) DatumGetExpandedArray(PG_GETARG_DATUM(n))
  236. #define PG_GETARG_EXPANDED_ARRAYX(n, metacache) \
  237. DatumGetExpandedArrayX(PG_GETARG_DATUM(n), metacache)
  238. #define PG_RETURN_EXPANDED_ARRAY(x) PG_RETURN_DATUM(EOHPGetRWDatum(&(x)->hdr))
  239. /* fmgr macros for AnyArrayType (ie, get either varlena or expanded form) */
  240. #define PG_GETARG_ANY_ARRAY_P(n) DatumGetAnyArrayP(PG_GETARG_DATUM(n))
  241. /*
  242. * Access macros for varlena array header fields.
  243. *
  244. * ARR_DIMS returns a pointer to an array of array dimensions (number of
  245. * elements along the various array axes).
  246. *
  247. * ARR_LBOUND returns a pointer to an array of array lower bounds.
  248. *
  249. * That is: if the third axis of an array has elements 5 through 8, then
  250. * ARR_DIMS(a)[2] == 4 and ARR_LBOUND(a)[2] == 5.
  251. *
  252. * Unlike C, the default lower bound is 1.
  253. */
  254. #define ARR_SIZE(a) VARSIZE(a)
  255. #define ARR_NDIM(a) ((a)->ndim)
  256. #define ARR_HASNULL(a) ((a)->dataoffset != 0)
  257. #define ARR_ELEMTYPE(a) ((a)->elemtype)
  258. #define ARR_DIMS(a) \
  259. ((int *) (((char *) (a)) + sizeof(ArrayType)))
  260. #define ARR_LBOUND(a) \
  261. ((int *) (((char *) (a)) + sizeof(ArrayType) + \
  262. sizeof(int) * ARR_NDIM(a)))
  263. #define ARR_NULLBITMAP(a) \
  264. (ARR_HASNULL(a) ? \
  265. (bits8 *) (((char *) (a)) + sizeof(ArrayType) + \
  266. 2 * sizeof(int) * ARR_NDIM(a)) \
  267. : (bits8 *) NULL)
  268. /*
  269. * The total array header size (in bytes) for an array with the specified
  270. * number of dimensions and total number of items.
  271. */
  272. #define ARR_OVERHEAD_NONULLS(ndims) \
  273. MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims))
  274. #define ARR_OVERHEAD_WITHNULLS(ndims, nitems) \
  275. MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims) + \
  276. ((nitems) + 7) / 8)
  277. #define ARR_DATA_OFFSET(a) \
  278. (ARR_HASNULL(a) ? (a)->dataoffset : ARR_OVERHEAD_NONULLS(ARR_NDIM(a)))
  279. /*
  280. * Returns a pointer to the actual array data.
  281. */
  282. #define ARR_DATA_PTR(a) \
  283. (((char *) (a)) + ARR_DATA_OFFSET(a))
  284. /*
  285. * Macros for working with AnyArrayType inputs. Beware multiple references!
  286. */
  287. #define AARR_NDIM(a) \
  288. (VARATT_IS_EXPANDED_HEADER(a) ? \
  289. (a)->xpn.ndims : ARR_NDIM((ArrayType *) (a)))
  290. #define AARR_HASNULL(a) \
  291. (VARATT_IS_EXPANDED_HEADER(a) ? \
  292. ((a)->xpn.dvalues != NULL ? (a)->xpn.dnulls != NULL : ARR_HASNULL((a)->xpn.fvalue)) : \
  293. ARR_HASNULL((ArrayType *) (a)))
  294. #define AARR_ELEMTYPE(a) \
  295. (VARATT_IS_EXPANDED_HEADER(a) ? \
  296. (a)->xpn.element_type : ARR_ELEMTYPE((ArrayType *) (a)))
  297. #define AARR_DIMS(a) \
  298. (VARATT_IS_EXPANDED_HEADER(a) ? \
  299. (a)->xpn.dims : ARR_DIMS((ArrayType *) (a)))
  300. #define AARR_LBOUND(a) \
  301. (VARATT_IS_EXPANDED_HEADER(a) ? \
  302. (a)->xpn.lbound : ARR_LBOUND((ArrayType *) (a)))
  303. /*
  304. * GUC parameter
  305. */
  306. extern bool Array_nulls;
  307. /*
  308. * prototypes for functions defined in arrayfuncs.c
  309. */
  310. extern void CopyArrayEls(ArrayType *array,
  311. Datum *values,
  312. bool *nulls,
  313. int nitems,
  314. int typlen,
  315. bool typbyval,
  316. char typalign,
  317. bool freedata);
  318. extern Datum array_get_element(Datum arraydatum, int nSubscripts, int *indx,
  319. int arraytyplen, int elmlen, bool elmbyval, char elmalign,
  320. bool *isNull);
  321. extern Datum array_set_element(Datum arraydatum, int nSubscripts, int *indx,
  322. Datum dataValue, bool isNull,
  323. int arraytyplen, int elmlen, bool elmbyval, char elmalign);
  324. extern Datum array_get_slice(Datum arraydatum, int nSubscripts,
  325. int *upperIndx, int *lowerIndx,
  326. bool *upperProvided, bool *lowerProvided,
  327. int arraytyplen, int elmlen, bool elmbyval, char elmalign);
  328. extern Datum array_set_slice(Datum arraydatum, int nSubscripts,
  329. int *upperIndx, int *lowerIndx,
  330. bool *upperProvided, bool *lowerProvided,
  331. Datum srcArrayDatum, bool isNull,
  332. int arraytyplen, int elmlen, bool elmbyval, char elmalign);
  333. extern Datum array_ref(ArrayType *array, int nSubscripts, int *indx,
  334. int arraytyplen, int elmlen, bool elmbyval, char elmalign,
  335. bool *isNull);
  336. extern ArrayType *array_set(ArrayType *array, int nSubscripts, int *indx,
  337. Datum dataValue, bool isNull,
  338. int arraytyplen, int elmlen, bool elmbyval, char elmalign);
  339. extern Datum array_map(Datum arrayd,
  340. struct ExprState *exprstate, struct ExprContext *econtext,
  341. Oid retType, ArrayMapState *amstate);
  342. extern void array_bitmap_copy(bits8 *destbitmap, int destoffset,
  343. const bits8 *srcbitmap, int srcoffset,
  344. int nitems);
  345. extern ArrayType *construct_array(Datum *elems, int nelems,
  346. Oid elmtype,
  347. int elmlen, bool elmbyval, char elmalign);
  348. extern ArrayType *construct_md_array(Datum *elems,
  349. bool *nulls,
  350. int ndims,
  351. int *dims,
  352. int *lbs,
  353. Oid elmtype, int elmlen, bool elmbyval, char elmalign);
  354. extern ArrayType *construct_empty_array(Oid elmtype);
  355. extern ExpandedArrayHeader *construct_empty_expanded_array(Oid element_type,
  356. MemoryContext parentcontext,
  357. ArrayMetaState *metacache);
  358. extern void deconstruct_array(ArrayType *array,
  359. Oid elmtype,
  360. int elmlen, bool elmbyval, char elmalign,
  361. Datum **elemsp, bool **nullsp, int *nelemsp);
  362. extern bool array_contains_nulls(ArrayType *array);
  363. extern ArrayBuildState *initArrayResult(Oid element_type,
  364. MemoryContext rcontext, bool subcontext);
  365. extern ArrayBuildState *accumArrayResult(ArrayBuildState *astate,
  366. Datum dvalue, bool disnull,
  367. Oid element_type,
  368. MemoryContext rcontext);
  369. extern Datum makeArrayResult(ArrayBuildState *astate,
  370. MemoryContext rcontext);
  371. extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims,
  372. int *dims, int *lbs, MemoryContext rcontext, bool release);
  373. extern ArrayBuildStateArr *initArrayResultArr(Oid array_type, Oid element_type,
  374. MemoryContext rcontext, bool subcontext);
  375. extern ArrayBuildStateArr *accumArrayResultArr(ArrayBuildStateArr *astate,
  376. Datum dvalue, bool disnull,
  377. Oid array_type,
  378. MemoryContext rcontext);
  379. extern Datum makeArrayResultArr(ArrayBuildStateArr *astate,
  380. MemoryContext rcontext, bool release);
  381. extern ArrayBuildStateAny *initArrayResultAny(Oid input_type,
  382. MemoryContext rcontext, bool subcontext);
  383. extern ArrayBuildStateAny *accumArrayResultAny(ArrayBuildStateAny *astate,
  384. Datum dvalue, bool disnull,
  385. Oid input_type,
  386. MemoryContext rcontext);
  387. extern Datum makeArrayResultAny(ArrayBuildStateAny *astate,
  388. MemoryContext rcontext, bool release);
  389. extern ArrayIterator array_create_iterator(ArrayType *arr, int slice_ndim, ArrayMetaState *mstate);
  390. extern bool array_iterate(ArrayIterator iterator, Datum *value, bool *isnull);
  391. extern void array_free_iterator(ArrayIterator iterator);
  392. /*
  393. * prototypes for functions defined in arrayutils.c
  394. */
  395. extern int ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx);
  396. extern int ArrayGetOffset0(int n, const int *tup, const int *scale);
  397. extern int ArrayGetNItems(int ndim, const int *dims);
  398. extern void mda_get_range(int n, int *span, const int *st, const int *endp);
  399. extern void mda_get_prod(int n, const int *range, int *prod);
  400. extern void mda_get_offset_values(int n, int *dist, const int *prod, const int *span);
  401. extern int mda_next_tuple(int n, int *curr, const int *span);
  402. extern int32 *ArrayGetIntegerTypmods(ArrayType *arr, int *n);
  403. /*
  404. * prototypes for functions defined in array_expanded.c
  405. */
  406. extern Datum expand_array(Datum arraydatum, MemoryContext parentcontext,
  407. ArrayMetaState *metacache);
  408. extern ExpandedArrayHeader *DatumGetExpandedArray(Datum d);
  409. extern ExpandedArrayHeader *DatumGetExpandedArrayX(Datum d,
  410. ArrayMetaState *metacache);
  411. extern AnyArrayType *DatumGetAnyArrayP(Datum d);
  412. extern void deconstruct_expanded_array(ExpandedArrayHeader *eah);
  413. #endif /* ARRAY_H */
上海开阖软件有限公司 沪ICP备12045867号-1