2008-05-17 03:28:26 +02:00
|
|
|
/*
|
2010-09-20 22:08:53 +02:00
|
|
|
* contrib/intarray/_intbig_gist.c
|
2008-05-17 03:28:26 +02:00
|
|
|
*/
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
2015-05-15 22:03:16 +02:00
|
|
|
#include "access/stratnum.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
#include "_int.h"
|
|
|
|
|
2004-03-30 17:45:33 +02:00
|
|
|
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
|
2003-06-11 21:31:05 +02:00
|
|
|
/*
|
|
|
|
** _intbig methods
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
2006-01-20 23:46:16 +01:00
|
|
|
/* Number of one-bits in an unsigned byte */
|
|
|
|
static const uint8 number_of_ones[256] = {
|
|
|
|
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
|
|
|
|
};
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(_intbig_in);
|
|
|
|
PG_FUNCTION_INFO_V1(_intbig_out);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
Datum
|
2004-08-29 07:07:03 +02:00
|
|
|
_intbig_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("_intbig_in() not implemented")));
|
|
|
|
PG_RETURN_DATUM(0);
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
Datum
|
2004-08-29 07:07:03 +02:00
|
|
|
_intbig_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("_intbig_out() not implemented")));
|
|
|
|
PG_RETURN_DATUM(0);
|
|
|
|
}
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
** intbig functions
|
|
|
|
*********************************************************************/
|
|
|
|
static bool
|
2009-06-11 16:49:15 +02:00
|
|
|
_intbig_overlap(GISTTYPE *a, ArrayType *b)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int num = ARRNELEMS(b);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *ptr = ARRPTR(b);
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2005-11-19 04:00:09 +01:00
|
|
|
CHECKARRVALID(b);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
while (num--)
|
|
|
|
{
|
|
|
|
if (GETBIT(GETSIGN(a), HASHVAL(*ptr)))
|
2003-06-11 21:31:05 +02:00
|
|
|
return true;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2009-06-11 16:49:15 +02:00
|
|
|
_intbig_contains(GISTTYPE *a, ArrayType *b)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int num = ARRNELEMS(b);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *ptr = ARRPTR(b);
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2005-11-19 04:00:09 +01:00
|
|
|
CHECKARRVALID(b);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
while (num--)
|
|
|
|
{
|
|
|
|
if (!GETBIT(GETSIGN(a), HASHVAL(*ptr)))
|
2003-06-11 21:31:05 +02:00
|
|
|
return false;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2004-08-29 07:07:03 +02:00
|
|
|
g_intbig_same(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-06-11 21:31:05 +02:00
|
|
|
GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
|
|
|
|
GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
|
2004-08-29 07:07:03 +02:00
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
if (ISALLTRUE(a) && ISALLTRUE(b))
|
|
|
|
*result = true;
|
|
|
|
else if (ISALLTRUE(a))
|
|
|
|
*result = false;
|
|
|
|
else if (ISALLTRUE(b))
|
|
|
|
*result = false;
|
2004-08-29 07:07:03 +02:00
|
|
|
else
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 i;
|
2004-08-29 07:07:03 +02:00
|
|
|
BITVECP sa = GETSIGN(a),
|
|
|
|
sb = GETSIGN(b);
|
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
*result = true;
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
if (sa[i] != sb[i])
|
|
|
|
{
|
|
|
|
*result = false;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
}
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
g_intbig_compress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (entry->leafkey)
|
|
|
|
{
|
2003-06-11 21:31:05 +02:00
|
|
|
GISTENTRY *retval;
|
2011-01-09 06:39:21 +01:00
|
|
|
ArrayType *in = DatumGetArrayTypeP(entry->key);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *ptr;
|
2004-08-29 07:07:03 +02:00
|
|
|
int num;
|
2007-02-28 23:44:38 +01:00
|
|
|
GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2005-11-19 04:00:09 +01:00
|
|
|
CHECKARRVALID(in);
|
2011-01-09 06:39:21 +01:00
|
|
|
if (ARRISEMPTY(in))
|
2005-11-19 04:00:09 +01:00
|
|
|
{
|
|
|
|
ptr = NULL;
|
|
|
|
num = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = ARRPTR(in);
|
|
|
|
num = ARRNELEMS(in);
|
|
|
|
}
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(res, CALCGTSIZE(0));
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
while (num--)
|
|
|
|
{
|
|
|
|
HASH(GETSIGN(res), *ptr);
|
2003-06-11 21:31:05 +02:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
2004-08-29 07:07:03 +02:00
|
|
|
entry->rel, entry->page,
|
2017-08-16 06:22:32 +02:00
|
|
|
entry->offset, false);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2011-01-09 06:39:21 +01:00
|
|
|
if (in != DatumGetArrayTypeP(entry->key))
|
2003-06-11 21:31:05 +02:00
|
|
|
pfree(in);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(retval);
|
2004-08-29 07:07:03 +02:00
|
|
|
}
|
|
|
|
else if (!ISALLTRUE(DatumGetPointer(entry->key)))
|
|
|
|
{
|
2003-06-11 21:31:05 +02:00
|
|
|
GISTENTRY *retval;
|
2004-08-29 07:07:03 +02:00
|
|
|
int i;
|
|
|
|
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
|
2003-06-11 21:31:05 +02:00
|
|
|
GISTTYPE *res;
|
|
|
|
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
if ((sign[i] & 0xff) != 0xff)
|
|
|
|
PG_RETURN_POINTER(entry);
|
|
|
|
}
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(res, CALCGTSIZE(ALLISTRUE));
|
2003-06-11 21:31:05 +02:00
|
|
|
res->flag = ALLISTRUE;
|
|
|
|
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
2004-08-29 07:07:03 +02:00
|
|
|
entry->rel, entry->page,
|
2017-08-16 06:22:32 +02:00
|
|
|
entry->offset, false);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
PG_RETURN_POINTER(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
static int32
|
2004-08-29 07:07:03 +02:00
|
|
|
sizebitvec(BITVECP sign)
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 size = 0,
|
2004-08-29 07:07:03 +02:00
|
|
|
i;
|
|
|
|
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
size += number_of_ones[(unsigned char) sign[i]];
|
2004-08-29 07:07:03 +02:00
|
|
|
return size;
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-08-29 07:07:03 +02:00
|
|
|
hemdistsign(BITVECP a, BITVECP b)
|
|
|
|
{
|
|
|
|
int i,
|
2006-01-20 23:46:16 +01:00
|
|
|
diff,
|
2004-08-29 07:07:03 +02:00
|
|
|
dist = 0;
|
|
|
|
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
diff = (unsigned char) (a[i] ^ b[i]);
|
|
|
|
dist += number_of_ones[diff];
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
return dist;
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-06-11 16:49:15 +02:00
|
|
|
hemdist(GISTTYPE *a, GISTTYPE *b)
|
2004-08-29 07:07:03 +02:00
|
|
|
{
|
|
|
|
if (ISALLTRUE(a))
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(b))
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return SIGLENBIT - sizebitvec(GETSIGN(b));
|
|
|
|
}
|
|
|
|
else if (ISALLTRUE(b))
|
|
|
|
return SIGLENBIT - sizebitvec(GETSIGN(a));
|
|
|
|
|
|
|
|
return hemdistsign(GETSIGN(a), GETSIGN(b));
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
g_intbig_decompress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
|
|
|
|
}
|
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
static int32
|
2009-06-11 16:49:15 +02:00
|
|
|
unionkey(BITVECP sbase, GISTTYPE *add)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 i;
|
2004-08-29 07:07:03 +02:00
|
|
|
BITVECP sadd = GETSIGN(add);
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
if (ISALLTRUE(add))
|
|
|
|
return 1;
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
sbase[i] |= sadd[i];
|
2003-06-11 21:31:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2004-08-29 07:07:03 +02:00
|
|
|
g_intbig_union(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
int *size = (int *) PG_GETARG_POINTER(1);
|
|
|
|
BITVEC base;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 i,
|
2004-08-29 07:07:03 +02:00
|
|
|
len;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 flag = 0;
|
2003-06-11 21:31:05 +02:00
|
|
|
GISTTYPE *result;
|
|
|
|
|
|
|
|
MemSet((void *) base, 0, sizeof(BITVEC));
|
2004-08-29 07:07:03 +02:00
|
|
|
for (i = 0; i < entryvec->n; i++)
|
|
|
|
{
|
|
|
|
if (unionkey(base, GETENTRY(entryvec, i)))
|
|
|
|
{
|
2003-06-11 21:31:05 +02:00
|
|
|
flag = ALLISTRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = CALCGTSIZE(flag);
|
|
|
|
result = (GISTTYPE *) palloc(len);
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(result, len);
|
2003-06-11 21:31:05 +02:00
|
|
|
result->flag = flag;
|
|
|
|
if (!ISALLTRUE(result))
|
|
|
|
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
|
2007-02-28 23:44:38 +01:00
|
|
|
*size = len;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2004-08-29 07:07:03 +02:00
|
|
|
g_intbig_penalty(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-06-11 21:31:05 +02:00
|
|
|
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
|
|
|
|
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
|
2004-08-29 07:07:03 +02:00
|
|
|
float *penalty = (float *) PG_GETARG_POINTER(2);
|
2003-06-11 21:31:05 +02:00
|
|
|
GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
|
|
|
|
GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
*penalty = hemdist(origval, newval);
|
2003-06-11 21:31:05 +02:00
|
|
|
PG_RETURN_POINTER(penalty);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2003-06-11 21:31:05 +02:00
|
|
|
OffsetNumber pos;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 cost;
|
2007-11-16 02:12:24 +01:00
|
|
|
} SPLITCOST;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
static int
|
2004-08-29 07:07:03 +02:00
|
|
|
comparecost(const void *a, const void *b)
|
|
|
|
{
|
2012-02-28 11:42:08 +01:00
|
|
|
return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2004-08-29 07:07:03 +02:00
|
|
|
g_intbig_picksplit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
|
|
|
|
OffsetNumber k,
|
|
|
|
j;
|
|
|
|
GISTTYPE *datum_l,
|
|
|
|
*datum_r;
|
|
|
|
BITVECP union_l,
|
|
|
|
union_r;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 size_alpha,
|
2004-08-29 07:07:03 +02:00
|
|
|
size_beta;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 size_waste,
|
2004-08-29 07:07:03 +02:00
|
|
|
waste = -1;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 nbytes;
|
2004-08-29 07:07:03 +02:00
|
|
|
OffsetNumber seed_1 = 0,
|
|
|
|
seed_2 = 0;
|
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
|
|
|
OffsetNumber maxoff;
|
|
|
|
BITVECP ptr;
|
|
|
|
int i;
|
|
|
|
SPLITCOST *costvector;
|
|
|
|
GISTTYPE *_k,
|
|
|
|
*_j;
|
|
|
|
|
|
|
|
maxoff = entryvec->n - 2;
|
|
|
|
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
|
|
|
v->spl_left = (OffsetNumber *) palloc(nbytes);
|
|
|
|
v->spl_right = (OffsetNumber *) palloc(nbytes);
|
|
|
|
|
|
|
|
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
|
|
|
|
{
|
|
|
|
_k = GETENTRY(entryvec, k);
|
|
|
|
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
|
|
|
size_waste = hemdist(_k, GETENTRY(entryvec, j));
|
|
|
|
if (size_waste > waste)
|
|
|
|
{
|
|
|
|
waste = size_waste;
|
|
|
|
seed_1 = k;
|
|
|
|
seed_2 = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
left = v->spl_left;
|
|
|
|
v->spl_nleft = 0;
|
|
|
|
right = v->spl_right;
|
|
|
|
v->spl_nright = 0;
|
|
|
|
|
|
|
|
if (seed_1 == 0 || seed_2 == 0)
|
|
|
|
{
|
|
|
|
seed_1 = 1;
|
|
|
|
seed_2 = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* form initial .. */
|
|
|
|
if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
|
|
|
|
{
|
|
|
|
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(datum_l, GTHDRSIZE);
|
2004-08-29 07:07:03 +02:00
|
|
|
datum_l->flag = ALLISTRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(datum_l, GTHDRSIZE + SIGLEN);
|
2004-08-29 07:07:03 +02:00
|
|
|
datum_l->flag = 0;
|
|
|
|
memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
if (ISALLTRUE(GETENTRY(entryvec, seed_2)))
|
|
|
|
{
|
|
|
|
datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(datum_r, GTHDRSIZE);
|
2004-08-29 07:07:03 +02:00
|
|
|
datum_r->flag = ALLISTRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(datum_r, GTHDRSIZE + SIGLEN);
|
2004-08-29 07:07:03 +02:00
|
|
|
datum_r->flag = 0;
|
|
|
|
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
|
|
|
|
maxoff = OffsetNumberNext(maxoff);
|
|
|
|
/* sort before ... */
|
|
|
|
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
|
|
|
|
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
|
|
|
costvector[j - 1].pos = j;
|
|
|
|
_j = GETENTRY(entryvec, j);
|
|
|
|
size_alpha = hemdist(datum_l, _j);
|
|
|
|
size_beta = hemdist(datum_r, _j);
|
2004-10-21 21:28:36 +02:00
|
|
|
costvector[j - 1].cost = Abs(size_alpha - size_beta);
|
2004-08-29 07:07:03 +02:00
|
|
|
}
|
|
|
|
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
|
|
|
|
|
|
|
|
union_l = GETSIGN(datum_l);
|
|
|
|
union_r = GETSIGN(datum_r);
|
|
|
|
|
|
|
|
for (k = 0; k < maxoff; k++)
|
|
|
|
{
|
|
|
|
j = costvector[k].pos;
|
|
|
|
if (j == seed_1)
|
|
|
|
{
|
|
|
|
*left++ = j;
|
|
|
|
v->spl_nleft++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (j == seed_2)
|
|
|
|
{
|
|
|
|
*right++ = j;
|
|
|
|
v->spl_nright++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_j = GETENTRY(entryvec, j);
|
|
|
|
size_alpha = hemdist(datum_l, _j);
|
|
|
|
size_beta = hemdist(datum_r, _j);
|
|
|
|
|
|
|
|
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.00001))
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_l) || ISALLTRUE(_j))
|
|
|
|
{
|
|
|
|
if (!ISALLTRUE(datum_l))
|
|
|
|
MemSet((void *) union_l, 0xff, sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = GETSIGN(_j);
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
union_l[i] |= ptr[i];
|
2004-08-29 07:07:03 +02:00
|
|
|
}
|
|
|
|
*left++ = j;
|
|
|
|
v->spl_nleft++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_r) || ISALLTRUE(_j))
|
|
|
|
{
|
|
|
|
if (!ISALLTRUE(datum_r))
|
|
|
|
MemSet((void *) union_r, 0xff, sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = GETSIGN(_j);
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
union_r[i] |= ptr[i];
|
2004-08-29 07:07:03 +02:00
|
|
|
}
|
|
|
|
*right++ = j;
|
|
|
|
v->spl_nright++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*right = *left = FirstOffsetNumber;
|
|
|
|
pfree(costvector);
|
|
|
|
|
|
|
|
v->spl_ldatum = PointerGetDatum(datum_l);
|
|
|
|
v->spl_rdatum = PointerGetDatum(datum_r);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(v);
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
g_intbig_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
2011-01-09 06:39:21 +01:00
|
|
|
ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
|
2003-06-11 21:31:05 +02:00
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2008-04-14 19:05:34 +02:00
|
|
|
/* Oid subtype = PG_GETARG_OID(3); */
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(4);
|
2003-06-11 21:31:05 +02:00
|
|
|
bool retval;
|
|
|
|
|
2008-04-14 19:05:34 +02:00
|
|
|
/* All cases served by this function are inexact */
|
|
|
|
*recheck = true;
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (ISALLTRUE(DatumGetPointer(entry->key)))
|
2003-06-11 21:31:05 +02:00
|
|
|
PG_RETURN_BOOL(true);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
|
|
|
if (strategy == BooleanSearchStrategy)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
retval = signconsistent((QUERYTYPE *) query,
|
|
|
|
GETSIGN(DatumGetPointer(entry->key)),
|
|
|
|
false);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2006-04-03 10:37:41 +02:00
|
|
|
PG_RETURN_BOOL(retval);
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
2005-11-19 04:00:09 +01:00
|
|
|
CHECKARRVALID(query);
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case RTOverlapStrategyNumber:
|
|
|
|
retval = _intbig_overlap((GISTTYPE *) DatumGetPointer(entry->key), query);
|
|
|
|
break;
|
|
|
|
case RTSameStrategyNumber:
|
2004-08-29 07:07:03 +02:00
|
|
|
if (GIST_LEAF(entry))
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
num = ARRNELEMS(query);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *ptr = ARRPTR(query);
|
2004-08-29 07:07:03 +02:00
|
|
|
BITVEC qp;
|
|
|
|
BITVECP dq,
|
|
|
|
de;
|
|
|
|
|
|
|
|
memset(qp, 0, sizeof(BITVEC));
|
|
|
|
|
|
|
|
while (num--)
|
|
|
|
{
|
2003-06-11 21:31:05 +02:00
|
|
|
HASH(qp, *ptr);
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
|
|
|
|
dq = qp;
|
|
|
|
retval = true;
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
if (de[i] != dq[i])
|
|
|
|
{
|
|
|
|
retval = false;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
}
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
}
|
|
|
|
else
|
2003-06-11 21:31:05 +02:00
|
|
|
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key), query);
|
|
|
|
break;
|
|
|
|
case RTContainsStrategyNumber:
|
2006-09-10 19:36:52 +02:00
|
|
|
case RTOldContainsStrategyNumber:
|
2003-06-11 21:31:05 +02:00
|
|
|
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key), query);
|
|
|
|
break;
|
|
|
|
case RTContainedByStrategyNumber:
|
2006-09-10 19:36:52 +02:00
|
|
|
case RTOldContainedByStrategyNumber:
|
2004-08-29 07:07:03 +02:00
|
|
|
if (GIST_LEAF(entry))
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
num = ARRNELEMS(query);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *ptr = ARRPTR(query);
|
2004-08-29 07:07:03 +02:00
|
|
|
BITVEC qp;
|
|
|
|
BITVECP dq,
|
|
|
|
de;
|
|
|
|
|
|
|
|
memset(qp, 0, sizeof(BITVEC));
|
|
|
|
|
|
|
|
while (num--)
|
|
|
|
{
|
2003-06-11 21:31:05 +02:00
|
|
|
HASH(qp, *ptr);
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
|
|
|
|
dq = qp;
|
|
|
|
retval = true;
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
if (de[i] & ~dq[i])
|
|
|
|
{
|
|
|
|
retval = false;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
Fix intarray's GiST opclasses to not fail for empty arrays with <@.
contrib/intarray considers "arraycol <@ constant-array" to be indexable,
but its GiST opclass code fails to reliably find index entries for empty
array values (which of course should trivially match such queries).
This is because the test condition to see whether we should descend
through a non-leaf node is wrong.
Unfortunately, empty array entries could be anywhere in the index,
as these index opclasses are currently designed. So there's no way
to fix this except by lobotomizing <@ indexscans to scan the whole
index ... which is what this patch does. That's pretty unfortunate:
the performance is now actually worse than a seqscan, in most cases.
We'd be better off to remove <@ from the GiST opclasses entirely,
and perhaps a future non-back-patchable patch will do so.
In the meantime, applications whose performance is adversely impacted
have a couple of options. They could switch to a GIN index, which
doesn't have this bug, or they could replace "arraycol <@ constant-array"
with "arraycol <@ constant-array AND arraycol && constant-array".
That will provide about the same performance as before, and it will find
all non-empty subsets of the given constant-array, which is all that
could reliably be expected of the query before.
While at it, add some more regression test cases to improve code
coverage of contrib/intarray.
In passing, adjust resize_intArrayType so that when it's returning an
empty array, it uses construct_empty_array for that rather than
cowboy hacking on the input array. While the hack produces an array
that looks valid for most purposes, it isn't bitwise equal to empty
arrays produced by other code paths, which could have subtle odd
effects. I don't think this code path is performance-critical
enough to justify such shortcuts. (Back-patch this part only as far
as v11; before commit 01783ac36 we were not careful about this in
other intarray code paths either.)
Back-patch the <@ fixes to all supported versions, since this was
broken from day one.
Patch by me; thanks to Alexander Korotkov for review.
Discussion: https://postgr.es/m/458.1565114141@sss.pgh.pa.us
2019-08-07 00:04:51 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Unfortunately, because empty arrays could be anywhere in
|
|
|
|
* the index, we must search the whole tree.
|
|
|
|
*/
|
|
|
|
retval = true;
|
|
|
|
}
|
2003-06-11 21:31:05 +02:00
|
|
|
break;
|
|
|
|
default:
|
2017-08-16 06:22:32 +02:00
|
|
|
retval = false;
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2003-06-11 21:31:05 +02:00
|
|
|
PG_RETURN_BOOL(retval);
|
|
|
|
}
|