2008-05-17 09:28:26 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/btree_gist/btree_utils_num.c
|
2008-05-17 09:28:26 +08:00
|
|
|
*/
|
2011-08-27 09:16:24 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
#include "btree_gist.h"
|
|
|
|
#include "btree_utils_num.h"
|
2008-04-21 08:26:47 +08:00
|
|
|
#include "utils/cash.h"
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
#include "utils/date.h"
|
2011-09-10 01:23:41 +08:00
|
|
|
#include "utils/timestamp.h"
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2008-04-21 08:26:47 +08:00
|
|
|
|
2005-05-12 08:39:37 +08:00
|
|
|
GISTENTRY *
|
2009-06-11 22:49:15 +08:00
|
|
|
gbt_num_compress(GISTENTRY *retval, GISTENTRY *entry, const gbtree_ninfo *tinfo)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
if (entry->leafkey)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
int16 i2;
|
|
|
|
int32 i4;
|
2008-04-21 08:26:47 +08:00
|
|
|
int64 i8;
|
2008-04-19 02:43:09 +08:00
|
|
|
float4 f4;
|
2008-04-21 08:26:47 +08:00
|
|
|
float8 f8;
|
2004-08-29 13:07:03 +08:00
|
|
|
DateADT dt;
|
2008-04-21 08:26:47 +08:00
|
|
|
TimeADT tm;
|
|
|
|
Timestamp ts;
|
|
|
|
Cash ch;
|
2004-08-29 13:07:03 +08:00
|
|
|
} v;
|
|
|
|
|
2008-04-19 02:43:09 +08:00
|
|
|
GBT_NUMKEY *r = (GBT_NUMKEY *) palloc0(2 * tinfo->size);
|
2004-08-29 13:07:03 +08:00
|
|
|
void *leaf = NULL;
|
|
|
|
|
|
|
|
switch (tinfo->t)
|
|
|
|
{
|
|
|
|
case gbt_t_int2:
|
|
|
|
v.i2 = DatumGetInt16(entry->key);
|
|
|
|
leaf = &v.i2;
|
|
|
|
break;
|
|
|
|
case gbt_t_int4:
|
|
|
|
v.i4 = DatumGetInt32(entry->key);
|
|
|
|
leaf = &v.i4;
|
|
|
|
break;
|
2008-04-21 08:26:47 +08:00
|
|
|
case gbt_t_int8:
|
|
|
|
v.i8 = DatumGetInt64(entry->key);
|
|
|
|
leaf = &v.i8;
|
|
|
|
break;
|
2004-08-29 13:07:03 +08:00
|
|
|
case gbt_t_oid:
|
|
|
|
v.i4 = DatumGetObjectId(entry->key);
|
|
|
|
leaf = &v.i4;
|
|
|
|
break;
|
2008-04-21 08:26:47 +08:00
|
|
|
case gbt_t_float4:
|
|
|
|
v.f4 = DatumGetFloat4(entry->key);
|
|
|
|
leaf = &v.f4;
|
|
|
|
break;
|
|
|
|
case gbt_t_float8:
|
|
|
|
v.f8 = DatumGetFloat8(entry->key);
|
|
|
|
leaf = &v.f8;
|
|
|
|
break;
|
2004-08-29 13:07:03 +08:00
|
|
|
case gbt_t_date:
|
|
|
|
v.dt = DatumGetDateADT(entry->key);
|
|
|
|
leaf = &v.dt;
|
|
|
|
break;
|
2008-04-21 08:26:47 +08:00
|
|
|
case gbt_t_time:
|
|
|
|
v.tm = DatumGetTimeADT(entry->key);
|
|
|
|
leaf = &v.tm;
|
|
|
|
break;
|
|
|
|
case gbt_t_ts:
|
|
|
|
v.ts = DatumGetTimestamp(entry->key);
|
|
|
|
leaf = &v.ts;
|
|
|
|
break;
|
|
|
|
case gbt_t_cash:
|
|
|
|
v.ch = DatumGetCash(entry->key);
|
|
|
|
leaf = &v.ch;
|
2008-04-19 02:43:09 +08:00
|
|
|
break;
|
2004-08-29 13:07:03 +08:00
|
|
|
default:
|
|
|
|
leaf = DatumGetPointer(entry->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy((void *) &r[0], leaf, tinfo->size);
|
|
|
|
memcpy((void *) &r[tinfo->size], leaf, tinfo->size);
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page,
|
2006-06-28 20:00:14 +08:00
|
|
|
entry->offset, FALSE);
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
retval = entry;
|
|
|
|
|
|
|
|
return retval;
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST union method for numerical values
|
|
|
|
*/
|
|
|
|
|
2005-05-12 08:39:37 +08:00
|
|
|
void *
|
2009-06-11 22:49:15 +08:00
|
|
|
gbt_num_union(GBT_NUMKEY *out, const GistEntryVector *entryvec, const gbtree_ninfo *tinfo)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
int i,
|
|
|
|
numranges;
|
|
|
|
GBT_NUMKEY *cur;
|
|
|
|
GBT_NUMKEY_R o,
|
|
|
|
c;
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
numranges = entryvec->n;
|
|
|
|
cur = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[0].key));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
o.lower = &((GBT_NUMKEY *) out)[0];
|
|
|
|
o.upper = &((GBT_NUMKEY *) out)[tinfo->size];
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
memcpy((void *) out, (void *) cur, 2 * tinfo->size);
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
for (i = 1; i < numranges; i++)
|
|
|
|
{
|
|
|
|
cur = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[i].key));
|
|
|
|
c.lower = &cur[0];
|
|
|
|
c.upper = &cur[tinfo->size];
|
|
|
|
if ((*tinfo->f_gt) (o.lower, c.lower)) /* out->lower > cur->lower */
|
|
|
|
memcpy((void *) o.lower, (void *) c.lower, tinfo->size);
|
|
|
|
if ((*tinfo->f_lt) (o.upper, c.upper)) /* out->upper < cur->upper */
|
|
|
|
memcpy((void *) o.upper, (void *) c.upper, tinfo->size);
|
|
|
|
}
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
return out;
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST same method for numerical values
|
|
|
|
*/
|
|
|
|
|
2005-05-12 08:39:37 +08:00
|
|
|
bool
|
2009-06-11 22:49:15 +08:00
|
|
|
gbt_num_same(const GBT_NUMKEY *a, const GBT_NUMKEY *b, const gbtree_ninfo *tinfo)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
GBT_NUMKEY_R b1,
|
|
|
|
b2;
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
b1.lower = &(((GBT_NUMKEY *) a)[0]);
|
|
|
|
b1.upper = &(((GBT_NUMKEY *) a)[tinfo->size]);
|
|
|
|
b2.lower = &(((GBT_NUMKEY *) b)[0]);
|
|
|
|
b2.upper = &(((GBT_NUMKEY *) b)[tinfo->size]);
|
|
|
|
|
|
|
|
if (
|
|
|
|
(*tinfo->f_eq) (b1.lower, b2.lower) &&
|
|
|
|
(*tinfo->f_eq) (b1.upper, b2.upper)
|
|
|
|
)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-12 08:39:37 +08:00
|
|
|
void
|
2009-06-11 22:49:15 +08:00
|
|
|
gbt_num_bin_union(Datum *u, GBT_NUMKEY *e, const gbtree_ninfo *tinfo)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
GBT_NUMKEY_R rd;
|
|
|
|
|
|
|
|
rd.lower = &e[0];
|
|
|
|
rd.upper = &e[tinfo->size];
|
|
|
|
|
|
|
|
if (!DatumGetPointer(*u))
|
|
|
|
{
|
|
|
|
*u = PointerGetDatum(palloc(2 * tinfo->size));
|
|
|
|
memcpy((void *) &(((GBT_NUMKEY *) DatumGetPointer(*u))[0]), (void *) rd.lower, tinfo->size);
|
|
|
|
memcpy((void *) &(((GBT_NUMKEY *) DatumGetPointer(*u))[tinfo->size]), (void *) rd.upper, tinfo->size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GBT_NUMKEY_R ur;
|
|
|
|
|
|
|
|
ur.lower = &(((GBT_NUMKEY *) DatumGetPointer(*u))[0]);
|
|
|
|
ur.upper = &(((GBT_NUMKEY *) DatumGetPointer(*u))[tinfo->size]);
|
|
|
|
if ((*tinfo->f_gt) ((void *) ur.lower, (void *) rd.lower))
|
|
|
|
memcpy((void *) ur.lower, (void *) rd.lower, tinfo->size);
|
|
|
|
if ((*tinfo->f_lt) ((void *) ur.upper, (void *) rd.upper))
|
|
|
|
memcpy((void *) ur.upper, (void *) rd.upper, tinfo->size);
|
|
|
|
}
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-04-23 08:19:58 +08:00
|
|
|
* The GiST consistent method
|
|
|
|
*
|
|
|
|
* Note: we currently assume that no datatypes that use this routine are
|
|
|
|
* collation-aware; so we don't bother passing collation through.
|
|
|
|
*/
|
2005-05-12 08:39:37 +08:00
|
|
|
bool
|
2011-03-03 03:43:24 +08:00
|
|
|
gbt_num_consistent(const GBT_NUMKEY_R *key,
|
2004-08-29 13:07:03 +08:00
|
|
|
const void *query,
|
|
|
|
const StrategyNumber *strategy,
|
|
|
|
bool is_leaf,
|
2011-03-03 03:43:24 +08:00
|
|
|
const gbtree_ninfo *tinfo)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2011-03-03 03:43:24 +08:00
|
|
|
bool retval;
|
2004-08-29 13:07:03 +08:00
|
|
|
|
|
|
|
switch (*strategy)
|
|
|
|
{
|
|
|
|
case BTLessEqualStrategyNumber:
|
|
|
|
retval = (*tinfo->f_ge) (query, key->lower);
|
|
|
|
break;
|
|
|
|
case BTLessStrategyNumber:
|
|
|
|
if (is_leaf)
|
|
|
|
retval = (*tinfo->f_gt) (query, key->lower);
|
|
|
|
else
|
|
|
|
retval = (*tinfo->f_ge) (query, key->lower);
|
|
|
|
break;
|
|
|
|
case BTEqualStrategyNumber:
|
|
|
|
if (is_leaf)
|
|
|
|
retval = (*tinfo->f_eq) (query, key->lower);
|
|
|
|
else
|
2011-03-03 03:43:24 +08:00
|
|
|
retval = ((*tinfo->f_le) (key->lower, query) && (*tinfo->f_le) (query, key->upper)) ? true : false;
|
2004-08-29 13:07:03 +08:00
|
|
|
break;
|
|
|
|
case BTGreaterStrategyNumber:
|
|
|
|
if (is_leaf)
|
|
|
|
retval = (*tinfo->f_lt) (query, key->upper);
|
|
|
|
else
|
|
|
|
retval = (*tinfo->f_le) (query, key->upper);
|
|
|
|
break;
|
|
|
|
case BTGreaterEqualStrategyNumber:
|
|
|
|
retval = (*tinfo->f_le) (query, key->upper);
|
|
|
|
break;
|
2010-08-03 00:26:48 +08:00
|
|
|
case BtreeGistNotEqualStrategyNumber:
|
2011-04-10 23:42:00 +08:00
|
|
|
retval = (!((*tinfo->f_eq) (query, key->lower) &&
|
|
|
|
(*tinfo->f_eq) (query, key->upper))) ? true : false;
|
2010-08-03 00:26:48 +08:00
|
|
|
break;
|
2004-08-29 13:07:03 +08:00
|
|
|
default:
|
2011-03-03 03:43:24 +08:00
|
|
|
retval = false;
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (retval);
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-03 03:43:24 +08:00
|
|
|
/*
|
|
|
|
** The GiST distance method (for KNN-Gist)
|
|
|
|
*/
|
|
|
|
|
|
|
|
float8
|
|
|
|
gbt_num_distance(const GBT_NUMKEY_R *key,
|
|
|
|
const void *query,
|
|
|
|
bool is_leaf,
|
|
|
|
const gbtree_ninfo *tinfo)
|
|
|
|
{
|
|
|
|
float8 retval;
|
|
|
|
|
|
|
|
if (tinfo->f_dist == NULL)
|
|
|
|
elog(ERROR, "KNN search is not supported for btree_gist type %d",
|
|
|
|
(int) tinfo->t);
|
2011-04-10 23:42:00 +08:00
|
|
|
if (tinfo->f_le(query, key->lower))
|
2011-03-03 03:43:24 +08:00
|
|
|
retval = tinfo->f_dist(query, key->lower);
|
2011-04-10 23:42:00 +08:00
|
|
|
else if (tinfo->f_ge(query, key->upper))
|
2011-03-03 03:43:24 +08:00
|
|
|
retval = tinfo->f_dist(query, key->upper);
|
|
|
|
else
|
|
|
|
retval = 0.0;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
GIST_SPLITVEC *
|
2004-08-29 13:07:03 +08:00
|
|
|
gbt_num_picksplit(const GistEntryVector *entryvec, GIST_SPLITVEC *v,
|
2009-06-11 22:49:15 +08:00
|
|
|
const gbtree_ninfo *tinfo)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
OffsetNumber i,
|
|
|
|
maxoff = entryvec->n - 1;
|
|
|
|
Nsrt *arr;
|
|
|
|
int nbytes;
|
|
|
|
|
|
|
|
arr = (Nsrt *) palloc((maxoff + 1) * sizeof(Nsrt));
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Sort entries */
|
|
|
|
|
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
arr[i].t = (GBT_NUMKEY *) DatumGetPointer((entryvec->vector[i].key));
|
|
|
|
arr[i].i = i;
|
|
|
|
}
|
|
|
|
qsort((void *) &arr[FirstOffsetNumber], maxoff - FirstOffsetNumber + 1, sizeof(Nsrt), tinfo->f_cmp);
|
|
|
|
|
|
|
|
/* We do simply create two parts */
|
|
|
|
|
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
|
|
|
|
{
|
|
|
|
gbt_num_bin_union(&v->spl_ldatum, arr[i].t, tinfo);
|
|
|
|
v->spl_left[v->spl_nleft] = arr[i].i;
|
|
|
|
v->spl_nleft++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gbt_num_bin_union(&v->spl_rdatum, arr[i].t, tinfo);
|
|
|
|
v->spl_right[v->spl_nright] = arr[i].i;
|
|
|
|
v->spl_nright++;
|
|
|
|
}
|
|
|
|
}
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
return v;
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|