aboutsummaryrefslogtreecommitdiff
path: root/contrib/btree_gist/btree_date.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2011-03-02 14:43:24 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2011-03-02 14:44:33 -0500
commit8436489c81c23af637696ac69cdaafddcc907ee1 (patch)
treef9aabf47bbe6dba2f132dc472f855b306c2fe30c /contrib/btree_gist/btree_date.c
parent6094c242d1ee40a08f3138811425d7540e8269e4 (diff)
downloadpostgresql-8436489c81c23af637696ac69cdaafddcc907ee1.tar.gz
postgresql-8436489c81c23af637696ac69cdaafddcc907ee1.zip
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_date.c')
-rw-r--r--contrib/btree_gist/btree_date.c49
1 files changed, 48 insertions, 1 deletions
diff --git a/contrib/btree_gist/btree_date.c b/contrib/btree_gist/btree_date.c
index 11904af2573..ccd7e2ad3f3 100644
--- a/contrib/btree_gist/btree_date.c
+++ b/contrib/btree_gist/btree_date.c
@@ -18,6 +18,7 @@ PG_FUNCTION_INFO_V1(gbt_date_compress);
PG_FUNCTION_INFO_V1(gbt_date_union);
PG_FUNCTION_INFO_V1(gbt_date_picksplit);
PG_FUNCTION_INFO_V1(gbt_date_consistent);
+PG_FUNCTION_INFO_V1(gbt_date_distance);
PG_FUNCTION_INFO_V1(gbt_date_penalty);
PG_FUNCTION_INFO_V1(gbt_date_same);
@@ -25,6 +26,7 @@ Datum gbt_date_compress(PG_FUNCTION_ARGS);
Datum gbt_date_union(PG_FUNCTION_ARGS);
Datum gbt_date_picksplit(PG_FUNCTION_ARGS);
Datum gbt_date_consistent(PG_FUNCTION_ARGS);
+Datum gbt_date_distance(PG_FUNCTION_ARGS);
Datum gbt_date_penalty(PG_FUNCTION_ARGS);
Datum gbt_date_same(PG_FUNCTION_ARGS);
@@ -84,6 +86,17 @@ gbt_datekey_cmp(const void *a, const void *b)
return res;
}
+static float8
+gdb_date_dist(const void *a, const void *b)
+{
+ /* we assume the difference can't overflow */
+ Datum diff = DirectFunctionCall2(date_mi,
+ DateADTGetDatum(*((const DateADT *) a)),
+ DateADTGetDatum(*((const DateADT *) b)));
+
+ return (float8) Abs(DatumGetInt32(diff));
+}
+
static const gbtree_ninfo tinfo =
{
@@ -94,10 +107,25 @@ static const gbtree_ninfo tinfo =
gbt_dateeq,
gbt_datele,
gbt_datelt,
- gbt_datekey_cmp
+ gbt_datekey_cmp,
+ gdb_date_dist
};
+PG_FUNCTION_INFO_V1(date_dist);
+Datum date_dist(PG_FUNCTION_ARGS);
+Datum
+date_dist(PG_FUNCTION_ARGS)
+{
+ /* we assume the difference can't overflow */
+ Datum diff = DirectFunctionCall2(date_mi,
+ PG_GETARG_DATUM(0),
+ PG_GETARG_DATUM(1));
+
+ PG_RETURN_INT32(Abs(DatumGetInt32(diff)));
+}
+
+
/**************************************************
* date ops
**************************************************/
@@ -140,6 +168,25 @@ gbt_date_consistent(PG_FUNCTION_ARGS)
Datum
+gbt_date_distance(PG_FUNCTION_ARGS)
+{
+ GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
+ DateADT query = PG_GETARG_DATEADT(1);
+
+ /* Oid subtype = PG_GETARG_OID(3); */
+ dateKEY *kkk = (dateKEY *) 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_date_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);