PostgreSQL Source Code git master
_int_gin.c
Go to the documentation of this file.
1/*
2 * contrib/intarray/_int_gin.c
3 */
4#include "postgres.h"
5
6#include "_int.h"
7#include "access/gin.h"
8#include "access/stratnum.h"
9
11
14{
15 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
16 StrategyNumber strategy = PG_GETARG_UINT16(2);
17 int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
18 Datum *res = NULL;
19
20 *nentries = 0;
21
22 if (strategy == BooleanSearchStrategy)
23 {
25 ITEM *items = GETQUERY(query);
26 int i;
27
28 /* empty query must fail */
29 if (query->size <= 0)
31
32 /*
33 * If the query doesn't have any required primitive values (for
34 * instance, it's something like '! 42'), we have to do a full index
35 * scan.
36 */
38 *searchMode = GIN_SEARCH_MODE_DEFAULT;
39 else
40 *searchMode = GIN_SEARCH_MODE_ALL;
41
42 /*
43 * Extract all the VAL items as things we want GIN to check for.
44 */
45 res = (Datum *) palloc(sizeof(Datum) * query->size);
46 *nentries = 0;
47
48 for (i = 0; i < query->size; i++)
49 {
50 if (items[i].type == VAL)
51 {
52 res[*nentries] = Int32GetDatum(items[i].val);
53 (*nentries)++;
54 }
55 }
56 }
57 else
58 {
60
61 CHECKARRVALID(query);
62 *nentries = ARRNELEMS(query);
63 if (*nentries > 0)
64 {
65 int32 *arr;
66 int32 i;
67
68 res = (Datum *) palloc(sizeof(Datum) * (*nentries));
69
70 arr = ARRPTR(query);
71 for (i = 0; i < *nentries; i++)
72 res[i] = Int32GetDatum(arr[i]);
73 }
74
75 switch (strategy)
76 {
78 *searchMode = GIN_SEARCH_MODE_DEFAULT;
79 break;
82 /* empty set is contained in everything */
84 break;
86 if (*nentries > 0)
87 *searchMode = GIN_SEARCH_MODE_DEFAULT;
88 else
90 break;
93 if (*nentries > 0)
94 *searchMode = GIN_SEARCH_MODE_DEFAULT;
95 else /* everything contains the empty set */
96 *searchMode = GIN_SEARCH_MODE_ALL;
97 break;
98 default:
99 elog(ERROR, "ginint4_queryextract: unknown strategy number: %d",
100 strategy);
101 }
102 }
103
105}
106
108
109Datum
111{
112 bool *check = (bool *) PG_GETARG_POINTER(0);
113 StrategyNumber strategy = PG_GETARG_UINT16(1);
114 int32 nkeys = PG_GETARG_INT32(3);
115
116 /* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
117 bool *recheck = (bool *) PG_GETARG_POINTER(5);
118 bool res = false;
119 int32 i;
120
121 switch (strategy)
122 {
124 /* result is not lossy */
125 *recheck = false;
126 /* at least one element in check[] is true, so result = true */
127 res = true;
128 break;
131 /* we will need recheck */
132 *recheck = true;
133 /* at least one element in check[] is true, so result = true */
134 res = true;
135 break;
137 /* we will need recheck */
138 *recheck = true;
139 /* Must have all elements in check[] true */
140 res = true;
141 for (i = 0; i < nkeys; i++)
142 {
143 if (!check[i])
144 {
145 res = false;
146 break;
147 }
148 }
149 break;
152 /* result is not lossy */
153 *recheck = false;
154 /* Must have all elements in check[] true */
155 res = true;
156 for (i = 0; i < nkeys; i++)
157 {
158 if (!check[i])
159 {
160 res = false;
161 break;
162 }
163 }
164 break;
166 {
168
169 /* result is not lossy */
170 *recheck = false;
171 res = gin_bool_consistent(query, check);
172 }
173 break;
174 default:
175 elog(ERROR, "ginint4_consistent: unknown strategy number: %d",
176 strategy);
177 }
178
180}
#define CHECKARRVALID(x)
Definition: _int.h:30
#define VAL
Definition: _int.h:162
bool query_has_required_values(QUERYTYPE *query)
Definition: _int_bool.c:397
#define GETQUERY(x)
Definition: _int.h:157
#define BooleanSearchStrategy
Definition: _int.h:134
#define PG_GETARG_QUERYTYPE_P(n)
Definition: _int.h:170
bool gin_bool_consistent(QUERYTYPE *query, bool *check)
Definition: _int_bool.c:334
Datum ginint4_consistent(PG_FUNCTION_ARGS)
Definition: _int_gin.c:110
PG_FUNCTION_INFO_V1(ginint4_queryextract)
Datum ginint4_queryextract(PG_FUNCTION_ARGS)
Definition: _int_gin.c:13
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
int32_t int32
Definition: c.h:484
#define ARRNELEMS(x)
Definition: cube.c:26
#define ARRPTR(x)
Definition: cube.c:25
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define GIN_SEARCH_MODE_ALL
Definition: gin.h:36
#define GIN_SEARCH_MODE_DEFAULT
Definition: gin.h:34
#define GIN_SEARCH_MODE_INCLUDE_EMPTY
Definition: gin.h:35
long val
Definition: informix.c:689
int i
Definition: isn.c:72
void * palloc(Size size)
Definition: mcxt.c:1317
uintptr_t Datum
Definition: postgres.h:69
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
#define RTOldContainsStrategyNumber
Definition: stratnum.h:63
uint16 StrategyNumber
Definition: stratnum.h:22
#define RTOverlapStrategyNumber
Definition: stratnum.h:53
#define RTSameStrategyNumber
Definition: stratnum.h:56
#define RTContainsStrategyNumber
Definition: stratnum.h:57
#define RTOldContainedByStrategyNumber
Definition: stratnum.h:64
#define RTContainedByStrategyNumber
Definition: stratnum.h:58
Definition: _int.h:141
int32 size
Definition: _int.h:150
static ItemArray items
Definition: test_tidstore.c:48
const char * type