aboutsummaryrefslogtreecommitdiff
path: root/contrib/btree_gist/btree_cash.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/btree_gist/btree_cash.c')
-rw-r--r--contrib/btree_gist/btree_cash.c52
1 files changed, 51 insertions, 1 deletions
diff --git a/contrib/btree_gist/btree_cash.c b/contrib/btree_gist/btree_cash.c
index bfda21b058c..7938a70f17a 100644
--- a/contrib/btree_gist/btree_cash.c
+++ b/contrib/btree_gist/btree_cash.c
@@ -18,6 +18,7 @@ PG_FUNCTION_INFO_V1(gbt_cash_compress);
PG_FUNCTION_INFO_V1(gbt_cash_union);
PG_FUNCTION_INFO_V1(gbt_cash_picksplit);
PG_FUNCTION_INFO_V1(gbt_cash_consistent);
+PG_FUNCTION_INFO_V1(gbt_cash_distance);
PG_FUNCTION_INFO_V1(gbt_cash_penalty);
PG_FUNCTION_INFO_V1(gbt_cash_same);
@@ -25,6 +26,7 @@ Datum gbt_cash_compress(PG_FUNCTION_ARGS);
Datum gbt_cash_union(PG_FUNCTION_ARGS);
Datum gbt_cash_picksplit(PG_FUNCTION_ARGS);
Datum gbt_cash_consistent(PG_FUNCTION_ARGS);
+Datum gbt_cash_distance(PG_FUNCTION_ARGS);
Datum gbt_cash_penalty(PG_FUNCTION_ARGS);
Datum gbt_cash_same(PG_FUNCTION_ARGS);
@@ -71,6 +73,12 @@ gbt_cashkey_cmp(const void *a, const void *b)
return (ia->lower > ib->lower) ? 1 : -1;
}
+static float8
+gbt_cash_dist(const void *a, const void *b)
+{
+ return GET_FLOAT_DISTANCE(Cash, a, b);
+}
+
static const gbtree_ninfo tinfo =
{
@@ -81,10 +89,33 @@ static const gbtree_ninfo tinfo =
gbt_casheq,
gbt_cashle,
gbt_cashlt,
- gbt_cashkey_cmp
+ gbt_cashkey_cmp,
+ gbt_cash_dist
};
+PG_FUNCTION_INFO_V1(cash_dist);
+Datum cash_dist(PG_FUNCTION_ARGS);
+Datum
+cash_dist(PG_FUNCTION_ARGS)
+{
+ Cash a = PG_GETARG_CASH(0);
+ Cash b = PG_GETARG_CASH(1);
+ Cash r;
+ Cash 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("money out of range")));
+
+ PG_RETURN_CASH(ra);
+}
+
/**************************************************
* Cash ops
**************************************************/
@@ -125,6 +156,25 @@ gbt_cash_consistent(PG_FUNCTION_ARGS)
Datum
+gbt_cash_distance(PG_FUNCTION_ARGS)
+{
+ GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
+ Cash query = PG_GETARG_CASH(1);
+
+ /* Oid subtype = PG_GETARG_OID(3); */
+ cashKEY *kkk = (cashKEY *) 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_cash_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);