1
0
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:
Tom Lane
2011-03-02 14:43:24 -05:00
parent 6094c242d1
commit 8436489c81
40 changed files with 1546 additions and 59 deletions

View File

@ -20,6 +20,7 @@ PG_FUNCTION_INFO_V1(gbt_timetz_compress);
PG_FUNCTION_INFO_V1(gbt_time_union);
PG_FUNCTION_INFO_V1(gbt_time_picksplit);
PG_FUNCTION_INFO_V1(gbt_time_consistent);
PG_FUNCTION_INFO_V1(gbt_time_distance);
PG_FUNCTION_INFO_V1(gbt_timetz_consistent);
PG_FUNCTION_INFO_V1(gbt_time_penalty);
PG_FUNCTION_INFO_V1(gbt_time_same);
@ -29,6 +30,7 @@ Datum gbt_timetz_compress(PG_FUNCTION_ARGS);
Datum gbt_time_union(PG_FUNCTION_ARGS);
Datum gbt_time_picksplit(PG_FUNCTION_ARGS);
Datum gbt_time_consistent(PG_FUNCTION_ARGS);
Datum gbt_time_distance(PG_FUNCTION_ARGS);
Datum gbt_timetz_consistent(PG_FUNCTION_ARGS);
Datum gbt_time_penalty(PG_FUNCTION_ARGS);
Datum gbt_time_same(PG_FUNCTION_ARGS);
@ -112,6 +114,19 @@ gbt_timekey_cmp(const void *a, const void *b)
return res;
}
static float8
gbt_time_dist(const void *a, const void *b)
{
const TimeADT *aa = (const TimeADT *) a;
const TimeADT *bb = (const TimeADT *) b;
Interval *i;
i = DatumGetIntervalP(DirectFunctionCall2(time_mi_time,
TimeADTGetDatumFast(*aa),
TimeADTGetDatumFast(*bb)));
return (float8) Abs(INTERVAL_TO_SEC(i));
}
static const gbtree_ninfo tinfo =
{
@ -122,10 +137,24 @@ static const gbtree_ninfo tinfo =
gbt_timeeq,
gbt_timele,
gbt_timelt,
gbt_timekey_cmp
gbt_timekey_cmp,
gbt_time_dist
};
PG_FUNCTION_INFO_V1(time_dist);
Datum time_dist(PG_FUNCTION_ARGS);
Datum
time_dist(PG_FUNCTION_ARGS)
{
Datum diff = DirectFunctionCall2(time_mi_time,
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(1));
PG_RETURN_INTERVAL_P(abs_interval(DatumGetIntervalP(diff)));
}
/**************************************************
* time ops
**************************************************/
@ -196,6 +225,24 @@ gbt_time_consistent(PG_FUNCTION_ARGS)
);
}
Datum
gbt_time_distance(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
TimeADT query = PG_GETARG_TIMEADT(1);
/* Oid subtype = PG_GETARG_OID(3); */
timeKEY *kkk = (timeKEY *) 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_timetz_consistent(PG_FUNCTION_ARGS)
{