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 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
|
|
|
|
2019-10-23 11:56:22 +08:00
|
|
|
#include "btree_gist.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
|
|
|
#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);
|
|
|
|
|
|
|
|
|
|
|
|
/* define for comparison */
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
static bool
|
2017-03-21 21:53:35 +08:00
|
|
|
gbt_numeric_gt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
|
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
|
2017-03-21 21:53:35 +08:00
|
|
|
gbt_numeric_ge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
|
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
|
2017-03-21 21:53:35 +08:00
|
|
|
gbt_numeric_eq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
|
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
|
2017-03-21 21:53:35 +08:00
|
|
|
gbt_numeric_le(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
|
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
|
2017-03-21 21:53:35 +08:00
|
|
|
gbt_numeric_lt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
|
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
|
2017-03-21 21:53:35 +08:00
|
|
|
gbt_numeric_cmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
|
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,
|
2017-08-16 12:22:32 +08:00
|
|
|
false,
|
2004-08-29 13:07:03 +08:00
|
|
|
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(),
|
2017-03-21 21:53:35 +08:00
|
|
|
GIST_LEAF(entry), &tinfo, fcinfo->flinfo);
|
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(),
|
2017-03-21 21:53:35 +08:00
|
|
|
&tinfo, fcinfo->flinfo));
|
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);
|
|
|
|
|
2017-03-21 21:53:35 +08:00
|
|
|
*result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
|
2011-04-23 08:19:58 +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
|
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);
|
2015-03-27 05:10:10 +08:00
|
|
|
uni = PointerGetDatum(gbt_var_key_copy(&rk));
|
2017-03-21 21:53:35 +08:00
|
|
|
gbt_var_bin_union(&uni, newe, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
|
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
|
|
|
|
2020-01-31 00:42:14 +08:00
|
|
|
us = DatumGetNumeric(DirectFunctionCall2(numeric_sub,
|
2004-08-29 13:07:03 +08:00
|
|
|
PointerGetDatum(uk.upper),
|
2020-01-31 00:42:14 +08:00
|
|
|
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
|
|
|
|
2020-01-31 00:42:14 +08:00
|
|
|
os = DatumGetNumeric(DirectFunctionCall2(numeric_sub,
|
2004-08-29 13:07:03 +08:00
|
|
|
PointerGetDatum(ok.upper),
|
2020-01-31 00:42:14 +08:00
|
|
|
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
|
|
|
|
2020-01-31 00:42:14 +08:00
|
|
|
ds = DatumGetNumeric(DirectFunctionCall2(numeric_sub,
|
2004-08-29 13:07:03 +08:00
|
|
|
NumericGetDatum(us),
|
2020-01-31 00:42:14 +08:00
|
|
|
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
|
|
|
|
{
|
2020-09-10 02:16:28 +08:00
|
|
|
Numeric nul = int64_to_numeric(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;
|
2020-01-31 00:42:14 +08:00
|
|
|
os = DatumGetNumeric(DirectFunctionCall2(numeric_div,
|
2004-08-29 13:07:03 +08:00
|
|
|
NumericGetDatum(ds),
|
2020-01-31 00:42:14 +08:00
|
|
|
NumericGetDatum(us)));
|
2004-08-29 13:07:03 +08:00
|
|
|
*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(),
|
2017-03-21 21:53:35 +08:00
|
|
|
&tinfo, fcinfo->flinfo);
|
2004-08-29 13:07:03 +08:00
|
|
|
PG_RETURN_POINTER(v);
|
|
|
|
}
|