mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
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
This commit is contained in:
@ -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
|
||||
**************************************************/
|
||||
@ -139,6 +167,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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user