2008-05-17 09:28:26 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/btree_gist/btree_numeric.c
|
2008-05-17 09:28:26 +08:00
|
|
|
*/
|
2011-08-27 09:16:24 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2006-07-14 13:28:29 +08:00
|
|
|
#include "btree_gist.h"
|
|
|
|
|
2006-07-14 00:57:31 +08:00
|
|
|
#include <math.h>
|
2006-07-14 12:44:46 +08:00
|
|
|
#include <float.h>
|
2006-07-14 00:57:31 +08:00
|
|
|
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
#include "btree_utils_var.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/numeric.h"
|
2008-06-19 08:46:06 +08:00
|
|
|
#include "utils/rel.h"
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Bytea ops
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(gbt_numeric_compress);
|
|
|
|
PG_FUNCTION_INFO_V1(gbt_numeric_union);
|
|
|
|
PG_FUNCTION_INFO_V1(gbt_numeric_picksplit);
|
|
|
|
PG_FUNCTION_INFO_V1(gbt_numeric_consistent);
|
|
|
|
PG_FUNCTION_INFO_V1(gbt_numeric_penalty);
|
|
|
|
PG_FUNCTION_INFO_V1(gbt_numeric_same);
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
Datum gbt_numeric_compress(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbt_numeric_union(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbt_numeric_picksplit(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbt_numeric_consistent(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbt_numeric_penalty(PG_FUNCTION_ARGS);
|
|
|
|
Datum gbt_numeric_same(PG_FUNCTION_ARGS);
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* define for comparison */
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
static bool
|
2011-04-23 08:19:58 +08:00
|
|
|
gbt_numeric_gt(const void *a, const void *b, Oid collation)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2011-04-23 08:19:58 +08:00
|
|
|
return DatumGetBool(DirectFunctionCall2(numeric_gt,
|
|
|
|
PointerGetDatum(a),
|
|
|
|
PointerGetDatum(b)));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
static bool
|
2011-04-23 08:19:58 +08:00
|
|
|
gbt_numeric_ge(const void *a, const void *b, Oid collation)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2011-04-23 08:19:58 +08:00
|
|
|
return DatumGetBool(DirectFunctionCall2(numeric_ge,
|
|
|
|
PointerGetDatum(a),
|
|
|
|
PointerGetDatum(b)));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
static bool
|
2011-04-23 08:19:58 +08:00
|
|
|
gbt_numeric_eq(const void *a, const void *b, Oid collation)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2011-04-23 08:19:58 +08:00
|
|
|
return DatumGetBool(DirectFunctionCall2(numeric_eq,
|
|
|
|
PointerGetDatum(a),
|
|
|
|
PointerGetDatum(b)));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
static bool
|
2011-04-23 08:19:58 +08:00
|
|
|
gbt_numeric_le(const void *a, const void *b, Oid collation)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2011-04-23 08:19:58 +08:00
|
|
|
return DatumGetBool(DirectFunctionCall2(numeric_le,
|
|
|
|
PointerGetDatum(a),
|
|
|
|
PointerGetDatum(b)));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
static bool
|
2011-04-23 08:19:58 +08:00
|
|
|
gbt_numeric_lt(const void *a, const void *b, Oid collation)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2011-04-23 08:19:58 +08:00
|
|
|
return DatumGetBool(DirectFunctionCall2(numeric_lt,
|
|
|
|
PointerGetDatum(a),
|
|
|
|
PointerGetDatum(b)));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
static int32
|
2011-04-23 08:19:58 +08:00
|
|
|
gbt_numeric_cmp(const void *a, const void *b, Oid collation)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2011-04-23 08:19:58 +08:00
|
|
|
return DatumGetInt32(DirectFunctionCall2(numeric_cmp,
|
|
|
|
PointerGetDatum(a),
|
|
|
|
PointerGetDatum(b)));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const gbtree_vinfo tinfo =
|
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
gbt_t_numeric,
|
2005-07-01 21:44:56 +08:00
|
|
|
0,
|
2004-08-29 13:07:03 +08:00
|
|
|
FALSE,
|
|
|
|
gbt_numeric_gt,
|
|
|
|
gbt_numeric_ge,
|
|
|
|
gbt_numeric_eq,
|
|
|
|
gbt_numeric_le,
|
|
|
|
gbt_numeric_lt,
|
|
|
|
gbt_numeric_cmp,
|
|
|
|
NULL
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************************
|
|
|
|
* Text ops
|
|
|
|
**************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2004-08-29 13:07:03 +08:00
|
|
|
gbt_numeric_compress(PG_FUNCTION_ARGS)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gbt_numeric_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
void *query = (void *) DatumGetNumeric(PG_GETARG_DATUM(1));
|
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2009-06-11 22:49:15 +08:00
|
|
|
|
2008-04-15 01:05:34 +08:00
|
|
|
/* Oid subtype = PG_GETARG_OID(3); */
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(4);
|
2005-05-21 20:08:06 +08:00
|
|
|
bool retval;
|
2008-04-15 01:05:34 +08:00
|
|
|
GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
|
2004-08-29 13:07:03 +08:00
|
|
|
GBT_VARKEY_R r = gbt_var_key_readable(key);
|
|
|
|
|
2008-04-15 01:05:34 +08:00
|
|
|
/* All cases served by this function are exact */
|
|
|
|
*recheck = false;
|
|
|
|
|
2011-04-23 08:19:58 +08:00
|
|
|
retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
|
|
|
|
GIST_LEAF(entry), &tinfo);
|
2004-08-29 13:07:03 +08:00
|
|
|
PG_RETURN_BOOL(retval);
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gbt_numeric_union(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
int32 *size = (int *) PG_GETARG_POINTER(1);
|
|
|
|
|
2011-04-23 08:19:58 +08:00
|
|
|
PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
|
|
|
|
&tinfo));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
|
|
|
Datum
|
|
|
|
gbt_numeric_same(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
Datum d1 = PG_GETARG_DATUM(0);
|
|
|
|
Datum d2 = PG_GETARG_DATUM(1);
|
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
|
|
|
|
2011-04-23 08:19:58 +08:00
|
|
|
*result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo);
|
|
|
|
PG_RETURN_POINTER(result);
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2004-08-29 13:07:03 +08:00
|
|
|
gbt_numeric_penalty(PG_FUNCTION_ARGS)
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
GISTENTRY *o = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *n = (GISTENTRY *) PG_GETARG_POINTER(1);
|
|
|
|
float *result = (float *) PG_GETARG_POINTER(2);
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
Numeric us,
|
|
|
|
os,
|
|
|
|
ds;
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
GBT_VARKEY *org = (GBT_VARKEY *) DatumGetPointer(o->key);
|
|
|
|
GBT_VARKEY *newe = (GBT_VARKEY *) DatumGetPointer(n->key);
|
|
|
|
Datum uni;
|
|
|
|
GBT_VARKEY_R rk,
|
|
|
|
ok,
|
|
|
|
uk;
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
rk = gbt_var_key_readable(org);
|
|
|
|
uni = PointerGetDatum(gbt_var_key_copy(&rk, TRUE));
|
2011-04-23 08:19:58 +08:00
|
|
|
gbt_var_bin_union(&uni, newe, PG_GET_COLLATION(), &tinfo);
|
2004-08-29 13:07:03 +08:00
|
|
|
ok = gbt_var_key_readable(org);
|
|
|
|
uk = gbt_var_key_readable((GBT_VARKEY *) DatumGetPointer(uni));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
us = DatumGetNumeric(DirectFunctionCall2(
|
|
|
|
numeric_sub,
|
|
|
|
PointerGetDatum(uk.upper),
|
|
|
|
PointerGetDatum(uk.lower)
|
|
|
|
));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
os = DatumGetNumeric(DirectFunctionCall2(
|
|
|
|
numeric_sub,
|
|
|
|
PointerGetDatum(ok.upper),
|
|
|
|
PointerGetDatum(ok.lower)
|
|
|
|
));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
ds = DatumGetNumeric(DirectFunctionCall2(
|
|
|
|
numeric_sub,
|
|
|
|
NumericGetDatum(us),
|
|
|
|
NumericGetDatum(os)
|
|
|
|
));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2010-07-30 12:30:23 +08:00
|
|
|
if (numeric_is_nan(us))
|
2004-08-29 13:07:03 +08:00
|
|
|
{
|
2010-07-30 12:30:23 +08:00
|
|
|
if (numeric_is_nan(os))
|
2004-08-29 13:07:03 +08:00
|
|
|
*result = 0.0;
|
|
|
|
else
|
|
|
|
*result = 1.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Numeric nul = DatumGetNumeric(DirectFunctionCall1(int4_numeric, Int32GetDatum(0)));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
*result = 0.0;
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (DirectFunctionCall2(numeric_gt, NumericGetDatum(ds), NumericGetDatum(nul)))
|
|
|
|
{
|
|
|
|
*result += FLT_MIN;
|
|
|
|
os = DatumGetNumeric(DirectFunctionCall2(
|
|
|
|
numeric_div,
|
|
|
|
NumericGetDatum(ds),
|
|
|
|
NumericGetDatum(us)
|
|
|
|
));
|
|
|
|
*result += (float4) DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow, NumericGetDatum(os)));
|
|
|
|
}
|
|
|
|
}
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (*result > 0)
|
|
|
|
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
PG_RETURN_POINTER(result);
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gbt_numeric_picksplit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
|
New version. Add support for int2, int8, float4, float8, timestamp with/without time zone, time with/without time zone, date, interval, oid, money and macaddr, char, varchar/text, bytea, numeric, bit, varbit, inet/cidr types for GiST
2004-05-28 18:43:32 +08:00
|
|
|
|
2011-04-23 08:19:58 +08:00
|
|
|
gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
|
|
|
|
&tinfo);
|
2004-08-29 13:07:03 +08:00
|
|
|
PG_RETURN_POINTER(v);
|
|
|
|
}
|