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
6781 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 : : {
1295 peter@eisentraut.org 102 : 0 : SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
103 : : char *outbuf;
104 : :
6781 tgl@sss.pgh.pa.us 105 [ # # ]: 0 : if (ISARRKEY(key))
586 heikki.linnakangas@i 106 : 0 : outbuf = psprintf("%d unique words", (int) ARRNELEM(key));
107 : : else
108 : : {
922 michael@paquier.xyz 109 [ # # ]: 0 : if (ISALLTRUE(key))
586 heikki.linnakangas@i 110 : 0 : outbuf = pstrdup("all true bits");
111 : : else
112 : : {
922 michael@paquier.xyz 113 : 0 : int siglen = GETSIGLEN(key);
114 : 0 : int cnttrue = sizebitvec(GETSIGN(key), siglen);
115 : :
586 heikki.linnakangas@i 116 : 0 : outbuf = psprintf("%d true bits, %d false bits",
117 : 0 : cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
118 : : }
119 : : }
120 : :
6781 tgl@sss.pgh.pa.us 121 [ # # ]: 0 : PG_FREE_IF_COPY(key, 0);
122 : 0 : PG_RETURN_POINTER(outbuf);
123 : : }
124 : :
125 : : static int
6760 teodor@sigaev.ru 126 :CBC 1813068 : compareint(const void *va, const void *vb)
127 : : {
5011 peter_e@gmx.net 128 : 1813068 : int32 a = *((const int32 *) va);
129 : 1813068 : int32 b = *((const int32 *) vb);
130 : :
758 nathan@postgresql.or 131 : 1813068 : return pg_cmp_s32(a, b);
132 : : }
133 : :
134 : : static void
2176 akorotkov@postgresql 135 : 37660 : makesign(BITVECP sign, SignTSVector *a, int siglen)
136 : : {
137 : : int32 k,
6781 tgl@sss.pgh.pa.us 138 : 37660 : len = ARRNELEM(a);
5011 peter_e@gmx.net 139 : 37660 : int32 *ptr = GETARR(a);
140 : :
1132 peter@eisentraut.org 141 [ + + - + : 37660 : MemSet(sign, 0, siglen);
- - - - -
- ]
6781 tgl@sss.pgh.pa.us 142 [ + + ]: 2104668 : for (k = 0; k < len; k++)
2176 akorotkov@postgresql 143 : 2067008 : HASH(sign, ptr[k], siglen);
6781 tgl@sss.pgh.pa.us 144 : 37660 : }
145 : :
146 : : static SignTSVector *
2176 akorotkov@postgresql 147 : 9718 : gtsvector_alloc(int flag, int len, BITVECP sign)
148 : : {
149 [ + + + + ]: 9718 : int size = CALCGTSIZE(flag, len);
150 : 9718 : SignTSVector *res = palloc(size);
151 : :
152 : 9718 : SET_VARSIZE(res, size);
153 : 9718 : res->flag = flag;
154 : :
155 [ + + + + ]: 9718 : if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
156 : 408 : memcpy(GETSIGN(res), sign, len);
157 : :
158 : 9718 : return res;
159 : : }
160 : :
161 : :
162 : : Datum
6781 tgl@sss.pgh.pa.us 163 : 7843 : gtsvector_compress(PG_FUNCTION_ARGS)
164 : : {
165 : 7843 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
2176 akorotkov@postgresql 166 [ + - ]: 7843 : int siglen = GET_SIGLEN();
6781 tgl@sss.pgh.pa.us 167 : 7843 : GISTENTRY *retval = entry;
168 : :
169 [ + + ]: 7843 : if (entry->leafkey)
170 : : { /* tsvector */
171 : 4572 : TSVector val = DatumGetTSVector(entry->key);
2176 akorotkov@postgresql 172 : 4572 : SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
173 : : int32 len;
174 : : int32 *arr;
6781 tgl@sss.pgh.pa.us 175 : 4572 : WordEntry *ptr = ARRPTR(val);
176 : 4572 : char *words = STRPTR(val);
177 : :
178 : 4572 : arr = GETARR(res);
179 : 4572 : len = val->size;
180 [ + + ]: 263772 : while (len--)
181 : : {
182 : : pg_crc32 c;
183 : :
4149 heikki.linnakangas@i 184 : 259200 : INIT_LEGACY_CRC32(c);
185 [ + + ]: 777600 : COMP_LEGACY_CRC32(c, words + ptr->pos, ptr->len);
186 : 259200 : FIN_LEGACY_CRC32(c);
187 : :
5011 peter_e@gmx.net 188 : 259200 : *arr = *(int32 *) &c;
6781 tgl@sss.pgh.pa.us 189 : 259200 : arr++;
190 : 259200 : ptr++;
191 : : }
192 : :
2320 tmunro@postgresql.or 193 : 4572 : qsort(GETARR(res), val->size, sizeof(int), compareint);
194 : 4572 : len = qunique(GETARR(res), val->size, sizeof(int), compareint);
6781 tgl@sss.pgh.pa.us 195 [ - + ]: 4572 : if (len != val->size)
196 : : {
197 : : /*
198 : : * there is a collision of hash-function; len is always less than
199 : : * val->size
200 : : */
6781 tgl@sss.pgh.pa.us 201 :UBC 0 : len = CALCGTSIZE(ARRKEY, len);
1132 peter@eisentraut.org 202 : 0 : res = (SignTSVector *) repalloc(res, len);
6781 tgl@sss.pgh.pa.us 203 : 0 : SET_VARSIZE(res, len);
204 : : }
205 : :
206 : : /* make signature, if array is too long */
6781 tgl@sss.pgh.pa.us 207 [ - + ]:CBC 4572 : if (VARSIZE(res) > TOAST_INDEX_TARGET)
208 : : {
2176 akorotkov@postgresql 209 :UBC 0 : SignTSVector *ressign = gtsvector_alloc(SIGNKEY, siglen, NULL);
210 : :
211 : 0 : makesign(GETSIGN(ressign), res, siglen);
6781 tgl@sss.pgh.pa.us 212 : 0 : res = ressign;
213 : : }
214 : :
95 michael@paquier.xyz 215 :GNC 4572 : retval = palloc_object(GISTENTRY);
6781 tgl@sss.pgh.pa.us 216 :CBC 4572 : gistentryinit(*retval, PointerGetDatum(res),
217 : : entry->rel, entry->page,
218 : : entry->offset, false);
219 : : }
220 [ + - ]: 3271 : else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
221 [ + - ]: 3271 : !ISALLTRUE(DatumGetPointer(entry->key)))
222 : : {
223 : : int32 i;
224 : : SignTSVector *res;
225 : 3271 : BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
226 : :
2176 akorotkov@postgresql 227 [ + + ]: 3457 : LOOPBYTE(siglen)
228 : : {
6694 bruce@momjian.us 229 [ + + ]: 3271 : if ((sign[i] & 0xff) != 0xff)
230 : 3085 : PG_RETURN_POINTER(retval);
231 : : }
232 : :
2176 akorotkov@postgresql 233 : 186 : res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
95 michael@paquier.xyz 234 :GNC 186 : retval = palloc_object(GISTENTRY);
6781 tgl@sss.pgh.pa.us 235 :CBC 186 : gistentryinit(*retval, PointerGetDatum(res),
236 : : entry->rel, entry->page,
237 : : entry->offset, false);
238 : : }
239 : 4758 : PG_RETURN_POINTER(retval);
240 : : }
241 : :
242 : : Datum
243 : 203195 : 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 : 203195 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
3100 250 : 203195 : SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(entry->key);
251 : :
6781 252 [ - + ]: 203195 : if (key != (SignTSVector *) DatumGetPointer(entry->key))
253 : : {
95 michael@paquier.xyz 254 :UNC 0 : GISTENTRY *retval = palloc_object(GISTENTRY);
255 : :
6781 tgl@sss.pgh.pa.us 256 :UBC 0 : gistentryinit(*retval, PointerGetDatum(key),
257 : : entry->rel, entry->page,
258 : : entry->offset, false);
259 : :
260 : 0 : PG_RETURN_POINTER(retval);
261 : : }
262 : :
6781 tgl@sss.pgh.pa.us 263 :CBC 203195 : 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
3629 teodor@sigaev.ru 276 : 209310 : checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
277 : : {
5011 peter_e@gmx.net 278 : 209310 : int32 *StopLow = ((CHKVAL *) checkval)->arrb;
279 : 209310 : 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 : : */
6121 bruce@momjian.us 287 [ + + ]: 209310 : if (val->prefix)
2060 tgl@sss.pgh.pa.us 288 : 12192 : return TS_MAYBE;
289 : :
6781 290 [ + + ]: 1267462 : while (StopLow < StopHigh)
291 : : {
292 : 1094596 : StopMiddle = StopLow + (StopHigh - StopLow) / 2;
6764 teodor@sigaev.ru 293 [ + + ]: 1094596 : if (*StopMiddle == val->valcrc)
2060 tgl@sss.pgh.pa.us 294 : 24252 : return TS_MAYBE;
6764 teodor@sigaev.ru 295 [ + + ]: 1070344 : else if (*StopMiddle < val->valcrc)
6781 tgl@sss.pgh.pa.us 296 : 455418 : StopLow = StopMiddle + 1;
297 : : else
298 : 614926 : StopHigh = StopMiddle;
299 : : }
300 : :
2060 301 : 172866 : return TS_NO;
302 : : }
303 : :
304 : : /*
305 : : * TS_execute callback for matching a tsquery operand to GIST non-leaf data
306 : : */
307 : : static TSTernaryValue
3629 teodor@sigaev.ru 308 : 7970 : checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
309 : : {
2131 tgl@sss.pgh.pa.us 310 : 7970 : void *key = (SignTSVector *) checkval;
311 : :
312 : : /*
313 : : * we are not able to find a prefix in signature tree
314 : : */
6121 bruce@momjian.us 315 [ + + ]: 7970 : if (val->prefix)
2060 tgl@sss.pgh.pa.us 316 : 354 : return TS_MAYBE;
317 : :
318 [ + + ]: 7616 : if (GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key))))
319 : 7246 : return TS_MAYBE;
320 : : else
321 : 370 : return TS_NO;
322 : : }
323 : :
324 : : Datum
6781 325 : 151834 : gtsvector_consistent(PG_FUNCTION_ARGS)
326 : : {
6544 327 : 151834 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
6781 328 : 151834 : 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
6544 333 : 151834 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
334 : 151834 : SignTSVector *key = (SignTSVector *) DatumGetPointer(entry->key);
335 : :
336 : : /* All cases served by this function are inexact */
337 : 151834 : *recheck = true;
338 : :
6781 339 [ - + ]: 151834 : if (!query->size)
6781 tgl@sss.pgh.pa.us 340 :UBC 0 : PG_RETURN_BOOL(false);
341 : :
6781 tgl@sss.pgh.pa.us 342 [ + + ]:CBC 151834 : if (ISSIGNKEY(key))
343 : : {
344 [ + + ]: 6711 : if (ISALLTRUE(key))
345 : 2400 : PG_RETURN_BOOL(true);
346 : :
3371 347 : 4311 : 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 : :
6781 356 : 145123 : chkval.arrb = GETARR(key);
357 : 145123 : chkval.arre = chkval.arrb + ARRNELEM(key);
3371 358 : 145123 : PG_RETURN_BOOL(TS_execute(GETQUERY(query),
359 : : &chkval,
360 : : TS_EXEC_PHRASE_NO_POS,
361 : : checkcondition_arr));
362 : : }
363 : : }
364 : :
365 : : static int32
2176 akorotkov@postgresql 366 : 7694 : unionkey(BITVECP sbase, SignTSVector *add, int siglen)
367 : : {
368 : : int32 i;
369 : :
6781 tgl@sss.pgh.pa.us 370 [ + + ]: 7694 : if (ISSIGNKEY(add))
371 : : {
372 : 4552 : BITVECP sadd = GETSIGN(add);
373 : :
374 [ + + ]: 4552 : if (ISALLTRUE(add))
375 : 1410 : return 1;
376 : :
2176 akorotkov@postgresql 377 [ - + ]: 3142 : Assert(GETSIGLEN(add) == siglen);
378 : :
379 [ + + ]: 1016270 : LOOPBYTE(siglen)
6694 bruce@momjian.us 380 : 1013128 : sbase[i] |= sadd[i];
381 : : }
382 : : else
383 : : {
5011 peter_e@gmx.net 384 : 3142 : int32 *ptr = GETARR(add);
385 : :
6781 tgl@sss.pgh.pa.us 386 [ + + ]: 184749 : for (i = 0; i < ARRNELEM(add); i++)
2176 akorotkov@postgresql 387 : 181607 : HASH(sbase, ptr[i], siglen);
388 : : }
6781 tgl@sss.pgh.pa.us 389 : 6284 : return 0;
390 : : }
391 : :
392 : :
393 : : Datum
394 : 4552 : gtsvector_union(PG_FUNCTION_ARGS)
395 : : {
396 : 4552 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
397 : 4552 : int *size = (int *) PG_GETARG_POINTER(1);
2176 akorotkov@postgresql 398 [ + - ]: 4552 : int siglen = GET_SIGLEN();
399 : 4552 : SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
400 : 4552 : BITVECP base = GETSIGN(result);
401 : : int32 i;
402 : :
403 : 4552 : memset(base, 0, siglen);
404 : :
6781 tgl@sss.pgh.pa.us 405 [ + + ]: 10836 : for (i = 0; i < entryvec->n; i++)
406 : : {
2176 akorotkov@postgresql 407 [ + + ]: 7694 : if (unionkey(base, GETENTRY(entryvec, i), siglen))
408 : : {
409 : 1410 : result->flag |= ALLISTRUE;
410 [ - + - + ]: 1410 : SET_VARSIZE(result, CALCGTSIZE(result->flag, siglen));
6781 tgl@sss.pgh.pa.us 411 : 1410 : break;
412 : : }
413 : : }
414 : :
2176 akorotkov@postgresql 415 : 4552 : *size = VARSIZE(result);
416 : :
6781 tgl@sss.pgh.pa.us 417 : 4552 : PG_RETURN_POINTER(result);
418 : : }
419 : :
420 : : Datum
421 : 4552 : gtsvector_same(PG_FUNCTION_ARGS)
422 : : {
423 : 4552 : SignTSVector *a = (SignTSVector *) PG_GETARG_POINTER(0);
424 : 4552 : SignTSVector *b = (SignTSVector *) PG_GETARG_POINTER(1);
425 : 4552 : bool *result = (bool *) PG_GETARG_POINTER(2);
2176 akorotkov@postgresql 426 [ + - ]: 4552 : int siglen = GET_SIGLEN();
427 : :
6781 tgl@sss.pgh.pa.us 428 [ + - ]: 4552 : if (ISSIGNKEY(a))
429 : : { /* then b also ISSIGNKEY */
430 [ + + + - ]: 4552 : if (ISALLTRUE(a) && ISALLTRUE(b))
431 : 1410 : *result = true;
432 [ - + ]: 3142 : else if (ISALLTRUE(a))
6781 tgl@sss.pgh.pa.us 433 :UBC 0 : *result = false;
6781 tgl@sss.pgh.pa.us 434 [ - + ]:CBC 3142 : else if (ISALLTRUE(b))
6781 tgl@sss.pgh.pa.us 435 :UBC 0 : *result = false;
436 : : else
437 : : {
438 : : int32 i;
6781 tgl@sss.pgh.pa.us 439 :CBC 3142 : BITVECP sa = GETSIGN(a),
440 : 3142 : sb = GETSIGN(b);
441 : :
2176 akorotkov@postgresql 442 [ + - - + ]: 3142 : Assert(GETSIGLEN(a) == siglen && GETSIGLEN(b) == siglen);
443 : :
6781 tgl@sss.pgh.pa.us 444 : 3142 : *result = true;
2176 akorotkov@postgresql 445 [ + + ]: 249208 : LOOPBYTE(siglen)
446 : : {
6694 bruce@momjian.us 447 [ + + ]: 248929 : if (sa[i] != sb[i])
448 : : {
449 : 2863 : *result = false;
450 : 2863 : break;
451 : : }
452 : : }
453 : : }
454 : : }
455 : : else
456 : : { /* a and b ISARRKEY */
5011 peter_e@gmx.net 457 :UBC 0 : int32 lena = ARRNELEM(a),
6781 tgl@sss.pgh.pa.us 458 : 0 : lenb = ARRNELEM(b);
459 : :
460 [ # # ]: 0 : if (lena != lenb)
461 : 0 : *result = false;
462 : : else
463 : : {
5011 peter_e@gmx.net 464 : 0 : int32 *ptra = GETARR(a),
6781 tgl@sss.pgh.pa.us 465 : 0 : *ptrb = GETARR(b);
466 : : int32 i;
467 : :
468 : 0 : *result = true;
469 [ # # ]: 0 : for (i = 0; i < lena; i++)
470 [ # # ]: 0 : if (ptra[i] != ptrb[i])
471 : : {
472 : 0 : *result = false;
473 : 0 : break;
474 : : }
475 : : }
476 : : }
477 : :
6781 tgl@sss.pgh.pa.us 478 :CBC 4552 : PG_RETURN_POINTER(result);
479 : : }
480 : :
481 : : static int32
2176 akorotkov@postgresql 482 : 5295 : sizebitvec(BITVECP sign, int siglen)
483 : : {
484 : 5295 : return pg_popcount(sign, siglen);
485 : : }
486 : :
487 : : static int
488 : 142127 : hemdistsign(BITVECP a, BITVECP b, int siglen)
489 : : {
490 : : int i,
491 : : diff,
6781 tgl@sss.pgh.pa.us 492 : 142127 : dist = 0;
493 : :
2176 akorotkov@postgresql 494 [ + + ]: 26813041 : LOOPBYTE(siglen)
495 : : {
6694 bruce@momjian.us 496 : 26670914 : diff = (unsigned char) (a[i] ^ b[i]);
497 : : /* Using the popcount functions here isn't likely to win */
2585 tgl@sss.pgh.pa.us 498 : 26670914 : dist += pg_number_of_ones[diff];
499 : : }
6781 500 : 142127 : return dist;
501 : : }
502 : :
503 : : static int
6694 bruce@momjian.us 504 :UBC 0 : hemdist(SignTSVector *a, SignTSVector *b)
505 : : {
2131 tgl@sss.pgh.pa.us 506 : 0 : int siglena = GETSIGLEN(a);
507 : 0 : int siglenb = GETSIGLEN(b);
508 : :
6781 509 [ # # ]: 0 : if (ISALLTRUE(a))
510 : : {
511 [ # # ]: 0 : if (ISALLTRUE(b))
512 : 0 : return 0;
513 : : else
2176 akorotkov@postgresql 514 : 0 : return SIGLENBIT(siglenb) - sizebitvec(GETSIGN(b), siglenb);
515 : : }
6781 tgl@sss.pgh.pa.us 516 [ # # ]: 0 : else if (ISALLTRUE(b))
2176 akorotkov@postgresql 517 : 0 : return SIGLENBIT(siglena) - sizebitvec(GETSIGN(a), siglena);
518 : :
519 [ # # ]: 0 : Assert(siglena == siglenb);
520 : :
521 : 0 : return hemdistsign(GETSIGN(a), GETSIGN(b), siglena);
522 : : }
523 : :
524 : : Datum
6781 tgl@sss.pgh.pa.us 525 :CBC 31336 : gtsvector_penalty(PG_FUNCTION_ARGS)
526 : : {
527 : 31336 : GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
528 : 31336 : GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
529 : 31336 : float *penalty = (float *) PG_GETARG_POINTER(2);
2176 akorotkov@postgresql 530 [ + - ]: 31336 : int siglen = GET_SIGLEN();
6781 tgl@sss.pgh.pa.us 531 : 31336 : SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
532 : 31336 : SignTSVector *newval = (SignTSVector *) DatumGetPointer(newentry->key);
533 : 31336 : BITVECP orig = GETSIGN(origval);
534 : :
535 : 31336 : *penalty = 0.0;
536 : :
537 [ + - ]: 31336 : if (ISARRKEY(newval))
538 : : {
2176 akorotkov@postgresql 539 : 31336 : BITVECP sign = palloc(siglen);
540 : :
541 : 31336 : makesign(sign, newval, siglen);
542 : :
6781 tgl@sss.pgh.pa.us 543 [ + + ]: 31336 : if (ISALLTRUE(origval))
544 : : {
2176 akorotkov@postgresql 545 : 5295 : int siglenbit = SIGLENBIT(siglen);
546 : :
547 : 5295 : *penalty =
548 : 5295 : (float) (siglenbit - sizebitvec(sign, siglen)) /
549 : 5295 : (float) (siglenbit + 1);
550 : : }
551 : : else
552 : 26041 : *penalty = hemdistsign(sign, orig, siglen);
553 : :
554 : 31336 : pfree(sign);
555 : : }
556 : : else
6781 tgl@sss.pgh.pa.us 557 :UBC 0 : *penalty = hemdist(origval, newval);
6781 tgl@sss.pgh.pa.us 558 :CBC 31336 : PG_RETURN_POINTER(penalty);
559 : : }
560 : :
561 : : typedef struct
562 : : {
563 : : bool allistrue;
564 : : BITVECP sign;
565 : : } CACHESIGN;
566 : :
567 : : static void
2176 akorotkov@postgresql 568 : 6369 : fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
569 : : {
6781 tgl@sss.pgh.pa.us 570 : 6369 : item->allistrue = false;
571 [ + + ]: 6369 : if (ISARRKEY(key))
2176 akorotkov@postgresql 572 : 6324 : makesign(item->sign, key, siglen);
6781 tgl@sss.pgh.pa.us 573 [ - + ]: 45 : else if (ISALLTRUE(key))
6781 tgl@sss.pgh.pa.us 574 :UBC 0 : item->allistrue = true;
575 : : else
1132 peter@eisentraut.org 576 :CBC 45 : memcpy(item->sign, GETSIGN(key), siglen);
6781 tgl@sss.pgh.pa.us 577 : 6369 : }
578 : :
579 : : #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
580 : : typedef struct
581 : : {
582 : : OffsetNumber pos;
583 : : int32 cost;
584 : : } SPLITCOST;
585 : :
586 : : static int
6760 teodor@sigaev.ru 587 : 16020 : comparecost(const void *va, const void *vb)
588 : : {
5026 bruce@momjian.us 589 : 16020 : const SPLITCOST *a = (const SPLITCOST *) va;
590 : 16020 : const SPLITCOST *b = (const SPLITCOST *) vb;
591 : :
758 nathan@postgresql.or 592 : 16020 : return pg_cmp_s32(a->cost, b->cost);
593 : : }
594 : :
595 : :
596 : : static int
2176 akorotkov@postgresql 597 : 104164 : hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
598 : : {
6781 tgl@sss.pgh.pa.us 599 [ - + ]: 104164 : if (a->allistrue)
600 : : {
6781 tgl@sss.pgh.pa.us 601 [ # # ]:UBC 0 : if (b->allistrue)
602 : 0 : return 0;
603 : : else
2176 akorotkov@postgresql 604 : 0 : return SIGLENBIT(siglen) - sizebitvec(b->sign, siglen);
605 : : }
6781 tgl@sss.pgh.pa.us 606 [ - + ]:CBC 104164 : else if (b->allistrue)
2176 akorotkov@postgresql 607 :UBC 0 : return SIGLENBIT(siglen) - sizebitvec(a->sign, siglen);
608 : :
2176 akorotkov@postgresql 609 :CBC 104164 : return hemdistsign(a->sign, b->sign, siglen);
610 : : }
611 : :
612 : : Datum
6781 tgl@sss.pgh.pa.us 613 : 204 : gtsvector_picksplit(PG_FUNCTION_ARGS)
614 : : {
615 : 204 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
616 : 204 : GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
2176 akorotkov@postgresql 617 [ + - ]: 204 : int siglen = GET_SIGLEN();
618 : : OffsetNumber k,
619 : : j;
620 : : SignTSVector *datum_l,
621 : : *datum_r;
622 : : BITVECP union_l,
623 : : union_r;
624 : : int32 size_alpha,
625 : : size_beta;
626 : : int32 size_waste,
6781 tgl@sss.pgh.pa.us 627 : 204 : waste = -1;
628 : : int32 nbytes;
629 : 204 : OffsetNumber seed_1 = 0,
630 : 204 : seed_2 = 0;
631 : : OffsetNumber *left,
632 : : *right;
633 : : OffsetNumber maxoff;
634 : : BITVECP ptr;
635 : : int i;
636 : : CACHESIGN *cache;
637 : : char *cache_sign;
638 : : SPLITCOST *costvector;
639 : :
640 : 204 : maxoff = entryvec->n - 2;
641 : 204 : nbytes = (maxoff + 2) * sizeof(OffsetNumber);
642 : 204 : v->spl_left = (OffsetNumber *) palloc(nbytes);
643 : 204 : v->spl_right = (OffsetNumber *) palloc(nbytes);
644 : :
95 michael@paquier.xyz 645 :GNC 204 : cache = palloc_array(CACHESIGN, maxoff + 2);
2176 akorotkov@postgresql 646 :CBC 204 : cache_sign = palloc(siglen * (maxoff + 2));
647 : :
648 [ + + ]: 6777 : for (j = 0; j < maxoff + 2; j++)
649 : 6573 : cache[j].sign = &cache_sign[siglen * j];
650 : :
651 : 204 : fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber),
652 : : siglen);
653 : :
6781 tgl@sss.pgh.pa.us 654 [ + + ]: 6165 : for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
655 : : {
656 [ + + ]: 97387 : for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
657 : : {
658 [ + + ]: 91426 : if (k == FirstOffsetNumber)
2176 akorotkov@postgresql 659 : 5961 : fillcache(&cache[j], GETENTRY(entryvec, j), siglen);
660 : :
661 : 91426 : size_waste = hemdistcache(&(cache[j]), &(cache[k]), siglen);
6781 tgl@sss.pgh.pa.us 662 [ + + ]: 91426 : if (size_waste > waste)
663 : : {
664 : 1294 : waste = size_waste;
665 : 1294 : seed_1 = k;
666 : 1294 : seed_2 = j;
667 : : }
668 : : }
669 : : }
670 : :
671 : 204 : left = v->spl_left;
672 : 204 : v->spl_nleft = 0;
673 : 204 : right = v->spl_right;
674 : 204 : v->spl_nright = 0;
675 : :
676 [ + - - + ]: 204 : if (seed_1 == 0 || seed_2 == 0)
677 : : {
6781 tgl@sss.pgh.pa.us 678 :UBC 0 : seed_1 = 1;
679 : 0 : seed_2 = 2;
680 : : }
681 : :
682 : : /* form initial .. */
2176 akorotkov@postgresql 683 :CBC 204 : datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0),
684 [ - + ]: 204 : siglen, cache[seed_1].sign);
685 : 204 : datum_r = gtsvector_alloc(SIGNKEY | (cache[seed_2].allistrue ? ALLISTRUE : 0),
686 [ - + ]: 204 : siglen, cache[seed_2].sign);
6781 tgl@sss.pgh.pa.us 687 : 204 : union_l = GETSIGN(datum_l);
688 : 204 : union_r = GETSIGN(datum_r);
689 : 204 : maxoff = OffsetNumberNext(maxoff);
2176 akorotkov@postgresql 690 : 204 : fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen);
691 : : /* sort before ... */
95 michael@paquier.xyz 692 :GNC 204 : costvector = palloc_array(SPLITCOST, maxoff);
6781 tgl@sss.pgh.pa.us 693 [ + + ]:CBC 6573 : for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
694 : : {
695 : 6369 : costvector[j - 1].pos = j;
2176 akorotkov@postgresql 696 : 6369 : size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
697 : 6369 : size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
1255 peter@eisentraut.org 698 : 6369 : costvector[j - 1].cost = abs(size_alpha - size_beta);
699 : : }
1132 700 : 204 : qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
701 : :
6781 tgl@sss.pgh.pa.us 702 [ + + ]: 6573 : for (k = 0; k < maxoff; k++)
703 : : {
704 : 6369 : j = costvector[k].pos;
705 [ + + ]: 6369 : if (j == seed_1)
706 : : {
707 : 204 : *left++ = j;
708 : 204 : v->spl_nleft++;
709 : 204 : continue;
710 : : }
711 [ + + ]: 6165 : else if (j == seed_2)
712 : : {
713 : 204 : *right++ = j;
714 : 204 : v->spl_nright++;
715 : 204 : continue;
716 : : }
717 : :
718 [ + - - + ]: 5961 : if (ISALLTRUE(datum_l) || cache[j].allistrue)
719 : : {
6781 tgl@sss.pgh.pa.us 720 [ # # # # ]:UBC 0 : if (ISALLTRUE(datum_l) && cache[j].allistrue)
721 : 0 : size_alpha = 0;
722 : : else
2176 akorotkov@postgresql 723 : 0 : size_alpha = SIGLENBIT(siglen) -
724 [ # # ]: 0 : sizebitvec((cache[j].allistrue) ?
725 : : GETSIGN(datum_l) :
923 michael@paquier.xyz 726 : 0 : cache[j].sign,
727 : : siglen);
728 : : }
729 : : else
2176 akorotkov@postgresql 730 :CBC 5961 : size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l), siglen);
731 : :
6781 tgl@sss.pgh.pa.us 732 [ + - - + ]: 5961 : if (ISALLTRUE(datum_r) || cache[j].allistrue)
733 : : {
6781 tgl@sss.pgh.pa.us 734 [ # # # # ]:UBC 0 : if (ISALLTRUE(datum_r) && cache[j].allistrue)
735 : 0 : size_beta = 0;
736 : : else
2176 akorotkov@postgresql 737 : 0 : size_beta = SIGLENBIT(siglen) -
738 [ # # ]: 0 : sizebitvec((cache[j].allistrue) ?
739 : : GETSIGN(datum_r) :
923 michael@paquier.xyz 740 : 0 : cache[j].sign,
741 : : siglen);
742 : : }
743 : : else
2176 akorotkov@postgresql 744 :CBC 5961 : size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r), siglen);
745 : :
6781 tgl@sss.pgh.pa.us 746 [ + + ]: 5961 : if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
747 : : {
748 [ + - - + ]: 2948 : if (ISALLTRUE(datum_l) || cache[j].allistrue)
749 : : {
6781 tgl@sss.pgh.pa.us 750 [ # # ]:UBC 0 : if (!ISALLTRUE(datum_l))
1132 peter@eisentraut.org 751 : 0 : memset(GETSIGN(datum_l), 0xff, siglen);
752 : : }
753 : : else
754 : : {
6781 tgl@sss.pgh.pa.us 755 :CBC 2948 : ptr = cache[j].sign;
2176 akorotkov@postgresql 756 [ + + ]: 497572 : LOOPBYTE(siglen)
6694 bruce@momjian.us 757 : 494624 : union_l[i] |= ptr[i];
758 : : }
6781 tgl@sss.pgh.pa.us 759 : 2948 : *left++ = j;
760 : 2948 : v->spl_nleft++;
761 : : }
762 : : else
763 : : {
764 [ + - - + ]: 3013 : if (ISALLTRUE(datum_r) || cache[j].allistrue)
765 : : {
6781 tgl@sss.pgh.pa.us 766 [ # # ]:UBC 0 : if (!ISALLTRUE(datum_r))
1132 peter@eisentraut.org 767 : 0 : memset(GETSIGN(datum_r), 0xff, siglen);
768 : : }
769 : : else
770 : : {
6781 tgl@sss.pgh.pa.us 771 :CBC 3013 : ptr = cache[j].sign;
2176 akorotkov@postgresql 772 [ + + ]: 497624 : LOOPBYTE(siglen)
6694 bruce@momjian.us 773 : 494611 : union_r[i] |= ptr[i];
774 : : }
6781 tgl@sss.pgh.pa.us 775 : 3013 : *right++ = j;
776 : 3013 : v->spl_nright++;
777 : : }
778 : : }
779 : :
780 : 204 : *right = *left = FirstOffsetNumber;
781 : 204 : v->spl_ldatum = PointerGetDatum(datum_l);
782 : 204 : v->spl_rdatum = PointerGetDatum(datum_r);
783 : :
784 : 204 : PG_RETURN_POINTER(v);
785 : : }
786 : :
787 : : /*
788 : : * Formerly, gtsvector_consistent was declared in pg_proc.h with arguments
789 : : * that did not match the documented conventions for GiST support functions.
790 : : * We fixed that, but we still need a pg_proc entry with the old signature
791 : : * to support reloading pre-9.6 contrib/tsearch2 opclass declarations.
792 : : * This compatibility function should go away eventually.
793 : : */
794 : : Datum
3665 tgl@sss.pgh.pa.us 795 :UBC 0 : gtsvector_consistent_oldsig(PG_FUNCTION_ARGS)
796 : : {
797 : 0 : return gtsvector_consistent(fcinfo);
798 : : }
799 : :
800 : : Datum
2176 akorotkov@postgresql 801 :CBC 177 : gtsvector_options(PG_FUNCTION_ARGS)
802 : : {
803 : 177 : local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
804 : :
805 : 177 : init_local_reloptions(relopts, sizeof(GistTsVectorOptions));
806 : 177 : add_local_int_reloption(relopts, "siglen", "signature length",
807 : : SIGLEN_DEFAULT, 1, SIGLEN_MAX,
808 : : offsetof(GistTsVectorOptions, siglen));
809 : :
810 : 177 : PG_RETURN_VOID();
811 : : }
|