static int gbt_vsrt_cmp(const void *a, const void *b, void *arg) { GBT_VARKEY_R ar = gbt_var_key_readable(((const Vsrt *) a)->t); GBT_VARKEY_R br = gbt_var_key_readable(((const Vsrt *) b)->t); const gbt_vsrt_arg *varg = (const gbt_vsrt_arg *) arg; int res; res = (*varg->tinfo->f_cmp) (ar.lower, br.lower, varg->collation); if (res == 0) return (*varg->tinfo->f_cmp) (ar.upper, br.upper, varg->collation); return res; }
bool gbt_var_same(Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo) { GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1); GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2); GBT_VARKEY_R r1, r2; r1 = gbt_var_key_readable(t1); r2 = gbt_var_key_readable(t2); return ((*tinfo->f_cmp) (r1.lower, r2.lower, collation) == 0 && (*tinfo->f_cmp) (r1.upper, r2.upper, collation) == 0); }
Datum gbt_bpchar_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); void *query = (void *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1))); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); /* Oid subtype = PG_GETARG_OID(3); */ bool *recheck = (bool *) PG_GETARG_POINTER(4); bool retval; GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key); GBT_VARKEY_R r = gbt_var_key_readable(key); void *trim = (void *) DatumGetPointer(DirectFunctionCall1(rtrim1, PointerGetDatum(query))); /* All cases served by this function are exact */ *recheck = false; if (tinfo.eml == 0) { tinfo.eml = pg_database_encoding_max_length(); } retval = gbt_var_consistent(&r, trim, &strategy, GIST_LEAF(entry), &tinfo); PG_RETURN_BOOL(retval); }
Datum gbt_bit_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); void *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1)); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); /* Oid subtype = PG_GETARG_OID(3); */ bool *recheck = (bool *) PG_GETARG_POINTER(4); bool retval; GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key); GBT_VARKEY_R r = gbt_var_key_readable(key); /* All cases served by this function are exact */ *recheck = false; if (GIST_LEAF(entry)) retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(), TRUE, &tinfo); else { bytea *q = gbt_bit_xfrm((bytea *) query); retval = gbt_var_consistent(&r, q, strategy, PG_GET_COLLATION(), FALSE, &tinfo); } PG_RETURN_BOOL(retval); }
/* * truncates / compresses the node key * cpf_length .. common prefix length */ static GBT_VARKEY * gbt_var_node_truncate(const GBT_VARKEY *node, int32 cpf_length, const gbtree_vinfo *tinfo) { GBT_VARKEY *out = NULL; GBT_VARKEY_R r = gbt_var_key_readable(node); int32 len1 = VARSIZE(r.lower) - VARHDRSZ; int32 len2 = VARSIZE(r.upper) - VARHDRSZ; int32 si; char *out2; len1 = Min(len1, (cpf_length + 1)); len2 = Min(len2, (cpf_length + 1)); si = 2 * VARHDRSZ + INTALIGN(len1 + VARHDRSZ) + len2; out = (GBT_VARKEY *) palloc(si); SET_VARSIZE(out, si); memcpy(VARDATA(out), r.lower, len1 + VARHDRSZ); SET_VARSIZE(VARDATA(out), len1 + VARHDRSZ); out2 = VARDATA(out) + INTALIGN(len1 + VARHDRSZ); memcpy(out2, r.upper, len2 + VARHDRSZ); SET_VARSIZE(out2, len2 + VARHDRSZ); return out; }
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo) { GBT_VARKEY_R eo = gbt_var_key_readable(e); GBT_VARKEY_R nr; if (eo.lower == eo.upper) /* leaf */ { GBT_VARKEY *tmp; tmp = gbt_var_leaf2node(e, tinfo); if (tmp != e) eo = gbt_var_key_readable(tmp); } if (DatumGetPointer(*u)) { GBT_VARKEY_R ro = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(*u)); bool update = false; nr.lower = ro.lower; nr.upper = ro.upper; if ((*tinfo->f_cmp) (ro.lower, eo.lower, collation) > 0) { nr.lower = eo.lower; update = true; } if ((*tinfo->f_cmp) (ro.upper, eo.upper, collation) < 0) { nr.upper = eo.upper; update = true; } if (update) *u = PointerGetDatum(gbt_var_key_copy(&nr)); } else { nr.lower = eo.lower; nr.upper = eo.upper; *u = PointerGetDatum(gbt_var_key_copy(&nr)); } }
void gbt_var_bin_union(Datum *u, GBT_VARKEY *e, Oid collation, const gbtree_vinfo *tinfo) { GBT_VARKEY *nk = NULL; GBT_VARKEY *tmp = NULL; GBT_VARKEY_R nr; GBT_VARKEY_R eo = gbt_var_key_readable(e); if (eo.lower == eo.upper) /* leaf */ { tmp = gbt_var_leaf2node(e, tinfo); if (tmp != e) eo = gbt_var_key_readable(tmp); } if (DatumGetPointer(*u)) { GBT_VARKEY_R ro = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(*u)); if ((*tinfo->f_cmp) (ro.lower, eo.lower, collation) > 0) { nr.lower = eo.lower; nr.upper = ro.upper; nk = gbt_var_key_copy(&nr, TRUE); } if ((*tinfo->f_cmp) (ro.upper, eo.upper, collation) < 0) { nr.upper = eo.upper; nr.lower = ro.lower; nk = gbt_var_key_copy(&nr, TRUE); } if (nk) *u = PointerGetDatum(nk); } else { nr.lower = eo.lower; nr.upper = eo.upper; *u = PointerGetDatum(gbt_var_key_copy(&nr, TRUE)); } }
bool gbt_var_same(Datum d1, Datum d2, Oid collation, const gbtree_vinfo *tinfo) { bool result; GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1); GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2); GBT_VARKEY_R r1, r2; r1 = gbt_var_key_readable(t1); r2 = gbt_var_key_readable(t2); if (t1 && t2) result = ((*tinfo->f_cmp) (r1.lower, r2.lower, collation) == 0 && (*tinfo->f_cmp) (r1.upper, r2.upper, collation) == 0); else result = (t1 == NULL && t2 == NULL); return result; }
bool gbt_var_same(bool *result, const Datum d1, const Datum d2, const gbtree_vinfo *tinfo) { GBT_VARKEY *t1 = (GBT_VARKEY *) DatumGetPointer(d1); GBT_VARKEY *t2 = (GBT_VARKEY *) DatumGetPointer(d2); GBT_VARKEY_R r1, r2; r1 = gbt_var_key_readable(t1); r2 = gbt_var_key_readable(t2); if (t1 && t2) { *result = (((*tinfo->f_cmp) ((bytea *) r1.lower, (bytea *) r2.lower) == 0 && (*tinfo->f_cmp) ((bytea *) r1.upper, (bytea *) r2.upper) == 0) ? TRUE : FALSE); } else *result = (t1 == NULL && t2 == NULL) ? TRUE : FALSE; PG_RETURN_POINTER(result); }
Datum gbt_var_fetch(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key)); GBT_VARKEY_R r = gbt_var_key_readable(key); GISTENTRY *retval; retval = palloc(sizeof(GISTENTRY)); gistentryinit(*retval, PointerGetDatum(r.lower), entry->rel, entry->page, entry->offset, TRUE); PG_RETURN_POINTER(retval); }
static GBT_VARKEY * gbt_bit_l2n(GBT_VARKEY *leaf) { GBT_VARKEY *out = leaf; GBT_VARKEY_R r = gbt_var_key_readable(leaf); bytea *o; o = gbt_bit_xfrm(r.lower); r.upper = r.lower = o; out = gbt_var_key_copy(&r); pfree(o); return out; }
/* * returns the common prefix length of a node key */ static int32 gbt_var_node_cp_len(const GBT_VARKEY *node, const gbtree_vinfo *tinfo) { GBT_VARKEY_R r = gbt_var_key_readable(node); int32 i = 0; int32 l = 0; int32 t1len = VARSIZE(r.lower) - VARHDRSZ; int32 t2len = VARSIZE(r.upper) - VARHDRSZ; int32 ml = Min(t1len, t2len); char *p1 = VARDATA(r.lower); char *p2 = VARDATA(r.upper); if (ml == 0) return 0; while (i < ml) { if (tinfo->eml > 1 && l == 0) { if ((l = pg_mblen(p1)) != pg_mblen(p2)) { return i; } } if (*p1 != *p2) { if (tinfo->eml > 1) { return (i - l + 1); } else { return i; } } p1++; p2++; l--; i++; } return (ml); /* lower == upper */ }
Datum gbt_text_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key); void *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1)); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); bool retval = FALSE; GBT_VARKEY_R r = gbt_var_key_readable(key); if (tinfo.eml == 0) { tinfo.eml = pg_database_encoding_max_length(); } retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo); PG_RETURN_BOOL(retval); }
Datum gbt_numeric_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); void *query = (void *) DatumGetNumeric(PG_GETARG_DATUM(1)); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); /* Oid subtype = PG_GETARG_OID(3); */ bool *recheck = (bool *) PG_GETARG_POINTER(4); bool retval; GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key); GBT_VARKEY_R r = gbt_var_key_readable(key); /* All cases served by this function are exact */ *recheck = false; retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo); PG_RETURN_BOOL(retval); }
Datum gbt_bytea_consistent(PG_FUNCTION_ARGS) { GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0); GBT_VARKEY *ktst = (GBT_VARKEY *) DatumGetPointer(entry->key); GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key)); void *qtst = (void *) DatumGetPointer(PG_GETARG_DATUM(1)); void *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1)); StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); bool retval = FALSE; GBT_VARKEY_R r = gbt_var_key_readable(key); retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo); if (ktst != key) pfree(key); if (qtst != query) pfree(query); PG_RETURN_BOOL(retval); }
GBT_VARKEY * gbt_var_union(const GistEntryVector *entryvec, int32 *size, Oid collation, const gbtree_vinfo *tinfo) { int i = 0, numranges = entryvec->n; GBT_VARKEY *cur; Datum out; GBT_VARKEY_R rk; *size = sizeof(GBT_VARKEY); cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[0].key); rk = gbt_var_key_readable(cur); out = PointerGetDatum(gbt_var_key_copy(&rk, TRUE)); for (i = 1; i < numranges; i++) { cur = (GBT_VARKEY *) DatumGetPointer(entryvec->vector[i].key); gbt_var_bin_union(&out, cur, collation, tinfo); } /* Truncate (=compress) key */ if (tinfo->trnc) { int32 plen; GBT_VARKEY *trc = NULL; plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo); trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo); out = PointerGetDatum(trc); } return ((GBT_VARKEY *) DatumGetPointer(out)); }
GIST_SPLITVEC * gbt_var_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v, Oid collation, const gbtree_vinfo *tinfo) { OffsetNumber i, maxoff = entryvec->n - 1; Vsrt *arr; int svcntr = 0, nbytes; char *cur; GBT_VARKEY **sv = NULL; gbt_vsrt_arg varg; arr = (Vsrt *) palloc((maxoff + 1) * sizeof(Vsrt)); nbytes = (maxoff + 2) * sizeof(OffsetNumber); v->spl_left = (OffsetNumber *) palloc(nbytes); v->spl_right = (OffsetNumber *) palloc(nbytes); v->spl_ldatum = PointerGetDatum(0); v->spl_rdatum = PointerGetDatum(0); v->spl_nleft = 0; v->spl_nright = 0; sv = palloc(sizeof(bytea *) * (maxoff + 1)); /* Sort entries */ for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) { GBT_VARKEY_R ro; cur = (char *) DatumGetPointer(entryvec->vector[i].key); ro = gbt_var_key_readable((GBT_VARKEY *) cur); if (ro.lower == ro.upper) /* leaf */ { sv[svcntr] = gbt_var_leaf2node((GBT_VARKEY *) cur, tinfo); arr[i].t = sv[svcntr]; if (sv[svcntr] != (GBT_VARKEY *) cur) svcntr++; } else arr[i].t = (GBT_VARKEY *) cur; arr[i].i = i; } /* sort */ varg.tinfo = tinfo; varg.collation = collation; qsort_arg((void *) &arr[FirstOffsetNumber], maxoff - FirstOffsetNumber + 1, sizeof(Vsrt), gbt_vsrt_cmp, (void *) &varg); /* We do simply create two parts */ for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) { if (i <= (maxoff - FirstOffsetNumber + 1) / 2) { gbt_var_bin_union(&v->spl_ldatum, arr[i].t, collation, tinfo); v->spl_left[v->spl_nleft] = arr[i].i; v->spl_nleft++; } else { gbt_var_bin_union(&v->spl_rdatum, arr[i].t, collation, tinfo); v->spl_right[v->spl_nright] = arr[i].i; v->spl_nright++; } } /* Truncate (=compress) key */ if (tinfo->trnc) { int32 ll = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), tinfo); int32 lr = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), tinfo); GBT_VARKEY *dl; GBT_VARKEY *dr; ll = Max(ll, lr); ll++; dl = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), ll, tinfo); dr = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), ll, tinfo); v->spl_ldatum = PointerGetDatum(dl); v->spl_rdatum = PointerGetDatum(dr); } return v; }
float * gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n, Oid collation, const gbtree_vinfo *tinfo) { GBT_VARKEY *orge = (GBT_VARKEY *) DatumGetPointer(o->key); GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key); GBT_VARKEY_R ok, nk; GBT_VARKEY *tmp = NULL; *res = 0.0; nk = gbt_var_key_readable(newe); if (nk.lower == nk.upper) /* leaf */ { tmp = gbt_var_leaf2node(newe, tinfo); if (tmp != newe) nk = gbt_var_key_readable(tmp); } ok = gbt_var_key_readable(orge); if ((VARSIZE(ok.lower) - VARHDRSZ) == 0 && (VARSIZE(ok.upper) - VARHDRSZ) == 0) *res = 0.0; else if (!(((*tinfo->f_cmp) (nk.lower, ok.lower, collation) >= 0 || gbt_bytea_pf_match(ok.lower, nk.lower, tinfo)) && ((*tinfo->f_cmp) (nk.upper, ok.upper, collation) <= 0 || gbt_bytea_pf_match(ok.upper, nk.upper, tinfo)))) { Datum d = PointerGetDatum(0); double dres = 0.0; int32 ol, ul; gbt_var_bin_union(&d, orge, collation, tinfo); ol = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo); gbt_var_bin_union(&d, newe, collation, tinfo); ul = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(d), tinfo); if (ul < ol) { dres = (ol - ul); /* lost of common prefix len */ } else { GBT_VARKEY_R uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(d)); char tmp[4]; tmp[0] = ((VARSIZE(ok.lower) - VARHDRSZ) == ul) ? (CHAR_MIN) : (VARDATA(ok.lower)[ul]); tmp[1] = ((VARSIZE(uk.lower) - VARHDRSZ) == ul) ? (CHAR_MIN) : (VARDATA(uk.lower)[ul]); tmp[2] = ((VARSIZE(ok.upper) - VARHDRSZ) == ul) ? (CHAR_MIN) : (VARDATA(ok.upper)[ul]); tmp[3] = ((VARSIZE(uk.upper) - VARHDRSZ) == ul) ? (CHAR_MIN) : (VARDATA(uk.upper)[ul]); dres = (tmp[0] - tmp[1]) + (tmp[3] - tmp[2]); dres /= 256.0; } *res += FLT_MIN; *res += (float) (dres / ((double) (ol + 1))); *res *= (FLT_MAX / (o->rel->rd_att->natts + 1)); } return res; }
Datum gbt_numeric_penalty(PG_FUNCTION_ARGS) { GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0); GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1); float *result = (float *) PG_GETARG_POINTER(2); Numeric us, os, ds; GBT_VARKEY *org = (GBT_VARKEY *) DatumGetPointer(o->key); GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key); Datum uni; GBT_VARKEY_R rk, ok, uk; rk = gbt_var_key_readable(org); uni = PointerGetDatum(gbt_var_key_copy(&rk, TRUE)); gbt_var_bin_union(&uni, newe, &tinfo); ok = gbt_var_key_readable(org); uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(uni)); us = DatumGetNumeric(DirectFunctionCall2( numeric_sub, PointerGetDatum(uk.upper), PointerGetDatum(uk.lower) )); os = DatumGetNumeric(DirectFunctionCall2( numeric_sub, PointerGetDatum(ok.upper), PointerGetDatum(ok.lower) )); ds = DatumGetNumeric(DirectFunctionCall2( numeric_sub, NumericGetDatum(us), NumericGetDatum(os) )); if (numeric_is_nan(us)) { if (numeric_is_nan(os)) *result = 0.0; else *result = 1.0; } else { Numeric nul = DatumGetNumeric(DirectFunctionCall1(int4_numeric, Int32GetDatum(0))); *result = 0.0; if (DirectFunctionCall2(numeric_gt, NumericGetDatum(ds), NumericGetDatum(nul))) { *result += FLT_MIN; os = DatumGetNumeric(DirectFunctionCall2( numeric_div, NumericGetDatum(ds), NumericGetDatum(us) )); *result += (float4) DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow, NumericGetDatum(os))); } } if (*result > 0) *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1)); PG_RETURN_POINTER(result); }