summaryrefslogtreecommitdiff
path: root/contrib/btree_gist/btree_int8.c
diff options
context:
space:
mode:
authorTom Lane2011-03-02 19:43:24 +0000
committerTom Lane2011-03-02 19:44:33 +0000
commit8436489c81c23af637696ac69cdaafddcc907ee1 (patch)
treef9aabf47bbe6dba2f132dc472f855b306c2fe30c /contrib/btree_gist/btree_int8.c
parent6094c242d1ee40a08f3138811425d7540e8269e4 (diff)
Add KNNGIST support to contrib/btree_gist.
This extends GiST's support for nearest-neighbor searches to many of the standard data types. Teodor Sigaev
Diffstat (limited to 'contrib/btree_gist/btree_int8.c')
-rw-r--r--contrib/btree_gist/btree_int8.c53
1 files changed, 52 insertions, 1 deletions
diff --git a/contrib/btree_gist/btree_int8.c b/contrib/btree_gist/btree_int8.c
index 8afa5b0af24..1f14d82891d 100644
--- a/contrib/btree_gist/btree_int8.c
+++ b/contrib/btree_gist/btree_int8.c
@@ -17,6 +17,7 @@ PG_FUNCTION_INFO_V1(gbt_int8_compress);
PG_FUNCTION_INFO_V1(gbt_int8_union);
PG_FUNCTION_INFO_V1(gbt_int8_picksplit);
PG_FUNCTION_INFO_V1(gbt_int8_consistent);
+PG_FUNCTION_INFO_V1(gbt_int8_distance);
PG_FUNCTION_INFO_V1(gbt_int8_penalty);
PG_FUNCTION_INFO_V1(gbt_int8_same);
@@ -24,6 +25,7 @@ Datum gbt_int8_compress(PG_FUNCTION_ARGS);
Datum gbt_int8_union(PG_FUNCTION_ARGS);
Datum gbt_int8_picksplit(PG_FUNCTION_ARGS);
Datum gbt_int8_consistent(PG_FUNCTION_ARGS);
+Datum gbt_int8_distance(PG_FUNCTION_ARGS);
Datum gbt_int8_penalty(PG_FUNCTION_ARGS);
Datum gbt_int8_same(PG_FUNCTION_ARGS);
@@ -71,6 +73,12 @@ gbt_int8key_cmp(const void *a, const void *b)
return (ia->lower > ib->lower) ? 1 : -1;
}
+static float8
+gbt_int8_dist(const void *a, const void *b)
+{
+ return GET_FLOAT_DISTANCE(int64, a, b);
+}
+
static const gbtree_ninfo tinfo =
{
@@ -81,10 +89,34 @@ static const gbtree_ninfo tinfo =
gbt_int8eq,
gbt_int8le,
gbt_int8lt,
- gbt_int8key_cmp
+ gbt_int8key_cmp,
+ gbt_int8_dist
};
+PG_FUNCTION_INFO_V1(int8_dist);
+Datum int8_dist(PG_FUNCTION_ARGS);
+Datum
+int8_dist(PG_FUNCTION_ARGS)
+{
+ int64 a = PG_GETARG_INT64(0);
+ int64 b = PG_GETARG_INT64(1);
+ int64 r;
+ int64 ra;
+
+ r = a - b;
+ ra = Abs(r);
+
+ /* Overflow check. */
+ if (ra < 0 || (!SAMESIGN(a, b) && !SAMESIGN(r, a)))
+ ereport(ERROR,
+ (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+ errmsg("bigint out of range")));
+
+ PG_RETURN_INT64(ra);
+}
+
+
/**************************************************
* int64 ops
**************************************************/
@@ -125,6 +157,25 @@ gbt_int8_consistent(PG_FUNCTION_ARGS)
Datum
+gbt_int8_distance(PG_FUNCTION_ARGS)
+{
+ GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
+ int64 query = PG_GETARG_INT64(1);
+
+ /* Oid subtype = PG_GETARG_OID(3); */
+ int64KEY *kkk = (int64KEY *) DatumGetPointer(entry->key);
+ GBT_NUMKEY_R key;
+
+ key.lower = (GBT_NUMKEY *) &kkk->lower;
+ key.upper = (GBT_NUMKEY *) &kkk->upper;
+
+ PG_RETURN_FLOAT8(
+ gbt_num_distance(&key, (void *) &query, GIST_LEAF(entry), &tinfo)
+ );
+}
+
+
+Datum
gbt_int8_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);