Add a planner support function for numeric generate_series().
[pgsql.git] / contrib / btree_gist / btree_utils_var.c
blobd9df2356cd1e43f58714505f144c4818374905af
1 /*
2 * contrib/btree_gist/btree_utils_var.c
3 */
4 #include "postgres.h"
6 #include <math.h>
7 #include <limits.h>
8 #include <float.h>
10 #include "btree_gist.h"
11 #include "btree_utils_var.h"
12 #include "mb/pg_wchar.h"
13 #include "utils/rel.h"
15 /* used for key sorting */
16 typedef struct
18 int i;
19 GBT_VARKEY *t;
20 } Vsrt;
22 typedef struct
24 const gbtree_vinfo *tinfo;
25 Oid collation;
26 FmgrInfo *flinfo;
27 } gbt_vsrt_arg;
30 PG_FUNCTION_INFO_V1(gbt_var_decompress);
31 PG_FUNCTION_INFO_V1(gbt_var_fetch);
34 Datum
35 gbt_var_decompress(PG_FUNCTION_ARGS)
37 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
38 GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
40 if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
42 GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
44 gistentryinit(*retval, PointerGetDatum(key),
45 entry->rel, entry->page,
46 entry->offset, false);
48 PG_RETURN_POINTER(retval);
51 PG_RETURN_POINTER(entry);
54 /* Returns a better readable representation of variable key ( sets pointer ) */
55 GBT_VARKEY_R
56 gbt_var_key_readable(const GBT_VARKEY *k)
58 GBT_VARKEY_R r;
60 r.lower = (bytea *) &(((char *) k)[VARHDRSZ]);
61 if (VARSIZE(k) > (VARHDRSZ + (VARSIZE(r.lower))))
62 r.upper = (bytea *) &(((char *) k)[VARHDRSZ + INTALIGN(VARSIZE(r.lower))]);
63 else
64 r.upper = r.lower;
65 return r;
70 * Create a leaf-entry to store in the index, from a single Datum.
72 static GBT_VARKEY *
73 gbt_var_key_from_datum(const struct varlena *u)
75 int32 lowersize = VARSIZE(u);
76 GBT_VARKEY *r;
78 r = (GBT_VARKEY *) palloc(lowersize + VARHDRSZ);
79 memcpy(VARDATA(r), u, lowersize);
80 SET_VARSIZE(r, lowersize + VARHDRSZ);
82 return r;
86 * Create an entry to store in the index, from lower and upper bound.
88 GBT_VARKEY *
89 gbt_var_key_copy(const GBT_VARKEY_R *u)
91 int32 lowersize = VARSIZE(u->lower);
92 int32 uppersize = VARSIZE(u->upper);
93 GBT_VARKEY *r;
95 r = (GBT_VARKEY *) palloc0(INTALIGN(lowersize) + uppersize + VARHDRSZ);
96 memcpy(VARDATA(r), u->lower, lowersize);
97 memcpy(VARDATA(r) + INTALIGN(lowersize), u->upper, uppersize);
98 SET_VARSIZE(r, INTALIGN(lowersize) + uppersize + VARHDRSZ);
100 return r;
104 static GBT_VARKEY *
105 gbt_var_leaf2node(GBT_VARKEY *leaf, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
107 GBT_VARKEY *out = leaf;
109 if (tinfo->f_l2n)
110 out = tinfo->f_l2n(leaf, flinfo);
112 return out;
117 * returns the common prefix length of a node key
119 static int32
120 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo)
122 GBT_VARKEY_R r = gbt_var_key_readable(node);
123 int32 i = 0;
124 int32 l = 0;
125 int32 t1len = VARSIZE(r.lower) - VARHDRSZ;
126 int32 t2len = VARSIZE(r.upper) - VARHDRSZ;
127 int32 ml = Min(t1len, t2len);
128 char *p1 = VARDATA(r.lower);
129 char *p2 = VARDATA(r.upper);
131 if (ml == 0)
132 return 0;
134 while (i < ml)
136 if (tinfo->eml > 1 && l == 0)
138 if ((l = pg_mblen(p1)) != pg_mblen(p2))
140 return i;
143 if (*p1 != *p2)
145 if (tinfo->eml > 1)
147 return (i - l + 1);
149 else
151 return i;
155 p1++;
156 p2++;
157 l--;
158 i++;
160 return ml; /* lower == upper */
165 * returns true, if query matches prefix ( common prefix )
167 static bool
168 gbt_bytea_pf_match(const bytea *pf, const bytea *query, const gbtree_vinfo *tinfo)
170 bool out = false;
171 int32 qlen = VARSIZE(query) - VARHDRSZ;
172 int32 nlen = VARSIZE(pf) - VARHDRSZ;
174 if (nlen <= qlen)
176 char *q = VARDATA(query);
177 char *n = VARDATA(pf);
179 out = (memcmp(q, n, nlen) == 0);
182 return out;
187 * returns true, if query matches node using common prefix
189 static bool
190 gbt_var_node_pf_match(const GBT_VARKEY_R *node, const bytea *query, const gbtree_vinfo *tinfo)
192 return (tinfo->trnc &&
193 (gbt_bytea_pf_match(node->lower, query, tinfo) ||
194 gbt_bytea_pf_match(node->upper, query, tinfo)));
199 * truncates / compresses the node key
200 * cpf_length .. common prefix length
202 static GBT_VARKEY *
203 gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo)
205 GBT_VARKEY *out = NULL;
206 GBT_VARKEY_R r = gbt_var_key_readable(node);
207 int32 len1 = VARSIZE(r.lower) - VARHDRSZ;
208 int32 len2 = VARSIZE(r.upper) - VARHDRSZ;
209 int32 si;
210 char *out2;
212 len1 = Min(len1, (cpf_length + 1));
213 len2 = Min(len2, (cpf_length + 1));
215 si = 2 * VARHDRSZ + INTALIGN(len1 + VARHDRSZ) + len2;
216 out = (GBT_VARKEY *) palloc0(si);
217 SET_VARSIZE(out, si);
219 memcpy(VARDATA(out), r.lower, len1 + VARHDRSZ);
220 SET_VARSIZE(VARDATA(out), len1 + VARHDRSZ);
222 out2 = VARDATA(out) + INTALIGN(len1 + VARHDRSZ);
223 memcpy(out2, r.upper, len2 + VARHDRSZ);
224 SET_VARSIZE(out2, len2 + VARHDRSZ);
226 return out;
231 void
232 gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation,
233 const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
235 GBT_VARKEY_R eo = gbt_var_key_readable(e);
236 GBT_VARKEY_R nr;
238 if (eo.lower == eo.upper) /* leaf */
240 GBT_VARKEY *tmp;
242 tmp = gbt_var_leaf2node(e, tinfo, flinfo);
243 if (tmp != e)
244 eo = gbt_var_key_readable(tmp);
247 if (DatumGetPointer(*u))
249 GBT_VARKEY_R ro = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(*u));
250 bool update = false;
252 nr.lower = ro.lower;
253 nr.upper = ro.upper;
255 if (tinfo->f_cmp(ro.lower, eo.lower, collation, flinfo) > 0)
257 nr.lower = eo.lower;
258 update = true;
261 if (tinfo->f_cmp(ro.upper, eo.upper, collation, flinfo) < 0)
263 nr.upper = eo.upper;
264 update = true;
267 if (update)
268 *u = PointerGetDatum(gbt_var_key_copy(&nr));
270 else
272 nr.lower = eo.lower;
273 nr.upper = eo.upper;
274 *u = PointerGetDatum(gbt_var_key_copy(&nr));
279 GISTENTRY *
280 gbt_var_compress(GISTENTRY *entry, const gbtree_vinfo *tinfo)
282 GISTENTRY *retval;
284 if (entry->leafkey)
286 struct varlena *leaf = PG_DETOAST_DATUM(entry->key);
287 GBT_VARKEY *r;
289 r = gbt_var_key_from_datum(leaf);
291 retval = palloc(sizeof(GISTENTRY));
292 gistentryinit(*retval, PointerGetDatum(r),
293 entry->rel, entry->page,
294 entry->offset, true);
296 else
297 retval = entry;
299 return retval;
303 Datum
304 gbt_var_fetch(PG_FUNCTION_ARGS)
306 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
307 GBT_VARKEY *key = (GBT_VARKEY *) PG_DETOAST_DATUM(entry->key);
308 GBT_VARKEY_R r = gbt_var_key_readable(key);
309 GISTENTRY *retval;
311 retval = palloc(sizeof(GISTENTRY));
312 gistentryinit(*retval, PointerGetDatum(r.lower),
313 entry->rel, entry->page,
314 entry->offset, true);
316 PG_RETURN_POINTER(retval);
320 GBT_VARKEY *
321 gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation,
322 const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
324 int i = 0,
325 numranges = entryvec->n;
326 GBT_VARKEY *cur;
327 Datum out;
328 GBT_VARKEY_R rk;
330 *size = sizeof(GBT_VARKEY);
332 cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key);
333 rk = gbt_var_key_readable(cur);
334 out = PointerGetDatum(gbt_var_key_copy(&rk));
336 for (i = 1; i < numranges; i++)
338 cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key);
339 gbt_var_bin_union(&out, cur, collation, tinfo, flinfo);
343 /* Truncate (=compress) key */
344 if (tinfo->trnc)
346 int32 plen;
347 GBT_VARKEY *trc = NULL;
349 plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo);
350 trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);
352 out = PointerGetDatum(trc);
355 return ((GBT_VARKEY *) DatumGetPointer(out));
359 bool
360 gbt_var_same(Datum d1, Datum d2, Oid collation,
361 const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
363 GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1);
364 GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2);
365 GBT_VARKEY_R r1,
368 r1 = gbt_var_key_readable(t1);
369 r2 = gbt_var_key_readable(t2);
371 return (tinfo->f_cmp(r1.lower, r2.lower, collation, flinfo) == 0 &&
372 tinfo->f_cmp(r1.upper, r2.upper, collation, flinfo) == 0);
376 float *
377 gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n,
378 Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
380 GBT_VARKEY *orge = (GBT_VARKEY *) DatumGetPointer(o->key);
381 GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
382 GBT_VARKEY_R ok,
385 *res = 0.0;
387 nk = gbt_var_key_readable(newe);
388 if (nk.lower == nk.upper) /* leaf */
390 GBT_VARKEY *tmp;
392 tmp = gbt_var_leaf2node(newe, tinfo, flinfo);
393 if (tmp != newe)
394 nk = gbt_var_key_readable(tmp);
396 ok = gbt_var_key_readable(orge);
398 if ((VARSIZE(ok.lower) - VARHDRSZ) == 0 && (VARSIZE(ok.upper) - VARHDRSZ) == 0)
399 *res = 0.0;
400 else if (!((tinfo->f_cmp(nk.lower, ok.lower, collation, flinfo) >= 0 ||
401 gbt_bytea_pf_match(ok.lower, nk.lower, tinfo)) &&
402 (tinfo->f_cmp(nk.upper, ok.upper, collation, flinfo) <= 0 ||
403 gbt_bytea_pf_match(ok.upper, nk.upper, tinfo))))
405 Datum d = PointerGetDatum(0);
406 double dres;
407 int32 ol,
410 gbt_var_bin_union(&d, orge, collation, tinfo, flinfo);
411 ol = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
412 gbt_var_bin_union(&d, newe, collation, tinfo, flinfo);
413 ul = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo);
415 if (ul < ol)
417 dres = (ol - ul); /* reduction of common prefix len */
419 else
421 GBT_VARKEY_R uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(d));
422 unsigned char tmp[4];
424 tmp[0] = (unsigned char) (((VARSIZE(ok.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.lower)[ul]));
425 tmp[1] = (unsigned char) (((VARSIZE(uk.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.lower)[ul]));
426 tmp[2] = (unsigned char) (((VARSIZE(ok.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.upper)[ul]));
427 tmp[3] = (unsigned char) (((VARSIZE(uk.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.upper)[ul]));
428 dres = abs(tmp[0] - tmp[1]) + abs(tmp[3] - tmp[2]);
429 dres /= 256.0;
432 *res += FLT_MIN;
433 *res += (float) (dres / ((double) (ol + 1)));
434 *res *= (FLT_MAX / (o->rel->rd_att->natts + 1));
437 return res;
441 static int
442 gbt_vsrt_cmp(const void *a, const void *b, void *arg)
444 GBT_VARKEY_R ar = gbt_var_key_readable(((const Vsrt *) a)->t);
445 GBT_VARKEY_R br = gbt_var_key_readable(((const Vsrt *) b)->t);
446 const gbt_vsrt_arg *varg = (const gbt_vsrt_arg *) arg;
447 int res;
449 res = varg->tinfo->f_cmp(ar.lower, br.lower, varg->collation, varg->flinfo);
450 if (res == 0)
451 return varg->tinfo->f_cmp(ar.upper, br.upper, varg->collation, varg->flinfo);
453 return res;
456 GIST_SPLITVEC *
457 gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v,
458 Oid collation, const gbtree_vinfo *tinfo, FmgrInfo *flinfo)
460 OffsetNumber i,
461 maxoff = entryvec->n - 1;
462 Vsrt *arr;
463 int svcntr = 0,
464 nbytes;
465 char *cur;
466 GBT_VARKEY **sv = NULL;
467 gbt_vsrt_arg varg;
469 arr = (Vsrt *) palloc((maxoff + 1) * sizeof(Vsrt));
470 nbytes = (maxoff + 2) * sizeof(OffsetNumber);
471 v->spl_left = (OffsetNumber *) palloc(nbytes);
472 v->spl_right = (OffsetNumber *) palloc(nbytes);
473 v->spl_ldatum = PointerGetDatum(0);
474 v->spl_rdatum = PointerGetDatum(0);
475 v->spl_nleft = 0;
476 v->spl_nright = 0;
478 sv = palloc(sizeof(bytea *) * (maxoff + 1));
480 /* Sort entries */
482 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
484 GBT_VARKEY_R ro;
486 cur = (char *) DatumGetPointer(entryvec->vector[i].key);
487 ro = gbt_var_key_readable((GBT_VARKEY *) cur);
488 if (ro.lower == ro.upper) /* leaf */
490 sv[svcntr] = gbt_var_leaf2node((GBT_VARKEY *) cur, tinfo, flinfo);
491 arr[i].t = sv[svcntr];
492 if (sv[svcntr] != (GBT_VARKEY *) cur)
493 svcntr++;
495 else
496 arr[i].t = (GBT_VARKEY *) cur;
497 arr[i].i = i;
500 /* sort */
501 varg.tinfo = tinfo;
502 varg.collation = collation;
503 varg.flinfo = flinfo;
504 qsort_arg(&arr[FirstOffsetNumber],
505 maxoff - FirstOffsetNumber + 1,
506 sizeof(Vsrt),
507 gbt_vsrt_cmp,
508 &varg);
510 /* We do simply create two parts */
512 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
514 if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
516 gbt_var_bin_union(&v->spl_ldatum, arr[i].t, collation, tinfo, flinfo);
517 v->spl_left[v->spl_nleft] = arr[i].i;
518 v->spl_nleft++;
520 else
522 gbt_var_bin_union(&v->spl_rdatum, arr[i].t, collation, tinfo, flinfo);
523 v->spl_right[v->spl_nright] = arr[i].i;
524 v->spl_nright++;
528 /* Truncate (=compress) key */
529 if (tinfo->trnc)
531 int32 ll = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), tinfo);
532 int32 lr = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), tinfo);
533 GBT_VARKEY *dl;
534 GBT_VARKEY *dr;
536 ll = Max(ll, lr);
537 ll++;
539 dl = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), ll, tinfo);
540 dr = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), ll, tinfo);
541 v->spl_ldatum = PointerGetDatum(dl);
542 v->spl_rdatum = PointerGetDatum(dr);
545 return v;
550 * The GiST consistent method
552 bool
553 gbt_var_consistent(GBT_VARKEY_R *key,
554 const void *query,
555 StrategyNumber strategy,
556 Oid collation,
557 bool is_leaf,
558 const gbtree_vinfo *tinfo,
559 FmgrInfo *flinfo)
561 bool retval = false;
563 switch (strategy)
565 case BTLessEqualStrategyNumber:
566 if (is_leaf)
567 retval = tinfo->f_ge(query, key->lower, collation, flinfo);
568 else
569 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
570 || gbt_var_node_pf_match(key, query, tinfo);
571 break;
572 case BTLessStrategyNumber:
573 if (is_leaf)
574 retval = tinfo->f_gt(query, key->lower, collation, flinfo);
575 else
576 retval = tinfo->f_cmp(query, key->lower, collation, flinfo) >= 0
577 || gbt_var_node_pf_match(key, query, tinfo);
578 break;
579 case BTEqualStrategyNumber:
580 if (is_leaf)
581 retval = tinfo->f_eq(query, key->lower, collation, flinfo);
582 else
583 retval =
584 (tinfo->f_cmp(key->lower, query, collation, flinfo) <= 0 &&
585 tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0) ||
586 gbt_var_node_pf_match(key, query, tinfo);
587 break;
588 case BTGreaterStrategyNumber:
589 if (is_leaf)
590 retval = tinfo->f_lt(query, key->upper, collation, flinfo);
591 else
592 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
593 || gbt_var_node_pf_match(key, query, tinfo);
594 break;
595 case BTGreaterEqualStrategyNumber:
596 if (is_leaf)
597 retval = tinfo->f_le(query, key->upper, collation, flinfo);
598 else
599 retval = tinfo->f_cmp(query, key->upper, collation, flinfo) <= 0
600 || gbt_var_node_pf_match(key, query, tinfo);
601 break;
602 case BtreeGistNotEqualStrategyNumber:
603 retval = !(tinfo->f_eq(query, key->lower, collation, flinfo) &&
604 tinfo->f_eq(query, key->upper, collation, flinfo));
605 break;
606 default:
607 retval = false;
610 return retval;