|
9 | 9 | /* number ranges for compression */
|
10 | 10 | #define MAXNUMRANGE 100
|
11 | 11 |
|
12 |
| -/* dimension of array */ |
13 |
| -#define NDIM 1 |
14 |
| - |
15 | 12 | /* useful macros for accessing int4 arrays */
|
16 | 13 | #define ARRPTR(x) ( (int4 *) ARR_DATA_PTR(x) )
|
17 | 14 | #define ARRNELEMS(x) ArrayGetNItems(ARR_NDIM(x), ARR_DIMS(x))
|
18 | 15 |
|
19 |
| -/* reject arrays we can't handle; but allow a NULL or empty array */ |
| 16 | +/* reject arrays we can't handle; to wit, those containing nulls */ |
20 | 17 | #define CHECKARRVALID(x) \
|
21 | 18 | do { \
|
22 |
| - if (x) { \ |
23 |
| - if (ARR_NDIM(x) != NDIM && ARR_NDIM(x) != 0) \ |
24 |
| - ereport(ERROR, \ |
25 |
| - (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), \ |
26 |
| - errmsg("array must be one-dimensional"))); \ |
27 |
| - if (ARR_HASNULL(x)) \ |
28 |
| - ereport(ERROR, \ |
29 |
| - (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), \ |
30 |
| - errmsg("array must not contain nulls"))); \ |
31 |
| - } \ |
| 19 | + if (ARR_HASNULL(x) && array_contains_nulls(x)) \ |
| 20 | + ereport(ERROR, \ |
| 21 | + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), \ |
| 22 | + errmsg("array must not contain nulls"))); \ |
32 | 23 | } while(0)
|
33 | 24 |
|
34 |
| -#define ARRISVOID(x) ((x) == NULL || ARRNELEMS(x) == 0) |
| 25 | +#define ARRISEMPTY(x) (ARRNELEMS(x) == 0) |
35 | 26 |
|
| 27 | +/* sort the elements of the array */ |
36 | 28 | #define SORT(x) \
|
37 | 29 | do { \
|
38 |
| - if ( ARRNELEMS( x ) > 1 ) \ |
39 |
| - isort( ARRPTR( x ), ARRNELEMS( x ) ); \ |
| 30 | + int _nelems_ = ARRNELEMS(x); \ |
| 31 | + if (_nelems_ > 1) \ |
| 32 | + isort(ARRPTR(x), _nelems_); \ |
40 | 33 | } while(0)
|
41 | 34 |
|
| 35 | +/* sort the elements of the array and remove duplicates */ |
42 | 36 | #define PREPAREARR(x) \
|
43 | 37 | do { \
|
44 |
| - if ( ARRNELEMS( x ) > 1 ) \ |
45 |
| - if ( isort( ARRPTR( x ), ARRNELEMS( x ) ) ) \ |
46 |
| - x = _int_unique( x ); \ |
| 38 | + int _nelems_ = ARRNELEMS(x); \ |
| 39 | + if (_nelems_ > 1) \ |
| 40 | + if (isort(ARRPTR(x), _nelems_)) \ |
| 41 | + (x) = _int_unique(x); \ |
47 | 42 | } while(0)
|
48 | 43 |
|
49 | 44 | /* "wish" function */
|
@@ -90,14 +85,14 @@ typedef struct
|
90 | 85 | #define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
|
91 | 86 |
|
92 | 87 | /*
|
93 |
| -** types for functions |
94 |
| -*/ |
| 88 | + * types for functions |
| 89 | + */ |
95 | 90 | typedef ArrayType *(*formarray) (ArrayType *, ArrayType *);
|
96 | 91 | typedef void (*formfloat) (ArrayType *, float *);
|
97 | 92 |
|
98 | 93 | /*
|
99 |
| -** useful function |
100 |
| -*/ |
| 94 | + * useful functions |
| 95 | + */ |
101 | 96 | bool isort(int4 *a, int len);
|
102 | 97 | ArrayType *new_intArrayType(int num);
|
103 | 98 | ArrayType *copy_intArrayType(ArrayType *a);
|
@@ -133,36 +128,47 @@ typedef struct ITEM
|
133 | 128 | int4 val;
|
134 | 129 | } ITEM;
|
135 | 130 |
|
136 |
| -typedef struct |
| 131 | +typedef struct QUERYTYPE |
137 | 132 | {
|
138 | 133 | int32 vl_len_; /* varlena header (do not touch directly!) */
|
139 |
| - int4 size; |
140 |
| - char data[1]; |
| 134 | + int4 size; /* number of ITEMs */ |
| 135 | + ITEM items[1]; /* variable length array */ |
141 | 136 | } QUERYTYPE;
|
142 | 137 |
|
143 |
| -#define HDRSIZEQT (VARHDRSZ + sizeof(int4)) |
144 |
| -#define COMPUTESIZE(size) ( HDRSIZEQT + size * sizeof(ITEM) ) |
145 |
| -#define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT ) |
| 138 | +#define HDRSIZEQT offsetof(QUERYTYPE, items) |
| 139 | +#define COMPUTESIZE(size) ( HDRSIZEQT + (size) * sizeof(ITEM) ) |
| 140 | +#define GETQUERY(x) ( (x)->items ) |
146 | 141 |
|
| 142 | +/* "type" codes for ITEM */ |
147 | 143 | #define END 0
|
148 | 144 | #define ERR 1
|
149 | 145 | #define VAL 2
|
150 | 146 | #define OPR 3
|
151 | 147 | #define OPEN 4
|
152 | 148 | #define CLOSE 5
|
153 | 149 |
|
| 150 | +/* fmgr macros for QUERYTYPE objects */ |
| 151 | +#define DatumGetQueryTypeP(X) ((QUERYTYPE *) PG_DETOAST_DATUM(X)) |
| 152 | +#define DatumGetQueryTypePCopy(X) ((QUERYTYPE *) PG_DETOAST_DATUM_COPY(X)) |
| 153 | +#define PG_GETARG_QUERYTYPE_P(n) DatumGetQueryTypeP(PG_GETARG_DATUM(n)) |
| 154 | +#define PG_GETARG_QUERYTYPE_P_COPY(n) DatumGetQueryTypePCopy(PG_GETARG_DATUM(n)) |
| 155 | + |
154 | 156 | bool signconsistent(QUERYTYPE *query, BITVEC sign, bool calcnot);
|
155 | 157 | bool execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot);
|
156 |
| -bool ginconsistent(QUERYTYPE *query, bool *check); |
157 |
| -int4 shorterquery(ITEM *q, int4 len); |
158 | 158 |
|
159 |
| -int compASC(const void *a, const void *b); |
| 159 | +bool gin_bool_consistent(QUERYTYPE *query, bool *check); |
| 160 | +bool query_has_required_values(QUERYTYPE *query); |
160 | 161 |
|
| 162 | +int compASC(const void *a, const void *b); |
161 | 163 | int compDESC(const void *a, const void *b);
|
162 | 164 |
|
163 |
| -#define QSORT(a, direction) \ |
164 |
| -if (ARRNELEMS(a) > 1) \ |
165 |
| - qsort((void*)ARRPTR(a), ARRNELEMS(a),sizeof(int4), \ |
166 |
| - (direction) ? compASC : compDESC ) |
| 165 | +/* sort, either ascending or descending */ |
| 166 | +#define QSORT(a, direction) \ |
| 167 | + do { \ |
| 168 | + int _nelems_ = ARRNELEMS(a); \ |
| 169 | + if (_nelems_ > 1) \ |
| 170 | + qsort((void*) ARRPTR(a), _nelems_, sizeof(int4), \ |
| 171 | + (direction) ? compASC : compDESC ); \ |
| 172 | + } while(0) |
167 | 173 |
|
168 | 174 | #endif /* ___INT_H__ */
|
0 commit comments