2007-08-21 03:11:32 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* tsgistidx.c
|
|
|
|
* GiST support functions for tsvector_ops
|
|
|
|
*
|
2017-01-03 19:48:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
2007-08-21 03:11:32 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/tsgistidx.c
|
2007-08-21 03:11:32 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/tuptoaster.h"
|
|
|
|
#include "tsearch/ts_utils.h"
|
2016-12-28 18:00:00 +01:00
|
|
|
#include "utils/builtins.h"
|
2015-04-14 16:03:42 +02:00
|
|
|
#include "utils/pg_crc.h"
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define SIGLENINT 31 /* >121 => key will toast, so it will not work
|
|
|
|
* !!! */
|
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
#define SIGLEN ( sizeof(int32) * SIGLENINT )
|
2007-08-21 03:11:32 +02:00
|
|
|
#define SIGLENBIT (SIGLEN * BITS_PER_BYTE)
|
|
|
|
|
|
|
|
typedef char BITVEC[SIGLEN];
|
|
|
|
typedef char *BITVECP;
|
|
|
|
|
2007-11-16 01:13:02 +01:00
|
|
|
#define LOOPBYTE \
|
|
|
|
for(i=0;i<SIGLEN;i++)
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITS_PER_BYTE ) ) )
|
|
|
|
#define GETBITBYTE(x,i) ( ((char)(x)) >> (i) & 0x01 )
|
|
|
|
#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITS_PER_BYTE ) )
|
|
|
|
#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITS_PER_BYTE ) )
|
|
|
|
#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITS_PER_BYTE )) & 0x01 )
|
|
|
|
|
|
|
|
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
|
|
|
|
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
|
|
|
|
|
|
|
|
#define GETENTRY(vec,pos) ((SignTSVector *) DatumGetPointer((vec)->vector[(pos)].key))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* type of GiST index key
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2007-11-16 02:12:24 +01:00
|
|
|
int32 vl_len_; /* varlena header (do not touch directly!) */
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 flag;
|
2015-02-21 22:12:14 +01:00
|
|
|
char data[FLEXIBLE_ARRAY_MEMBER];
|
2007-08-21 03:11:32 +02:00
|
|
|
} SignTSVector;
|
|
|
|
|
|
|
|
#define ARRKEY 0x01
|
|
|
|
#define SIGNKEY 0x02
|
|
|
|
#define ALLISTRUE 0x04
|
|
|
|
|
|
|
|
#define ISARRKEY(x) ( ((SignTSVector*)(x))->flag & ARRKEY )
|
|
|
|
#define ISSIGNKEY(x) ( ((SignTSVector*)(x))->flag & SIGNKEY )
|
|
|
|
#define ISALLTRUE(x) ( ((SignTSVector*)(x))->flag & ALLISTRUE )
|
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
#define GTHDRSIZE ( VARHDRSZ + sizeof(int32) )
|
|
|
|
#define CALCGTSIZE(flag, len) ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int32)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
#define GETSIGN(x) ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )
|
2012-06-25 00:51:46 +02:00
|
|
|
#define GETARR(x) ( (int32*)( (char*)(x)+GTHDRSIZE ) )
|
|
|
|
#define ARRNELEM(x) ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int32) )
|
2007-08-21 03:11:32 +02: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
|
|
|
|
};
|
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
static int32 sizebitvec(BITVECP sign);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
Datum
|
|
|
|
gtsvectorin(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("gtsvector_in not implemented")));
|
|
|
|
PG_RETURN_DATUM(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SINGOUTSTR "%d true bits, %d false bits"
|
|
|
|
#define ARROUTSTR "%d unique words"
|
|
|
|
#define EXTRALEN ( 2*13 )
|
|
|
|
|
|
|
|
static int outbuf_maxlen = 0;
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtsvectorout(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(PG_GETARG_POINTER(0));
|
2007-08-21 03:11:32 +02:00
|
|
|
char *outbuf;
|
|
|
|
|
|
|
|
if (outbuf_maxlen == 0)
|
|
|
|
outbuf_maxlen = 2 * EXTRALEN + Max(strlen(SINGOUTSTR), strlen(ARROUTSTR)) + 1;
|
|
|
|
outbuf = palloc(outbuf_maxlen);
|
|
|
|
|
|
|
|
if (ISARRKEY(key))
|
|
|
|
sprintf(outbuf, ARROUTSTR, (int) ARRNELEM(key));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int cnttrue = (ISALLTRUE(key)) ? SIGLENBIT : sizebitvec(GETSIGN(key));
|
|
|
|
|
|
|
|
sprintf(outbuf, SINGOUTSTR, cnttrue, (int) SIGLENBIT - cnttrue);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(key, 0);
|
|
|
|
PG_RETURN_POINTER(outbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-09-11 10:46:29 +02:00
|
|
|
compareint(const void *va, const void *vb)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 a = *((const int32 *) va);
|
|
|
|
int32 b = *((const int32 *) vb);
|
2007-09-11 10:46:29 +02:00
|
|
|
|
|
|
|
if (a == b)
|
2007-08-21 03:11:32 +02:00
|
|
|
return 0;
|
2007-09-11 10:46:29 +02:00
|
|
|
return (a > b) ? 1 : -1;
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
2007-09-11 10:46:29 +02:00
|
|
|
/*
|
2012-06-25 00:51:46 +02:00
|
|
|
* Removes duplicates from an array of int32. 'l' is
|
2007-09-11 10:46:29 +02:00
|
|
|
* size of the input array. Returns the new size of the array.
|
|
|
|
*/
|
2007-08-21 03:11:32 +02:00
|
|
|
static int
|
2012-06-25 00:51:46 +02:00
|
|
|
uniqueint(int32 *a, int32 l)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *ptr,
|
2007-08-21 03:11:32 +02:00
|
|
|
*res;
|
|
|
|
|
2007-09-11 10:46:29 +02:00
|
|
|
if (l <= 1)
|
2007-08-21 03:11:32 +02:00
|
|
|
return l;
|
|
|
|
|
|
|
|
ptr = res = a;
|
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
qsort((void *) a, l, sizeof(int32), compareint);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
while (ptr - a < l)
|
|
|
|
if (*ptr != *res)
|
|
|
|
*(++res) = *ptr++;
|
|
|
|
else
|
|
|
|
ptr++;
|
|
|
|
return res + 1 - a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-16 02:12:24 +01:00
|
|
|
makesign(BITVECP sign, SignTSVector *a)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 k,
|
2007-08-21 03:11:32 +02:00
|
|
|
len = ARRNELEM(a);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *ptr = GETARR(a);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
MemSet((void *) sign, 0, sizeof(BITVEC));
|
|
|
|
for (k = 0; k < len; k++)
|
|
|
|
HASH(sign, ptr[k]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtsvector_compress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *retval = entry;
|
|
|
|
|
|
|
|
if (entry->leafkey)
|
|
|
|
{ /* tsvector */
|
|
|
|
SignTSVector *res;
|
|
|
|
TSVector val = DatumGetTSVector(entry->key);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 len;
|
|
|
|
int32 *arr;
|
2007-08-21 03:11:32 +02:00
|
|
|
WordEntry *ptr = ARRPTR(val);
|
|
|
|
char *words = STRPTR(val);
|
|
|
|
|
|
|
|
len = CALCGTSIZE(ARRKEY, val->size);
|
|
|
|
res = (SignTSVector *) palloc(len);
|
|
|
|
SET_VARSIZE(res, len);
|
|
|
|
res->flag = ARRKEY;
|
|
|
|
arr = GETARR(res);
|
|
|
|
len = val->size;
|
|
|
|
while (len--)
|
|
|
|
{
|
|
|
|
pg_crc32 c;
|
|
|
|
|
Switch to CRC-32C in WAL and other places.
The old algorithm was found to not be the usual CRC-32 algorithm, used by
Ethernet et al. We were using a non-reflected lookup table with code meant
for a reflected lookup table. That's a strange combination that AFAICS does
not correspond to any bit-wise CRC calculation, which makes it difficult to
reason about its properties. Although it has worked well in practice, seems
safer to use a well-known algorithm.
Since we're changing the algorithm anyway, we might as well choose a
different polynomial. The Castagnoli polynomial has better error-correcting
properties than the traditional CRC-32 polynomial, even if we had
implemented it correctly. Another reason for picking that is that some new
CPUs have hardware support for calculating CRC-32C, but not CRC-32, let
alone our strange variant of it. This patch doesn't add any support for such
hardware, but a future patch could now do that.
The old algorithm is kept around for tsquery and pg_trgm, which use the
values in indexes that need to remain compatible so that pg_upgrade works.
While we're at it, share the old lookup table for CRC-32 calculation
between hstore, ltree and core. They all use the same table, so might as
well.
2014-11-04 10:35:15 +01:00
|
|
|
INIT_LEGACY_CRC32(c);
|
|
|
|
COMP_LEGACY_CRC32(c, words + ptr->pos, ptr->len);
|
|
|
|
FIN_LEGACY_CRC32(c);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
*arr = *(int32 *) &c;
|
2007-08-21 03:11:32 +02:00
|
|
|
arr++;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = uniqueint(GETARR(res), val->size);
|
|
|
|
if (len != val->size)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* there is a collision of hash-function; len is always less than
|
|
|
|
* val->size
|
|
|
|
*/
|
|
|
|
len = CALCGTSIZE(ARRKEY, len);
|
|
|
|
res = (SignTSVector *) repalloc((void *) res, len);
|
|
|
|
SET_VARSIZE(res, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make signature, if array is too long */
|
|
|
|
if (VARSIZE(res) > TOAST_INDEX_TARGET)
|
|
|
|
{
|
|
|
|
SignTSVector *ressign;
|
|
|
|
|
|
|
|
len = CALCGTSIZE(SIGNKEY, 0);
|
|
|
|
ressign = (SignTSVector *) palloc(len);
|
|
|
|
SET_VARSIZE(ressign, len);
|
|
|
|
ressign->flag = SIGNKEY;
|
|
|
|
makesign(GETSIGN(ressign), res);
|
|
|
|
res = ressign;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
|
|
|
entry->rel, entry->page,
|
|
|
|
entry->offset, FALSE);
|
|
|
|
}
|
|
|
|
else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
|
|
|
|
!ISALLTRUE(DatumGetPointer(entry->key)))
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 i,
|
2007-08-21 03:11:32 +02:00
|
|
|
len;
|
|
|
|
SignTSVector *res;
|
|
|
|
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
|
|
|
|
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
2007-11-16 02:12:24 +01:00
|
|
|
if ((sign[i] & 0xff) != 0xff)
|
|
|
|
PG_RETURN_POINTER(retval);
|
2007-11-16 01:13:02 +01:00
|
|
|
}
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
|
|
|
|
res = (SignTSVector *) palloc(len);
|
|
|
|
SET_VARSIZE(res, len);
|
|
|
|
res->flag = SIGNKEY | ALLISTRUE;
|
|
|
|
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
|
|
|
entry->rel, entry->page,
|
|
|
|
entry->offset, FALSE);
|
|
|
|
}
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtsvector_decompress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
2017-09-18 21:21:23 +02:00
|
|
|
SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(entry->key);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
if (key != (SignTSVector *) DatumGetPointer(entry->key))
|
|
|
|
{
|
|
|
|
GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
|
|
|
|
gistentryinit(*retval, PointerGetDatum(key),
|
|
|
|
entry->rel, entry->page,
|
|
|
|
entry->offset, FALSE);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *arrb;
|
|
|
|
int32 *arre;
|
2007-08-21 03:11:32 +02:00
|
|
|
} CHKVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* is there value 'val' in array or not ?
|
|
|
|
*/
|
|
|
|
static bool
|
2016-04-07 17:44:18 +02:00
|
|
|
checkcondition_arr(void *checkval, QueryOperand *val, ExecPhraseData *data)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *StopLow = ((CHKVAL *) checkval)->arrb;
|
|
|
|
int32 *StopHigh = ((CHKVAL *) checkval)->arre;
|
|
|
|
int32 *StopMiddle;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
/* Loop invariant: StopLow <= val < StopHigh */
|
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
/*
|
2014-10-20 16:23:40 +02:00
|
|
|
* we are not able to find a prefix by hash value
|
2008-05-16 18:31:02 +02:00
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
if (val->prefix)
|
2008-05-16 18:31:02 +02:00
|
|
|
return true;
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
while (StopLow < StopHigh)
|
|
|
|
{
|
|
|
|
StopMiddle = StopLow + (StopHigh - StopLow) / 2;
|
2007-09-07 17:09:56 +02:00
|
|
|
if (*StopMiddle == val->valcrc)
|
2017-08-17 18:39:20 +02:00
|
|
|
return true;
|
2007-09-07 17:09:56 +02:00
|
|
|
else if (*StopMiddle < val->valcrc)
|
2007-08-21 03:11:32 +02:00
|
|
|
StopLow = StopMiddle + 1;
|
|
|
|
else
|
|
|
|
StopHigh = StopMiddle;
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:39:20 +02:00
|
|
|
return false;
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-04-07 17:44:18 +02:00
|
|
|
checkcondition_bit(void *checkval, QueryOperand *val, ExecPhraseData *data)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
/*
|
2014-10-20 16:23:40 +02:00
|
|
|
* we are not able to find a prefix in signature tree
|
2008-05-16 18:31:02 +02:00
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
if (val->prefix)
|
|
|
|
return true;
|
2007-09-07 17:09:56 +02:00
|
|
|
return GETBIT(checkval, HASHVAL(val->valcrc));
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtsvector_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2008-04-14 19:05:34 +02:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
2007-08-21 03:11:32 +02:00
|
|
|
TSQuery query = PG_GETARG_TSQUERY(1);
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2008-04-14 19:05:34 +02:00
|
|
|
/* StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2); */
|
|
|
|
/* Oid subtype = PG_GETARG_OID(3); */
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(4);
|
|
|
|
SignTSVector *key = (SignTSVector *) DatumGetPointer(entry->key);
|
|
|
|
|
|
|
|
/* All cases served by this function are inexact */
|
|
|
|
*recheck = true;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
if (!query->size)
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
|
|
|
|
if (ISSIGNKEY(key))
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(key))
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
|
Fix strange behavior (and possible crashes) in full text phrase search.
In an attempt to simplify the tsquery matching engine, the original
phrase search patch invented rewrite rules that would rearrange a
tsquery so that no AND/OR/NOT operator appeared below a PHRASE operator.
But this approach had numerous problems. The rearrangement step was
missed by ts_rewrite (and perhaps other places), allowing tsqueries
to be created that would cause Assert failures or perhaps crashes at
execution, as reported by Andreas Seltenreich. The rewrite rules
effectively defined semantics for operators underneath PHRASE that were
buggy, or at least unintuitive. And because rewriting was done in
tsqueryin() rather than at execution, the rearrangement was user-visible,
which is not very desirable --- for example, it might cause unexpected
matches or failures to match in ts_rewrite.
As a somewhat independent problem, the behavior of nested PHRASE operators
was only sane for left-deep trees; queries like "x <-> (y <-> z)" did not
behave intuitively at all.
To fix, get rid of the rewrite logic altogether, and instead teach the
tsquery execution engine to manage AND/OR/NOT below a PHRASE operator
by explicitly computing the match location(s) and match widths for these
operators.
This requires introducing some additional fields into the publicly visible
ExecPhraseData struct; but since there's no way for third-party code to
pass such a struct to TS_phrase_execute, it shouldn't create an ABI problem
as long as we don't move the offsets of the existing fields.
Another related problem was that index searches supposed that "!x <-> y"
could be lossily approximated as "!x & y", which isn't correct because
the latter will reject, say, "x q y" which the query itself accepts.
This required some tweaking in TS_execute_ternary along with the main
tsquery engine.
Back-patch to 9.6 where phrase operators were introduced. While this
could be argued to change behavior more than we'd like in a stable branch,
we have to do something about the crash hazards and index-vs-seqscan
inconsistency, and it doesn't seem desirable to let the unintuitive
behaviors induced by the rewriting implementation stand as precedent.
Discussion: https://postgr.es/m/28215.1481999808@sss.pgh.pa.us
Discussion: https://postgr.es/m/26706.1482087250@sss.pgh.pa.us
2016-12-21 21:18:25 +01:00
|
|
|
/* since signature is lossy, cannot specify CALC_NOT here */
|
|
|
|
PG_RETURN_BOOL(TS_execute(GETQUERY(query),
|
2016-06-27 19:47:32 +02:00
|
|
|
(void *) GETSIGN(key),
|
Fix strange behavior (and possible crashes) in full text phrase search.
In an attempt to simplify the tsquery matching engine, the original
phrase search patch invented rewrite rules that would rearrange a
tsquery so that no AND/OR/NOT operator appeared below a PHRASE operator.
But this approach had numerous problems. The rearrangement step was
missed by ts_rewrite (and perhaps other places), allowing tsqueries
to be created that would cause Assert failures or perhaps crashes at
execution, as reported by Andreas Seltenreich. The rewrite rules
effectively defined semantics for operators underneath PHRASE that were
buggy, or at least unintuitive. And because rewriting was done in
tsqueryin() rather than at execution, the rearrangement was user-visible,
which is not very desirable --- for example, it might cause unexpected
matches or failures to match in ts_rewrite.
As a somewhat independent problem, the behavior of nested PHRASE operators
was only sane for left-deep trees; queries like "x <-> (y <-> z)" did not
behave intuitively at all.
To fix, get rid of the rewrite logic altogether, and instead teach the
tsquery execution engine to manage AND/OR/NOT below a PHRASE operator
by explicitly computing the match location(s) and match widths for these
operators.
This requires introducing some additional fields into the publicly visible
ExecPhraseData struct; but since there's no way for third-party code to
pass such a struct to TS_phrase_execute, it shouldn't create an ABI problem
as long as we don't move the offsets of the existing fields.
Another related problem was that index searches supposed that "!x <-> y"
could be lossily approximated as "!x & y", which isn't correct because
the latter will reject, say, "x q y" which the query itself accepts.
This required some tweaking in TS_execute_ternary along with the main
tsquery engine.
Back-patch to 9.6 where phrase operators were introduced. While this
could be argued to change behavior more than we'd like in a stable branch,
we have to do something about the crash hazards and index-vs-seqscan
inconsistency, and it doesn't seem desirable to let the unintuitive
behaviors induced by the rewriting implementation stand as precedent.
Discussion: https://postgr.es/m/28215.1481999808@sss.pgh.pa.us
Discussion: https://postgr.es/m/26706.1482087250@sss.pgh.pa.us
2016-12-21 21:18:25 +01:00
|
|
|
TS_EXEC_PHRASE_NO_POS,
|
|
|
|
checkcondition_bit));
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* only leaf pages */
|
|
|
|
CHKVAL chkval;
|
|
|
|
|
|
|
|
chkval.arrb = GETARR(key);
|
|
|
|
chkval.arre = chkval.arrb + ARRNELEM(key);
|
Fix strange behavior (and possible crashes) in full text phrase search.
In an attempt to simplify the tsquery matching engine, the original
phrase search patch invented rewrite rules that would rearrange a
tsquery so that no AND/OR/NOT operator appeared below a PHRASE operator.
But this approach had numerous problems. The rearrangement step was
missed by ts_rewrite (and perhaps other places), allowing tsqueries
to be created that would cause Assert failures or perhaps crashes at
execution, as reported by Andreas Seltenreich. The rewrite rules
effectively defined semantics for operators underneath PHRASE that were
buggy, or at least unintuitive. And because rewriting was done in
tsqueryin() rather than at execution, the rearrangement was user-visible,
which is not very desirable --- for example, it might cause unexpected
matches or failures to match in ts_rewrite.
As a somewhat independent problem, the behavior of nested PHRASE operators
was only sane for left-deep trees; queries like "x <-> (y <-> z)" did not
behave intuitively at all.
To fix, get rid of the rewrite logic altogether, and instead teach the
tsquery execution engine to manage AND/OR/NOT below a PHRASE operator
by explicitly computing the match location(s) and match widths for these
operators.
This requires introducing some additional fields into the publicly visible
ExecPhraseData struct; but since there's no way for third-party code to
pass such a struct to TS_phrase_execute, it shouldn't create an ABI problem
as long as we don't move the offsets of the existing fields.
Another related problem was that index searches supposed that "!x <-> y"
could be lossily approximated as "!x & y", which isn't correct because
the latter will reject, say, "x q y" which the query itself accepts.
This required some tweaking in TS_execute_ternary along with the main
tsquery engine.
Back-patch to 9.6 where phrase operators were introduced. While this
could be argued to change behavior more than we'd like in a stable branch,
we have to do something about the crash hazards and index-vs-seqscan
inconsistency, and it doesn't seem desirable to let the unintuitive
behaviors induced by the rewriting implementation stand as precedent.
Discussion: https://postgr.es/m/28215.1481999808@sss.pgh.pa.us
Discussion: https://postgr.es/m/26706.1482087250@sss.pgh.pa.us
2016-12-21 21:18:25 +01:00
|
|
|
PG_RETURN_BOOL(TS_execute(GETQUERY(query),
|
2016-06-27 19:47:32 +02:00
|
|
|
(void *) &chkval,
|
Fix strange behavior (and possible crashes) in full text phrase search.
In an attempt to simplify the tsquery matching engine, the original
phrase search patch invented rewrite rules that would rearrange a
tsquery so that no AND/OR/NOT operator appeared below a PHRASE operator.
But this approach had numerous problems. The rearrangement step was
missed by ts_rewrite (and perhaps other places), allowing tsqueries
to be created that would cause Assert failures or perhaps crashes at
execution, as reported by Andreas Seltenreich. The rewrite rules
effectively defined semantics for operators underneath PHRASE that were
buggy, or at least unintuitive. And because rewriting was done in
tsqueryin() rather than at execution, the rearrangement was user-visible,
which is not very desirable --- for example, it might cause unexpected
matches or failures to match in ts_rewrite.
As a somewhat independent problem, the behavior of nested PHRASE operators
was only sane for left-deep trees; queries like "x <-> (y <-> z)" did not
behave intuitively at all.
To fix, get rid of the rewrite logic altogether, and instead teach the
tsquery execution engine to manage AND/OR/NOT below a PHRASE operator
by explicitly computing the match location(s) and match widths for these
operators.
This requires introducing some additional fields into the publicly visible
ExecPhraseData struct; but since there's no way for third-party code to
pass such a struct to TS_phrase_execute, it shouldn't create an ABI problem
as long as we don't move the offsets of the existing fields.
Another related problem was that index searches supposed that "!x <-> y"
could be lossily approximated as "!x & y", which isn't correct because
the latter will reject, say, "x q y" which the query itself accepts.
This required some tweaking in TS_execute_ternary along with the main
tsquery engine.
Back-patch to 9.6 where phrase operators were introduced. While this
could be argued to change behavior more than we'd like in a stable branch,
we have to do something about the crash hazards and index-vs-seqscan
inconsistency, and it doesn't seem desirable to let the unintuitive
behaviors induced by the rewriting implementation stand as precedent.
Discussion: https://postgr.es/m/28215.1481999808@sss.pgh.pa.us
Discussion: https://postgr.es/m/26706.1482087250@sss.pgh.pa.us
2016-12-21 21:18:25 +01:00
|
|
|
TS_EXEC_PHRASE_NO_POS | TS_EXEC_CALC_NOT,
|
|
|
|
checkcondition_arr));
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
static int32
|
2007-11-16 02:12:24 +01:00
|
|
|
unionkey(BITVECP sbase, SignTSVector *add)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 i;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
if (ISSIGNKEY(add))
|
|
|
|
{
|
|
|
|
BITVECP sadd = GETSIGN(add);
|
|
|
|
|
|
|
|
if (ISALLTRUE(add))
|
|
|
|
return 1;
|
|
|
|
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
sbase[i] |= sadd[i];
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *ptr = GETARR(add);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ARRNELEM(add); i++)
|
|
|
|
HASH(sbase, ptr[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtsvector_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,
|
2007-08-21 03:11:32 +02:00
|
|
|
len;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 flag = 0;
|
2007-08-21 03:11:32 +02:00
|
|
|
SignTSVector *result;
|
|
|
|
|
|
|
|
MemSet((void *) base, 0, sizeof(BITVEC));
|
|
|
|
for (i = 0; i < entryvec->n; i++)
|
|
|
|
{
|
|
|
|
if (unionkey(base, GETENTRY(entryvec, i)))
|
|
|
|
{
|
|
|
|
flag = ALLISTRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flag |= SIGNKEY;
|
|
|
|
len = CALCGTSIZE(flag, 0);
|
|
|
|
result = (SignTSVector *) palloc(len);
|
|
|
|
*size = len;
|
|
|
|
SET_VARSIZE(result, len);
|
|
|
|
result->flag = flag;
|
|
|
|
if (!ISALLTRUE(result))
|
|
|
|
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtsvector_same(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
SignTSVector *a = (SignTSVector *) PG_GETARG_POINTER(0);
|
|
|
|
SignTSVector *b = (SignTSVector *) PG_GETARG_POINTER(1);
|
|
|
|
bool *result = (bool *) PG_GETARG_POINTER(2);
|
|
|
|
|
|
|
|
if (ISSIGNKEY(a))
|
|
|
|
{ /* then b also ISSIGNKEY */
|
|
|
|
if (ISALLTRUE(a) && ISALLTRUE(b))
|
|
|
|
*result = true;
|
|
|
|
else if (ISALLTRUE(a))
|
|
|
|
*result = false;
|
|
|
|
else if (ISALLTRUE(b))
|
|
|
|
*result = false;
|
|
|
|
else
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 i;
|
2007-08-21 03:11:32 +02:00
|
|
|
BITVECP sa = GETSIGN(a),
|
|
|
|
sb = GETSIGN(b);
|
|
|
|
|
|
|
|
*result = true;
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
if (sa[i] != sb[i])
|
|
|
|
{
|
|
|
|
*result = false;
|
|
|
|
break;
|
|
|
|
}
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* a and b ISARRKEY */
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 lena = ARRNELEM(a),
|
2007-08-21 03:11:32 +02:00
|
|
|
lenb = ARRNELEM(b);
|
|
|
|
|
|
|
|
if (lena != lenb)
|
|
|
|
*result = false;
|
|
|
|
else
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *ptra = GETARR(a),
|
2007-08-21 03:11:32 +02:00
|
|
|
*ptrb = GETARR(b);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 i;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
*result = true;
|
|
|
|
for (i = 0; i < lena; i++)
|
|
|
|
if (ptra[i] != ptrb[i])
|
|
|
|
{
|
|
|
|
*result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
static int32
|
2007-08-21 03:11:32 +02:00
|
|
|
sizebitvec(BITVECP sign)
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 size = 0,
|
2007-08-21 03:11:32 +02:00
|
|
|
i;
|
|
|
|
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
2007-11-16 02:12:24 +01:00
|
|
|
size += number_of_ones[(unsigned char) sign[i]];
|
2007-08-21 03:11:32 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hemdistsign(BITVECP a, BITVECP b)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
diff,
|
|
|
|
dist = 0;
|
|
|
|
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
diff = (unsigned char) (a[i] ^ b[i]);
|
|
|
|
dist += number_of_ones[diff];
|
|
|
|
}
|
2007-08-21 03:11:32 +02:00
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-11-16 02:12:24 +01:00
|
|
|
hemdist(SignTSVector *a, SignTSVector *b)
|
2007-08-21 03:11:32 +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));
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtsvector_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);
|
|
|
|
SignTSVector *origval = (SignTSVector *) DatumGetPointer(origentry->key);
|
|
|
|
SignTSVector *newval = (SignTSVector *) DatumGetPointer(newentry->key);
|
|
|
|
BITVECP orig = GETSIGN(origval);
|
|
|
|
|
|
|
|
*penalty = 0.0;
|
|
|
|
|
|
|
|
if (ISARRKEY(newval))
|
|
|
|
{
|
|
|
|
BITVEC sign;
|
|
|
|
|
|
|
|
makesign(sign, newval);
|
|
|
|
|
|
|
|
if (ISALLTRUE(origval))
|
|
|
|
*penalty = ((float) (SIGLENBIT - sizebitvec(sign))) / (float) (SIGLENBIT + 1);
|
|
|
|
else
|
|
|
|
*penalty = hemdistsign(sign, orig);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*penalty = hemdist(origval, newval);
|
|
|
|
PG_RETURN_POINTER(penalty);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
bool allistrue;
|
|
|
|
BITVEC sign;
|
|
|
|
} CACHESIGN;
|
|
|
|
|
|
|
|
static void
|
2007-11-16 02:12:24 +01:00
|
|
|
fillcache(CACHESIGN *item, SignTSVector *key)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
item->allistrue = false;
|
|
|
|
if (ISARRKEY(key))
|
|
|
|
makesign(item->sign, key);
|
|
|
|
else if (ISALLTRUE(key))
|
|
|
|
item->allistrue = true;
|
|
|
|
else
|
|
|
|
memcpy((void *) item->sign, (void *) GETSIGN(key), sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
OffsetNumber pos;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 cost;
|
2007-08-21 03:11:32 +02:00
|
|
|
} SPLITCOST;
|
|
|
|
|
|
|
|
static int
|
2007-09-11 10:46:29 +02:00
|
|
|
comparecost(const void *va, const void *vb)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
const SPLITCOST *a = (const SPLITCOST *) va;
|
|
|
|
const SPLITCOST *b = (const SPLITCOST *) vb;
|
2007-09-11 10:46:29 +02:00
|
|
|
|
|
|
|
if (a->cost == b->cost)
|
2007-08-21 03:11:32 +02:00
|
|
|
return 0;
|
|
|
|
else
|
2007-09-11 10:46:29 +02:00
|
|
|
return (a->cost > b->cost) ? 1 : -1;
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2007-11-16 02:12:24 +01:00
|
|
|
hemdistcache(CACHESIGN *a, CACHESIGN *b)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
|
|
|
if (a->allistrue)
|
|
|
|
{
|
|
|
|
if (b->allistrue)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return SIGLENBIT - sizebitvec(b->sign);
|
|
|
|
}
|
|
|
|
else if (b->allistrue)
|
|
|
|
return SIGLENBIT - sizebitvec(a->sign);
|
|
|
|
|
|
|
|
return hemdistsign(a->sign, b->sign);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtsvector_picksplit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
|
|
|
|
OffsetNumber k,
|
|
|
|
j;
|
|
|
|
SignTSVector *datum_l,
|
|
|
|
*datum_r;
|
|
|
|
BITVECP union_l,
|
|
|
|
union_r;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 size_alpha,
|
2007-08-21 03:11:32 +02:00
|
|
|
size_beta;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 size_waste,
|
2007-08-21 03:11:32 +02:00
|
|
|
waste = -1;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 nbytes;
|
2007-08-21 03:11:32 +02:00
|
|
|
OffsetNumber seed_1 = 0,
|
|
|
|
seed_2 = 0;
|
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
|
|
|
OffsetNumber maxoff;
|
|
|
|
BITVECP ptr;
|
|
|
|
int i;
|
|
|
|
CACHESIGN *cache;
|
|
|
|
SPLITCOST *costvector;
|
|
|
|
|
|
|
|
maxoff = entryvec->n - 2;
|
|
|
|
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
|
|
|
v->spl_left = (OffsetNumber *) palloc(nbytes);
|
|
|
|
v->spl_right = (OffsetNumber *) palloc(nbytes);
|
|
|
|
|
|
|
|
cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
|
|
|
|
fillcache(&cache[FirstOffsetNumber], GETENTRY(entryvec, FirstOffsetNumber));
|
|
|
|
|
|
|
|
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
|
|
|
|
{
|
|
|
|
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
|
|
|
if (k == FirstOffsetNumber)
|
|
|
|
fillcache(&cache[j], GETENTRY(entryvec, j));
|
|
|
|
|
|
|
|
size_waste = hemdistcache(&(cache[j]), &(cache[k]));
|
|
|
|
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 (cache[seed_1].allistrue)
|
|
|
|
{
|
|
|
|
datum_l = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
|
|
|
SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
|
|
|
datum_l->flag = SIGNKEY | ALLISTRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
datum_l = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY, 0));
|
|
|
|
SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY, 0));
|
|
|
|
datum_l->flag = SIGNKEY;
|
|
|
|
memcpy((void *) GETSIGN(datum_l), (void *) cache[seed_1].sign, sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
if (cache[seed_2].allistrue)
|
|
|
|
{
|
|
|
|
datum_r = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
|
|
|
SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
|
|
|
datum_r->flag = SIGNKEY | ALLISTRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
datum_r = (SignTSVector *) palloc(CALCGTSIZE(SIGNKEY, 0));
|
|
|
|
SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY, 0));
|
|
|
|
datum_r->flag = SIGNKEY;
|
|
|
|
memcpy((void *) GETSIGN(datum_r), (void *) cache[seed_2].sign, sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
|
|
|
|
union_l = GETSIGN(datum_l);
|
|
|
|
union_r = GETSIGN(datum_r);
|
|
|
|
maxoff = OffsetNumberNext(maxoff);
|
|
|
|
fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff));
|
|
|
|
/* sort before ... */
|
|
|
|
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
|
|
|
|
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
|
|
|
costvector[j - 1].pos = j;
|
|
|
|
size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]));
|
|
|
|
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
|
|
|
|
costvector[j - 1].cost = Abs(size_alpha - size_beta);
|
|
|
|
}
|
|
|
|
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ISALLTRUE(datum_l) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_l) && cache[j].allistrue)
|
|
|
|
size_alpha = 0;
|
|
|
|
else
|
|
|
|
size_alpha = SIGLENBIT - sizebitvec(
|
|
|
|
(cache[j].allistrue) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l));
|
|
|
|
|
|
|
|
if (ISALLTRUE(datum_r) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_r) && cache[j].allistrue)
|
|
|
|
size_beta = 0;
|
|
|
|
else
|
|
|
|
size_beta = SIGLENBIT - sizebitvec(
|
|
|
|
(cache[j].allistrue) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r));
|
|
|
|
|
|
|
|
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.1))
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_l) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (!ISALLTRUE(datum_l))
|
|
|
|
MemSet((void *) GETSIGN(datum_l), 0xff, sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = cache[j].sign;
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
union_l[i] |= ptr[i];
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
*left++ = j;
|
|
|
|
v->spl_nleft++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_r) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (!ISALLTRUE(datum_r))
|
|
|
|
MemSet((void *) GETSIGN(datum_r), 0xff, sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = cache[j].sign;
|
2007-11-16 01:13:02 +01:00
|
|
|
LOOPBYTE
|
|
|
|
union_r[i] |= ptr[i];
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
*right++ = j;
|
|
|
|
v->spl_nright++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*right = *left = FirstOffsetNumber;
|
|
|
|
v->spl_ldatum = PointerGetDatum(datum_l);
|
|
|
|
v->spl_rdatum = PointerGetDatum(datum_r);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(v);
|
|
|
|
}
|
2016-03-02 23:37:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Formerly, gtsvector_consistent was declared in pg_proc.h with arguments
|
|
|
|
* that did not match the documented conventions for GiST support functions.
|
|
|
|
* We fixed that, but we still need a pg_proc entry with the old signature
|
|
|
|
* to support reloading pre-9.6 contrib/tsearch2 opclass declarations.
|
|
|
|
* This compatibility function should go away eventually.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
gtsvector_consistent_oldsig(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return gtsvector_consistent(fcinfo);
|
|
|
|
}
|