postgresql/contrib/btree_gist/btree_time.c

261 lines
6.0 KiB
C
Raw Normal View History

#include "btree_gist.h"
#include "btree_utils_num.h"
#include "utils/date.h"
typedef struct
{
2004-08-29 07:07:03 +02:00
TimeADT lower;
TimeADT upper;
} timeKEY;
/*
** time ops
*/
PG_FUNCTION_INFO_V1(gbt_time_compress);
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_timetz_consistent);
PG_FUNCTION_INFO_V1(gbt_time_penalty);
PG_FUNCTION_INFO_V1(gbt_time_same);
2004-08-29 07:07:03 +02:00
Datum gbt_time_compress(PG_FUNCTION_ARGS);
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_timetz_consistent(PG_FUNCTION_ARGS);
Datum gbt_time_penalty(PG_FUNCTION_ARGS);
Datum gbt_time_same(PG_FUNCTION_ARGS);
#define P_TimeADTGetDatum(x) PointerGetDatum( &(x) )
2004-08-29 07:07:03 +02:00
static bool
gbt_timegt(const void *a, const void *b)
{
2004-08-29 07:07:03 +02:00
return DatumGetBool(
2005-02-21 15:09:49 +01:00
DirectFunctionCall2(time_gt, PointerGetDatum(a), PointerGetDatum(b))
2004-08-29 07:07:03 +02:00
);
}
2004-08-29 07:07:03 +02:00
static bool
gbt_timege(const void *a, const void *b)
{
2004-08-29 07:07:03 +02:00
return DatumGetBool(
2005-02-21 15:09:49 +01:00
DirectFunctionCall2(time_ge, PointerGetDatum(a), PointerGetDatum(b))
2004-08-29 07:07:03 +02:00
);
}
2004-08-29 07:07:03 +02:00
static bool
gbt_timeeq(const void *a, const void *b)
{
2004-08-29 07:07:03 +02:00
return DatumGetBool(
2005-02-21 15:09:49 +01:00
DirectFunctionCall2(time_eq, PointerGetDatum(a), PointerGetDatum(b))
2004-08-29 07:07:03 +02:00
);
}
2004-08-29 07:07:03 +02:00
static bool
gbt_timele(const void *a, const void *b)
{
2004-08-29 07:07:03 +02:00
return DatumGetBool(
2005-02-21 15:09:49 +01:00
DirectFunctionCall2(time_le, PointerGetDatum(a), PointerGetDatum(b))
2004-08-29 07:07:03 +02:00
);
}
2004-08-29 07:07:03 +02:00
static bool
gbt_timelt(const void *a, const void *b)
{
2004-08-29 07:07:03 +02:00
return DatumGetBool(
2005-02-21 15:09:49 +01:00
DirectFunctionCall2(time_lt, PointerGetDatum(a), PointerGetDatum(b))
2004-08-29 07:07:03 +02:00
);
}
static int
gbt_timekey_cmp(const void *a, const void *b)
{
2004-08-29 07:07:03 +02:00
if (gbt_timegt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
return 1;
else if (gbt_timelt((void *) &(((Nsrt *) a)->t[0]), (void *) &(((Nsrt *) b)->t[0])))
return -1;
return 0;
}
2004-08-29 07:07:03 +02:00
static const gbtree_ninfo tinfo =
{
2004-08-29 07:07:03 +02:00
gbt_t_time,
sizeof(TimeADT),
gbt_timegt,
gbt_timege,
gbt_timeeq,
gbt_timele,
gbt_timelt,
gbt_timekey_cmp
};
/**************************************************
* time ops
**************************************************/
Datum
gbt_time_compress(PG_FUNCTION_ARGS)
{
2004-08-29 07:07:03 +02:00
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = NULL;
PG_RETURN_POINTER(gbt_num_compress(retval, entry, &tinfo));
}
Datum
gbt_timetz_compress(PG_FUNCTION_ARGS)
{
2004-08-29 07:07:03 +02:00
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval;
if (entry->leafkey)
{
timeKEY *r = (timeKEY *) palloc(sizeof(timeKEY));
TimeTzADT *tz = DatumGetTimeTzADTP(entry->key);
retval = palloc(sizeof(GISTENTRY));
/* We are using the time + zone only to compress */
r->lower = r->upper = (tz->time + tz->zone);
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, sizeof(timeKEY), FALSE);
}
else
retval = entry;
PG_RETURN_POINTER(retval);
}
Datum
gbt_time_consistent(PG_FUNCTION_ARGS)
{
2004-08-29 07:07:03 +02:00
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
TimeADT query = PG_GETARG_TIMEADT(1);
timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &query, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_timetz_consistent(PG_FUNCTION_ARGS)
{
2004-08-29 07:07:03 +02:00
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
TimeTzADT *query = PG_GETARG_TIMETZADT_P(1);
TimeADT qqq = query->time + query->zone;
timeKEY *kkk = (timeKEY *) DatumGetPointer(entry->key);
GBT_NUMKEY_R key;
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
key.lower = (GBT_NUMKEY *) & kkk->lower;
key.upper = (GBT_NUMKEY *) & kkk->upper;
PG_RETURN_BOOL(
gbt_num_consistent(&key, (void *) &qqq, &strategy, GIST_LEAF(entry), &tinfo)
);
}
Datum
gbt_time_union(PG_FUNCTION_ARGS)
{
2004-08-29 07:07:03 +02:00
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
void *out = palloc(sizeof(timeKEY));
*(int *) PG_GETARG_POINTER(1) = sizeof(timeKEY);
PG_RETURN_POINTER(gbt_num_union((void *) out, entryvec, &tinfo));
}
Datum
gbt_time_penalty(PG_FUNCTION_ARGS)
{
2004-08-29 07:07:03 +02:00
timeKEY *origentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
timeKEY *newentry = (timeKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
Interval *intr;
#ifdef HAVE_INT64_TIMESTAMP
2004-08-29 07:07:03 +02:00
int64 res;
#else
2004-08-29 07:07:03 +02:00
double res;
#endif
2004-08-29 07:07:03 +02:00
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
P_TimeADTGetDatum(newentry->upper),
P_TimeADTGetDatum(origentry->upper)));
2004-08-29 07:07:03 +02:00
/* see interval_larger */
res = Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
P_TimeADTGetDatum(origentry->lower),
P_TimeADTGetDatum(newentry->lower)));
2004-08-29 07:07:03 +02:00
/* see interval_larger */
res += Max(intr->time + intr->month * (30 * 86400), 0);
pfree(intr);
*result = 0.0;
if (res > 0)
{
intr = DatumGetIntervalP(DirectFunctionCall2(
time_mi_time,
P_TimeADTGetDatum(origentry->upper),
P_TimeADTGetDatum(origentry->lower)));
2004-08-29 07:07:03 +02:00
*result += FLT_MIN;
*result += (float) (res / ((double) (res + intr->time + intr->month * (30 * 86400))));
*result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
pfree(intr);
}
PG_RETURN_POINTER(result);
}
Datum
gbt_time_picksplit(PG_FUNCTION_ARGS)
{
2004-08-29 07:07:03 +02:00
PG_RETURN_POINTER(gbt_num_picksplit(
(GistEntryVector *) PG_GETARG_POINTER(0),
(GIST_SPLITVEC *) PG_GETARG_POINTER(1),
&tinfo
));
}
Datum
gbt_time_same(PG_FUNCTION_ARGS)
{
2004-08-29 07:07:03 +02:00
timeKEY *b1 = (timeKEY *) PG_GETARG_POINTER(0);
timeKEY *b2 = (timeKEY *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
2004-08-29 07:07:03 +02:00
*result = gbt_num_same((void *) b1, (void *) b2, &tinfo);
PG_RETURN_POINTER(result);
}