PostgreSQL Source Code git master
Loading...
Searching...
No Matches
arrayaccess.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * arrayaccess.h
4 * Declarations for element-by-element access to Postgres arrays.
5 *
6 *
7 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/utils/arrayaccess.h
11 *
12 *-------------------------------------------------------------------------
13 */
14#ifndef ARRAYACCESS_H
15#define ARRAYACCESS_H
16
17#include "access/tupmacs.h"
18#include "utils/array.h"
19
20
21/*
22 * Functions for iterating through elements of a flat or expanded array.
23 * These require a state struct "array_iter iter".
24 *
25 * Use "array_iter_setup(&iter, arrayptr, ...);" to prepare to iterate,
26 * and "datumvar = array_iter_next(&iter, &isnullvar, index);" to fetch
27 * the next element into datumvar/isnullvar.
28 * "index" must be the zero-origin element number; we make caller provide
29 * this since caller is generally counting the elements anyway. Despite
30 * that, these functions can only fetch elements sequentially.
31 */
32
33typedef struct array_iter
34{
35 /* datumptr being NULL or not tells if we have flat or expanded array */
36
37 /* Fields used when we have an expanded array */
38 Datum *datumptr; /* Pointer to Datum array */
39 bool *isnullptr; /* Pointer to isnull array */
40
41 /* Fields used when we have a flat array */
42 char *dataptr; /* Current spot in the data area */
43 bits8 *bitmapptr; /* Current byte of the nulls bitmap, or NULL */
44 int bitmask; /* mask for current bit in nulls bitmap */
45
46 /* Fields used in both cases: data about array's element type */
47 int elmlen;
51
52
53static inline void
55 int elmlen, bool elmbyval, char elmalign)
56{
58 {
59 if (a->xpn.dvalues)
60 {
61 it->datumptr = a->xpn.dvalues;
62 it->isnullptr = a->xpn.dnulls;
63 /* we must fill all fields to prevent compiler warnings */
64 it->dataptr = NULL;
65 it->bitmapptr = NULL;
66 }
67 else
68 {
69 /* Work with flat array embedded in the expanded datum */
70 it->datumptr = NULL;
71 it->isnullptr = NULL;
72 it->dataptr = ARR_DATA_PTR(a->xpn.fvalue);
73 it->bitmapptr = ARR_NULLBITMAP(a->xpn.fvalue);
74 }
75 }
76 else
77 {
78 it->datumptr = NULL;
79 it->isnullptr = NULL;
80 it->dataptr = ARR_DATA_PTR((ArrayType *) a);
81 it->bitmapptr = ARR_NULLBITMAP((ArrayType *) a);
82 }
83 it->bitmask = 1;
84 it->elmlen = elmlen;
85 it->elmbyval = elmbyval;
86 it->elmalignby = typalign_to_alignby(elmalign);
87}
88
89static inline Datum
90array_iter_next(array_iter *it, bool *isnull, int i)
91{
92 Datum ret;
93
94 if (it->datumptr)
95 {
96 ret = it->datumptr[i];
97 *isnull = it->isnullptr ? it->isnullptr[i] : false;
98 }
99 else
100 {
101 if (it->bitmapptr && (*(it->bitmapptr) & it->bitmask) == 0)
102 {
103 *isnull = true;
104 ret = (Datum) 0;
105 }
106 else
107 {
108 *isnull = false;
109 ret = fetch_att(it->dataptr, it->elmbyval, it->elmlen);
110 it->dataptr = att_addlength_pointer(it->dataptr, it->elmlen,
111 it->dataptr);
112 it->dataptr = (char *) att_nominal_alignby(it->dataptr,
113 it->elmalignby);
114 }
115 it->bitmask <<= 1;
116 if (it->bitmask == 0x100)
117 {
118 if (it->bitmapptr)
119 it->bitmapptr++;
120 it->bitmask = 1;
121 }
122 }
123
124 return ret;
125}
126
127#endif /* ARRAYACCESS_H */
#define ARR_DATA_PTR(a)
Definition array.h:322
#define ARR_NULLBITMAP(a)
Definition array.h:300
static Datum array_iter_next(array_iter *it, bool *isnull, int i)
Definition arrayaccess.h:90
static void array_iter_setup(array_iter *it, AnyArrayType *a, int elmlen, bool elmbyval, char elmalign)
Definition arrayaccess.h:54
uint8_t uint8
Definition c.h:544
uint8 bits8
Definition c.h:553
#define VARATT_IS_EXPANDED_HEADER(PTR)
int a
Definition isn.c:73
int i
Definition isn.c:77
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
Datum * datumptr
Definition arrayaccess.h:38
bits8 * bitmapptr
Definition arrayaccess.h:43
bool * isnullptr
Definition arrayaccess.h:39
char * dataptr
Definition arrayaccess.h:42
uint8 elmalignby
Definition arrayaccess.h:49
bool elmbyval
Definition arrayaccess.h:48
#define att_nominal_alignby(cur_offset, attalignby)
Definition tupmacs.h:189
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition tupmacs.h:209
static uint8 typalign_to_alignby(char typalign)
Definition tupmacs.h:80
static Datum fetch_att(const void *T, bool attbyval, int attlen)
Definition tupmacs.h:50