2001-01-12 08:16:26 +08:00
|
|
|
/******************************************************************************
|
|
|
|
This file contains routines that can be bound to a Postgres backend and
|
|
|
|
called by the backend in the process of processing queries. The calling
|
|
|
|
format for these routines is dictated by Postgres architecture.
|
|
|
|
******************************************************************************/
|
|
|
|
|
2001-03-20 11:08:12 +08:00
|
|
|
/*
|
|
|
|
#define GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
#define GIST_QUERY_DEBUG
|
2001-03-20 11:08:12 +08:00
|
|
|
*/
|
|
|
|
|
2001-03-19 07:36:55 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2001-01-12 08:16:26 +08:00
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
|
|
|
#include "access/rtree.h"
|
|
|
|
#include "utils/elog.h"
|
|
|
|
#include "utils/palloc.h"
|
|
|
|
#include "utils/array.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "storage/bufpage.h"
|
|
|
|
|
2001-03-20 11:08:12 +08:00
|
|
|
/* number ranges for compression */
|
2001-03-22 23:24:12 +08:00
|
|
|
#define MAXNUMRANGE 100
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
#define max(a,b) ((a) > (b) ? (a) : (b))
|
|
|
|
#define min(a,b) ((a) <= (b) ? (a) : (b))
|
|
|
|
#define abs(a) ((a) < (0) ? -(a) : (a))
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-20 11:08:12 +08:00
|
|
|
/* dimension of array */
|
|
|
|
#define NDIM 1
|
|
|
|
|
|
|
|
/* useful macros for accessing int4 arrays */
|
2001-01-12 08:16:26 +08:00
|
|
|
#define ARRPTR(x) ( (int4 *) ARR_DATA_PTR(x) )
|
2001-03-20 11:08:12 +08:00
|
|
|
#define ARRNELEMS(x) ArrayGetNItems( ARR_NDIM(x), ARR_DIMS(x))
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-08-05 03:36:45 +08:00
|
|
|
#define ARRISNULL(x) ( (x) ? ( ( ARR_NDIM(x) == NDIM ) ? ( ( ARRNELEMS( x ) ) ? 0 : 1 ) : ( ( ARR_NDIM(x) ) ? (elog(ERROR,"Array is not one-dimensional: %d dimensions", ARR_NDIM(x)),1) : 1 ) ) : 1 )
|
2001-08-05 03:35:32 +08:00
|
|
|
#define ARRISVOID(x) ( (x) ? ( ( ARR_NDIM(x) == NDIM ) ? ( ( ARRNELEMS( x ) ) ? 0 : 1 ) : 1 ) : 0 )
|
2001-03-20 11:08:12 +08:00
|
|
|
|
|
|
|
#define SORT(x) \
|
|
|
|
do { \
|
|
|
|
if ( ARRNELEMS( x ) > 1 ) \
|
2001-06-11 10:31:25 +08:00
|
|
|
isort( ARRPTR( x ), ARRNELEMS( x ) ); \
|
2001-03-20 11:08:12 +08:00
|
|
|
} while(0)
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-01-12 08:16:26 +08:00
|
|
|
#define PREPAREARR(x) \
|
2001-03-20 11:08:12 +08:00
|
|
|
do { \
|
|
|
|
if ( ARRNELEMS( x ) > 1 ) \
|
2001-06-11 10:31:25 +08:00
|
|
|
if ( isort( ARRPTR( x ), ARRNELEMS( x ) ) ) \
|
2001-03-20 11:08:12 +08:00
|
|
|
x = _int_unique( x ); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
/* "wish" function */
|
|
|
|
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
|
|
|
|
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
/* bigint defines */
|
|
|
|
#define BITBYTE 8
|
2001-03-22 23:24:12 +08:00
|
|
|
#define SIGLENINT 64 /* >122 => key will toast, so very slow!!! */
|
|
|
|
#define SIGLEN ( sizeof(int)*SIGLENINT )
|
2001-03-18 05:59:42 +08:00
|
|
|
#define SIGLENBIT (SIGLEN*BITBYTE)
|
|
|
|
|
|
|
|
typedef char BITVEC[SIGLEN];
|
2001-03-22 23:24:12 +08:00
|
|
|
typedef char *BITVECP;
|
|
|
|
|
2001-03-18 05:59:42 +08:00
|
|
|
#define SIGPTR(x) ( (BITVECP) ARR_DATA_PTR(x) )
|
|
|
|
|
|
|
|
|
|
|
|
#define LOOPBYTE(a) \
|
2001-03-22 23:24:12 +08:00
|
|
|
for(i=0;i<SIGLEN;i++) {\
|
|
|
|
a;\
|
|
|
|
}
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
#define LOOPBIT(a) \
|
2001-03-22 23:24:12 +08:00
|
|
|
for(i=0;i<SIGLENBIT;i++) {\
|
|
|
|
a;\
|
|
|
|
}
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-03-19 07:36:55 +08:00
|
|
|
/* beware of multiple evaluation of arguments to these macros! */
|
|
|
|
#define GETBYTEBIT(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
|
|
|
|
#define CLRBIT(x,i) GETBYTEBIT(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
|
|
|
|
#define SETBIT(x,i) GETBYTEBIT(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
|
|
|
|
#define GETBIT(x,i) ( (GETBYTEBIT(x,i) >> ( (i) % BITBYTE )) & 0x01 )
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
|
2001-01-12 08:16:26 +08:00
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
static void
|
|
|
|
printarr(ArrayType *a, int num)
|
|
|
|
{
|
|
|
|
char bbb[16384];
|
|
|
|
char *cur;
|
|
|
|
int l;
|
|
|
|
int *d;
|
|
|
|
|
|
|
|
d = ARRPTR(a);
|
2001-01-12 08:16:26 +08:00
|
|
|
*bbb = '\0';
|
|
|
|
cur = bbb;
|
2001-03-22 23:24:12 +08:00
|
|
|
for (l = 0; l < min(num, ARRNELEMS(a)); l++)
|
|
|
|
{
|
|
|
|
sprintf(cur, "%d ", d[l]);
|
|
|
|
cur = strchr(cur, '\0');
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
elog(NOTICE, "\t\t%s", bbb);
|
|
|
|
}
|
2001-03-22 23:24:12 +08:00
|
|
|
static void
|
|
|
|
printbitvec(BITVEC bv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char str[SIGLENBIT + 1];
|
|
|
|
|
|
|
|
str[SIGLENBIT] = '\0';
|
|
|
|
LOOPBIT(str[i] = (GETBIT(bv, i)) ? '1' : '0');
|
|
|
|
|
|
|
|
elog(NOTICE, "BV: %s", str);
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
2001-03-22 23:24:12 +08:00
|
|
|
|
2001-01-12 08:16:26 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-18 05:59:42 +08:00
|
|
|
/*
|
|
|
|
** types for functions
|
|
|
|
*/
|
2001-03-22 23:24:12 +08:00
|
|
|
typedef ArrayType *(*formarray) (ArrayType *, ArrayType *);
|
|
|
|
typedef void (*formfloat) (ArrayType *, float *);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-01-12 08:16:26 +08:00
|
|
|
/*
|
|
|
|
** usefull function
|
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
static bool isort(int4 *a, const int len);
|
2001-03-22 23:24:12 +08:00
|
|
|
static ArrayType *new_intArrayType(int num);
|
|
|
|
static ArrayType *copy_intArrayType(ArrayType *a);
|
|
|
|
static ArrayType *resize_intArrayType(ArrayType *a, int num);
|
|
|
|
static int internal_size(int *a, int len);
|
|
|
|
static ArrayType *_int_unique(ArrayType *a);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-03-20 11:08:12 +08:00
|
|
|
/* common GiST function*/
|
2001-03-22 23:24:12 +08:00
|
|
|
static GIST_SPLITVEC *_int_common_picksplit(bytea *entryvec,
|
|
|
|
GIST_SPLITVEC *v,
|
|
|
|
formarray unionf,
|
|
|
|
formarray interf,
|
|
|
|
formfloat sizef,
|
|
|
|
float coef);
|
|
|
|
static float *_int_common_penalty(GISTENTRY *origentry,
|
|
|
|
GISTENTRY *newentry,
|
|
|
|
float *result,
|
|
|
|
formarray unionf,
|
|
|
|
formfloat sizef);
|
|
|
|
static ArrayType *_int_common_union(bytea *entryvec,
|
|
|
|
int *sizep,
|
|
|
|
formarray unionf);
|
|
|
|
|
|
|
|
/*
|
2001-01-12 08:16:26 +08:00
|
|
|
** GiST support methods
|
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_FUNCTION_INFO_V1( g_int_consistent );
|
|
|
|
PG_FUNCTION_INFO_V1( g_int_compress );
|
|
|
|
PG_FUNCTION_INFO_V1( g_int_decompress );
|
|
|
|
PG_FUNCTION_INFO_V1( g_int_penalty );
|
|
|
|
PG_FUNCTION_INFO_V1( g_int_picksplit );
|
|
|
|
PG_FUNCTION_INFO_V1( g_int_union );
|
|
|
|
PG_FUNCTION_INFO_V1( g_int_same );
|
|
|
|
|
|
|
|
Datum g_int_consistent(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_compress(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_decompress(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_penalty(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_picksplit(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_union(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_int_same(PG_FUNCTION_ARGS);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2001-03-20 11:08:12 +08:00
|
|
|
** R-tree support functions
|
2001-01-12 08:16:26 +08:00
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
static bool inner_int_contains(ArrayType *a, ArrayType *b);
|
|
|
|
static bool inner_int_overlap(ArrayType *a, ArrayType *b);
|
|
|
|
static ArrayType *inner_int_union(ArrayType *a, ArrayType *b);
|
|
|
|
static ArrayType *inner_int_inter(ArrayType *a, ArrayType *b);
|
|
|
|
static void rt__int_size(ArrayType *a, float *sz);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1( _int_different );
|
|
|
|
PG_FUNCTION_INFO_V1( _int_same );
|
|
|
|
PG_FUNCTION_INFO_V1( _int_contains );
|
|
|
|
PG_FUNCTION_INFO_V1( _int_contained );
|
|
|
|
PG_FUNCTION_INFO_V1( _int_overlap );
|
|
|
|
PG_FUNCTION_INFO_V1( _int_union );
|
|
|
|
PG_FUNCTION_INFO_V1( _int_inter );
|
|
|
|
|
|
|
|
Datum _int_different(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_same(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_contains(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_contained(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_overlap(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_union(PG_FUNCTION_ARGS);
|
|
|
|
Datum _int_inter(PG_FUNCTION_ARGS);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-18 05:59:42 +08:00
|
|
|
/*
|
|
|
|
** _intbig methods
|
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_FUNCTION_INFO_V1( g_intbig_consistent );
|
|
|
|
PG_FUNCTION_INFO_V1( g_intbig_compress );
|
|
|
|
PG_FUNCTION_INFO_V1( g_intbig_decompress );
|
|
|
|
PG_FUNCTION_INFO_V1( g_intbig_penalty );
|
|
|
|
PG_FUNCTION_INFO_V1( g_intbig_picksplit );
|
|
|
|
PG_FUNCTION_INFO_V1( g_intbig_union );
|
|
|
|
PG_FUNCTION_INFO_V1( g_intbig_same );
|
|
|
|
|
|
|
|
Datum g_intbig_consistent(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_compress(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_decompress(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_penalty(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_picksplit(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_union(PG_FUNCTION_ARGS);
|
|
|
|
Datum g_intbig_same(PG_FUNCTION_ARGS);
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
static bool _intbig_contains(ArrayType *a, ArrayType *b);
|
|
|
|
static bool _intbig_overlap(ArrayType *a, ArrayType *b);
|
|
|
|
static ArrayType *_intbig_union(ArrayType *a, ArrayType *b);
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
static ArrayType * _intbig_inter(ArrayType *a, ArrayType *b);
|
2001-03-22 23:24:12 +08:00
|
|
|
static void rt__intbig_size(ArrayType *a, float *sz);
|
|
|
|
static void gensign(BITVEC sign, int *a, int len);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
/*****************************************************************************
|
2001-03-22 23:24:12 +08:00
|
|
|
* GiST functions
|
2001-01-12 08:16:26 +08:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Consistent method for _intments
|
|
|
|
** Should return false if for all data items x below entry,
|
|
|
|
** the predicate x op query == FALSE, where op is the oper
|
|
|
|
** corresponding to strategy in the pg_amop table.
|
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_int_consistent(PG_FUNCTION_ARGS) {
|
|
|
|
GISTENTRY *entry = (GISTENTRY *)PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *query = ( ArrayType * )PG_GETARG_POINTER(1);
|
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2001-03-22 23:24:12 +08:00
|
|
|
bool retval;
|
|
|
|
|
|
|
|
/* sort query for fast search, key is already sorted */
|
2001-03-20 11:08:12 +08:00
|
|
|
/* XXX are we sure it's safe to scribble on the query object here? */
|
|
|
|
/* XXX what about toasted input? */
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(query))
|
|
|
|
return FALSE;
|
|
|
|
PREPAREARR(query);
|
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTOverlapStrategyNumber:
|
2001-06-01 02:16:55 +08:00
|
|
|
retval = inner_int_overlap((ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
query);
|
2001-03-22 23:24:12 +08:00
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
|
|
|
case RTContainsStrategyNumber:
|
2001-06-01 02:16:55 +08:00
|
|
|
retval = inner_int_contains((ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
query);
|
2001-03-22 23:24:12 +08:00
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2001-06-01 02:16:55 +08:00
|
|
|
retval = inner_int_overlap((ArrayType *) DatumGetPointer(entry->key),
|
|
|
|
query);
|
2001-03-22 23:24:12 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = FALSE;
|
|
|
|
}
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_BOOL(retval);
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_int_union(PG_FUNCTION_ARGS)
|
2001-01-12 08:16:26 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( _int_common_union(
|
|
|
|
(bytea *) PG_GETARG_POINTER(0),
|
|
|
|
(int *) PG_GETARG_POINTER(1),
|
|
|
|
inner_int_union
|
|
|
|
) );
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GiST Compress and Decompress methods
|
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_int_compress(PG_FUNCTION_ARGS)
|
2001-01-12 08:16:26 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *)PG_GETARG_POINTER(0);
|
2001-03-22 23:24:12 +08:00
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *r;
|
|
|
|
int len;
|
|
|
|
int *dr;
|
|
|
|
int i,
|
|
|
|
min,
|
|
|
|
cand;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
if (DatumGetPointer(entry->key) != NULL)
|
|
|
|
r = (ArrayType *) PG_DETOAST_DATUM_COPY(entry->key);
|
2001-03-22 23:24:12 +08:00
|
|
|
else
|
2001-03-20 11:08:12 +08:00
|
|
|
r = NULL;
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(r))
|
|
|
|
{
|
2001-08-05 03:35:32 +08:00
|
|
|
if ( ARRISVOID(r) ) {
|
|
|
|
ArrayType *out = new_intArrayType( 0 );
|
|
|
|
gistentryinit(*retval, PointerGetDatum(out),
|
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(out), FALSE);
|
|
|
|
} else {
|
|
|
|
gistentryinit(*retval, (Datum) 0, entry->rel, entry->page, entry->offset,
|
2001-03-22 23:24:12 +08:00
|
|
|
0, FALSE);
|
2001-08-05 03:35:32 +08:00
|
|
|
}
|
|
|
|
if (r) pfree(r);
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER(retval);
|
2001-03-22 23:24:12 +08:00
|
|
|
}
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (entry->leafkey)
|
|
|
|
PREPAREARR(r);
|
|
|
|
len = ARRNELEMS(r);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "COMP IN: %d leaf; %d rel; %d page; %d offset; %d bytes; %d elems", entry->leafkey, (int) entry->rel, (int) entry->page, (int) entry->offset, (int) entry->bytes, len);
|
2001-01-12 08:16:26 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (len >= 2 * MAXNUMRANGE)
|
|
|
|
{ /* compress */
|
|
|
|
r = resize_intArrayType(r, 2 * (len));
|
|
|
|
|
|
|
|
dr = ARRPTR(r);
|
|
|
|
|
|
|
|
for (i = len - 1; i >= 0; i--)
|
|
|
|
dr[2 * i] = dr[2 * i + 1] = dr[i];
|
|
|
|
|
|
|
|
len *= 2;
|
|
|
|
cand = 1;
|
|
|
|
while (len > MAXNUMRANGE * 2)
|
|
|
|
{
|
|
|
|
min = 0x7fffffff;
|
|
|
|
for (i = 2; i < len; i += 2)
|
|
|
|
if (min > (dr[i] - dr[i - 1]))
|
|
|
|
{
|
|
|
|
min = (dr[i] - dr[i - 1]);
|
|
|
|
cand = i;
|
|
|
|
}
|
|
|
|
memmove((void *) &dr[cand - 1], (void *) &dr[cand + 1], (len - cand - 1) * sizeof(int));
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
r = resize_intArrayType(r, len);
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER(retval);
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_int_decompress(PG_FUNCTION_ARGS)
|
2001-01-12 08:16:26 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *)PG_GETARG_POINTER(0);
|
2001-03-22 23:24:12 +08:00
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *r;
|
|
|
|
int *dr,
|
|
|
|
lenr;
|
|
|
|
ArrayType *in;
|
|
|
|
int lenin;
|
|
|
|
int *din;
|
|
|
|
int i,
|
|
|
|
j;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
if (DatumGetPointer(entry->key) != NULL)
|
|
|
|
in = (ArrayType *) PG_DETOAST_DATUM(entry->key);
|
2001-03-22 23:24:12 +08:00
|
|
|
else
|
2001-03-20 11:08:12 +08:00
|
|
|
in = NULL;
|
|
|
|
|
2001-08-05 03:35:32 +08:00
|
|
|
if (ARRISNULL(in))
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-08-05 03:35:32 +08:00
|
|
|
if ( ARRISVOID(in) ) {
|
|
|
|
r = new_intArrayType( 0 );
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
|
|
|
} else {
|
|
|
|
gistentryinit(*retval, (Datum) 0, entry->rel, entry->page, entry->offset, 0, FALSE);
|
|
|
|
}
|
2001-03-22 23:24:12 +08:00
|
|
|
if (in)
|
2001-06-01 02:16:55 +08:00
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
2001-03-22 23:24:12 +08:00
|
|
|
pfree(in);
|
2001-01-12 08:16:26 +08:00
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "DECOMP IN: NULL");
|
2001-01-12 08:16:26 +08:00
|
|
|
#endif
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER(retval);
|
2001-03-22 23:24:12 +08:00
|
|
|
}
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
lenin = ARRNELEMS(in);
|
|
|
|
din = ARRPTR(in);
|
|
|
|
|
|
|
|
if (lenin < 2 * MAXNUMRANGE)
|
|
|
|
{ /* not comressed value */
|
|
|
|
/* sometimes strange bytesize */
|
2001-06-01 02:16:55 +08:00
|
|
|
gistentryinit(*entry, PointerGetDatum(in), entry->rel, entry->page, entry->offset, VARSIZE(in), FALSE);
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER(entry);
|
2001-03-22 23:24:12 +08:00
|
|
|
}
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "DECOMP IN: %d leaf; %d rel; %d page; %d offset; %d bytes; %d elems", entry->leafkey, (int) entry->rel, (int) entry->page, (int) entry->offset, (int) entry->bytes, lenin);
|
2001-01-12 08:16:26 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
lenr = internal_size(din, lenin);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
r = new_intArrayType(lenr);
|
|
|
|
dr = ARRPTR(r);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
for (i = 0; i < lenin; i += 2)
|
|
|
|
for (j = din[i]; j <= din[i + 1]; j++)
|
|
|
|
if ((!i) || *(dr - 1) != j)
|
|
|
|
*dr++ = j;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
2001-03-22 23:24:12 +08:00
|
|
|
pfree(in);
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER(retval);
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Penalty method for _intments
|
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_int_penalty(PG_FUNCTION_ARGS)
|
2001-01-12 08:16:26 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( _int_common_penalty(
|
|
|
|
(GISTENTRY *)PG_GETARG_POINTER(0),
|
|
|
|
(GISTENTRY *)PG_GETARG_POINTER(1),
|
|
|
|
(float *) PG_GETARG_POINTER(2),
|
|
|
|
inner_int_union, rt__int_size
|
|
|
|
) );
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_int_picksplit(PG_FUNCTION_ARGS)
|
2001-01-12 08:16:26 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( _int_common_picksplit(
|
|
|
|
(bytea *)PG_GETARG_POINTER(0),
|
|
|
|
(GIST_SPLITVEC *)PG_GETARG_POINTER(1),
|
|
|
|
inner_int_union,
|
|
|
|
inner_int_inter,
|
|
|
|
rt__int_size,
|
|
|
|
1e-8
|
|
|
|
) );
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Equality methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_int_same(PG_FUNCTION_ARGS)
|
2001-01-12 08:16:26 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
bool *result = (bool *)PG_GETARG_POINTER(2);
|
|
|
|
*result = DatumGetBool(
|
|
|
|
DirectFunctionCall2(
|
|
|
|
_int_same,
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(0)),
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(1))
|
|
|
|
)
|
|
|
|
);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER(result);
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
_int_contained(PG_FUNCTION_ARGS)
|
2001-01-12 08:16:26 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_BOOL( DatumGetBool(
|
|
|
|
DirectFunctionCall2(
|
|
|
|
_int_contains,
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(1)),
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(0))
|
|
|
|
)
|
|
|
|
));
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
_int_contains(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
ArrayType *a = (ArrayType *)PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *b = (ArrayType *)PG_GETARG_POINTER(1);
|
2001-03-22 23:24:12 +08:00
|
|
|
bool res;
|
|
|
|
ArrayType *an,
|
|
|
|
*bn;
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
|
|
|
return FALSE;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
an = copy_intArrayType(a);
|
|
|
|
bn = copy_intArrayType(b);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
PREPAREARR(an);
|
|
|
|
PREPAREARR(bn);
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
res = inner_int_contains(an, bn);
|
|
|
|
pfree(an);
|
|
|
|
pfree(bn);
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_BOOL( res );
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
static bool
|
2001-03-22 23:24:12 +08:00
|
|
|
inner_int_contains(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
n;
|
|
|
|
int *da,
|
|
|
|
*db;
|
|
|
|
|
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "contains %d %d", na, nb);
|
2001-01-12 08:16:26 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
i = j = n = 0;
|
2001-03-22 23:24:12 +08:00
|
|
|
while (i < na && j < nb)
|
|
|
|
if (da[i] < db[j])
|
2001-01-12 08:16:26 +08:00
|
|
|
i++;
|
2001-03-22 23:24:12 +08:00
|
|
|
else if (da[i] == db[j])
|
|
|
|
{
|
|
|
|
n++;
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
else
|
2001-01-12 08:16:26 +08:00
|
|
|
j++;
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
return (n == nb) ? TRUE : FALSE;
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Operator class for R-tree indexing
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
_int_different(PG_FUNCTION_ARGS)
|
2001-01-12 08:16:26 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_BOOL( ! DatumGetBool(
|
|
|
|
DirectFunctionCall2(
|
|
|
|
_int_same,
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(0)),
|
|
|
|
PointerGetDatum(PG_GETARG_POINTER(1))
|
|
|
|
)
|
|
|
|
));
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
_int_same(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
ArrayType *a = (ArrayType *)PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *b = (ArrayType *)PG_GETARG_POINTER(1);
|
2001-03-22 23:24:12 +08:00
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int n;
|
|
|
|
int *da,
|
|
|
|
*db;
|
|
|
|
bool result;
|
|
|
|
ArrayType *an,
|
|
|
|
*bn;
|
|
|
|
bool anull = ARRISNULL(a);
|
|
|
|
bool bnull = ARRISNULL(b);
|
|
|
|
|
|
|
|
if (anull || bnull)
|
|
|
|
return (anull && bnull) ? TRUE : FALSE;
|
|
|
|
|
|
|
|
an = copy_intArrayType(a);
|
|
|
|
bn = copy_intArrayType(b);
|
|
|
|
|
|
|
|
SORT(an);
|
|
|
|
SORT(bn);
|
|
|
|
na = ARRNELEMS(an);
|
|
|
|
nb = ARRNELEMS(bn);
|
|
|
|
da = ARRPTR(an);
|
|
|
|
db = ARRPTR(bn);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
|
|
|
result = FALSE;
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (na == nb)
|
2001-03-20 11:08:12 +08:00
|
|
|
{
|
|
|
|
result = TRUE;
|
2001-03-22 23:24:12 +08:00
|
|
|
for (n = 0; n < na; n++)
|
|
|
|
if (da[n] != db[n])
|
|
|
|
{
|
|
|
|
result = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2001-03-20 11:08:12 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
pfree(an);
|
|
|
|
pfree(bn);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_BOOL(result);
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
/* _int_overlap -- does a overlap b?
|
2001-01-12 08:16:26 +08:00
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
_int_overlap(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
ArrayType *a = (ArrayType *)PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *b = (ArrayType *)PG_GETARG_POINTER(1);
|
2001-03-22 23:24:12 +08:00
|
|
|
bool result;
|
|
|
|
ArrayType *an,
|
|
|
|
*bn;
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
an = copy_intArrayType(a);
|
|
|
|
bn = copy_intArrayType(b);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-20 11:08:12 +08:00
|
|
|
SORT(an);
|
|
|
|
SORT(bn);
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
result = inner_int_overlap(an, bn);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
pfree(an);
|
|
|
|
pfree(bn);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_BOOL( result );
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
static bool
|
2001-03-22 23:24:12 +08:00
|
|
|
inner_int_overlap(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
int *da,
|
|
|
|
*db;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "g_int_overlap");
|
2001-01-12 08:16:26 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
i = j = 0;
|
2001-03-22 23:24:12 +08:00
|
|
|
while (i < na && j < nb)
|
|
|
|
if (da[i] < db[j])
|
2001-01-12 08:16:26 +08:00
|
|
|
i++;
|
2001-03-22 23:24:12 +08:00
|
|
|
else if (da[i] == db[j])
|
|
|
|
return TRUE;
|
|
|
|
else
|
2001-01-12 08:16:26 +08:00
|
|
|
j++;
|
2001-03-22 23:24:12 +08:00
|
|
|
|
2001-01-12 08:16:26 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
_int_union(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
ArrayType *a = (ArrayType *)PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *b = (ArrayType *)PG_GETARG_POINTER(1);
|
2001-03-22 23:24:12 +08:00
|
|
|
ArrayType *result;
|
|
|
|
ArrayType *an,
|
|
|
|
*bn;
|
|
|
|
|
|
|
|
an = copy_intArrayType(a);
|
|
|
|
bn = copy_intArrayType(b);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (!ARRISNULL(an))
|
|
|
|
SORT(an);
|
|
|
|
if (!ARRISNULL(bn))
|
|
|
|
SORT(bn);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
result = inner_int_union(an, bn);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (an)
|
|
|
|
pfree(an);
|
|
|
|
if (bn)
|
|
|
|
pfree(bn);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( result );
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
static ArrayType *
|
2001-03-22 23:24:12 +08:00
|
|
|
inner_int_union(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *r = NULL;
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int *da,
|
|
|
|
*db,
|
|
|
|
*dr;
|
|
|
|
int i,
|
|
|
|
j;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "inner_union %d %d", ARRISNULL(a), ARRISNULL(b));
|
2001-01-12 08:16:26 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a) && ARRISNULL(b))
|
|
|
|
return new_intArrayType(0);
|
|
|
|
if (ARRISNULL(a))
|
|
|
|
r = copy_intArrayType(b);
|
|
|
|
if (ARRISNULL(b))
|
|
|
|
r = copy_intArrayType(a);
|
|
|
|
|
|
|
|
if (r)
|
|
|
|
dr = ARRPTR(r);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
r = new_intArrayType(na + nb);
|
|
|
|
dr = ARRPTR(r);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
/* union */
|
2001-01-12 08:16:26 +08:00
|
|
|
i = j = 0;
|
2001-03-22 23:24:12 +08:00
|
|
|
while (i < na && j < nb)
|
|
|
|
if (da[i] < db[j])
|
2001-01-12 08:16:26 +08:00
|
|
|
*dr++ = da[i++];
|
|
|
|
else
|
|
|
|
*dr++ = db[j++];
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
while (i < na)
|
|
|
|
*dr++ = da[i++];
|
|
|
|
while (j < nb)
|
|
|
|
*dr++ = db[j++];
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ARRNELEMS(r) > 1)
|
|
|
|
r = _int_unique(r);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
_int_inter(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
ArrayType *a = (ArrayType *)PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *b = (ArrayType *)PG_GETARG_POINTER(1);
|
2001-03-22 23:24:12 +08:00
|
|
|
ArrayType *result;
|
|
|
|
ArrayType *an,
|
|
|
|
*bn;
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER(new_intArrayType(0));
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
an = copy_intArrayType(a);
|
|
|
|
bn = copy_intArrayType(b);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
|
|
|
SORT(an);
|
|
|
|
SORT(bn);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
result = inner_int_inter(an, bn);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
pfree(an);
|
|
|
|
pfree(bn);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( result );
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
static ArrayType *
|
2001-03-22 23:24:12 +08:00
|
|
|
inner_int_inter(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
int na,
|
|
|
|
nb;
|
|
|
|
int *da,
|
|
|
|
*db,
|
|
|
|
*dr;
|
|
|
|
int i,
|
|
|
|
j;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "inner_inter %d %d", ARRISNULL(a), ARRISNULL(b));
|
2001-01-12 08:16:26 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
|
|
|
return new_intArrayType(0);
|
|
|
|
|
|
|
|
na = ARRNELEMS(a);
|
|
|
|
nb = ARRNELEMS(b);
|
|
|
|
da = ARRPTR(a);
|
|
|
|
db = ARRPTR(b);
|
|
|
|
r = new_intArrayType(min(na, nb));
|
|
|
|
dr = ARRPTR(r);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
i = j = 0;
|
2001-03-22 23:24:12 +08:00
|
|
|
while (i < na && j < nb)
|
|
|
|
if (da[i] < db[j])
|
2001-01-12 08:16:26 +08:00
|
|
|
i++;
|
2001-03-22 23:24:12 +08:00
|
|
|
else if (da[i] == db[j])
|
|
|
|
{
|
|
|
|
if (i + j == 0 || (i + j > 0 && *(dr - 1) != db[j]))
|
2001-01-12 08:16:26 +08:00
|
|
|
*dr++ = db[j];
|
2001-03-22 23:24:12 +08:00
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
else
|
2001-01-12 08:16:26 +08:00
|
|
|
j++;
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if ((dr - ARRPTR(r)) == 0)
|
|
|
|
{
|
|
|
|
pfree(r);
|
2001-03-20 11:08:12 +08:00
|
|
|
return new_intArrayType(0);
|
2001-03-22 23:24:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return resize_intArrayType(r, dr - ARRPTR(r));
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
static void
|
2001-01-12 08:16:26 +08:00
|
|
|
rt__int_size(ArrayType *a, float *size)
|
|
|
|
{
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a))
|
|
|
|
*size = 0.0;
|
|
|
|
else
|
|
|
|
*size = (float) ARRNELEMS(a);
|
|
|
|
|
|
|
|
return;
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2001-03-22 23:24:12 +08:00
|
|
|
* Miscellaneous operators and functions
|
2001-01-12 08:16:26 +08:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/* len >= 2 */
|
2001-03-22 23:24:12 +08:00
|
|
|
static bool
|
2001-06-11 10:31:25 +08:00
|
|
|
isort(int4 *a, int len)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
int4 tmp,
|
2001-03-22 23:24:12 +08:00
|
|
|
index;
|
2001-06-11 10:31:25 +08:00
|
|
|
int4 *cur,
|
2001-03-22 23:24:12 +08:00
|
|
|
*end;
|
|
|
|
bool r = FALSE;
|
|
|
|
|
|
|
|
end = a + len;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
cur = a + 1;
|
|
|
|
while (cur < end)
|
|
|
|
{
|
|
|
|
if (*(cur - 1) > *cur)
|
|
|
|
{
|
|
|
|
tmp = *(cur - 1);
|
|
|
|
*(cur - 1) = *cur;
|
|
|
|
*cur = tmp;
|
|
|
|
index = 1;
|
|
|
|
}
|
|
|
|
else if (!r && *(cur - 1) == *cur)
|
2001-01-12 08:16:26 +08:00
|
|
|
r = TRUE;
|
2001-03-22 23:24:12 +08:00
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
} while (index);
|
2001-01-12 08:16:26 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
static ArrayType *
|
|
|
|
new_intArrayType(int num)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
int nbytes = ARR_OVERHEAD(NDIM) + sizeof(int) * num;
|
|
|
|
|
|
|
|
r = (ArrayType *) palloc(nbytes);
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-01-12 08:16:26 +08:00
|
|
|
MemSet(r, 0, nbytes);
|
|
|
|
r->size = nbytes;
|
|
|
|
r->ndim = NDIM;
|
2001-03-22 23:24:12 +08:00
|
|
|
*((int *) ARR_DIMS(r)) = num;
|
|
|
|
*((int *) ARR_LBOUND(r)) = 1;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayType *
|
|
|
|
resize_intArrayType(ArrayType *a, int num)
|
|
|
|
{
|
|
|
|
int nbytes = ARR_OVERHEAD(NDIM) + sizeof(int) * num;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (num == ARRNELEMS(a))
|
|
|
|
return a;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
a = (ArrayType *) repalloc(a, nbytes);
|
2001-01-12 08:16:26 +08:00
|
|
|
|
|
|
|
a->size = nbytes;
|
2001-03-22 23:24:12 +08:00
|
|
|
*((int *) ARR_DIMS(a)) = num;
|
2001-01-12 08:16:26 +08:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
static ArrayType *
|
|
|
|
copy_intArrayType(ArrayType *a)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
|
|
|
|
if (ARRISNULL(a))
|
|
|
|
return NULL;
|
|
|
|
r = new_intArrayType(ARRNELEMS(a));
|
|
|
|
memmove(r, a, VARSIZE(a));
|
2001-01-12 08:16:26 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* num for compressed key */
|
2001-03-22 23:24:12 +08:00
|
|
|
static int
|
|
|
|
internal_size(int *a, int len)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
size = 0;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
for (i = 0; i < len; i += 2)
|
|
|
|
if (!i || a[i] != a[i - 1]) /* do not count repeated range */
|
|
|
|
size += a[i + 1] - a[i] + 1;
|
2001-01-12 08:16:26 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
return size;
|
2001-01-12 08:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* r is sorted and size of r > 1 */
|
2001-03-22 23:24:12 +08:00
|
|
|
static ArrayType *
|
|
|
|
_int_unique(ArrayType *r)
|
|
|
|
{
|
|
|
|
int *tmp,
|
|
|
|
*dr,
|
|
|
|
*data;
|
|
|
|
int num = ARRNELEMS(r);
|
|
|
|
|
|
|
|
data = tmp = dr = ARRPTR(r);
|
|
|
|
while (tmp - data < num)
|
|
|
|
if (*tmp != *dr)
|
2001-01-12 08:16:26 +08:00
|
|
|
*(++dr) = *tmp++;
|
2001-03-22 23:24:12 +08:00
|
|
|
else
|
|
|
|
tmp++;
|
|
|
|
return resize_intArrayType(r, dr + 1 - ARRPTR(r));
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
** intbig functions
|
|
|
|
*********************************************************************/
|
2001-03-22 23:24:12 +08:00
|
|
|
static void
|
|
|
|
gensign(BITVEC sign, int *a, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* we assume that the sign vector is previously zeroed */
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
SETBIT(sign, (*a) % SIGLENBIT);
|
|
|
|
a++;
|
|
|
|
}
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
static bool
|
|
|
|
_intbig_overlap(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BITVECP da,
|
|
|
|
db;
|
|
|
|
|
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
|
|
|
return FALSE;
|
2001-03-18 05:59:42 +08:00
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
LOOPBYTE(if (da[i] & db[i]) return TRUE);
|
2001-03-18 05:59:42 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
static bool
|
|
|
|
_intbig_contains(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BITVECP da,
|
|
|
|
db;
|
|
|
|
|
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
|
|
|
return FALSE;
|
2001-03-18 05:59:42 +08:00
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
LOOPBYTE(if (db[i] & ~da[i]) return FALSE);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
static void
|
|
|
|
rt__intbig_size(ArrayType *a, float *sz)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
len = 0;
|
|
|
|
BITVECP bv;
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a))
|
|
|
|
{
|
|
|
|
*sz = 0.0;
|
2001-03-18 05:59:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bv = SIGPTR(a);
|
2001-03-22 23:24:12 +08:00
|
|
|
LOOPBIT(len += GETBIT(bv, i));
|
2001-03-18 05:59:42 +08:00
|
|
|
*sz = (float) len;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
static ArrayType *
|
|
|
|
_intbig_union(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
BITVECP da,
|
|
|
|
db,
|
|
|
|
dr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (ARRISNULL(a) && ARRISNULL(b))
|
|
|
|
return new_intArrayType(0);
|
|
|
|
if (ARRISNULL(a))
|
|
|
|
return copy_intArrayType(b);
|
|
|
|
if (ARRISNULL(b))
|
|
|
|
return copy_intArrayType(a);
|
|
|
|
|
|
|
|
r = new_intArrayType(SIGLENINT);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
dr = SIGPTR(r);
|
|
|
|
|
|
|
|
LOOPBYTE(dr[i] = da[i] | db[i]);
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
return r;
|
|
|
|
}
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
static ArrayType *
|
|
|
|
_intbig_inter(ArrayType *a, ArrayType *b)
|
|
|
|
{
|
|
|
|
ArrayType *r;
|
|
|
|
BITVECP da,
|
|
|
|
db,
|
|
|
|
dr;
|
|
|
|
int i;
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
|
|
|
return new_intArrayType(0);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
r = new_intArrayType(SIGLENINT);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
dr = SIGPTR(r);
|
|
|
|
|
|
|
|
LOOPBYTE(dr[i] = da[i] & db[i]);
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
return r;
|
|
|
|
}
|
2001-03-20 11:08:12 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_intbig_same(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
ArrayType *a = (ArrayType *)PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *b = (ArrayType *)PG_GETARG_POINTER(1);
|
|
|
|
bool *result = (bool *)PG_GETARG_POINTER(2);
|
2001-03-22 23:24:12 +08:00
|
|
|
BITVECP da,
|
|
|
|
db;
|
|
|
|
int i;
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(a) || ARRISNULL(b))
|
|
|
|
{
|
|
|
|
*result = (ARRISNULL(a) && ARRISNULL(b)) ? TRUE : FALSE;
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( result );
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
da = SIGPTR(a);
|
|
|
|
db = SIGPTR(b);
|
|
|
|
|
|
|
|
LOOPBYTE(
|
2001-03-22 23:24:12 +08:00
|
|
|
if (da[i] != db[i])
|
|
|
|
{
|
|
|
|
*result = FALSE;
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( result );
|
2001-03-22 23:24:12 +08:00
|
|
|
}
|
2001-03-18 05:59:42 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
*result = TRUE;
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( result );
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_intbig_compress(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *)PG_GETARG_POINTER(0);
|
2001-03-22 23:24:12 +08:00
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *r,
|
|
|
|
*in;
|
2001-06-11 10:31:25 +08:00
|
|
|
bool maycompress = true;
|
|
|
|
int i;
|
2001-03-22 23:24:12 +08:00
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
if (DatumGetPointer(entry->key) != NULL)
|
|
|
|
in = (ArrayType *) PG_DETOAST_DATUM(entry->key);
|
2001-03-22 23:24:12 +08:00
|
|
|
else
|
2001-03-18 05:59:42 +08:00
|
|
|
in = NULL;
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
if (!entry->leafkey) {
|
|
|
|
if ( ! ARRISNULL(in) ) {
|
|
|
|
LOOPBYTE(
|
|
|
|
if ( ( ((char*)ARRPTR(in))[i] & 0xff ) != 0xff ) {
|
|
|
|
maycompress = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if ( maycompress ) {
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
r = new_intArrayType(1);
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
|
|
|
PG_RETURN_POINTER( retval );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PG_RETURN_POINTER( entry );
|
|
|
|
}
|
2001-03-22 23:24:12 +08:00
|
|
|
|
2001-03-18 05:59:42 +08:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(in))
|
|
|
|
{
|
2001-08-05 03:35:32 +08:00
|
|
|
if ( ARRISVOID(in) ) {
|
|
|
|
r = new_intArrayType( SIGLENINT );
|
|
|
|
gistentryinit(*retval, PointerGetDatum(r),
|
|
|
|
entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
|
|
|
} else {
|
|
|
|
gistentryinit(*retval, (Datum) 0, entry->rel, entry->page, entry->offset,
|
|
|
|
0, FALSE);
|
|
|
|
}
|
2001-03-22 23:24:12 +08:00
|
|
|
if (in)
|
2001-06-01 02:16:55 +08:00
|
|
|
if (in != (ArrayType *) DatumGetPointer(entry->key))
|
2001-03-22 23:24:12 +08:00
|
|
|
pfree(in);
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER (retval);
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
r = new_intArrayType(SIGLENINT);
|
|
|
|
gensign(SIGPTR(r),
|
|
|
|
ARRPTR(in),
|
|
|
|
ARRNELEMS(in));
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
LOOPBYTE(
|
|
|
|
if( ( ((char*)ARRPTR(in))[i] & 0xff ) != 0xff ) {
|
|
|
|
maycompress = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( maycompress ) {
|
|
|
|
pfree(r);
|
|
|
|
r = new_intArrayType(1);
|
|
|
|
}
|
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
gistentryinit(*retval, PointerGetDatum(r), entry->rel, entry->page, entry->offset, VARSIZE(r), FALSE);
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
if (in)
|
2001-06-11 10:31:25 +08:00
|
|
|
if ( in != (ArrayType *) DatumGetPointer(entry->key))
|
2001-03-22 23:24:12 +08:00
|
|
|
pfree(in);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER (retval);
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_intbig_decompress(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *)PG_GETARG_POINTER(0);
|
2001-03-22 23:24:12 +08:00
|
|
|
ArrayType *key;
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
if ( DatumGetPointer(entry->key) != NULL )
|
|
|
|
key = (ArrayType *) PG_DETOAST_DATUM(entry->key);
|
|
|
|
else
|
|
|
|
key = NULL;
|
|
|
|
|
|
|
|
if ( key != (ArrayType *) DatumGetPointer(entry->key))
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
|
|
|
GISTENTRY *retval;
|
|
|
|
|
2001-03-20 11:08:12 +08:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
gistentryinit(*retval, PointerGetDatum(key), entry->rel, entry->page, entry->offset, (key) ? VARSIZE(key) : 0, FALSE);
|
|
|
|
PG_RETURN_POINTER( retval );
|
2001-03-20 11:08:12 +08:00
|
|
|
}
|
2001-06-11 10:31:25 +08:00
|
|
|
if ( ! ARRISNULL(key) )
|
|
|
|
if ( ARRNELEMS(key) == 1 ) {
|
|
|
|
GISTENTRY *retval;
|
|
|
|
ArrayType *newkey;
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
newkey = new_intArrayType(SIGLENINT);
|
|
|
|
MemSet( (void*)ARRPTR(newkey), 0xff, SIGLEN );
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
gistentryinit(*retval, PointerGetDatum(newkey), entry->rel, entry->page, entry->offset, VARSIZE(newkey), FALSE);
|
|
|
|
PG_RETURN_POINTER( retval );
|
|
|
|
}
|
|
|
|
PG_RETURN_POINTER( entry );
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_intbig_picksplit(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( _int_common_picksplit(
|
|
|
|
(bytea *)PG_GETARG_POINTER(0),
|
|
|
|
(GIST_SPLITVEC *)PG_GETARG_POINTER(1),
|
|
|
|
_intbig_union,
|
|
|
|
_intbig_inter,
|
|
|
|
rt__intbig_size,
|
|
|
|
1.0
|
|
|
|
) );
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_intbig_union(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( _int_common_union(
|
|
|
|
(bytea *) PG_GETARG_POINTER(0),
|
|
|
|
(int *) PG_GETARG_POINTER(1),
|
|
|
|
_intbig_union
|
|
|
|
) );
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
Datum
|
|
|
|
g_intbig_penalty(PG_FUNCTION_ARGS)
|
2001-03-22 23:24:12 +08:00
|
|
|
{
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_POINTER( _int_common_penalty(
|
|
|
|
(GISTENTRY *)PG_GETARG_POINTER(0),
|
|
|
|
(GISTENTRY *)PG_GETARG_POINTER(1),
|
|
|
|
(float *) PG_GETARG_POINTER(2),
|
|
|
|
_intbig_union, rt__intbig_size
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
g_intbig_consistent(PG_FUNCTION_ARGS) {
|
|
|
|
GISTENTRY *entry = (GISTENTRY *)PG_GETARG_POINTER(0);
|
|
|
|
ArrayType *query = ( ArrayType * )PG_GETARG_POINTER(1);
|
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2001-03-22 23:24:12 +08:00
|
|
|
bool retval;
|
|
|
|
ArrayType *q;
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-03-20 11:08:12 +08:00
|
|
|
/* XXX what about toasted input? */
|
2001-03-22 23:24:12 +08:00
|
|
|
if (ARRISNULL(query))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
q = new_intArrayType(SIGLENINT);
|
|
|
|
gensign(SIGPTR(q),
|
|
|
|
ARRPTR(query),
|
|
|
|
ARRNELEMS(query));
|
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTOverlapStrategyNumber:
|
2001-06-01 02:16:55 +08:00
|
|
|
retval = _intbig_overlap((ArrayType *) DatumGetPointer(entry->key), q);
|
2001-03-22 23:24:12 +08:00
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
|
|
|
case RTContainsStrategyNumber:
|
2001-06-01 02:16:55 +08:00
|
|
|
retval = _intbig_contains((ArrayType *) DatumGetPointer(entry->key), q);
|
2001-03-22 23:24:12 +08:00
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2001-06-01 02:16:55 +08:00
|
|
|
retval = _intbig_overlap((ArrayType *) DatumGetPointer(entry->key), q);
|
2001-03-22 23:24:12 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = FALSE;
|
|
|
|
}
|
|
|
|
pfree(q);
|
2001-06-11 10:31:25 +08:00
|
|
|
PG_RETURN_BOOL(retval);
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************
|
|
|
|
** Common GiST Method
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST Union method for _intments
|
|
|
|
** returns the minimal set that encloses all the entries in entryvec
|
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
static ArrayType *
|
2001-03-22 23:24:12 +08:00
|
|
|
_int_common_union(bytea *entryvec, int *sizep, formarray unionf)
|
|
|
|
{
|
|
|
|
int numranges,
|
|
|
|
i;
|
|
|
|
ArrayType *out = (ArrayType *) NULL;
|
|
|
|
ArrayType *tmp;
|
|
|
|
|
2001-03-18 05:59:42 +08:00
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "_int_common_union in");
|
2001-03-18 05:59:42 +08:00
|
|
|
#endif
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
numranges = (VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY);
|
2001-06-01 02:16:55 +08:00
|
|
|
tmp = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[0].key);
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
for (i = 1; i < numranges; i++)
|
|
|
|
{
|
|
|
|
out = (*unionf) (tmp, (ArrayType *)
|
2001-06-01 02:16:55 +08:00
|
|
|
DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key));
|
2001-03-22 23:24:12 +08:00
|
|
|
if (i > 1 && tmp)
|
|
|
|
pfree(tmp);
|
|
|
|
tmp = out;
|
|
|
|
}
|
|
|
|
|
|
|
|
*sizep = VARSIZE(out);
|
|
|
|
if (*sizep == 0)
|
|
|
|
{
|
|
|
|
pfree(out);
|
2001-03-18 05:59:42 +08:00
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "_int_common_union out1");
|
2001-03-18 05:59:42 +08:00
|
|
|
#endif
|
2001-03-22 23:24:12 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-03-18 05:59:42 +08:00
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "_int_common_union out");
|
2001-03-18 05:59:42 +08:00
|
|
|
#endif
|
2001-03-22 23:24:12 +08:00
|
|
|
return (out);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-03-20 11:08:12 +08:00
|
|
|
/*****************************************
|
|
|
|
* The GiST Penalty method for _intments *
|
|
|
|
*****************************************/
|
|
|
|
|
2001-06-11 10:31:25 +08:00
|
|
|
static float *
|
2001-03-18 05:59:42 +08:00
|
|
|
_int_common_penalty(GISTENTRY *origentry, GISTENTRY *newentry, float *result,
|
2001-03-22 23:24:12 +08:00
|
|
|
formarray unionf,
|
|
|
|
formfloat sizef)
|
2001-03-18 05:59:42 +08:00
|
|
|
{
|
2001-06-01 02:16:55 +08:00
|
|
|
ArrayType *ud;
|
2001-03-22 23:24:12 +08:00
|
|
|
float tmp1,
|
|
|
|
tmp2;
|
|
|
|
|
2001-03-18 05:59:42 +08:00
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "penalty");
|
2001-03-18 05:59:42 +08:00
|
|
|
#endif
|
2001-06-01 02:16:55 +08:00
|
|
|
ud = (*unionf) ((ArrayType *) DatumGetPointer(origentry->key),
|
|
|
|
(ArrayType *) DatumGetPointer(newentry->key));
|
|
|
|
(*sizef) (ud, &tmp1);
|
|
|
|
(*sizef) ((ArrayType *) DatumGetPointer(origentry->key), &tmp2);
|
2001-03-22 23:24:12 +08:00
|
|
|
*result = tmp1 - tmp2;
|
2001-06-01 02:16:55 +08:00
|
|
|
pfree(ud);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "--penalty\t%g", *result);
|
2001-03-18 05:59:42 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
return (result);
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The GiST PickSplit method for _intments
|
2001-03-22 23:24:12 +08:00
|
|
|
** We use Guttman's poly time split algorithm
|
2001-03-18 05:59:42 +08:00
|
|
|
*/
|
2001-06-11 10:31:25 +08:00
|
|
|
static GIST_SPLITVEC *
|
2001-03-18 05:59:42 +08:00
|
|
|
_int_common_picksplit(bytea *entryvec,
|
2001-03-22 23:24:12 +08:00
|
|
|
GIST_SPLITVEC *v,
|
|
|
|
formarray unionf,
|
|
|
|
formarray interf,
|
|
|
|
formfloat sizef,
|
|
|
|
float coef)
|
2001-03-18 05:59:42 +08:00
|
|
|
{
|
2001-03-22 23:24:12 +08:00
|
|
|
OffsetNumber i,
|
|
|
|
j;
|
|
|
|
ArrayType *datum_alpha,
|
|
|
|
*datum_beta;
|
|
|
|
ArrayType *datum_l,
|
|
|
|
*datum_r;
|
|
|
|
ArrayType *union_d,
|
|
|
|
*union_dl,
|
|
|
|
*union_dr;
|
|
|
|
ArrayType *inter_d;
|
|
|
|
bool firsttime;
|
|
|
|
float size_alpha,
|
|
|
|
size_beta,
|
|
|
|
size_union,
|
|
|
|
size_inter;
|
|
|
|
float size_waste,
|
|
|
|
waste;
|
|
|
|
float size_l,
|
|
|
|
size_r;
|
|
|
|
int nbytes;
|
|
|
|
OffsetNumber seed_1 = 0,
|
|
|
|
seed_2 = 0;
|
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
|
|
|
OffsetNumber maxoff;
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "--------picksplit %d", (VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY));
|
2001-03-18 05:59:42 +08:00
|
|
|
#endif
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
maxoff = ((VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY)) - 2;
|
|
|
|
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
|
|
|
v->spl_left = (OffsetNumber *) palloc(nbytes);
|
|
|
|
v->spl_right = (OffsetNumber *) palloc(nbytes);
|
|
|
|
|
|
|
|
firsttime = true;
|
|
|
|
waste = 0.0;
|
|
|
|
|
|
|
|
for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
2001-06-01 02:16:55 +08:00
|
|
|
datum_alpha = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key);
|
2001-03-22 23:24:12 +08:00
|
|
|
for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
2001-06-01 02:16:55 +08:00
|
|
|
datum_beta = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[j].key);
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
/* compute the wasted space by unioning these guys */
|
|
|
|
/* size_waste = size_union - size_inter; */
|
|
|
|
union_d = (*unionf) (datum_alpha, datum_beta);
|
|
|
|
(*sizef) (union_d, &size_union);
|
|
|
|
inter_d = (*interf) (datum_alpha, datum_beta);
|
|
|
|
(*sizef) (inter_d, &size_inter);
|
|
|
|
size_waste = size_union - size_inter;
|
|
|
|
|
|
|
|
pfree(union_d);
|
|
|
|
|
|
|
|
if (inter_d != (ArrayType *) NULL)
|
|
|
|
pfree(inter_d);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* are these a more promising split that what we've already
|
|
|
|
* seen?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (size_waste > waste || firsttime)
|
|
|
|
{
|
|
|
|
waste = size_waste;
|
|
|
|
seed_1 = i;
|
|
|
|
seed_2 = j;
|
|
|
|
firsttime = false;
|
|
|
|
}
|
|
|
|
}
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
left = v->spl_left;
|
|
|
|
v->spl_nleft = 0;
|
|
|
|
right = v->spl_right;
|
|
|
|
v->spl_nright = 0;
|
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
datum_alpha = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[seed_1].key);
|
2001-03-22 23:24:12 +08:00
|
|
|
datum_l = copy_intArrayType(datum_alpha);
|
2001-06-01 02:16:55 +08:00
|
|
|
(*sizef) (datum_l, &size_l);
|
|
|
|
datum_beta = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[seed_2].key);
|
2001-03-22 23:24:12 +08:00
|
|
|
datum_r = copy_intArrayType(datum_beta);
|
2001-06-01 02:16:55 +08:00
|
|
|
(*sizef) (datum_r, &size_r);
|
2001-03-22 23:24:12 +08:00
|
|
|
|
2001-03-18 05:59:42 +08:00
|
|
|
/*
|
2001-03-22 23:24:12 +08:00
|
|
|
* Now split up the regions between the two seeds. An important
|
|
|
|
* property of this split algorithm is that the split vector v has the
|
|
|
|
* indices of items to be split in order in its left and right
|
|
|
|
* vectors. We exploit this property by doing a merge in the code
|
|
|
|
* that actually splits the page.
|
|
|
|
*
|
|
|
|
* For efficiency, we also place the new index tuple in this loop. This
|
|
|
|
* is handled at the very end, when we have placed all the existing
|
|
|
|
* tuples and i == maxoff + 1.
|
2001-03-18 05:59:42 +08:00
|
|
|
*/
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
maxoff = OffsetNumberNext(maxoff);
|
|
|
|
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we've already decided where to place this item, just put it
|
|
|
|
* on the right list. Otherwise, we need to figure out which page
|
|
|
|
* needs the least enlargement in order to store the item.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (i == seed_1)
|
|
|
|
{
|
|
|
|
*left++ = i;
|
|
|
|
v->spl_nleft++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (i == seed_2)
|
|
|
|
{
|
|
|
|
*right++ = i;
|
|
|
|
v->spl_nright++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* okay, which page needs least enlargement? */
|
2001-06-01 02:16:55 +08:00
|
|
|
datum_alpha = (ArrayType *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key);
|
|
|
|
union_dl = (*unionf) (datum_l, datum_alpha);
|
|
|
|
union_dr = (*unionf) (datum_r, datum_alpha);
|
|
|
|
(*sizef) (union_dl, &size_alpha);
|
|
|
|
(*sizef) (union_dr, &size_beta);
|
2001-03-22 23:24:12 +08:00
|
|
|
|
|
|
|
/* pick which page to add it to */
|
|
|
|
if (size_alpha - size_l < size_beta - size_r + WISH_F(v->spl_nleft, v->spl_nright, coef))
|
|
|
|
{
|
|
|
|
if (datum_l)
|
|
|
|
pfree(datum_l);
|
|
|
|
if (union_dr)
|
|
|
|
pfree(union_dr);
|
|
|
|
datum_l = union_dl;
|
|
|
|
size_l = size_alpha;
|
|
|
|
*left++ = i;
|
|
|
|
v->spl_nleft++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (datum_r)
|
|
|
|
pfree(datum_r);
|
|
|
|
if (union_dl)
|
|
|
|
pfree(union_dl);
|
|
|
|
datum_r = union_dr;
|
|
|
|
size_r = size_beta;
|
|
|
|
*right++ = i;
|
|
|
|
v->spl_nright++;
|
|
|
|
}
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
2001-03-22 23:24:12 +08:00
|
|
|
if (*(left - 1) > *(right - 1))
|
|
|
|
{
|
|
|
|
*right = FirstOffsetNumber;
|
|
|
|
*(left - 1) = InvalidOffsetNumber;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*left = FirstOffsetNumber;
|
|
|
|
*(right - 1) = InvalidOffsetNumber;
|
|
|
|
}
|
2001-03-18 05:59:42 +08:00
|
|
|
|
2001-06-01 02:16:55 +08:00
|
|
|
v->spl_ldatum = PointerGetDatum(datum_l);
|
|
|
|
v->spl_rdatum = PointerGetDatum(datum_r);
|
2001-03-18 05:59:42 +08:00
|
|
|
|
|
|
|
#ifdef GIST_DEBUG
|
2001-03-22 23:24:12 +08:00
|
|
|
elog(NOTICE, "--------ENDpicksplit %d %d", v->spl_nleft, v->spl_nright);
|
2001-03-18 05:59:42 +08:00
|
|
|
#endif
|
2001-03-22 23:24:12 +08:00
|
|
|
return v;
|
2001-03-18 05:59:42 +08:00
|
|
|
}
|