2 * contrib/ltree/_ltree_gist.c
5 * GiST support for ltree[]
6 * Teodor Sigaev <teodor@stack.net>
10 #include "access/gist.h"
11 #include "access/stratnum.h"
14 #include "port/pg_bitutils.h"
16 PG_FUNCTION_INFO_V1(_ltree_compress);
17 PG_FUNCTION_INFO_V1(_ltree_same);
18 PG_FUNCTION_INFO_V1(_ltree_union);
19 PG_FUNCTION_INFO_V1(_ltree_penalty);
20 PG_FUNCTION_INFO_V1(_ltree_picksplit);
21 PG_FUNCTION_INFO_V1(_ltree_consistent);
23 #define GETENTRY(vec,pos) ((ltree_gist *) DatumGetPointer((vec)->vector[(pos)].key))
24 #define NEXTVAL(x) ( (ltree*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) )
26 #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
30 hashing(BITVECP sign, ltree *t)
32 int tlen = t->numlevel;
33 ltree_level *cur = LTREE_FIRST(t);
38 hash = ltree_crc32_sz(cur->name, cur->len);
40 cur = LEVEL_NEXT(cur);
46 _ltree_compress(PG_FUNCTION_ARGS)
48 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
49 GISTENTRY *retval = entry;
54 ArrayType *val = DatumGetArrayTypeP(entry->key);
55 int32 len = LTG_HDRSIZE + ASIGLEN;
56 int num = ArrayGetNItems(ARR_NDIM(val), ARR_DIMS(val));
57 ltree *item = (ltree *) ARR_DATA_PTR(val);
59 if (ARR_NDIM(val) > 1)
61 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
62 errmsg("array must be one-dimensional")));
63 if (array_contains_nulls(val))
65 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
66 errmsg("array must not contain nulls")));
68 key = (ltree_gist *) palloc0(len);
69 SET_VARSIZE(key, len);
72 MemSet(LTG_SIGN(key), 0, ASIGLEN);
75 hashing(LTG_SIGN(key), item);
80 retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
81 gistentryinit(*retval, PointerGetDatum(key),
82 entry->rel, entry->page,
83 entry->offset, false);
85 else if (!LTG_ISALLTRUE(entry->key))
91 BITVECP sign = LTG_SIGN(DatumGetPointer(entry->key));
95 if ((sign[i] & 0xff) != 0xff)
96 PG_RETURN_POINTER(retval);
99 key = (ltree_gist *) palloc0(len);
100 SET_VARSIZE(key, len);
101 key->flag = LTG_ALLTRUE;
103 retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
104 gistentryinit(*retval, PointerGetDatum(key),
105 entry->rel, entry->page,
106 entry->offset, false);
108 PG_RETURN_POINTER(retval);
112 _ltree_same(PG_FUNCTION_ARGS)
114 ltree_gist *a = (ltree_gist *) PG_GETARG_POINTER(0);
115 ltree_gist *b = (ltree_gist *) PG_GETARG_POINTER(1);
116 bool *result = (bool *) PG_GETARG_POINTER(2);
118 if (LTG_ISALLTRUE(a) && LTG_ISALLTRUE(b))
120 else if (LTG_ISALLTRUE(a))
122 else if (LTG_ISALLTRUE(b))
127 BITVECP sa = LTG_SIGN(a),
140 PG_RETURN_POINTER(result);
144 unionkey(BITVECP sbase, ltree_gist *add)
147 BITVECP sadd = LTG_SIGN(add);
149 if (LTG_ISALLTRUE(add))
158 _ltree_union(PG_FUNCTION_ARGS)
160 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
161 int *size = (int *) PG_GETARG_POINTER(1);
168 MemSet((void *) base, 0, sizeof(ABITVEC));
169 for (i = 0; i < entryvec->n; i++)
171 if (unionkey(base, GETENTRY(entryvec, i)))
178 len = LTG_HDRSIZE + ((flag & LTG_ALLTRUE) ? 0 : ASIGLEN);
179 result = (ltree_gist *) palloc0(len);
180 SET_VARSIZE(result, len);
182 if (!LTG_ISALLTRUE(result))
183 memcpy((void *) LTG_SIGN(result), (void *) base, sizeof(ABITVEC));
186 PG_RETURN_POINTER(result);
190 sizebitvec(BITVECP sign)
192 return pg_popcount((const char *) sign, ASIGLEN);
196 hemdistsign(BITVECP a, BITVECP b)
204 diff = (unsigned char) (a[i] ^ b[i]);
205 /* Using the popcount functions here isn't likely to win */
206 dist += pg_number_of_ones[diff];
212 hemdist(ltree_gist *a, ltree_gist *b)
214 if (LTG_ISALLTRUE(a))
216 if (LTG_ISALLTRUE(b))
219 return ASIGLENBIT - sizebitvec(LTG_SIGN(b));
221 else if (LTG_ISALLTRUE(b))
222 return ASIGLENBIT - sizebitvec(LTG_SIGN(a));
224 return hemdistsign(LTG_SIGN(a), LTG_SIGN(b));
229 _ltree_penalty(PG_FUNCTION_ARGS)
231 ltree_gist *origval = (ltree_gist *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
232 ltree_gist *newval = (ltree_gist *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
233 float *penalty = (float *) PG_GETARG_POINTER(2);
235 *penalty = hemdist(origval, newval);
236 PG_RETURN_POINTER(penalty);
246 comparecost(const void *a, const void *b)
248 return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
252 _ltree_picksplit(PG_FUNCTION_ARGS)
254 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
255 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
267 OffsetNumber seed_1 = 0,
274 SPLITCOST *costvector;
278 maxoff = entryvec->n - 2;
279 nbytes = (maxoff + 2) * sizeof(OffsetNumber);
280 v->spl_left = (OffsetNumber *) palloc(nbytes);
281 v->spl_right = (OffsetNumber *) palloc(nbytes);
283 for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
285 _k = GETENTRY(entryvec, k);
286 for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
288 size_waste = hemdist(_k, GETENTRY(entryvec, j));
289 if (size_waste > waste)
300 right = v->spl_right;
303 if (seed_1 == 0 || seed_2 == 0)
309 /* form initial .. */
310 if (LTG_ISALLTRUE(GETENTRY(entryvec, seed_1)))
312 datum_l = (ltree_gist *) palloc0(LTG_HDRSIZE);
313 SET_VARSIZE(datum_l, LTG_HDRSIZE);
314 datum_l->flag = LTG_ALLTRUE;
318 datum_l = (ltree_gist *) palloc0(LTG_HDRSIZE + ASIGLEN);
319 SET_VARSIZE(datum_l, LTG_HDRSIZE + ASIGLEN);
321 memcpy((void *) LTG_SIGN(datum_l), (void *) LTG_SIGN(GETENTRY(entryvec, seed_1)), sizeof(ABITVEC));
323 if (LTG_ISALLTRUE(GETENTRY(entryvec, seed_2)))
325 datum_r = (ltree_gist *) palloc0(LTG_HDRSIZE);
326 SET_VARSIZE(datum_r, LTG_HDRSIZE);
327 datum_r->flag = LTG_ALLTRUE;
331 datum_r = (ltree_gist *) palloc0(LTG_HDRSIZE + ASIGLEN);
332 SET_VARSIZE(datum_r, LTG_HDRSIZE + ASIGLEN);
334 memcpy((void *) LTG_SIGN(datum_r), (void *) LTG_SIGN(GETENTRY(entryvec, seed_2)), sizeof(ABITVEC));
337 maxoff = OffsetNumberNext(maxoff);
338 /* sort before ... */
339 costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
340 for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
342 costvector[j - 1].pos = j;
343 _j = GETENTRY(entryvec, j);
344 size_alpha = hemdist(datum_l, _j);
345 size_beta = hemdist(datum_r, _j);
346 costvector[j - 1].cost = Abs(size_alpha - size_beta);
348 qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
350 union_l = LTG_SIGN(datum_l);
351 union_r = LTG_SIGN(datum_r);
353 for (k = 0; k < maxoff; k++)
355 j = costvector[k].pos;
362 else if (j == seed_2)
368 _j = GETENTRY(entryvec, j);
369 size_alpha = hemdist(datum_l, _j);
370 size_beta = hemdist(datum_r, _j);
372 if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001))
374 if (LTG_ISALLTRUE(datum_l) || LTG_ISALLTRUE(_j))
376 if (!LTG_ISALLTRUE(datum_l))
377 MemSet((void *) union_l, 0xff, sizeof(ABITVEC));
383 union_l[i] |= ptr[i];
390 if (LTG_ISALLTRUE(datum_r) || LTG_ISALLTRUE(_j))
392 if (!LTG_ISALLTRUE(datum_r))
393 MemSet((void *) union_r, 0xff, sizeof(ABITVEC));
399 union_r[i] |= ptr[i];
406 *right = *left = FirstOffsetNumber;
408 v->spl_ldatum = PointerGetDatum(datum_l);
409 v->spl_rdatum = PointerGetDatum(datum_r);
411 PG_RETURN_POINTER(v);
415 gist_te(ltree_gist *key, ltree *query)
417 ltree_level *curq = LTREE_FIRST(query);
418 BITVECP sign = LTG_SIGN(key);
419 int qlen = query->numlevel;
422 if (LTG_ISALLTRUE(key))
427 hv = ltree_crc32_sz(curq->name, curq->len);
428 if (!GETBIT(sign, AHASHVAL(hv)))
430 curq = LEVEL_NEXT(curq);
438 checkcondition_bit(void *checkval, ITEM *val)
440 return (FLG_CANLOOKSIGN(val->flag)) ? GETBIT(checkval, AHASHVAL(val->val)) : true;
444 gist_qtxt(ltree_gist *key, ltxtquery *query)
446 if (LTG_ISALLTRUE(key))
449 return ltree_execute(
451 (void *) LTG_SIGN(key), false,
457 gist_qe(ltree_gist *key, lquery *query)
459 lquery_level *curq = LQUERY_FIRST(query);
460 BITVECP sign = LTG_SIGN(key);
461 int qlen = query->numlevel;
463 if (LTG_ISALLTRUE(key))
468 if (curq->numvar && LQL_CANLOOKSIGN(curq))
470 bool isexist = false;
471 int vlen = curq->numvar;
472 lquery_variant *curv = LQL_FIRST(curq);
476 if (GETBIT(sign, AHASHVAL(curv->val)))
481 curv = LVAR_NEXT(curv);
488 curq = LQL_NEXT(curq);
496 _arrq_cons(ltree_gist *key, ArrayType *_query)
498 lquery *query = (lquery *) ARR_DATA_PTR(_query);
499 int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
501 if (ARR_NDIM(_query) > 1)
503 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
504 errmsg("array must be one-dimensional")));
505 if (array_contains_nulls(_query))
507 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
508 errmsg("array must not contain nulls")));
512 if (gist_qe(key, query))
515 query = (lquery *) NEXTVAL(query);
521 _ltree_consistent(PG_FUNCTION_ARGS)
523 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
524 void *query = (void *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
525 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
527 /* Oid subtype = PG_GETARG_OID(3); */
528 bool *recheck = (bool *) PG_GETARG_POINTER(4);
529 ltree_gist *key = (ltree_gist *) DatumGetPointer(entry->key);
532 /* All cases served by this function are inexact */
539 res = gist_te(key, (ltree *) query);
543 res = gist_qe(key, (lquery *) query);
547 res = gist_qtxt(key, (ltxtquery *) query);
551 res = _arrq_cons(key, (ArrayType *) query);
555 elog(ERROR, "unrecognized StrategyNumber: %d", strategy);
557 PG_FREE_IF_COPY(query, 1);