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-2025, 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
6591 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 : : {
1105 peter@eisentraut.org 102 : 0 : SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
103 : : char *outbuf;
104 : :
6591 tgl@sss.pgh.pa.us 105 [ # # ]: 0 : if (ISARRKEY(key))
396 heikki.linnakangas@i 106 : 0 : outbuf = psprintf("%d unique words", (int) ARRNELEM(key));
107 : : else
108 : : {
732 michael@paquier.xyz 109 [ # # ]: 0 : if (ISALLTRUE(key))
396 heikki.linnakangas@i 110 : 0 : outbuf = pstrdup("all true bits");
111 : : else
112 : : {
732 michael@paquier.xyz 113 : 0 : int siglen = GETSIGLEN(key);
114 : 0 : int cnttrue = sizebitvec(GETSIGN(key), siglen);
115 : :
396 heikki.linnakangas@i 116 : 0 : outbuf = psprintf("%d true bits, %d false bits",
117 : 0 : cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
118 : : }
119 : : }
120 : :
6591 tgl@sss.pgh.pa.us 121 [ # # ]: 0 : PG_FREE_IF_COPY(key, 0);
122 : 0 : PG_RETURN_POINTER(outbuf);
123 : : }
124 : :
125 : : static int
6570 teodor@sigaev.ru 126 :CBC 1813068 : compareint(const void *va, const void *vb)
127 : : {
4821 peter_e@gmx.net 128 : 1813068 : int32 a = *((const int32 *) va);
129 : 1813068 : int32 b = *((const int32 *) vb);
130 : :
568 nathan@postgresql.or 131 : 1813068 : return pg_cmp_s32(a, b);
132 : : }
133 : :
134 : : static void
1986 akorotkov@postgresql 135 : 37715 : makesign(BITVECP sign, SignTSVector *a, int siglen)
136 : : {
137 : : int32 k,
6591 tgl@sss.pgh.pa.us 138 : 37715 : len = ARRNELEM(a);
4821 peter_e@gmx.net 139 : 37715 : int32 *ptr = GETARR(a);
140 : :
942 peter@eisentraut.org 141 [ + + - + : 37715 : MemSet(sign, 0, siglen);
- - - - -
- ]
6591 tgl@sss.pgh.pa.us 142 [ + + ]: 2108019 : for (k = 0; k < len; k++)
1986 akorotkov@postgresql 143 : 2070304 : HASH(sign, ptr[k], siglen);
6591 tgl@sss.pgh.pa.us 144 : 37715 : }
145 : :
146 : : static SignTSVector *
1986 akorotkov@postgresql 147 : 9732 : gtsvector_alloc(int flag, int len, BITVECP sign)
148 : : {
149 [ + + + + ]: 9732 : int size = CALCGTSIZE(flag, len);
150 : 9732 : SignTSVector *res = palloc(size);
151 : :
152 : 9732 : SET_VARSIZE(res, size);
153 : 9732 : res->flag = flag;
154 : :
155 [ + + + + ]: 9732 : if ((flag & (SIGNKEY | ALLISTRUE)) == SIGNKEY && sign)
156 : 412 : memcpy(GETSIGN(res), sign, len);
157 : :
158 : 9732 : return res;
159 : : }
160 : :
161 : :
162 : : Datum
6591 tgl@sss.pgh.pa.us 163 : 7840 : gtsvector_compress(PG_FUNCTION_ARGS)
164 : : {
165 : 7840 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
1986 akorotkov@postgresql 166 [ + - ]: 7840 : int siglen = GET_SIGLEN();
6591 tgl@sss.pgh.pa.us 167 : 7840 : GISTENTRY *retval = entry;
168 : :
169 [ + + ]: 7840 : if (entry->leafkey)
170 : : { /* tsvector */
171 : 4572 : TSVector val = DatumGetTSVector(entry->key);
1986 akorotkov@postgresql 172 : 4572 : SignTSVector *res = gtsvector_alloc(ARRKEY, val->size, NULL);
173 : : int32 len;
174 : : int32 *arr;
6591 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 : :
3959 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 : :
4821 peter_e@gmx.net 188 : 259200 : *arr = *(int32 *) &c;
6591 tgl@sss.pgh.pa.us 189 : 259200 : arr++;
190 : 259200 : ptr++;
191 : : }
192 : :
2130 tmunro@postgresql.or 193 : 4572 : qsort(GETARR(res), val->size, sizeof(int), compareint);
194 : 4572 : len = qunique(GETARR(res), val->size, sizeof(int), compareint);
6591 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 : : */
6591 tgl@sss.pgh.pa.us 201 :UBC 0 : len = CALCGTSIZE(ARRKEY, len);
942 peter@eisentraut.org 202 : 0 : res = (SignTSVector *) repalloc(res, len);
6591 tgl@sss.pgh.pa.us 203 : 0 : SET_VARSIZE(res, len);
204 : : }
205 : :
206 : : /* make signature, if array is too long */
6591 tgl@sss.pgh.pa.us 207 [ - + ]:CBC 4572 : if (VARSIZE(res) > TOAST_INDEX_TARGET)
208 : : {
1986 akorotkov@postgresql 209 :UBC 0 : SignTSVector *ressign = gtsvector_alloc(SIGNKEY, siglen, NULL);
210 : :
211 : 0 : makesign(GETSIGN(ressign), res, siglen);
6591 tgl@sss.pgh.pa.us 212 : 0 : res = ressign;
213 : : }
214 : :
6591 tgl@sss.pgh.pa.us 215 :CBC 4572 : retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
216 : 4572 : gistentryinit(*retval, PointerGetDatum(res),
217 : : entry->rel, entry->page,
218 : : entry->offset, false);
219 : : }
220 [ + - ]: 3268 : else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
221 [ + - ]: 3268 : !ISALLTRUE(DatumGetPointer(entry->key)))
222 : : {
223 : : int32 i;
224 : : SignTSVector *res;
225 : 3268 : BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
226 : :
1986 akorotkov@postgresql 227 [ + + ]: 3458 : LOOPBYTE(siglen)
228 : : {
6504 bruce@momjian.us 229 [ + + ]: 3268 : if ((sign[i] & 0xff) != 0xff)
230 : 3078 : PG_RETURN_POINTER(retval);
231 : : }
232 : :
1986 akorotkov@postgresql 233 : 190 : res = gtsvector_alloc(SIGNKEY | ALLISTRUE, siglen, sign);
6591 tgl@sss.pgh.pa.us 234 : 190 : retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
235 : 190 : gistentryinit(*retval, PointerGetDatum(res),
236 : : entry->rel, entry->page,
237 : : entry->offset, false);
238 : : }
239 : 4762 : PG_RETURN_POINTER(retval);
240 : : }
241 : :
242 : : Datum
243 : 203210 : 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 : 203210 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
2910 250 : 203210 : SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(entry->key);
251 : :
6591 252 [ - + ]: 203210 : if (key != (SignTSVector *) DatumGetPointer(entry->key))
253 : : {
6591 tgl@sss.pgh.pa.us 254 :UBC 0 : GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
255 : :
256 : 0 : gistentryinit(*retval, PointerGetDatum(key),
257 : : entry->rel, entry->page,
258 : : entry->offset, false);
259 : :
260 : 0 : PG_RETURN_POINTER(retval);
261 : : }
262 : :
6591 tgl@sss.pgh.pa.us 263 :CBC 203210 : 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
3439 teodor@sigaev.ru 276 : 209286 : checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
277 : : {
4821 peter_e@gmx.net 278 : 209286 : int32 *StopLow = ((CHKVAL *) checkval)->arrb;
279 : 209286 : 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 : : */
5931 bruce@momjian.us 287 [ + + ]: 209286 : if (val->prefix)
1870 tgl@sss.pgh.pa.us 288 : 12192 : return TS_MAYBE;
289 : :
6591 290 [ + + ]: 1267184 : while (StopLow < StopHigh)
291 : : {
292 : 1094356 : StopMiddle = StopLow + (StopHigh - StopLow) / 2;
6574 teodor@sigaev.ru 293 [ + + ]: 1094356 : if (*StopMiddle == val->valcrc)
1870 tgl@sss.pgh.pa.us 294 : 24266 : return TS_MAYBE;
6574 teodor@sigaev.ru 295 [ + + ]: 1070090 : else if (*StopMiddle < val->valcrc)
6591 tgl@sss.pgh.pa.us 296 : 455034 : StopLow = StopMiddle + 1;
297 : : else
298 : 615056 : StopHigh = StopMiddle;
299 : : }
300 : :
1870 301 : 172828 : return TS_NO;
302 : : }
303 : :
304 : : /*
305 : : * TS_execute callback for matching a tsquery operand to GIST non-leaf data
306 : : */
307 : : static TSTernaryValue
3439 teodor@sigaev.ru 308 : 7966 : checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
309 : : {
1941 tgl@sss.pgh.pa.us 310 : 7966 : void *key = (SignTSVector *) checkval;
311 : :
312 : : /*
313 : : * we are not able to find a prefix in signature tree
314 : : */
5931 bruce@momjian.us 315 [ + + ]: 7966 : if (val->prefix)
1870 tgl@sss.pgh.pa.us 316 : 354 : return TS_MAYBE;
317 : :
318 [ + + ]: 7612 : if (GETBIT(GETSIGN(key), HASHVAL(val->valcrc, GETSIGLEN(key))))
319 : 7230 : return TS_MAYBE;
320 : : else
321 : 382 : return TS_NO;
322 : : }
323 : :
324 : : Datum
6591 325 : 151776 : gtsvector_consistent(PG_FUNCTION_ARGS)
326 : : {
6354 327 : 151776 : GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
6591 328 : 151776 : TSQuery query = PG_GETARG_TSQUERY(1);
329 : :
330 : : /* StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); */
331 : : /* Oid subtype = PG_GETARG_OID(3); */
6354 332 : 151776 : bool *recheck = (bool *) PG_GETARG_POINTER(4);
333 : 151776 : SignTSVector *key = (SignTSVector *) DatumGetPointer(entry->key);
334 : :
335 : : /* All cases served by this function are inexact */
336 : 151776 : *recheck = true;
337 : :
6591 338 [ - + ]: 151776 : if (!query->size)
6591 tgl@sss.pgh.pa.us 339 :UBC 0 : PG_RETURN_BOOL(false);
340 : :
6591 tgl@sss.pgh.pa.us 341 [ + + ]:CBC 151776 : if (ISSIGNKEY(key))
342 : : {
343 [ + + ]: 6761 : if (ISALLTRUE(key))
344 : 2450 : PG_RETURN_BOOL(true);
345 : :
3181 346 : 4311 : PG_RETURN_BOOL(TS_execute(GETQUERY(query),
347 : : key,
348 : : TS_EXEC_PHRASE_NO_POS,
349 : : checkcondition_bit));
350 : : }
351 : : else
352 : : { /* only leaf pages */
353 : : CHKVAL chkval;
354 : :
6591 355 : 145015 : chkval.arrb = GETARR(key);
356 : 145015 : chkval.arre = chkval.arrb + ARRNELEM(key);
3181 357 : 145015 : PG_RETURN_BOOL(TS_execute(GETQUERY(query),
358 : : &chkval,
359 : : TS_EXEC_PHRASE_NO_POS,
360 : : checkcondition_arr));
361 : : }
362 : : }
363 : :
364 : : static int32
1986 akorotkov@postgresql 365 : 7706 : unionkey(BITVECP sbase, SignTSVector *add, int siglen)
366 : : {
367 : : int32 i;
368 : :
6591 tgl@sss.pgh.pa.us 369 [ + + ]: 7706 : if (ISSIGNKEY(add))
370 : : {
371 : 4558 : BITVECP sadd = GETSIGN(add);
372 : :
373 [ + + ]: 4558 : if (ISALLTRUE(add))
374 : 1410 : return 1;
375 : :
1986 akorotkov@postgresql 376 [ - + ]: 3148 : Assert(GETSIGLEN(add) == siglen);
377 : :
378 [ + + ]: 1019180 : LOOPBYTE(siglen)
6504 bruce@momjian.us 379 : 1016032 : sbase[i] |= sadd[i];
380 : : }
381 : : else
382 : : {
4821 peter_e@gmx.net 383 : 3148 : int32 *ptr = GETARR(add);
384 : :
6591 tgl@sss.pgh.pa.us 385 [ + + ]: 185036 : for (i = 0; i < ARRNELEM(add); i++)
1986 akorotkov@postgresql 386 : 181888 : HASH(sbase, ptr[i], siglen);
387 : : }
6591 tgl@sss.pgh.pa.us 388 : 6296 : return 0;
389 : : }
390 : :
391 : :
392 : : Datum
393 : 4558 : gtsvector_union(PG_FUNCTION_ARGS)
394 : : {
395 : 4558 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
396 : 4558 : int *size = (int *) PG_GETARG_POINTER(1);
1986 akorotkov@postgresql 397 [ + - ]: 4558 : int siglen = GET_SIGLEN();
398 : 4558 : SignTSVector *result = gtsvector_alloc(SIGNKEY, siglen, NULL);
399 : 4558 : BITVECP base = GETSIGN(result);
400 : : int32 i;
401 : :
402 : 4558 : memset(base, 0, siglen);
403 : :
6591 tgl@sss.pgh.pa.us 404 [ + + ]: 10854 : for (i = 0; i < entryvec->n; i++)
405 : : {
1986 akorotkov@postgresql 406 [ + + ]: 7706 : if (unionkey(base, GETENTRY(entryvec, i), siglen))
407 : : {
408 : 1410 : result->flag |= ALLISTRUE;
409 [ - + - + ]: 1410 : SET_VARSIZE(result, CALCGTSIZE(result->flag, siglen));
6591 tgl@sss.pgh.pa.us 410 : 1410 : break;
411 : : }
412 : : }
413 : :
1986 akorotkov@postgresql 414 : 4558 : *size = VARSIZE(result);
415 : :
6591 tgl@sss.pgh.pa.us 416 : 4558 : PG_RETURN_POINTER(result);
417 : : }
418 : :
419 : : Datum
420 : 4558 : gtsvector_same(PG_FUNCTION_ARGS)
421 : : {
422 : 4558 : SignTSVector *a = (SignTSVector *) PG_GETARG_POINTER(0);
423 : 4558 : SignTSVector *b = (SignTSVector *) PG_GETARG_POINTER(1);
424 : 4558 : bool *result = (bool *) PG_GETARG_POINTER(2);
1986 akorotkov@postgresql 425 [ + - ]: 4558 : int siglen = GET_SIGLEN();
426 : :
6591 tgl@sss.pgh.pa.us 427 [ + - ]: 4558 : if (ISSIGNKEY(a))
428 : : { /* then b also ISSIGNKEY */
429 [ + + + - ]: 4558 : if (ISALLTRUE(a) && ISALLTRUE(b))
430 : 1410 : *result = true;
431 [ - + ]: 3148 : else if (ISALLTRUE(a))
6591 tgl@sss.pgh.pa.us 432 :UBC 0 : *result = false;
6591 tgl@sss.pgh.pa.us 433 [ - + ]:CBC 3148 : else if (ISALLTRUE(b))
6591 tgl@sss.pgh.pa.us 434 :UBC 0 : *result = false;
435 : : else
436 : : {
437 : : int32 i;
6591 tgl@sss.pgh.pa.us 438 :CBC 3148 : BITVECP sa = GETSIGN(a),
439 : 3148 : sb = GETSIGN(b);
440 : :
1986 akorotkov@postgresql 441 [ + - - + ]: 3148 : Assert(GETSIGLEN(a) == siglen && GETSIGLEN(b) == siglen);
442 : :
6591 tgl@sss.pgh.pa.us 443 : 3148 : *result = true;
1986 akorotkov@postgresql 444 [ + + ]: 254535 : LOOPBYTE(siglen)
445 : : {
6504 bruce@momjian.us 446 [ + + ]: 254243 : if (sa[i] != sb[i])
447 : : {
448 : 2856 : *result = false;
449 : 2856 : break;
450 : : }
451 : : }
452 : : }
453 : : }
454 : : else
455 : : { /* a and b ISARRKEY */
4821 peter_e@gmx.net 456 :UBC 0 : int32 lena = ARRNELEM(a),
6591 tgl@sss.pgh.pa.us 457 : 0 : lenb = ARRNELEM(b);
458 : :
459 [ # # ]: 0 : if (lena != lenb)
460 : 0 : *result = false;
461 : : else
462 : : {
4821 peter_e@gmx.net 463 : 0 : int32 *ptra = GETARR(a),
6591 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 : :
6591 tgl@sss.pgh.pa.us 477 :CBC 4558 : PG_RETURN_POINTER(result);
478 : : }
479 : :
480 : : static int32
1986 akorotkov@postgresql 481 : 5288 : sizebitvec(BITVECP sign, int siglen)
482 : : {
483 : 5288 : return pg_popcount(sign, siglen);
484 : : }
485 : :
486 : : static int
487 : 140777 : hemdistsign(BITVECP a, BITVECP b, int siglen)
488 : : {
489 : : int i,
490 : : diff,
6591 tgl@sss.pgh.pa.us 491 : 140777 : dist = 0;
492 : :
1986 akorotkov@postgresql 493 [ + + ]: 26004544 : LOOPBYTE(siglen)
494 : : {
6504 bruce@momjian.us 495 : 25863767 : diff = (unsigned char) (a[i] ^ b[i]);
496 : : /* Using the popcount functions here isn't likely to win */
2395 tgl@sss.pgh.pa.us 497 : 25863767 : dist += pg_number_of_ones[diff];
498 : : }
6591 499 : 140777 : return dist;
500 : : }
501 : :
502 : : static int
6504 bruce@momjian.us 503 :UBC 0 : hemdist(SignTSVector *a, SignTSVector *b)
504 : : {
1941 tgl@sss.pgh.pa.us 505 : 0 : int siglena = GETSIGLEN(a);
506 : 0 : int siglenb = GETSIGLEN(b);
507 : :
6591 508 [ # # ]: 0 : if (ISALLTRUE(a))
509 : : {
510 [ # # ]: 0 : if (ISALLTRUE(b))
511 : 0 : return 0;
512 : : else
1986 akorotkov@postgresql 513 : 0 : return SIGLENBIT(siglenb) - sizebitvec(GETSIGN(b), siglenb);
514 : : }
6591 tgl@sss.pgh.pa.us 515 [ # # ]: 0 : else if (ISALLTRUE(b))
1986 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
6591 tgl@sss.pgh.pa.us 524 :CBC 31406 : gtsvector_penalty(PG_FUNCTION_ARGS)
525 : : {
526 : 31406 : GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
527 : 31406 : GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
528 : 31406 : float *penalty = (float *) PG_GETARG_POINTER(2);
1986 akorotkov@postgresql 529 [ + - ]: 31406 : int siglen = GET_SIGLEN();
6591 tgl@sss.pgh.pa.us 530 : 31406 : SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
531 : 31406 : SignTSVector *newval = (SignTSVector *) DatumGetPointer(newentry->key);
532 : 31406 : BITVECP orig = GETSIGN(origval);
533 : :
534 : 31406 : *penalty = 0.0;
535 : :
536 [ + - ]: 31406 : if (ISARRKEY(newval))
537 : : {
1986 akorotkov@postgresql 538 : 31406 : BITVECP sign = palloc(siglen);
539 : :
540 : 31406 : makesign(sign, newval, siglen);
541 : :
6591 tgl@sss.pgh.pa.us 542 [ + + ]: 31406 : if (ISALLTRUE(origval))
543 : : {
1986 akorotkov@postgresql 544 : 5288 : int siglenbit = SIGLENBIT(siglen);
545 : :
546 : 5288 : *penalty =
547 : 5288 : (float) (siglenbit - sizebitvec(sign, siglen)) /
548 : 5288 : (float) (siglenbit + 1);
549 : : }
550 : : else
551 : 26118 : *penalty = hemdistsign(sign, orig, siglen);
552 : :
553 : 31406 : pfree(sign);
554 : : }
555 : : else
6591 tgl@sss.pgh.pa.us 556 :UBC 0 : *penalty = hemdist(origval, newval);
6591 tgl@sss.pgh.pa.us 557 :CBC 31406 : PG_RETURN_POINTER(penalty);
558 : : }
559 : :
560 : : typedef struct
561 : : {
562 : : bool allistrue;
563 : : BITVECP sign;
564 : : } CACHESIGN;
565 : :
566 : : static void
1986 akorotkov@postgresql 567 : 6354 : fillcache(CACHESIGN *item, SignTSVector *key, int siglen)
568 : : {
6591 tgl@sss.pgh.pa.us 569 : 6354 : item->allistrue = false;
570 [ + + ]: 6354 : if (ISARRKEY(key))
1986 akorotkov@postgresql 571 : 6309 : makesign(item->sign, key, siglen);
6591 tgl@sss.pgh.pa.us 572 [ - + ]: 45 : else if (ISALLTRUE(key))
6591 tgl@sss.pgh.pa.us 573 :UBC 0 : item->allistrue = true;
574 : : else
942 peter@eisentraut.org 575 :CBC 45 : memcpy(item->sign, GETSIGN(key), siglen);
6591 tgl@sss.pgh.pa.us 576 : 6354 : }
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
6570 teodor@sigaev.ru 586 : 15607 : comparecost(const void *va, const void *vb)
587 : : {
4836 bruce@momjian.us 588 : 15607 : const SPLITCOST *a = (const SPLITCOST *) va;
589 : 15607 : const SPLITCOST *b = (const SPLITCOST *) vb;
590 : :
568 nathan@postgresql.or 591 : 15607 : return pg_cmp_s32(a->cost, b->cost);
592 : : }
593 : :
594 : :
595 : : static int
1986 akorotkov@postgresql 596 : 102775 : hemdistcache(CACHESIGN *a, CACHESIGN *b, int siglen)
597 : : {
6591 tgl@sss.pgh.pa.us 598 [ - + ]: 102775 : if (a->allistrue)
599 : : {
6591 tgl@sss.pgh.pa.us 600 [ # # ]:UBC 0 : if (b->allistrue)
601 : 0 : return 0;
602 : : else
1986 akorotkov@postgresql 603 : 0 : return SIGLENBIT(siglen) - sizebitvec(b->sign, siglen);
604 : : }
6591 tgl@sss.pgh.pa.us 605 [ - + ]:CBC 102775 : else if (b->allistrue)
1986 akorotkov@postgresql 606 :UBC 0 : return SIGLENBIT(siglen) - sizebitvec(a->sign, siglen);
607 : :
1986 akorotkov@postgresql 608 :CBC 102775 : return hemdistsign(a->sign, b->sign, siglen);
609 : : }
610 : :
611 : : Datum
6591 tgl@sss.pgh.pa.us 612 : 206 : gtsvector_picksplit(PG_FUNCTION_ARGS)
613 : : {
614 : 206 : GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
615 : 206 : GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
1986 akorotkov@postgresql 616 [ + - ]: 206 : 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,
6591 tgl@sss.pgh.pa.us 626 : 206 : waste = -1;
627 : : int32 nbytes;
628 : 206 : OffsetNumber seed_1 = 0,
629 : 206 : 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 : 206 : maxoff = entryvec->n - 2;
640 : 206 : nbytes = (maxoff + 2) * sizeof(OffsetNumber);
641 : 206 : v->spl_left = (OffsetNumber *) palloc(nbytes);
642 : 206 : v->spl_right = (OffsetNumber *) palloc(nbytes);
643 : :
644 : 206 : cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
1986 akorotkov@postgresql 645 : 206 : cache_sign = palloc(siglen * (maxoff + 2));
646 : :
647 [ + + ]: 6766 : for (j = 0; j < maxoff + 2; j++)
648 : 6560 : cache[j].sign = &cache_sign[siglen * j];
649 : :
650 : 206 : fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber),
651 : : siglen);
652 : :
6591 tgl@sss.pgh.pa.us 653 [ + + ]: 6148 : for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
654 : : {
655 [ + + ]: 96009 : for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
656 : : {
657 [ + + ]: 90067 : if (k == FirstOffsetNumber)
1986 akorotkov@postgresql 658 : 5942 : fillcache(&cache[j], GETENTRY(entryvec, j), siglen);
659 : :
660 : 90067 : size_waste = hemdistcache(&(cache[j]), &(cache[k]), siglen);
6591 tgl@sss.pgh.pa.us 661 [ + + ]: 90067 : if (size_waste > waste)
662 : : {
663 : 1315 : waste = size_waste;
664 : 1315 : seed_1 = k;
665 : 1315 : seed_2 = j;
666 : : }
667 : : }
668 : : }
669 : :
670 : 206 : left = v->spl_left;
671 : 206 : v->spl_nleft = 0;
672 : 206 : right = v->spl_right;
673 : 206 : v->spl_nright = 0;
674 : :
675 [ + - - + ]: 206 : if (seed_1 == 0 || seed_2 == 0)
676 : : {
6591 tgl@sss.pgh.pa.us 677 :UBC 0 : seed_1 = 1;
678 : 0 : seed_2 = 2;
679 : : }
680 : :
681 : : /* form initial .. */
1986 akorotkov@postgresql 682 :CBC 206 : datum_l = gtsvector_alloc(SIGNKEY | (cache[seed_1].allistrue ? ALLISTRUE : 0),
683 [ - + ]: 206 : siglen, cache[seed_1].sign);
684 : 206 : datum_r = gtsvector_alloc(SIGNKEY | (cache[seed_2].allistrue ? ALLISTRUE : 0),
685 [ - + ]: 206 : siglen, cache[seed_2].sign);
6591 tgl@sss.pgh.pa.us 686 : 206 : union_l = GETSIGN(datum_l);
687 : 206 : union_r = GETSIGN(datum_r);
688 : 206 : maxoff = OffsetNumberNext(maxoff);
1986 akorotkov@postgresql 689 : 206 : fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff), siglen);
690 : : /* sort before ... */
6591 tgl@sss.pgh.pa.us 691 : 206 : costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
692 [ + + ]: 6560 : for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
693 : : {
694 : 6354 : costvector[j - 1].pos = j;
1986 akorotkov@postgresql 695 : 6354 : size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
696 : 6354 : size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
1065 peter@eisentraut.org 697 : 6354 : costvector[j - 1].cost = abs(size_alpha - size_beta);
698 : : }
942 699 : 206 : qsort(costvector, maxoff, sizeof(SPLITCOST), comparecost);
700 : :
6591 tgl@sss.pgh.pa.us 701 [ + + ]: 6560 : for (k = 0; k < maxoff; k++)
702 : : {
703 : 6354 : j = costvector[k].pos;
704 [ + + ]: 6354 : if (j == seed_1)
705 : : {
706 : 206 : *left++ = j;
707 : 206 : v->spl_nleft++;
708 : 206 : continue;
709 : : }
710 [ + + ]: 6148 : else if (j == seed_2)
711 : : {
712 : 206 : *right++ = j;
713 : 206 : v->spl_nright++;
714 : 206 : continue;
715 : : }
716 : :
717 [ + - - + ]: 5942 : if (ISALLTRUE(datum_l) || cache[j].allistrue)
718 : : {
6591 tgl@sss.pgh.pa.us 719 [ # # # # ]:UBC 0 : if (ISALLTRUE(datum_l) && cache[j].allistrue)
720 : 0 : size_alpha = 0;
721 : : else
1986 akorotkov@postgresql 722 : 0 : size_alpha = SIGLENBIT(siglen) -
723 [ # # ]: 0 : sizebitvec((cache[j].allistrue) ?
724 : : GETSIGN(datum_l) :
733 michael@paquier.xyz 725 : 0 : cache[j].sign,
726 : : siglen);
727 : : }
728 : : else
1986 akorotkov@postgresql 729 :CBC 5942 : size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l), siglen);
730 : :
6591 tgl@sss.pgh.pa.us 731 [ + - - + ]: 5942 : if (ISALLTRUE(datum_r) || cache[j].allistrue)
732 : : {
6591 tgl@sss.pgh.pa.us 733 [ # # # # ]:UBC 0 : if (ISALLTRUE(datum_r) && cache[j].allistrue)
734 : 0 : size_beta = 0;
735 : : else
1986 akorotkov@postgresql 736 : 0 : size_beta = SIGLENBIT(siglen) -
737 [ # # ]: 0 : sizebitvec((cache[j].allistrue) ?
738 : : GETSIGN(datum_r) :
733 michael@paquier.xyz 739 : 0 : cache[j].sign,
740 : : siglen);
741 : : }
742 : : else
1986 akorotkov@postgresql 743 :CBC 5942 : size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r), siglen);
744 : :
6591 tgl@sss.pgh.pa.us 745 [ + + ]: 5942 : if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
746 : : {
747 [ + - - + ]: 2940 : if (ISALLTRUE(datum_l) || cache[j].allistrue)
748 : : {
6591 tgl@sss.pgh.pa.us 749 [ # # ]:UBC 0 : if (!ISALLTRUE(datum_l))
942 peter@eisentraut.org 750 : 0 : memset(GETSIGN(datum_l), 0xff, siglen);
751 : : }
752 : : else
753 : : {
6591 tgl@sss.pgh.pa.us 754 :CBC 2940 : ptr = cache[j].sign;
1986 akorotkov@postgresql 755 [ + + ]: 482460 : LOOPBYTE(siglen)
6504 bruce@momjian.us 756 : 479520 : union_l[i] |= ptr[i];
757 : : }
6591 tgl@sss.pgh.pa.us 758 : 2940 : *left++ = j;
759 : 2940 : v->spl_nleft++;
760 : : }
761 : : else
762 : : {
763 [ + - - + ]: 3002 : if (ISALLTRUE(datum_r) || cache[j].allistrue)
764 : : {
6591 tgl@sss.pgh.pa.us 765 [ # # ]:UBC 0 : if (!ISALLTRUE(datum_r))
942 peter@eisentraut.org 766 : 0 : memset(GETSIGN(datum_r), 0xff, siglen);
767 : : }
768 : : else
769 : : {
6591 tgl@sss.pgh.pa.us 770 :CBC 3002 : ptr = cache[j].sign;
1986 akorotkov@postgresql 771 [ + + ]: 486361 : LOOPBYTE(siglen)
6504 bruce@momjian.us 772 : 483359 : union_r[i] |= ptr[i];
773 : : }
6591 tgl@sss.pgh.pa.us 774 : 3002 : *right++ = j;
775 : 3002 : v->spl_nright++;
776 : : }
777 : : }
778 : :
779 : 206 : *right = *left = FirstOffsetNumber;
780 : 206 : v->spl_ldatum = PointerGetDatum(datum_l);
781 : 206 : v->spl_rdatum = PointerGetDatum(datum_r);
782 : :
783 : 206 : PG_RETURN_POINTER(v);
784 : : }
785 : :
786 : : /*
787 : : * Formerly, gtsvector_consistent was declared in pg_proc.h with arguments
788 : : * that did not match the documented conventions for GiST support functions.
789 : : * We fixed that, but we still need a pg_proc entry with the old signature
790 : : * to support reloading pre-9.6 contrib/tsearch2 opclass declarations.
791 : : * This compatibility function should go away eventually.
792 : : */
793 : : Datum
3475 tgl@sss.pgh.pa.us 794 :UBC 0 : gtsvector_consistent_oldsig(PG_FUNCTION_ARGS)
795 : : {
796 : 0 : return gtsvector_consistent(fcinfo);
797 : : }
798 : :
799 : : Datum
1986 akorotkov@postgresql 800 :CBC 177 : gtsvector_options(PG_FUNCTION_ARGS)
801 : : {
802 : 177 : local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
803 : :
804 : 177 : init_local_reloptions(relopts, sizeof(GistTsVectorOptions));
805 : 177 : add_local_int_reloption(relopts, "siglen", "signature length",
806 : : SIGLEN_DEFAULT, 1, SIGLEN_MAX,
807 : : offsetof(GistTsVectorOptions, siglen));
808 : :
809 : 177 : PG_RETURN_VOID();
810 : : }
|