-
Notifications
You must be signed in to change notification settings - Fork 5
/
hstore_ops.c
277 lines (238 loc) · 6.37 KB
/
hstore_ops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*-------------------------------------------------------------------------
*
* hstore_ops.c
* Definitions of gin_hstore_hash_ops operator class for hstore.
*
* Copyright (c) 2014, PostgreSQL Global Development Group
* Author: Alexander Korotkov <[email protected]>
*
* IDENTIFICATION
* contrib/hstore_ops/hstore_ops.c
*
* GIN keys in this opclass are 64-bit integers where higher 32-bits are hash
* of hstore key and lower 32-bits are hash of hstore value.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/gin.h"
#include "access/skey.h"
#include "catalog/pg_type.h"
#if PG_VERSION_NUM >= 130000
#include "common/hashfn.h"
#endif
#include "utils/hsearch.h"
#include "hstore.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(gin_compare_hstore_hash);
Datum gin_compare_hstore_hash(PG_FUNCTION_ARGS);
/*
* GIN comparison function: compare keys as _unsigned_ 64-bit integers. So, keys
* with same higher 32-bits will be together.
*/
Datum
gin_compare_hstore_hash(PG_FUNCTION_ARGS)
{
uint64 arg1 = (uint64)PG_GETARG_INT64(0);
uint64 arg2 = (uint64)PG_GETARG_INT64(1);
int32 result;
if (arg1 < arg2)
result = -1;
else if (arg1 == arg2)
result = 0;
else
result = 1;
PG_RETURN_INT32(result);
}
PG_FUNCTION_INFO_V1(gin_compare_partial_hstore_hash);
Datum gin_compare_partial_hstore_hash(PG_FUNCTION_ARGS);
/*
* GIN comparison function: select GIN keys with same hash of hstore key.
*/
Datum
gin_compare_partial_hstore_hash(PG_FUNCTION_ARGS)
{
uint64 partial_key = PG_GETARG_INT64(0);
uint64 key = PG_GETARG_INT64(1);
int32 result;
if ((key & 0xFFFFFFFF00000000) > partial_key)
{
result = 1;
}
else
{
if ((key & 0xFFFFFFFF00000000) == partial_key)
result = 0;
else
result = -1;
}
PG_RETURN_INT32(result);
}
/*
* Get hashed representation of key-value pair.
*/
static uint64
get_entry_hash(HEntry *hsent, char *ptr, int i)
{
uint64 result = 0;
result |= (uint64) tag_hash(HS_KEY(hsent, ptr, i), HS_KEYLEN(hsent, i)) << 32;
if (!HS_VALISNULL(hsent, i))
result |= (uint64) tag_hash(HS_VAL(hsent, ptr, i), HS_VALLEN(hsent, i));
return result;
}
/*
* Get hstore key hash in same format as hash of key-value pair.
*/
static uint64
get_key_hash(text *key)
{
uint64 result = 0;
result |= (uint64) tag_hash(VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key)) << 32;
return result;
}
PG_FUNCTION_INFO_V1(gin_extract_hstore_hash);
Datum gin_extract_hstore_hash(PG_FUNCTION_ARGS);
/*
* Split hstore into hashes of key-value pairs.
*/
Datum
gin_extract_hstore_hash(PG_FUNCTION_ARGS)
{
HStore *hs = PG_GETARG_HS(0);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
Datum *entries = NULL;
HEntry *hsent = ARRPTR(hs);
char *ptr = STRPTR(hs);
int count = HS_COUNT(hs);
int i;
*nentries = count;
if (count)
entries = (Datum *) palloc(sizeof(Datum) * count);
for (i = 0; i < count; ++i)
entries[i] = Int64GetDatum(get_entry_hash(hsent, ptr, i));
PG_RETURN_POINTER(entries);
}
PG_FUNCTION_INFO_V1(gin_extract_hstore_query_hash);
Datum gin_extract_hstore_query_hash(PG_FUNCTION_ARGS);
/*
* Provide relevant hashes for either hstore, key or array of keys.
*/
Datum
gin_extract_hstore_query_hash(PG_FUNCTION_ARGS)
{
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
StrategyNumber strategy = PG_GETARG_UINT16(2);
bool **pmatch = (bool **) PG_GETARG_POINTER(3);
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
Datum *entries;
if (strategy == HStoreContainsStrategyNumber)
{
/* Query is an hstore, so just apply gin_extract_hstore_hash... */
entries = (Datum *)
DatumGetPointer(DirectFunctionCall2(gin_extract_hstore_hash,
PG_GETARG_DATUM(0),
PointerGetDatum(nentries)));
/* ... except that "contains {}" requires a full index scan */
if (entries == NULL)
*searchMode = GIN_SEARCH_MODE_ALL;
}
else if (strategy == HStoreExistsStrategyNumber)
{
text *query = PG_GETARG_TEXT_PP(0);
*nentries = 1;
entries = (Datum *) palloc(sizeof(Datum));
*pmatch = (bool *) palloc(sizeof(bool));
entries[0] = Int64GetDatum(get_key_hash(query));
(*pmatch)[0] = true;
}
else if (strategy == HStoreExistsAnyStrategyNumber ||
strategy == HStoreExistsAllStrategyNumber)
{
ArrayType *query = PG_GETARG_ARRAYTYPE_P(0);
Datum *key_datums;
bool *key_nulls;
int key_count;
int i,
j;
deconstruct_array(query,
TEXTOID, -1, false, 'i',
&key_datums, &key_nulls, &key_count);
entries = (Datum *) palloc(sizeof(Datum) * key_count);
*pmatch = (bool *) palloc(sizeof(bool) * key_count);
for (i = 0, j = 0; i < key_count; ++i)
{
/* Nulls in the array are ignored, cf hstoreArrayToPairs */
if (key_nulls[i])
continue;
(*pmatch)[j] = true;
entries[j++] = Int64GetDatum(get_key_hash(DatumGetTextPP(key_datums[i])));
}
*nentries = j;
/* ExistsAll with no keys should match everything */
if (j == 0 && strategy == HStoreExistsAllStrategyNumber)
*searchMode = GIN_SEARCH_MODE_ALL;
}
else
{
elog(ERROR, "unrecognized strategy number: %d", strategy);
entries = NULL; /* keep compiler quiet */
}
PG_RETURN_POINTER(entries);
}
PG_FUNCTION_INFO_V1(gin_consistent_hstore_hash);
Datum gin_consistent_hstore_hash(PG_FUNCTION_ARGS);
/*
* Consistent
*/
Datum
gin_consistent_hstore_hash(PG_FUNCTION_ARGS)
{
bool *check = (bool *) PG_GETARG_POINTER(0);
StrategyNumber strategy = PG_GETARG_UINT16(1);
/* HStore *query = PG_GETARG_HS(2); */
int32 nkeys = PG_GETARG_INT32(3);
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
bool *recheck = (bool *) PG_GETARG_POINTER(5);
bool res = true;
int32 i;
/* All cases are inexact because of hashing */
*recheck = true;
if (strategy == HStoreContainsStrategyNumber)
{
/*
* Index doesn't have information about correspondence of keys and
* values, so we need recheck. However, if not all the keys are
* present, we can fail at once.
*/
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
}
else if (strategy == HStoreExistsStrategyNumber)
{
res = true;
}
else if (strategy == HStoreExistsAnyStrategyNumber)
{
res = true;
}
else if (strategy == HStoreExistsAllStrategyNumber)
{
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
}
else
elog(ERROR, "unrecognized strategy number: %d", strategy);
PG_RETURN_BOOL(res);
}