postgresql/contrib/intarray/_intbig_gist.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

605 lines
12 KiB
C
Raw Normal View History

/*
2010-09-20 22:08:53 +02:00
* contrib/intarray/_intbig_gist.c
*/
#include "postgres.h"
#include "access/gist.h"
#include "access/stratnum.h"
2003-06-11 21:31:05 +02:00
#include "_int.h"
#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);
/* 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
_intbig_in(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("_intbig_in() not implemented")));
2003-06-11 21:31:05 +02:00
PG_RETURN_DATUM(0);
}
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
Datum
_intbig_out(PG_FUNCTION_ARGS)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("_intbig_out() not implemented")));
2003-06-11 21:31:05 +02:00
PG_RETURN_DATUM(0);
}
/*********************************************************************
** intbig functions
*********************************************************************/
static bool
_intbig_overlap(GISTTYPE *a, ArrayType *b)
{
int num = ARRNELEMS(b);
int32 *ptr = ARRPTR(b);
2003-06-11 21:31:05 +02:00
CHECKARRVALID(b);
2003-06-11 21:31:05 +02:00
while (num--)
{
if (GETBIT(GETSIGN(a), HASHVAL(*ptr)))
return true;
ptr++;
}
return false;
}
static bool
_intbig_contains(GISTTYPE *a, ArrayType *b)
{
int num = ARRNELEMS(b);
int32 *ptr = ARRPTR(b);
2003-06-11 21:31:05 +02:00
CHECKARRVALID(b);
2003-06-11 21:31:05 +02:00
while (num--)
{
if (!GETBIT(GETSIGN(a), HASHVAL(*ptr)))
return false;
ptr++;
}
return true;
}
Datum
g_intbig_same(PG_FUNCTION_ARGS)
{
GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
if (ISALLTRUE(a) && ISALLTRUE(b))
*result = true;
else if (ISALLTRUE(a))
*result = false;
else if (ISALLTRUE(b))
*result = false;
else
{
int32 i;
2003-06-11 21:31:05 +02:00
BITVECP sa = GETSIGN(a),
sb = GETSIGN(b);
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
*result = true;
LOOPBYTE
{
if (sa[i] != sb[i])
{
*result = false;
break;
}
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);
if (entry->leafkey)
{
GISTENTRY *retval;
ArrayType *in = DatumGetArrayTypeP(entry->key);
int32 *ptr;
2003-06-11 21:31:05 +02:00
int num;
GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
2003-06-11 21:31:05 +02:00
CHECKARRVALID(in);
if (ARRISEMPTY(in))
{
ptr = NULL;
num = 0;
}
else
{
ptr = ARRPTR(in);
num = ARRNELEMS(in);
}
SET_VARSIZE(res, CALCGTSIZE(0));
2003-06-11 21:31:05 +02:00
while (num--)
{
HASH(GETSIGN(res), *ptr);
ptr++;
}
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(res),
entry->rel, entry->page,
entry->offset, false);
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
PG_RETURN_POINTER(retval);
}
else if (!ISALLTRUE(DatumGetPointer(entry->key)))
{
GISTENTRY *retval;
int i;
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
GISTTYPE *res;
LOOPBYTE
{
if ((sign[i] & 0xff) != 0xff)
PG_RETURN_POINTER(entry);
}
2003-06-11 21:31:05 +02:00
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
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),
entry->rel, entry->page,
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);
}
static int32
2003-06-11 21:31:05 +02:00
sizebitvec(BITVECP sign)
{
int32 size = 0,
2003-06-11 21:31:05 +02:00
i;
2004-08-29 07:07:03 +02:00
LOOPBYTE
size += number_of_ones[(unsigned char) sign[i]];
2003-06-11 21:31:05 +02:00
return size;
}
static int
hemdistsign(BITVECP a, BITVECP b)
{
int i,
diff,
2003-06-11 21:31:05 +02:00
dist = 0;
2004-08-29 07:07:03 +02:00
LOOPBYTE
{
diff = (unsigned char) (a[i] ^ b[i]);
dist += number_of_ones[diff];
}
2003-06-11 21:31:05 +02:00
return dist;
}
static int
hemdist(GISTTYPE *a, GISTTYPE *b)
{
if (ISALLTRUE(a))
2004-08-29 07:07:03 +02:00
{
2003-06-11 21:31:05 +02:00
if (ISALLTRUE(b))
return 0;
else
return SIGLENBIT - sizebitvec(GETSIGN(b));
}
else if (ISALLTRUE(b))
return SIGLENBIT - sizebitvec(GETSIGN(a));
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
return hemdistsign(GETSIGN(a), GETSIGN(b));
}
Datum
g_intbig_decompress(PG_FUNCTION_ARGS)
{
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
}
static int32
2003-06-11 21:31:05 +02:00
unionkey(BITVECP sbase, GISTTYPE *add)
{
int32 i;
2003-06-11 21:31:05 +02:00
BITVECP sadd = GETSIGN(add);
if (ISALLTRUE(add))
return 1;
LOOPBYTE
sbase[i] |= sadd[i];
2003-06-11 21:31:05 +02:00
return 0;
}
Datum
g_intbig_union(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
2003-06-11 21:31:05 +02:00
int *size = (int *) PG_GETARG_POINTER(1);
BITVEC base;
int32 i,
len;
int32 flag = 0;
2003-06-11 21:31:05 +02:00
GISTTYPE *result;
MemSet((void *) base, 0, sizeof(BITVEC));
for (i = 0; i < entryvec->n; i++)
{
2003-06-11 21:31:05 +02:00
if (unionkey(base, GETENTRY(entryvec, i)))
{
flag = ALLISTRUE;
break;
}
}
len = CALCGTSIZE(flag);
result = (GISTTYPE *) palloc(len);
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));
*size = len;
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
PG_RETURN_POINTER(result);
}
Datum
g_intbig_penalty(PG_FUNCTION_ARGS)
{
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
float *penalty = (float *) PG_GETARG_POINTER(2);
GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
*penalty = hemdist(origval, newval);
PG_RETURN_POINTER(penalty);
}
typedef struct
{
OffsetNumber pos;
int32 cost;
} SPLITCOST;
2003-06-11 21:31:05 +02:00
static int
comparecost(const void *a, const void *b)
{
return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
2003-06-11 21:31:05 +02:00
}
Datum
g_intbig_picksplit(PG_FUNCTION_ARGS)
{
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
2003-06-11 21:31:05 +02:00
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber k,
j;
GISTTYPE *datum_l,
*datum_r;
BITVECP union_l,
union_r;
int32 size_alpha,
2003-06-11 21:31:05 +02:00
size_beta;
int32 size_waste,
2003-06-11 21:31:05 +02:00
waste = -1;
int32 nbytes;
2003-06-11 21:31:05 +02:00
OffsetNumber seed_1 = 0,
seed_2 = 0;
OffsetNumber *left,
*right;
OffsetNumber maxoff;
BITVECP ptr;
int i;
SPLITCOST *costvector;
GISTTYPE *_k,
*_j;
2004-08-29 07:07:03 +02:00
maxoff = entryvec->n - 2;
2003-06-11 21:31:05 +02:00
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes);
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
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;
}
2004-08-29 07:07:03 +02:00
}
}
2003-06-11 21:31:05 +02:00
left = v->spl_left;
v->spl_nleft = 0;
right = v->spl_right;
v->spl_nright = 0;
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
if (seed_1 == 0 || seed_2 == 0)
{
seed_1 = 1;
seed_2 = 2;
}
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
/* form initial .. */
if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
SET_VARSIZE(datum_l, GTHDRSIZE);
2003-06-11 21:31:05 +02:00
datum_l->flag = ALLISTRUE;
}
else
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
SET_VARSIZE(datum_l, GTHDRSIZE + SIGLEN);
2003-06-11 21:31:05 +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);
SET_VARSIZE(datum_r, GTHDRSIZE);
2003-06-11 21:31:05 +02:00
datum_r->flag = ALLISTRUE;
}
else
{
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
SET_VARSIZE(datum_r, GTHDRSIZE + SIGLEN);
2003-06-11 21:31:05 +02:00
datum_r->flag = 0;
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
}
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
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);
costvector[j - 1].cost = Abs(size_alpha - size_beta);
2003-06-11 21:31:05 +02:00
}
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
union_l = GETSIGN(datum_l);
union_r = GETSIGN(datum_r);
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
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);
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
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);
LOOPBYTE
union_l[i] |= ptr[i];
2003-06-11 21:31:05 +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);
LOOPBYTE
union_r[i] |= ptr[i];
2003-06-11 21:31:05 +02:00
}
*right++ = j;
v->spl_nright++;
}
2004-08-29 07:07:03 +02:00
}
2003-06-11 21:31:05 +02:00
*right = *left = FirstOffsetNumber;
pfree(costvector);
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
v->spl_ldatum = PointerGetDatum(datum_l);
v->spl_rdatum = PointerGetDatum(datum_r);
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
PG_RETURN_POINTER(v);
}
Datum
g_intbig_consistent(PG_FUNCTION_ARGS)
{
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
2003-06-11 21:31:05 +02:00
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
/* Oid subtype = PG_GETARG_OID(3); */
bool *recheck = (bool *) PG_GETARG_POINTER(4);
2003-06-11 21:31:05 +02:00
bool retval;
/* All cases served by this function are inexact */
*recheck = true;
2003-06-11 21:31:05 +02:00
if (ISALLTRUE(DatumGetPointer(entry->key)))
PG_RETURN_BOOL(true);
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
if (strategy == BooleanSearchStrategy)
{
retval = signconsistent((QUERYTYPE *) query,
2003-06-11 21:31:05 +02:00
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
}
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:
if (GIST_LEAF(entry))
{
int i,
num = ARRNELEMS(query);
int32 *ptr = ARRPTR(query);
2003-06-11 21:31:05 +02:00
BITVEC qp;
BITVECP dq,
de;
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
memset(qp, 0, sizeof(BITVEC));
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
while (num--)
{
HASH(qp, *ptr);
ptr++;
}
de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
dq = qp;
retval = true;
LOOPBYTE
{
if (de[i] != dq[i])
{
retval = false;
break;
}
2003-06-11 21:31:05 +02:00
}
}
else
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key), query);
break;
case RTContainsStrategyNumber:
case RTOldContainsStrategyNumber:
2003-06-11 21:31:05 +02:00
retval = _intbig_contains((GISTTYPE *) DatumGetPointer(entry->key), query);
break;
case RTContainedByStrategyNumber:
case RTOldContainedByStrategyNumber:
2003-06-11 21:31:05 +02:00
if (GIST_LEAF(entry))
{
int i,
num = ARRNELEMS(query);
int32 *ptr = ARRPTR(query);
2003-06-11 21:31:05 +02:00
BITVEC qp;
BITVECP dq,
de;
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
memset(qp, 0, sizeof(BITVEC));
2004-08-29 07:07:03 +02:00
2003-06-11 21:31:05 +02:00
while (num--)
{
HASH(qp, *ptr);
ptr++;
}
de = GETSIGN((GISTTYPE *) DatumGetPointer(entry->key));
dq = qp;
retval = true;
LOOPBYTE
{
if (de[i] & ~dq[i])
{
retval = false;
break;
}
2003-06-11 21:31:05 +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:
retval = false;
2003-06-11 21:31:05 +02:00
}
PG_FREE_IF_COPY(query, 1);
2003-06-11 21:31:05 +02:00
PG_RETURN_BOOL(retval);
}