2 * contrib/intarray/_int_gist.c
6 #include "access/gist.h"
7 #include "access/skey.h"
11 #define GETENTRY(vec,pos) ((ArrayType *) DatumGetPointer((vec)->vector[(pos)].key))
14 ** GiST support methods
16 PG_FUNCTION_INFO_V1(g_int_consistent);
17 PG_FUNCTION_INFO_V1(g_int_compress);
18 PG_FUNCTION_INFO_V1(g_int_decompress);
19 PG_FUNCTION_INFO_V1(g_int_penalty);
20 PG_FUNCTION_INFO_V1(g_int_picksplit);
21 PG_FUNCTION_INFO_V1(g_int_union);
22 PG_FUNCTION_INFO_V1(g_int_same);
24 Datum g_int_consistent(PG_FUNCTION_ARGS);
25 Datum g_int_compress(PG_FUNCTION_ARGS);
26 Datum g_int_decompress(PG_FUNCTION_ARGS);
27 Datum g_int_penalty(PG_FUNCTION_ARGS);
28 Datum g_int_picksplit(PG_FUNCTION_ARGS);
29 Datum g_int_union(PG_FUNCTION_ARGS);
30 Datum g_int_same(PG_FUNCTION_ARGS);
34 ** The GiST Consistent method for _intments
35 ** Should return false if for all data items x below entry,
36 ** the predicate x op query == FALSE, where op is the oper
37 ** corresponding to strategy in the pg_amop table.
40 g_int_consistent(PG_FUNCTION_ARGS)
42 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
43 ArrayType *query = PG_GETARG_ARRAYTYPE_P_COPY(1);
44 StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
46 /* Oid subtype = PG_GETARG_OID(3); */
47 bool *recheck = (bool *) PG_GETARG_POINTER(4);
50 /* this is exact except for RTSameStrategyNumber */
51 *recheck = (strategy == RTSameStrategyNumber);
53 if (strategy == BooleanSearchStrategy)
55 retval = execconsistent((QUERYTYPE *) query,
56 (ArrayType *) DatumGetPointer(entry->key),
60 PG_RETURN_BOOL(retval);
63 /* sort query for fast search, key is already sorted */
69 case RTOverlapStrategyNumber:
70 retval = inner_int_overlap((ArrayType *) DatumGetPointer(entry->key),
73 case RTSameStrategyNumber:
75 DirectFunctionCall3(g_int_same,
77 PointerGetDatum(query),
78 PointerGetDatum(&retval));
80 retval = inner_int_contains((ArrayType *) DatumGetPointer(entry->key),
83 case RTContainsStrategyNumber:
84 case RTOldContainsStrategyNumber:
85 retval = inner_int_contains((ArrayType *) DatumGetPointer(entry->key),
88 case RTContainedByStrategyNumber:
89 case RTOldContainedByStrategyNumber:
91 retval = inner_int_contains(query,
92 (ArrayType *) DatumGetPointer(entry->key));
94 retval = inner_int_overlap((ArrayType *) DatumGetPointer(entry->key),
101 PG_RETURN_BOOL(retval);
105 g_int_union(PG_FUNCTION_ARGS)
107 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
108 int *size = (int *) PG_GETARG_POINTER(1);
114 for (i = 0; i < entryvec->n; i++)
116 ArrayType *ent = GETENTRY(entryvec, i);
119 totlen += ARRNELEMS(ent);
122 res = new_intArrayType(totlen);
125 for (i = 0; i < entryvec->n; i++)
127 ArrayType *ent = GETENTRY(entryvec, i);
130 nel = ARRNELEMS(ent);
131 memcpy(ptr, ARRPTR(ent), nel * sizeof(int4));
136 res = _int_unique(res);
137 *size = VARSIZE(res);
138 PG_RETURN_POINTER(res);
142 ** GiST Compress and Decompress methods
145 g_int_compress(PG_FUNCTION_ARGS)
147 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
158 r = DatumGetArrayTypePCopy(entry->key);
162 if (ARRNELEMS(r) >= 2 * MAXNUMRANGE)
163 elog(NOTICE, "input array is too big (%d maximum allowed, %d current), use gist__intbig_ops opclass instead",
164 2 * MAXNUMRANGE - 1, ARRNELEMS(r));
166 retval = palloc(sizeof(GISTENTRY));
167 gistentryinit(*retval, PointerGetDatum(r),
168 entry->rel, entry->page, entry->offset, FALSE);
170 PG_RETURN_POINTER(retval);
174 * leaf entries never compress one more time, only when entry->leafkey
175 * ==true, so now we work only with internal keys
178 r = DatumGetArrayTypeP(entry->key);
182 if (r != (ArrayType *) DatumGetPointer(entry->key))
184 PG_RETURN_POINTER(entry);
187 if ((len = ARRNELEMS(r)) >= 2 * MAXNUMRANGE)
189 if (r == (ArrayType *) DatumGetPointer(entry->key))
190 r = DatumGetArrayTypePCopy(entry->key);
191 r = resize_intArrayType(r, 2 * (len));
195 for (i = len - 1; i >= 0; i--)
196 dr[2 * i] = dr[2 * i + 1] = dr[i];
200 while (len > MAXNUMRANGE * 2)
203 for (i = 2; i < len; i += 2)
204 if (min > (dr[i] - dr[i - 1]))
206 min = (dr[i] - dr[i - 1]);
209 memmove((void *) &dr[cand - 1], (void *) &dr[cand + 1], (len - cand - 1) * sizeof(int32));
212 r = resize_intArrayType(r, len);
213 retval = palloc(sizeof(GISTENTRY));
214 gistentryinit(*retval, PointerGetDatum(r),
215 entry->rel, entry->page, entry->offset, FALSE);
216 PG_RETURN_POINTER(retval);
219 PG_RETURN_POINTER(entry);
221 PG_RETURN_POINTER(entry);
225 g_int_decompress(PG_FUNCTION_ARGS)
227 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
238 in = DatumGetArrayTypeP(entry->key);
243 if (in != (ArrayType *) DatumGetPointer(entry->key))
245 retval = palloc(sizeof(GISTENTRY));
246 gistentryinit(*retval, PointerGetDatum(in),
247 entry->rel, entry->page, entry->offset, FALSE);
248 PG_RETURN_POINTER(retval);
251 PG_RETURN_POINTER(entry);
254 lenin = ARRNELEMS(in);
256 if (lenin < 2 * MAXNUMRANGE)
257 { /* not compressed value */
258 if (in != (ArrayType *) DatumGetPointer(entry->key))
260 retval = palloc(sizeof(GISTENTRY));
261 gistentryinit(*retval, PointerGetDatum(in),
262 entry->rel, entry->page, entry->offset, FALSE);
264 PG_RETURN_POINTER(retval);
266 PG_RETURN_POINTER(entry);
270 lenr = internal_size(din, lenin);
272 r = new_intArrayType(lenr);
275 for (i = 0; i < lenin; i += 2)
276 for (j = din[i]; j <= din[i + 1]; j++)
277 if ((!i) || *(dr - 1) != j)
280 if (in != (ArrayType *) DatumGetPointer(entry->key))
282 retval = palloc(sizeof(GISTENTRY));
283 gistentryinit(*retval, PointerGetDatum(r),
284 entry->rel, entry->page, entry->offset, FALSE);
286 PG_RETURN_POINTER(retval);
290 ** The GiST Penalty method for _intments
293 g_int_penalty(PG_FUNCTION_ARGS)
295 GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
296 GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
297 float *result = (float *) PG_GETARG_POINTER(2);
302 ud = inner_int_union((ArrayType *) DatumGetPointer(origentry->key),
303 (ArrayType *) DatumGetPointer(newentry->key));
304 rt__int_size(ud, &tmp1);
305 rt__int_size((ArrayType *) DatumGetPointer(origentry->key), &tmp2);
306 *result = tmp1 - tmp2;
309 PG_RETURN_POINTER(result);
315 g_int_same(PG_FUNCTION_ARGS)
317 ArrayType *a = PG_GETARG_ARRAYTYPE_P(0);
318 ArrayType *b = PG_GETARG_ARRAYTYPE_P(1);
319 bool *result = (bool *) PG_GETARG_POINTER(2);
320 int4 n = ARRNELEMS(a);
327 if (n != ARRNELEMS(b))
330 PG_RETURN_POINTER(result);
344 PG_RETURN_POINTER(result);
347 /*****************************************************************
348 ** Common GiST Method
349 *****************************************************************/
358 comparecost(const void *a, const void *b)
360 if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
363 return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
367 ** The GiST PickSplit method for _intments
368 ** We use Guttman's poly time split algorithm
371 g_int_picksplit(PG_FUNCTION_ARGS)
373 GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
374 GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
377 ArrayType *datum_alpha,
395 OffsetNumber seed_1 = 0,
400 SPLITCOST *costvector;
403 elog(DEBUG3, "--------picksplit %d", entryvec->n);
406 maxoff = entryvec->n - 2;
407 nbytes = (maxoff + 2) * sizeof(OffsetNumber);
408 v->spl_left = (OffsetNumber *) palloc(nbytes);
409 v->spl_right = (OffsetNumber *) palloc(nbytes);
413 for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
415 datum_alpha = GETENTRY(entryvec, i);
416 for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
418 datum_beta = GETENTRY(entryvec, j);
420 /* compute the wasted space by unioning these guys */
421 /* size_waste = size_union - size_inter; */
422 union_d = inner_int_union(datum_alpha, datum_beta);
423 rt__int_size(union_d, &size_union);
424 inter_d = inner_int_inter(datum_alpha, datum_beta);
425 rt__int_size(inter_d, &size_inter);
426 size_waste = size_union - size_inter;
430 if (inter_d != (ArrayType *) NULL)
434 * are these a more promising split that what we've already seen?
437 if (size_waste > waste || firsttime)
449 right = v->spl_right;
451 if (seed_1 == 0 || seed_2 == 0)
457 datum_alpha = GETENTRY(entryvec, seed_1);
458 datum_l = copy_intArrayType(datum_alpha);
459 rt__int_size(datum_l, &size_l);
460 datum_beta = GETENTRY(entryvec, seed_2);
461 datum_r = copy_intArrayType(datum_beta);
462 rt__int_size(datum_r, &size_r);
464 maxoff = OffsetNumberNext(maxoff);
469 costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
470 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
472 costvector[i - 1].pos = i;
473 datum_alpha = GETENTRY(entryvec, i);
474 union_d = inner_int_union(datum_l, datum_alpha);
475 rt__int_size(union_d, &size_alpha);
477 union_d = inner_int_union(datum_r, datum_alpha);
478 rt__int_size(union_d, &size_beta);
480 costvector[i - 1].cost = Abs((size_alpha - size_l) - (size_beta - size_r));
482 qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
485 * Now split up the regions between the two seeds. An important property
486 * of this split algorithm is that the split vector v has the indices of
487 * items to be split in order in its left and right vectors. We exploit
488 * this property by doing a merge in the code that actually splits the
491 * For efficiency, we also place the new index tuple in this loop. This is
492 * handled at the very end, when we have placed all the existing tuples
493 * and i == maxoff + 1.
497 for (j = 0; j < maxoff; j++)
499 i = costvector[j].pos;
502 * If we've already decided where to place this item, just put it on
503 * the right list. Otherwise, we need to figure out which page needs
504 * the least enlargement in order to store the item.
513 else if (i == seed_2)
520 /* okay, which page needs least enlargement? */
521 datum_alpha = GETENTRY(entryvec, i);
522 union_dl = inner_int_union(datum_l, datum_alpha);
523 union_dr = inner_int_union(datum_r, datum_alpha);
524 rt__int_size(union_dl, &size_alpha);
525 rt__int_size(union_dr, &size_beta);
527 /* pick which page to add it to */
528 if (size_alpha - size_l < size_beta - size_r + WISH_F(v->spl_nleft, v->spl_nright, 0.01))
552 *right = *left = FirstOffsetNumber;
554 v->spl_ldatum = PointerGetDatum(datum_l);
555 v->spl_rdatum = PointerGetDatum(datum_r);
557 PG_RETURN_POINTER(v);