Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * tsgistidx.c
4 : : * GiST support functions for tsvector_ops
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : *
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/utils/adt/tsgistidx.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : :
15 : : #include "postgres.h"
16 : :
17 : : #include "access/gist.h"
18 : : #include "access/heaptoast.h"
19 : : #include "access/reloptions.h"
20 : : #include "common/int.h"
21 : : #include "lib/qunique.h"
22 : : #include "port/pg_bitutils.h"
23 : : #include "tsearch/ts_utils.h"
24 : : #include "utils/fmgrprotos.h"
25 : : #include "utils/pg_crc.h"
26 : :
27 : :
28 : : /* tsvector_ops opclass options */
29 : : typedef struct
30 : : {
31 : : int32 vl_len_; /* varlena header (do not touch directly!) */
32 : : int siglen; /* signature length */
33 : : } GistTsVectorOptions;
34 : :
35 : : #define SIGLEN_DEFAULT (31 * 4)
36 : : #define SIGLEN_MAX GISTMaxIndexKeySize
37 : : #define GET_SIGLEN() (PG_HAS_OPCLASS_OPTIONS() ? \
38 : : ((GistTsVectorOptions *) PG_GET_OPCLASS_OPTIONS())->siglen : \
39 : : SIGLEN_DEFAULT)
40 : :
41 : : #define SIGLENBIT(siglen) ((siglen) * BITS_PER_BYTE)
42 : :
43 : : typedef char *BITVECP;
44 : :
45 : : #define LOOPBYTE(siglen) \
46 : : for (i = 0; i < siglen; i++)
47 : :
48 : : #define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
49 : : #define GETBITBYTE(x,i) ( ((char)(x)) >> (i) & 0x01 )
50 : : #define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
51 : : #define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )
52 : : #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
53 : :
54 : : #define HASHVAL(val, siglen) (((unsigned int)(val)) % SIGLENBIT(siglen))
55 : : #define HASH(sign, val, siglen) SETBIT((sign), HASHVAL(val, siglen))
56 : :
57 : : #define GETENTRY(vec,pos) ((SignTSVector *) DatumGetPointer((vec)->vector[(pos)].key))
58 : :
59 : : /*
60 : : * type of GiST index key
61 : : */
62 : :
63 : : typedef struct
64 : : {
65 : : int32 vl_len_; /* varlena header (do not touch directly!) */
66 : : int32 flag;
67 : : char data[FLEXIBLE_ARRAY_MEMBER];
68 : : } SignTSVector;
69 : :
70 : : #define ARRKEY 0x01
71 : : #define SIGNKEY 0x02
72 : : #define ALLISTRUE 0x04
73 : :
74 : : #define ISARRKEY(x) ( ((SignTSVector*)(x))->flag & ARRKEY )
75 : : #define ISSIGNKEY(x) ( ((SignTSVector*)(x))->flag & SIGNKEY )
76 : : #define ISALLTRUE(x) ( ((SignTSVector*)(x))->flag & ALLISTRUE )
77 : :
78 : : #define GTHDRSIZE ( VARHDRSZ + sizeof(int32) )
79 : : #define CALCGTSIZE(flag, len) ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int32)) : (((flag) & ALLISTRUE) ? 0 : (len)) ) )
80 : :
81 : : #define GETSIGN(x) ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )
82 : : #define GETSIGLEN(x)( VARSIZE(x) - GTHDRSIZE )
83 : : #define GETARR(x) ( (int32*)( (char*)(x)+GTHDRSIZE ) )
84 : : #define ARRNELEM(x) ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int32) )
85 : :
86 : : static int32 sizebitvec(BITVECP sign, int siglen);
87 : :
88 : : Datum
7039 tgl@sss.pgh.pa.us 89 :UBC 0 : gtsvectorin(PG_FUNCTION_ARGS)
90 : : {
91 : : /* There's no need to support input of gtsvectors */
92 [ # # ]: 0 : ereport(ERROR,
93 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
94 : : errmsg("cannot accept a value of type %s", "gtsvector")));
95 : :
96 : : PG_RETURN_VOID(); /* keep compiler quiet */
97 : : }
98 : :
99 : : Datum
100 : 0 : gtsvectorout(PG_FUNCTION_ARGS)
101 : : {
1553 peter@eisentraut.org 102 : 0 : SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
103 : : char *outbuf;
104 : :
7039 tgl@sss.pgh.pa.us 105 [ # # ]: 0 : if (ISARRKEY(key))
844 heikki.linnakangas@i 106 : 0 : outbuf = psprintf("%d unique words", (int) ARRNELEM(key));
107 : : else
108 : : {
1180 michael@paquier.xyz 109 [ # # ]: 0 : if (ISALLTRUE(key))
844 heikki.linnakangas@i 110 : 0 : outbuf = pstrdup("all true bits");
111 : : else
112 : : {
1180 michael@paquier.xyz 113 : 0 : int siglen = GETSIGLEN(key);
114 : 0 : int cnttrue = sizebitvec(GETSIGN(key), siglen);
115 : :
844 heikki.linnakangas@i 116 : 0 : outbuf = psprintf("%d true bits, %d false bits",
117 : 0 : cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
118 : : }
119 : : }
120 : :
7039 tgl@sss.pgh.pa.us 121 [ # # ]: 0 : PG_FREE_IF_COPY(key, 0);
122 : 0 : PG_RETURN_POINTER(outbuf);
123 : : }
124 : :
125 : : static int
7018 teodor@sigaev.ru 126 :CBC 2417424 : compareint(const void *va, const void *vb)
127 : : {
5269 peter_e@gmx.net 128 : 2417424 : int32 a = *((const int32 *) va);
129 : 2417424 : int32 b = *((const int32 *) vb);
130 : :
1016 nathan@postgresql.or 131 : 2417424 : return pg_cmp_s32(a, b);
132 : : }
133 : :
134 : : static void
2434 akorotkov@postgresql 135 : 50237 : makesign(BITVECP sign, SignTSVector *a, int siglen)
136 : : {
137 : : int32 k,
7039 tgl@sss.pgh.pa.us 138 : 50237 : len = ARRNELEM(a);
5269 peter_e@gmx.net 139 : 50237 : int32 *ptr = GETARR(a);
140 : :
1390 peter@eisentraut.org 141 [ + + - + : 50237 : MemSet(sign, 0, siglen);
- - - - -
- ]
7039 tgl@sss.pgh.pa.us 142 [ + + ]: 2802857 : for (k = 0; k < len; k++)
2434 akorotkov@postgresql 143 : 2752620 : HASH(sign, ptr[k], siglen);
7039 tgl@sss.pgh.pa.us 144 : 50237 : }
145 : :
146 : : static SignTSVector *
2434 akorotkov@postgresql 147 : 12970 : gtsvector_alloc(int flag, int len, BITVECP sign)
148 : : {
149 [ + + + + ]: 12970 : int size = CALCGTSIZE(flag, len);
150 : 12970 : SignTSVector *res = palloc(size);
151 : :
152 : 12970 : SET_VARSIZE(res, size);
153 : 12970 : res->flag = flag;
154 : :
155 [ + + + + ]: 12970 : if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
156 : 548 : memcpy(GETSIGN(res), sign, len);
157 : :
158 : 12970 : return res;
159 : : }
160 : :
161 : :
162 : : Datum
7039 tgl@sss.pgh.pa.us 163 : 10455 : gtsvector_compress(PG_FUNCTION_ARGS)
164 : : {
165 : 10455 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
2434 akorotkov@postgresql 166 [ + - ]: 10455 : int siglen = GET_SIGLEN();
7039 tgl@sss.pgh.pa.us 167 : 10455 : GISTENTRY *retval = entry;
168 : :
169 [ + + ]: 10455 : if (entry->leafkey)
170 : : { /* tsvector */
171 : 6104 : TSVector val = DatumGetTSVector(entry->key);
2434 akorotkov@postgresql 172 : 6104 : SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
173 : : int32 len;
174 : : int32 *arr;
7039 tgl@sss.pgh.pa.us 175 : 6104 : WordEntry *ptr = ARRPTR(val);
176 : 6104 : char *words = STRPTR(val);
177 : :
178 : 6104 : arr = GETARR(res);
179 : 6104 : len = val->size;
180 [ + + ]: 351712 : while (len--)
181 : : {
182 : : pg_crc32 c;
183 : :
4407 heikki.linnakangas@i 184 : 345608 : INIT_LEGACY_CRC32(c);
185 [ + + ]: 1036832 : COMP_LEGACY_CRC32(c, words + ptr->pos, ptr->len);
186 : 345608 : FIN_LEGACY_CRC32(c);
187 : :
5269 peter_e@gmx.net 188 : 345608 : *arr = *(int32 *) &c;
7039 tgl@sss.pgh.pa.us 189 : 345608 : arr++;
190 : 345608 : ptr++;
191 : : }
192 : :
2578 tmunro@postgresql.or 193 : 6104 : qsort(GETARR(res), val->size, sizeof(int), compareint);
194 : 6104 : len = qunique(GETARR(res), val->size, sizeof(int), compareint);
7039 tgl@sss.pgh.pa.us 195 [ - + ]: 6104 : if (len != val->size)
196 : : {
197 : : /*
198 : : * there is a collision of hash-function; len is always less than
199 : : * val->size
200 : : */
7039 tgl@sss.pgh.pa.us 201 :UBC 0 : len = CALCGTSIZE(ARRKEY, len);
1390 peter@eisentraut.org 202 : 0 : res = (SignTSVector *) repalloc(res, len);
7039 tgl@sss.pgh.pa.us 203 : 0 : SET_VARSIZE(res, len);
204 : : }
205 : :
206 : : /* make signature, if array is too long */
7039 tgl@sss.pgh.pa.us 207 [ - + ]:CBC 6104 : if (VARSIZE(res) > TOAST_INDEX_TARGET)
208 : : {
2434 akorotkov@postgresql 209 :UBC 0 : SignTSVector *ressign = gtsvector_alloc(SIGNKEY, siglen, NULL);
210 : :
211 : 0 : makesign(GETSIGN(ressign), res, siglen);
7039 tgl@sss.pgh.pa.us 212 : 0 : res = ressign;
213 : : }
214 : :
353 michael@paquier.xyz 215 :CBC 6104 : retval = palloc_object(GISTENTRY);
7039 tgl@sss.pgh.pa.us 216 : 6104 : gistentryinit(*retval, PointerGetDatum(res),
217 : : entry->rel, entry->page,
218 : : entry->offset, false);
219 : : }
220 [ + - ]: 4351 : else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
221 [ + - ]: 4351 : !ISALLTRUE(DatumGetPointer(entry->key)))
222 : : {
223 : : int32 i;
224 : : SignTSVector *res;
225 : 4351 : BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
226 : :
2434 akorotkov@postgresql 227 [ + + ]: 4599 : LOOPBYTE(siglen)
228 : : {
6952 bruce@momjian.us 229 [ + + ]: 4351 : if ((sign[i] & 0xff) != 0xff)
230 : 4103 : PG_RETURN_POINTER(retval);
231 : : }
232 : :
2434 akorotkov@postgresql 233 : 248 : res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
353 michael@paquier.xyz 234 : 248 : retval = palloc_object(GISTENTRY);
7039 tgl@sss.pgh.pa.us 235 : 248 : gistentryinit(*retval, PointerGetDatum(res),
236 : : entry->rel, entry->page,
237 : : entry->offset, false);
238 : : }
239 : 6352 : PG_RETURN_POINTER(retval);
240 : : }
241 : :
242 : : Datum
243 : 271847 : gtsvector_decompress(PG_FUNCTION_ARGS)
244 : : {
245 : : /*
246 : : * We need to detoast the stored value, because the other gtsvector
247 : : * support functions don't cope with toasted values.
248 : : */
249 : 271847 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
3358 250 : 271847 : SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(entry->key);
251 : :
7039 252 [ - + ]: 271847 : if (key != (SignTSVector *) DatumGetPointer(entry->key))
253 : : {
353 michael@paquier.xyz 254 :UBC 0 : GISTENTRY *retval = palloc_object(GISTENTRY);
255 : :
7039 tgl@sss.pgh.pa.us 256 : 0 : gistentryinit(*retval, PointerGetDatum(key),
257 : : entry->rel, entry->page,
258 : : entry->offset, false);
259 : :
260 : 0 : PG_RETURN_POINTER(retval);
261 : : }
262 : :
7039 tgl@sss.pgh.pa.us 263 :CBC 271847 : PG_RETURN_POINTER(entry);
264 : : }
265 : :
266 : : typedef struct
267 : : {
268 : : int32 *arrb;
269 : : int32 *arre;
270 : : } CHKVAL;
271 : :
272 : : /*
273 : : * TS_execute callback for matching a tsquery operand to GIST leaf-page data
274 : : */
275 : : static TSTernaryValue
3887 teodor@sigaev.ru 276 : 280418 : checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
277 : : {
5269 peter_e@gmx.net 278 : 280418 : int32 *StopLow = ((CHKVAL *) checkval)->arrb;
279 : 280418 : int32 *StopHigh = ((CHKVAL *) checkval)->arre;
280 : : int32 *StopMiddle;
281 : :
282 : : /* Loop invariant: StopLow <= val < StopHigh */
283 : :
284 : : /*
285 : : * we are not able to find a prefix by hash value
286 : : */
6379 bruce@momjian.us 287 [ + + ]: 280418 : if (val->prefix)
2318 tgl@sss.pgh.pa.us 288 : 16256 : return TS_MAYBE;
289 : :
7039 290 [ + + ]: 1698749 : while (StopLow < StopHigh)
291 : : {
292 : 1466988 : StopMiddle = StopLow + (StopHigh - StopLow) / 2;
7022 teodor@sigaev.ru 293 [ + + ]: 1466988 : if (*StopMiddle == val->valcrc)
2318 tgl@sss.pgh.pa.us 294 : 32401 : return TS_MAYBE;
7022 teodor@sigaev.ru 295 [ + + ]: 1434587 : else if (*StopMiddle < val->valcrc)
7039 tgl@sss.pgh.pa.us 296 : 610410 : StopLow = StopMiddle + 1;
297 : : else
298 : 824177 : StopHigh = StopMiddle;
299 : : }
300 : :
2318 301 : 231761 : return TS_NO;
302 : : }
303 : :
304 : : /*
305 : : * TS_execute callback for matching a tsquery operand to GIST non-leaf data
306 : : */
307 : : static TSTernaryValue
3887 teodor@sigaev.ru 308 : 10472 : checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
309 : : {
2389 tgl@sss.pgh.pa.us 310 : 10472 : void *key = (SignTSVector *) checkval;
311 : :
312 : : /*
313 : : * we are not able to find a prefix in signature tree
314 : : */
6379 bruce@momjian.us 315 [ + + ]: 10472 : if (val->prefix)
2318 tgl@sss.pgh.pa.us 316 : 464 : return TS_MAYBE;
317 : :
318 [ + + ]: 10008 : if (GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key))))
319 : 9611 : return TS_MAYBE;
320 : : else
321 : 397 : return TS_NO;
322 : : }
323 : :
324 : : Datum
7039 325 : 203340 : gtsvector_consistent(PG_FUNCTION_ARGS)
326 : : {
6802 327 : 203340 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
7039 328 : 203340 : TSQuery query = PG_GETARG_TSQUERY(1);
329 : : #ifdef NOT_USED
330 : : StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
331 : : Oid subtype = PG_GETARG_OID(3);
332 : : #endif
6802 333 : 203340 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
334 : 203340 : SignTSVector *key = (SignTSVector *) DatumGetPointer(entry->key);
335 : :
336 : : /* All cases served by this function are inexact */
337 : 203340 : *recheck = true;
338 : :
7039 339 [ - + ]: 203340 : if (!query->size)
7039 tgl@sss.pgh.pa.us 340 :UBC 0 : PG_RETURN_BOOL(false);
341 : :
7039 tgl@sss.pgh.pa.us 342 [ + + ]:CBC 203340 : if (ISSIGNKEY(key))
343 : : {
344 [ + + ]: 8860 : if (ISALLTRUE(key))
345 : 3200 : PG_RETURN_BOOL(true);
346 : :
3629 347 : 5660 : PG_RETURN_BOOL(TS_execute(GETQUERY(query),
348 : : key,
349 : : TS_EXEC_PHRASE_NO_POS,
350 : : checkcondition_bit));
351 : : }
352 : : else
353 : : { /* only leaf pages */
354 : : CHKVAL chkval;
355 : :
7039 356 : 194480 : chkval.arrb = GETARR(key);
357 : 194480 : chkval.arre = chkval.arrb + ARRNELEM(key);
3629 358 : 194480 : PG_RETURN_BOOL(TS_execute(GETQUERY(query),
359 : : &chkval,
360 : : TS_EXEC_PHRASE_NO_POS,
361 : : checkcondition_arr));
362 : : }
363 : : }
364 : :
365 : : static int32
2434 akorotkov@postgresql 366 : 10260 : unionkey(BITVECP sbase, SignTSVector *add, int siglen)
367 : : {
7039 tgl@sss.pgh.pa.us 368 [ + + ]: 10260 : if (ISSIGNKEY(add))
369 : : {
370 : : int32 i;
371 : 6070 : BITVECP sadd = GETSIGN(add);
372 : :
373 [ + + ]: 6070 : if (ISALLTRUE(add))
374 : 1880 : return 1;
375 : :
2434 akorotkov@postgresql 376 [ - + ]: 4190 : Assert(GETSIGLEN(add) == siglen);
377 : :
378 [ + + ]: 1355350 : LOOPBYTE(siglen)
6952 bruce@momjian.us 379 : 1351160 : sbase[i] |= sadd[i];
380 : : }
381 : : else
382 : : {
5269 peter_e@gmx.net 383 : 4190 : int32 *ptr = GETARR(add);
384 : :
140 peter@eisentraut.org 385 [ + + ]:GNC 246369 : for (uint32 i = 0; i < ARRNELEM(add); i++)
2434 akorotkov@postgresql 386 :CBC 242179 : HASH(sbase, ptr[i], siglen);
387 : : }
7039 tgl@sss.pgh.pa.us 388 : 8380 : return 0;
389 : : }
390 : :
391 : :
392 : : Datum
393 : 6070 : gtsvector_union(PG_FUNCTION_ARGS)
394 : : {
395 : 6070 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
396 : 6070 : int *size = (int *) PG_GETARG_POINTER(1);
2434 akorotkov@postgresql 397 [ + - ]: 6070 : int siglen = GET_SIGLEN();
398 : 6070 : SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
399 : 6070 : BITVECP base = GETSIGN(result);
400 : : int32 i;
401 : :
402 : 6070 : memset(base, 0, siglen);
403 : :
7039 tgl@sss.pgh.pa.us 404 [ + + ]: 14450 : for (i = 0; i < entryvec->n; i++)
405 : : {
2434 akorotkov@postgresql 406 [ + + ]: 10260 : if (unionkey(base, GETENTRY(entryvec, i), siglen))
407 : : {
408 : 1880 : result->flag |= ALLISTRUE;
409 [ - + - + ]: 1880 : SET_VARSIZE(result, CALCGTSIZE(result->flag, siglen));
7039 tgl@sss.pgh.pa.us 410 : 1880 : break;
411 : : }
412 : : }
413 : :
2434 akorotkov@postgresql 414 : 6070 : *size = VARSIZE(result);
415 : :
7039 tgl@sss.pgh.pa.us 416 : 6070 : PG_RETURN_POINTER(result);
417 : : }
418 : :
419 : : Datum
420 : 6070 : gtsvector_same(PG_FUNCTION_ARGS)
421 : : {
422 : 6070 : SignTSVector *a = (SignTSVector *) PG_GETARG_POINTER(0);
423 : 6070 : SignTSVector *b = (SignTSVector *) PG_GETARG_POINTER(1);
424 : 6070 : bool *result = (bool *) PG_GETARG_POINTER(2);
2434 akorotkov@postgresql 425 [ + - ]: 6070 : int siglen = GET_SIGLEN();
426 : :
7039 tgl@sss.pgh.pa.us 427 [ + - ]: 6070 : if (ISSIGNKEY(a))
428 : : { /* then b also ISSIGNKEY */
429 [ + + + - ]: 6070 : if (ISALLTRUE(a) && ISALLTRUE(b))
430 : 1880 : *result = true;
431 [ - + ]: 4190 : else if (ISALLTRUE(a))
7039 tgl@sss.pgh.pa.us 432 :UBC 0 : *result = false;
7039 tgl@sss.pgh.pa.us 433 [ - + ]:CBC 4190 : else if (ISALLTRUE(b))
7039 tgl@sss.pgh.pa.us 434 :UBC 0 : *result = false;
435 : : else
436 : : {
437 : : int32 i;
7039 tgl@sss.pgh.pa.us 438 :CBC 4190 : BITVECP sa = GETSIGN(a),
439 : 4190 : sb = GETSIGN(b);
440 : :
2434 akorotkov@postgresql 441 [ + - - + ]: 4190 : Assert(GETSIGLEN(a) == siglen && GETSIGLEN(b) == siglen);
442 : :
7039 tgl@sss.pgh.pa.us 443 : 4190 : *result = true;
2434 akorotkov@postgresql 444 [ + + ]: 340967 : LOOPBYTE(siglen)
445 : : {
6952 bruce@momjian.us 446 [ + + ]: 340580 : if (sa[i] != sb[i])
447 : : {
448 : 3803 : *result = false;
449 : 3803 : break;
450 : : }
451 : : }
452 : : }
453 : : }
454 : : else
455 : : { /* a and b ISARRKEY */
5269 peter_e@gmx.net 456 :UBC 0 : int32 lena = ARRNELEM(a),
7039 tgl@sss.pgh.pa.us 457 : 0 : lenb = ARRNELEM(b);
458 : :
459 [ # # ]: 0 : if (lena != lenb)
460 : 0 : *result = false;
461 : : else
462 : : {
5269 peter_e@gmx.net 463 : 0 : int32 *ptra = GETARR(a),
7039 tgl@sss.pgh.pa.us 464 : 0 : *ptrb = GETARR(b);
465 : : int32 i;
466 : :
467 : 0 : *result = true;
468 [ # # ]: 0 : for (i = 0; i < lena; i++)
469 [ # # ]: 0 : if (ptra[i] != ptrb[i])
470 : : {
471 : 0 : *result = false;
472 : 0 : break;
473 : : }
474 : : }
475 : : }
476 : :
7039 tgl@sss.pgh.pa.us 477 :CBC 6070 : PG_RETURN_POINTER(result);
478 : : }
479 : :
480 : : static int32
2434 akorotkov@postgresql 481 : 7059 : sizebitvec(BITVECP sign, int siglen)
482 : : {
483 : 7059 : return pg_popcount(sign, siglen);
484 : : }
485 : :
486 : : static int
487 : 187857 : hemdistsign(BITVECP a, BITVECP b, int siglen)
488 : : {
489 : : int i,
490 : : diff,
7039 tgl@sss.pgh.pa.us 491 : 187857 : dist = 0;
492 : :
2434 akorotkov@postgresql 493 [ + + ]: 35053860 : LOOPBYTE(siglen)
494 : : {
6952 bruce@momjian.us 495 : 34866003 : diff = (unsigned char) (a[i] ^ b[i]);
496 : : /* Using the popcount functions here isn't likely to win */
2843 tgl@sss.pgh.pa.us 497 : 34866003 : dist += pg_number_of_ones[diff];
498 : : }
7039 499 : 187857 : return dist;
500 : : }
501 : :
502 : : static int
6952 bruce@momjian.us 503 :UBC 0 : hemdist(SignTSVector *a, SignTSVector *b)
504 : : {
2389 tgl@sss.pgh.pa.us 505 : 0 : int siglena = GETSIGLEN(a);
506 : 0 : int siglenb = GETSIGLEN(b);
507 : :
7039 508 [ # # ]: 0 : if (ISALLTRUE(a))
509 : : {
510 [ # # ]: 0 : if (ISALLTRUE(b))
511 : 0 : return 0;
512 : : else
2434 akorotkov@postgresql 513 : 0 : return SIGLENBIT(siglenb) - sizebitvec(GETSIGN(b), siglenb);
514 : : }
7039 tgl@sss.pgh.pa.us 515 [ # # ]: 0 : else if (ISALLTRUE(b))
2434 akorotkov@postgresql 516 : 0 : return SIGLENBIT(siglena) - sizebitvec(GETSIGN(a), siglena);
517 : :
518 [ # # ]: 0 : Assert(siglena == siglenb);
519 : :
520 : 0 : return hemdistsign(GETSIGN(a), GETSIGN(b), siglena);
521 : : }
522 : :
523 : : Datum
7039 tgl@sss.pgh.pa.us 524 :CBC 41877 : gtsvector_penalty(PG_FUNCTION_ARGS)
525 : : {
526 : 41877 : GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
527 : 41877 : GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
528 : 41877 : float *penalty = (float *) PG_GETARG_POINTER(2);
2434 akorotkov@postgresql 529 [ + - ]: 41877 : int siglen = GET_SIGLEN();
7039 tgl@sss.pgh.pa.us 530 : 41877 : SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
531 : 41877 : SignTSVector *newval = (SignTSVector *) DatumGetPointer(newentry->key);
532 : 41877 : BITVECP orig = GETSIGN(origval);
533 : :
534 : 41877 : *penalty = 0.0;
535 : :
536 [ + - ]: 41877 : if (ISARRKEY(newval))
537 : : {
2434 akorotkov@postgresql 538 : 41877 : BITVECP sign = palloc(siglen);
539 : :
540 : 41877 : makesign(sign, newval, siglen);
541 : :
7039 tgl@sss.pgh.pa.us 542 [ + + ]: 41877 : if (ISALLTRUE(origval))
543 : : {
2434 akorotkov@postgresql 544 : 7059 : int siglenbit = SIGLENBIT(siglen);
545 : :
546 : 7059 : *penalty =
547 : 7059 : (float) (siglenbit - sizebitvec(sign, siglen)) /
548 : 7059 : (float) (siglenbit + 1);
549 : : }
550 : : else
551 : 34818 : *penalty = hemdistsign(sign, orig, siglen);
552 : :
553 : 41877 : pfree(sign);
554 : : }
555 : : else
7039 tgl@sss.pgh.pa.us 556 :UBC 0 : *penalty = hemdist(origval, newval);
7039 tgl@sss.pgh.pa.us 557 :CBC 41877 : PG_RETURN_POINTER(penalty);
558 : : }
559 : :
560 : : typedef struct
561 : : {
562 : : bool allistrue;
563 : : BITVECP sign;
564 : : } CACHESIGN;
565 : :
566 : : static void
2434 akorotkov@postgresql 567 : 8420 : fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
568 : : {
7039 tgl@sss.pgh.pa.us 569 : 8420 : item->allistrue = false;
570 [ + + ]: 8420 : if (ISARRKEY(key))
2434 akorotkov@postgresql 571 : 8360 : makesign(item->sign, key, siglen);
7039 tgl@sss.pgh.pa.us 572 [ - + ]: 60 : else if (ISALLTRUE(key))
7039 tgl@sss.pgh.pa.us 573 :UBC 0 : item->allistrue = true;
574 : : else
1390 peter@eisentraut.org 575 :CBC 60 : memcpy(item->sign, GETSIGN(key), siglen);
7039 tgl@sss.pgh.pa.us 576 : 8420 : }
577 : :
578 : : #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
579 : : typedef struct
580 : : {
581 : : OffsetNumber pos;
582 : : int32 cost;
583 : : } SPLITCOST;
584 : :
585 : : static int
7018 teodor@sigaev.ru 586 : 20840 : comparecost(const void *va, const void *vb)
587 : : {
5284 bruce@momjian.us 588 : 20840 : const SPLITCOST *a = (const SPLITCOST *) va;
589 : 20840 : const SPLITCOST *b = (const SPLITCOST *) vb;
590 : :
1016 nathan@postgresql.or 591 : 20840 : return pg_cmp_s32(a->cost, b->cost);
592 : : }
593 : :
594 : :
595 : : static int
2434 akorotkov@postgresql 596 : 137295 : hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
597 : : {
7039 tgl@sss.pgh.pa.us 598 [ - + ]: 137295 : if (a->allistrue)
599 : : {
7039 tgl@sss.pgh.pa.us 600 [ # # ]:UBC 0 : if (b->allistrue)
601 : 0 : return 0;
602 : : else
2434 akorotkov@postgresql 603 : 0 : return SIGLENBIT(siglen) - sizebitvec(b->sign, siglen);
604 : : }
7039 tgl@sss.pgh.pa.us 605 [ - + ]:CBC 137295 : else if (b->allistrue)
2434 akorotkov@postgresql 606 :UBC 0 : return SIGLENBIT(siglen) - sizebitvec(a->sign, siglen);
607 : :
2434 akorotkov@postgresql 608 :CBC 137295 : return hemdistsign(a->sign, b->sign, siglen);
609 : : }
610 : :
611 : : Datum
7039 tgl@sss.pgh.pa.us 612 : 274 : gtsvector_picksplit(PG_FUNCTION_ARGS)
613 : : {
614 : 274 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
615 : 274 : GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
2434 akorotkov@postgresql 616 [ + - ]: 274 : int siglen = GET_SIGLEN();
617 : : OffsetNumber k,
618 : : j;
619 : : SignTSVector *datum_l,
620 : : *datum_r;
621 : : BITVECP union_l,
622 : : union_r;
623 : : int32 size_alpha,
624 : : size_beta;
625 : : int32 size_waste,
7039 tgl@sss.pgh.pa.us 626 : 274 : waste = -1;
627 : : int32 nbytes;
628 : 274 : OffsetNumber seed_1 = 0,
629 : 274 : seed_2 = 0;
630 : : OffsetNumber *left,
631 : : *right;
632 : : OffsetNumber maxoff;
633 : : BITVECP ptr;
634 : : int i;
635 : : CACHESIGN *cache;
636 : : char *cache_sign;
637 : : SPLITCOST *costvector;
638 : :
639 : 274 : maxoff = entryvec->n - 2;
640 : 274 : nbytes = (maxoff + 2) * sizeof(OffsetNumber);
641 : 274 : v->spl_left = (OffsetNumber *) palloc(nbytes);
642 : 274 : v->spl_right = (OffsetNumber *) palloc(nbytes);
643 : :
353 michael@paquier.xyz 644 : 274 : cache = palloc_array(CACHESIGN, maxoff + 2);
2434 akorotkov@postgresql 645 : 274 : cache_sign = palloc(siglen * (maxoff + 2));
646 : :
647 [ + + ]: 8968 : for (j = 0; j < maxoff + 2; j++)
648 : 8694 : cache[j].sign = &cache_sign[siglen * j];
649 : :
650 : 274 : fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber),
651 : : siglen);
652 : :
7039 tgl@sss.pgh.pa.us 653 [ + + ]: 8146 : for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
654 : : {
655 [ + + ]: 128327 : for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
656 : : {
657 [ + + ]: 120455 : if (k == FirstOffsetNumber)
2434 akorotkov@postgresql 658 : 7872 : fillcache(&cache[j], GETENTRY(entryvec, j), siglen);
659 : :
660 : 120455 : size_waste = hemdistcache(&(cache[j]), &(cache[k]), siglen);
7039 tgl@sss.pgh.pa.us 661 [ + + ]: 120455 : if (size_waste > waste)
662 : : {
663 : 1703 : waste = size_waste;
664 : 1703 : seed_1 = k;
665 : 1703 : seed_2 = j;
666 : : }
667 : : }
668 : : }
669 : :
670 : 274 : left = v->spl_left;
671 : 274 : v->spl_nleft = 0;
672 : 274 : right = v->spl_right;
673 : 274 : v->spl_nright = 0;
674 : :
675 [ + + - + ]: 274 : if (seed_1 == 0 || seed_2 == 0)
676 : : {
677 : 4 : seed_1 = 1;
678 : 4 : seed_2 = 2;
679 : : }
680 : :
681 : : /* seed_2 can be the last entry, so fill its cache first */
17 michael@paquier.xyz 682 : 274 : maxoff = OffsetNumberNext(maxoff);
683 : 274 : fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen);
684 : :
685 : : /* form initial .. */
2434 akorotkov@postgresql 686 : 274 : datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0),
687 [ - + ]: 274 : siglen, cache[seed_1].sign);
688 : 274 : datum_r = gtsvector_alloc(SIGNKEY | (cache[seed_2].allistrue ? ALLISTRUE : 0),
689 [ - + ]: 274 : siglen, cache[seed_2].sign);
7039 tgl@sss.pgh.pa.us 690 : 274 : union_l = GETSIGN(datum_l);
691 : 274 : union_r = GETSIGN(datum_r);
692 : : /* sort before ... */
353 michael@paquier.xyz 693 : 274 : costvector = palloc_array(SPLITCOST, maxoff);
7039 tgl@sss.pgh.pa.us 694 [ + + ]: 8694 : for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
695 : : {
696 : 8420 : costvector[j - 1].pos = j;
2434 akorotkov@postgresql 697 : 8420 : size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
698 : 8420 : size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
1513 peter@eisentraut.org 699 : 8420 : costvector[j - 1].cost = abs(size_alpha - size_beta);
700 : : }
1390 701 : 274 : qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
702 : :
7039 tgl@sss.pgh.pa.us 703 [ + + ]: 8694 : for (k = 0; k < maxoff; k++)
704 : : {
705 : 8420 : j = costvector[k].pos;
706 [ + + ]: 8420 : if (j == seed_1)
707 : : {
708 : 274 : *left++ = j;
709 : 274 : v->spl_nleft++;
710 : 274 : continue;
711 : : }
712 [ + + ]: 8146 : else if (j == seed_2)
713 : : {
714 : 274 : *right++ = j;
715 : 274 : v->spl_nright++;
716 : 274 : continue;
717 : : }
718 : :
719 [ + - - + ]: 7872 : if (ISALLTRUE(datum_l) || cache[j].allistrue)
720 : : {
7039 tgl@sss.pgh.pa.us 721 [ # # # # ]:UBC 0 : if (ISALLTRUE(datum_l) && cache[j].allistrue)
722 : 0 : size_alpha = 0;
723 : : else
2434 akorotkov@postgresql 724 : 0 : size_alpha = SIGLENBIT(siglen) -
725 [ # # ]: 0 : sizebitvec((cache[j].allistrue) ?
726 : : GETSIGN(datum_l) :
1181 michael@paquier.xyz 727 : 0 : cache[j].sign,
728 : : siglen);
729 : : }
730 : : else
2434 akorotkov@postgresql 731 :CBC 7872 : size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l), siglen);
732 : :
7039 tgl@sss.pgh.pa.us 733 [ + - - + ]: 7872 : if (ISALLTRUE(datum_r) || cache[j].allistrue)
734 : : {
7039 tgl@sss.pgh.pa.us 735 [ # # # # ]:UBC 0 : if (ISALLTRUE(datum_r) && cache[j].allistrue)
736 : 0 : size_beta = 0;
737 : : else
2434 akorotkov@postgresql 738 : 0 : size_beta = SIGLENBIT(siglen) -
739 [ # # ]: 0 : sizebitvec((cache[j].allistrue) ?
740 : : GETSIGN(datum_r) :
1181 michael@paquier.xyz 741 : 0 : cache[j].sign,
742 : : siglen);
743 : : }
744 : : else
2434 akorotkov@postgresql 745 :CBC 7872 : size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r), siglen);
746 : :
7039 tgl@sss.pgh.pa.us 747 [ + + ]: 7872 : if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
748 : : {
749 [ + - - + ]: 3922 : if (ISALLTRUE(datum_l) || cache[j].allistrue)
750 : : {
7039 tgl@sss.pgh.pa.us 751 [ # # ]:UBC 0 : if (!ISALLTRUE(datum_l))
1390 peter@eisentraut.org 752 : 0 : memset(GETSIGN(datum_l), 0xff, siglen);
753 : : }
754 : : else
755 : : {
7039 tgl@sss.pgh.pa.us 756 :CBC 3922 : ptr = cache[j].sign;
2434 akorotkov@postgresql 757 [ + + ]: 653735 : LOOPBYTE(siglen)
6952 bruce@momjian.us 758 : 649813 : union_l[i] |= ptr[i];
759 : : }
7039 tgl@sss.pgh.pa.us 760 : 3922 : *left++ = j;
761 : 3922 : v->spl_nleft++;
762 : : }
763 : : else
764 : : {
765 [ + - - + ]: 3950 : if (ISALLTRUE(datum_r) || cache[j].allistrue)
766 : : {
7039 tgl@sss.pgh.pa.us 767 [ # # ]:UBC 0 : if (!ISALLTRUE(datum_r))
1390 peter@eisentraut.org 768 : 0 : memset(GETSIGN(datum_r), 0xff, siglen);
769 : : }
770 : : else
771 : : {
7039 tgl@sss.pgh.pa.us 772 :CBC 3950 : ptr = cache[j].sign;
2434 akorotkov@postgresql 773 [ + + ]: 648055 : LOOPBYTE(siglen)
6952 bruce@momjian.us 774 : 644105 : union_r[i] |= ptr[i];
775 : : }
7039 tgl@sss.pgh.pa.us 776 : 3950 : *right++ = j;
777 : 3950 : v->spl_nright++;
778 : : }
779 : : }
780 : :
781 : 274 : *right = *left = FirstOffsetNumber;
782 : 274 : v->spl_ldatum = PointerGetDatum(datum_l);
783 : 274 : v->spl_rdatum = PointerGetDatum(datum_r);
784 : :
785 : 274 : PG_RETURN_POINTER(v);
786 : : }
787 : :
788 : : /*
789 : : * Formerly, gtsvector_consistent was declared in pg_proc.h with arguments
790 : : * that did not match the documented conventions for GiST support functions.
791 : : * We fixed that, but we still need a pg_proc entry with the old signature
792 : : * to support reloading pre-9.6 contrib/tsearch2 opclass declarations.
793 : : * This compatibility function should go away eventually.
794 : : */
795 : : Datum
3923 tgl@sss.pgh.pa.us 796 :UBC 0 : gtsvector_consistent_oldsig(PG_FUNCTION_ARGS)
797 : : {
798 : 0 : return gtsvector_consistent(fcinfo);
799 : : }
800 : :
801 : : Datum
2434 akorotkov@postgresql 802 :CBC 248 : gtsvector_options(PG_FUNCTION_ARGS)
803 : : {
804 : 248 : local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
805 : :
806 : 248 : init_local_reloptions(relopts, sizeof(GistTsVectorOptions));
807 : 248 : add_local_int_reloption(relopts, "siglen", "signature length",
808 : : SIGLEN_DEFAULT, 1, SIGLEN_MAX,
809 : : offsetof(GistTsVectorOptions, siglen));
810 : :
811 : 248 : PG_RETURN_VOID();
812 : : }
|