2008-05-17 09:28:26 +08:00
|
|
|
/*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_trgm/trgm_gist.c
|
2008-05-17 09:28:26 +08:00
|
|
|
*/
|
2010-12-04 13:16:21 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
#include "trgm.h"
|
|
|
|
|
2010-12-04 13:16:21 +08:00
|
|
|
#include "access/skey.h"
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2010-12-04 13:16:21 +08:00
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_in);
|
|
|
|
Datum gtrgm_in(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_out);
|
|
|
|
Datum gtrgm_out(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_compress);
|
|
|
|
Datum gtrgm_compress(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_decompress);
|
|
|
|
Datum gtrgm_decompress(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_consistent);
|
|
|
|
Datum gtrgm_consistent(PG_FUNCTION_ARGS);
|
|
|
|
|
2010-12-04 13:16:21 +08:00
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_distance);
|
|
|
|
Datum gtrgm_distance(PG_FUNCTION_ARGS);
|
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_union);
|
|
|
|
Datum gtrgm_union(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_same);
|
|
|
|
Datum gtrgm_same(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_penalty);
|
|
|
|
Datum gtrgm_penalty(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(gtrgm_picksplit);
|
|
|
|
Datum gtrgm_picksplit(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
#define GETENTRY(vec,pos) ((TRGM *) DatumGetPointer((vec)->vector[(pos)].key))
|
|
|
|
|
2006-01-21 06:46:16 +08: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
|
|
|
|
};
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-03-01 14:30:32 +08:00
|
|
|
elog(ERROR, "not implemented");
|
2004-06-01 01:18:12 +08:00
|
|
|
PG_RETURN_DATUM(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-03-01 14:30:32 +08:00
|
|
|
elog(ERROR, "not implemented");
|
2004-06-01 01:18:12 +08:00
|
|
|
PG_RETURN_DATUM(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-06-11 22:49:15 +08:00
|
|
|
makesign(BITVECP sign, TRGM *a)
|
2004-06-01 01:18:12 +08:00
|
|
|
{
|
|
|
|
int4 k,
|
|
|
|
len = ARRNELEM(a);
|
|
|
|
trgm *ptr = GETARR(a);
|
2004-08-29 13:07:03 +08:00
|
|
|
int4 tmp = 0;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
MemSet((void *) sign, 0, sizeof(BITVEC));
|
2004-08-29 13:07:03 +08:00
|
|
|
SETBIT(sign, SIGLENBIT); /* set last unused bit */
|
|
|
|
for (k = 0; k < len; k++)
|
|
|
|
{
|
|
|
|
CPTRGM(((char *) &tmp), ptr + k);
|
2004-06-01 01:18:12 +08:00
|
|
|
HASH(sign, tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_compress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *retval = entry;
|
|
|
|
|
|
|
|
if (entry->leafkey)
|
|
|
|
{ /* trgm */
|
2004-08-29 13:07:03 +08:00
|
|
|
TRGM *res;
|
2007-04-06 12:21:44 +08:00
|
|
|
text *val = DatumGetTextP(entry->key);
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
res = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
|
|
|
entry->rel, entry->page,
|
2006-06-28 20:00:14 +08:00
|
|
|
entry->offset, FALSE);
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
else if (ISSIGNKEY(DatumGetPointer(entry->key)) &&
|
|
|
|
!ISALLTRUE(DatumGetPointer(entry->key)))
|
|
|
|
{
|
|
|
|
int4 i,
|
|
|
|
len;
|
2004-08-29 13:07:03 +08:00
|
|
|
TRGM *res;
|
2004-06-01 01:18:12 +08:00
|
|
|
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
2007-11-16 09:12:24 +08:00
|
|
|
if ((sign[i] & 0xff) != 0xff)
|
|
|
|
PG_RETURN_POINTER(retval);
|
2007-11-16 08:13:02 +08:00
|
|
|
}
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
|
|
|
|
res = (TRGM *) palloc(len);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(res, len);
|
2004-06-01 01:18:12 +08:00
|
|
|
res->flag = SIGNKEY | ALLISTRUE;
|
|
|
|
|
|
|
|
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(res),
|
|
|
|
entry->rel, entry->page,
|
2006-06-28 20:00:14 +08:00
|
|
|
entry->offset, FALSE);
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_decompress(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-04-06 12:21:44 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
GISTENTRY *retval;
|
2007-11-16 09:12:24 +08:00
|
|
|
text *key;
|
2007-04-06 12:21:44 +08:00
|
|
|
|
|
|
|
key = DatumGetTextP(entry->key);
|
|
|
|
|
|
|
|
if (key != (text *) DatumGetPointer(entry->key))
|
|
|
|
{
|
|
|
|
/* need to pass back the decompressed item */
|
|
|
|
retval = palloc(sizeof(GISTENTRY));
|
|
|
|
gistentryinit(*retval, PointerGetDatum(key),
|
|
|
|
entry->rel, entry->page, entry->offset, entry->leafkey);
|
|
|
|
PG_RETURN_POINTER(retval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we can return the entry as-is */
|
|
|
|
PG_RETURN_POINTER(entry);
|
|
|
|
}
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
|
2010-12-04 13:16:21 +08:00
|
|
|
static int4
|
|
|
|
cnt_sml_sign_common(TRGM *qtrg, BITVECP sign)
|
|
|
|
{
|
|
|
|
int4 count = 0;
|
|
|
|
int4 k,
|
|
|
|
len = ARRNELEM(qtrg);
|
|
|
|
trgm *ptr = GETARR(qtrg);
|
|
|
|
int4 tmp = 0;
|
|
|
|
|
|
|
|
for (k = 0; k < len; k++)
|
|
|
|
{
|
|
|
|
CPTRGM(((char *) &tmp), ptr + k);
|
|
|
|
count += GETBIT(sign, HASHVAL(tmp));
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
Datum
|
|
|
|
gtrgm_consistent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2008-04-15 01:05:34 +08:00
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
text *query = PG_GETARG_TEXT_P(1);
|
2010-12-04 13:16:21 +08:00
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2011-04-10 23:42:00 +08:00
|
|
|
|
2008-04-15 01:05:34 +08:00
|
|
|
/* Oid subtype = PG_GETARG_OID(3); */
|
|
|
|
bool *recheck = (bool *) PG_GETARG_POINTER(4);
|
|
|
|
TRGM *key = (TRGM *) DatumGetPointer(entry->key);
|
2008-07-11 19:56:48 +08:00
|
|
|
TRGM *qtrg;
|
2010-12-04 13:16:21 +08:00
|
|
|
bool res;
|
2011-10-01 11:54:27 +08:00
|
|
|
Size querysize = VARSIZE(query);
|
2011-02-01 10:33:55 +08:00
|
|
|
char *cache = (char *) fcinfo->flinfo->fn_extra,
|
2011-10-01 11:54:27 +08:00
|
|
|
*cachedQuery = cache + MAXALIGN(sizeof(StrategyNumber));
|
2011-02-01 10:33:55 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Store both the strategy number and extracted trigrams in cache, because
|
2011-04-10 23:42:00 +08:00
|
|
|
* trigram extraction is relatively CPU-expensive. We must include
|
2011-02-01 10:33:55 +08:00
|
|
|
* strategy number because trigram extraction depends on strategy.
|
2011-10-01 11:54:27 +08:00
|
|
|
*
|
|
|
|
* The cached structure contains the strategy number, then the input
|
|
|
|
* query (starting at a MAXALIGN boundary), then the TRGM value (also
|
|
|
|
* starting at a MAXALIGN boundary).
|
2011-02-01 10:33:55 +08:00
|
|
|
*/
|
2011-10-01 11:54:27 +08:00
|
|
|
if (cache == NULL ||
|
|
|
|
strategy != *((StrategyNumber *) cache) ||
|
|
|
|
VARSIZE(cachedQuery) != querysize ||
|
|
|
|
memcmp(cachedQuery, query, querysize) != 0)
|
2008-07-11 19:56:48 +08:00
|
|
|
{
|
2011-10-01 11:54:27 +08:00
|
|
|
char *newcache;
|
|
|
|
|
2011-02-01 10:33:55 +08:00
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case SimilarityStrategyNumber:
|
2011-10-01 11:54:27 +08:00
|
|
|
qtrg = generate_trgm(VARDATA(query),
|
|
|
|
querysize - VARHDRSZ);
|
2011-02-01 10:33:55 +08:00
|
|
|
break;
|
|
|
|
case ILikeStrategyNumber:
|
|
|
|
#ifndef IGNORECASE
|
|
|
|
elog(ERROR, "cannot handle ~~* with case-sensitive trigrams");
|
|
|
|
#endif
|
|
|
|
/* FALL THRU */
|
|
|
|
case LikeStrategyNumber:
|
2011-10-01 11:54:27 +08:00
|
|
|
qtrg = generate_wildcard_trgm(VARDATA(query),
|
|
|
|
querysize - VARHDRSZ);
|
2011-02-01 10:33:55 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unrecognized strategy number: %d", strategy);
|
2011-04-10 23:42:00 +08:00
|
|
|
qtrg = NULL; /* keep compiler quiet */
|
2011-02-01 10:33:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-07-11 19:56:48 +08:00
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
newcache = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
|
|
|
|
MAXALIGN(sizeof(StrategyNumber)) +
|
|
|
|
MAXALIGN(querysize) +
|
|
|
|
VARSIZE(qtrg));
|
|
|
|
cachedQuery = newcache + MAXALIGN(sizeof(StrategyNumber));
|
|
|
|
|
|
|
|
*((StrategyNumber *) newcache) = strategy;
|
|
|
|
memcpy(cachedQuery, query, querysize);
|
|
|
|
memcpy(cachedQuery + MAXALIGN(querysize), qtrg, VARSIZE(qtrg));
|
|
|
|
|
2008-07-11 19:56:48 +08:00
|
|
|
if (cache)
|
|
|
|
pfree(cache);
|
2011-10-01 11:54:27 +08:00
|
|
|
fcinfo->flinfo->fn_extra = newcache;
|
2008-07-11 19:56:48 +08:00
|
|
|
}
|
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
qtrg = (TRGM *) (cachedQuery + MAXALIGN(querysize));
|
2008-07-11 19:56:48 +08:00
|
|
|
|
2010-12-04 13:16:21 +08:00
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case SimilarityStrategyNumber:
|
2011-02-01 10:33:55 +08:00
|
|
|
/* Similarity search is exact */
|
|
|
|
*recheck = false;
|
|
|
|
|
2010-12-04 13:16:21 +08:00
|
|
|
if (GIST_LEAF(entry))
|
2011-04-10 23:42:00 +08:00
|
|
|
{ /* all leafs contains orig trgm */
|
|
|
|
float4 tmpsml = cnt_sml(key, qtrg);
|
2010-12-04 13:16:21 +08:00
|
|
|
|
|
|
|
/* strange bug at freebsd 5.2.1 and gcc 3.3.3 */
|
|
|
|
res = (*(int *) &tmpsml == *(int *) &trgm_limit || tmpsml > trgm_limit) ? true : false;
|
|
|
|
}
|
|
|
|
else if (ISALLTRUE(key))
|
2011-04-10 23:42:00 +08:00
|
|
|
{ /* non-leaf contains signature */
|
2010-12-04 13:16:21 +08:00
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
else
|
2011-04-10 23:42:00 +08:00
|
|
|
{ /* non-leaf contains signature */
|
|
|
|
int4 count = cnt_sml_sign_common(qtrg, GETSIGN(key));
|
|
|
|
int4 len = ARRNELEM(qtrg);
|
2010-12-04 13:16:21 +08:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
res = false;
|
|
|
|
else
|
|
|
|
res = (((((float8) count) / ((float8) len))) >= trgm_limit) ? true : false;
|
|
|
|
}
|
|
|
|
break;
|
2011-02-01 10:33:55 +08:00
|
|
|
case ILikeStrategyNumber:
|
|
|
|
#ifndef IGNORECASE
|
|
|
|
elog(ERROR, "cannot handle ~~* with case-sensitive trigrams");
|
|
|
|
#endif
|
|
|
|
/* FALL THRU */
|
|
|
|
case LikeStrategyNumber:
|
|
|
|
/* Wildcard search is inexact */
|
|
|
|
*recheck = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if all the extracted trigrams can be present in child
|
|
|
|
* nodes.
|
|
|
|
*/
|
|
|
|
if (GIST_LEAF(entry))
|
2011-04-10 23:42:00 +08:00
|
|
|
{ /* all leafs contains orig trgm */
|
2011-02-01 10:33:55 +08:00
|
|
|
res = trgm_contained_by(qtrg, key);
|
|
|
|
}
|
|
|
|
else if (ISALLTRUE(key))
|
2011-04-10 23:42:00 +08:00
|
|
|
{ /* non-leaf contains signature */
|
2011-02-01 10:33:55 +08:00
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
else
|
2011-04-10 23:42:00 +08:00
|
|
|
{ /* non-leaf contains signature */
|
|
|
|
int32 k,
|
|
|
|
tmp = 0,
|
|
|
|
len = ARRNELEM(qtrg);
|
|
|
|
trgm *ptr = GETARR(qtrg);
|
|
|
|
BITVECP sign = GETSIGN(key);
|
2011-02-01 10:33:55 +08:00
|
|
|
|
|
|
|
res = true;
|
|
|
|
for (k = 0; k < len; k++)
|
|
|
|
{
|
|
|
|
CPTRGM(((char *) &tmp), ptr + k);
|
|
|
|
if (!GETBIT(sign, HASHVAL(tmp)))
|
|
|
|
{
|
|
|
|
res = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2010-12-04 13:16:21 +08:00
|
|
|
default:
|
|
|
|
elog(ERROR, "unrecognized strategy number: %d", strategy);
|
|
|
|
res = false; /* keep compiler quiet */
|
|
|
|
break;
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
2010-12-04 13:16:21 +08:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_distance(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
|
|
|
|
text *query = PG_GETARG_TEXT_P(1);
|
|
|
|
StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
|
2011-04-10 23:42:00 +08:00
|
|
|
|
2010-12-04 13:16:21 +08:00
|
|
|
/* Oid subtype = PG_GETARG_OID(3); */
|
|
|
|
TRGM *key = (TRGM *) DatumGetPointer(entry->key);
|
|
|
|
TRGM *qtrg;
|
|
|
|
float8 res;
|
2011-10-01 11:54:27 +08:00
|
|
|
Size querysize = VARSIZE(query);
|
2010-12-04 13:16:21 +08:00
|
|
|
char *cache = (char *) fcinfo->flinfo->fn_extra;
|
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
/*
|
|
|
|
* Cache the generated trigrams across multiple calls with the same
|
|
|
|
* query.
|
|
|
|
*/
|
|
|
|
if (cache == NULL ||
|
|
|
|
VARSIZE(cache) != querysize ||
|
|
|
|
memcmp(cache, query, querysize) != 0)
|
2010-12-04 13:16:21 +08:00
|
|
|
{
|
2011-10-01 11:54:27 +08:00
|
|
|
char *newcache;
|
2010-12-04 13:16:21 +08:00
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
qtrg = generate_trgm(VARDATA(query), querysize - VARHDRSZ);
|
|
|
|
|
|
|
|
newcache = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
|
|
|
|
MAXALIGN(querysize) +
|
|
|
|
VARSIZE(qtrg));
|
2010-12-04 13:16:21 +08:00
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
memcpy(newcache, query, querysize);
|
|
|
|
memcpy(newcache + MAXALIGN(querysize), qtrg, VARSIZE(qtrg));
|
2010-12-04 13:16:21 +08:00
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
if (cache)
|
|
|
|
pfree(cache);
|
|
|
|
fcinfo->flinfo->fn_extra = newcache;
|
|
|
|
cache = newcache;
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
qtrg = (TRGM *) (cache + MAXALIGN(querysize));
|
2010-12-04 13:16:21 +08:00
|
|
|
|
|
|
|
switch (strategy)
|
|
|
|
{
|
|
|
|
case DistanceStrategyNumber:
|
|
|
|
if (GIST_LEAF(entry))
|
2011-04-10 23:42:00 +08:00
|
|
|
{ /* all leafs contains orig trgm */
|
2010-12-04 13:16:21 +08:00
|
|
|
res = 1.0 - cnt_sml(key, qtrg);
|
|
|
|
}
|
|
|
|
else if (ISALLTRUE(key))
|
2011-04-10 23:42:00 +08:00
|
|
|
{ /* all leafs contains orig trgm */
|
2010-12-04 13:16:21 +08:00
|
|
|
res = 0.0;
|
|
|
|
}
|
|
|
|
else
|
2011-04-10 23:42:00 +08:00
|
|
|
{ /* non-leaf contains signature */
|
|
|
|
int4 count = cnt_sml_sign_common(qtrg, GETSIGN(key));
|
|
|
|
int4 len = ARRNELEM(qtrg);
|
2010-12-04 13:16:21 +08:00
|
|
|
|
|
|
|
res = (len == 0) ? -1.0 : 1.0 - ((float8) count) / ((float8) len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unrecognized strategy number: %d", strategy);
|
|
|
|
res = 0; /* keep compiler quiet */
|
|
|
|
break;
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
|
2010-12-04 13:16:21 +08:00
|
|
|
PG_RETURN_FLOAT8(res);
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int4
|
2009-06-11 22:49:15 +08:00
|
|
|
unionkey(BITVECP sbase, TRGM *add)
|
2004-06-01 01:18:12 +08:00
|
|
|
{
|
|
|
|
int4 i;
|
|
|
|
|
|
|
|
if (ISSIGNKEY(add))
|
|
|
|
{
|
|
|
|
BITVECP sadd = GETSIGN(add);
|
|
|
|
|
|
|
|
if (ISALLTRUE(add))
|
|
|
|
return 1;
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
2007-11-16 09:12:24 +08:00
|
|
|
sbase[i] |= sadd[i];
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trgm *ptr = GETARR(add);
|
2004-08-29 13:07:03 +08:00
|
|
|
int4 tmp = 0;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
for (i = 0; i < ARRNELEM(add); i++)
|
|
|
|
{
|
|
|
|
CPTRGM(((char *) &tmp), ptr + i);
|
2004-06-01 01:18:12 +08:00
|
|
|
HASH(sbase, tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_union(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
int4 len = entryvec->n;
|
2004-06-01 01:18:12 +08:00
|
|
|
int *size = (int *) PG_GETARG_POINTER(1);
|
|
|
|
BITVEC base;
|
|
|
|
int4 i;
|
|
|
|
int4 flag = 0;
|
2004-08-29 13:07:03 +08:00
|
|
|
TRGM *result;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
MemSet((void *) base, 0, sizeof(BITVEC));
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (unionkey(base, GETENTRY(entryvec, i)))
|
|
|
|
{
|
|
|
|
flag = ALLISTRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flag |= SIGNKEY;
|
|
|
|
len = CALCGTSIZE(flag, 0);
|
|
|
|
result = (TRGM *) palloc(len);
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(result, len);
|
2004-06-01 01:18:12 +08:00
|
|
|
result->flag = flag;
|
|
|
|
if (!ISALLTRUE(result))
|
|
|
|
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
|
2007-03-01 06:44:38 +08:00
|
|
|
*size = len;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_same(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
TRGM *a = (TRGM *) PG_GETARG_POINTER(0);
|
|
|
|
TRGM *b = (TRGM *) PG_GETARG_POINTER(1);
|
2004-06-01 01:18:12 +08:00
|
|
|
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
|
|
|
|
{
|
|
|
|
int4 i;
|
|
|
|
BITVECP sa = GETSIGN(a),
|
|
|
|
sb = GETSIGN(b);
|
|
|
|
|
|
|
|
*result = true;
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
if (sa[i] != sb[i])
|
|
|
|
{
|
|
|
|
*result = false;
|
|
|
|
break;
|
|
|
|
}
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* a and b ISARRKEY */
|
|
|
|
int4 lena = ARRNELEM(a),
|
|
|
|
lenb = ARRNELEM(b);
|
|
|
|
|
|
|
|
if (lena != lenb)
|
|
|
|
*result = false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trgm *ptra = GETARR(a),
|
|
|
|
*ptrb = GETARR(b);
|
|
|
|
int4 i;
|
|
|
|
|
|
|
|
*result = true;
|
|
|
|
for (i = 0; i < lena; i++)
|
2004-08-29 13:07:03 +08:00
|
|
|
if (CMPTRGM(ptra + i, ptrb + i))
|
2004-06-01 01:18:12 +08:00
|
|
|
{
|
|
|
|
*result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int4
|
|
|
|
sizebitvec(BITVECP sign)
|
|
|
|
{
|
|
|
|
int4 size = 0,
|
|
|
|
i;
|
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
2007-11-16 09:12:24 +08:00
|
|
|
size += number_of_ones[(unsigned char) sign[i]];
|
2004-06-01 01:18:12 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-08-29 13:07:03 +08:00
|
|
|
hemdistsign(BITVECP a, BITVECP b)
|
|
|
|
{
|
|
|
|
int i,
|
2006-01-21 06:46:16 +08:00
|
|
|
diff,
|
2004-08-29 13:07:03 +08:00
|
|
|
dist = 0;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
|
|
|
{
|
|
|
|
diff = (unsigned char) (a[i] ^ b[i]);
|
|
|
|
dist += number_of_ones[diff];
|
|
|
|
}
|
2004-06-01 01:18:12 +08:00
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-06-11 22:49:15 +08:00
|
|
|
hemdist(TRGM *a, TRGM *b)
|
2004-08-29 13:07:03 +08:00
|
|
|
{
|
|
|
|
if (ISALLTRUE(a))
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
if (ISALLTRUE(b))
|
|
|
|
return 0;
|
|
|
|
else
|
2004-08-29 13:07:03 +08:00
|
|
|
return SIGLENBIT - sizebitvec(GETSIGN(b));
|
|
|
|
}
|
|
|
|
else if (ISALLTRUE(b))
|
|
|
|
return SIGLENBIT - sizebitvec(GETSIGN(a));
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
return hemdistsign(GETSIGN(a), GETSIGN(b));
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_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);
|
2004-08-29 13:07:03 +08:00
|
|
|
TRGM *origval = (TRGM *) DatumGetPointer(origentry->key);
|
|
|
|
TRGM *newval = (TRGM *) DatumGetPointer(newentry->key);
|
2004-06-01 01:18:12 +08:00
|
|
|
BITVECP orig = GETSIGN(origval);
|
|
|
|
|
|
|
|
*penalty = 0.0;
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (ISARRKEY(newval))
|
|
|
|
{
|
2011-10-01 11:54:27 +08:00
|
|
|
char *cache = (char *) fcinfo->flinfo->fn_extra;
|
|
|
|
TRGM *cachedVal = (TRGM *) (cache + MAXALIGN(sizeof(BITVEC)));
|
|
|
|
Size newvalsize = VARSIZE(newval);
|
|
|
|
BITVECP sign;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache the sign data across multiple calls with the same newval.
|
|
|
|
*/
|
|
|
|
if (cache == NULL ||
|
|
|
|
VARSIZE(cachedVal) != newvalsize ||
|
|
|
|
memcmp(cachedVal, newval, newvalsize) != 0)
|
|
|
|
{
|
|
|
|
char *newcache;
|
|
|
|
|
|
|
|
newcache = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
|
|
|
|
MAXALIGN(sizeof(BITVEC)) +
|
|
|
|
newvalsize);
|
|
|
|
|
|
|
|
makesign((BITVECP) newcache, newval);
|
2004-08-29 13:07:03 +08:00
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
cachedVal = (TRGM *) (newcache + MAXALIGN(sizeof(BITVEC)));
|
|
|
|
memcpy(cachedVal, newval, newvalsize);
|
|
|
|
|
|
|
|
if (cache)
|
|
|
|
pfree(cache);
|
|
|
|
fcinfo->flinfo->fn_extra = newcache;
|
|
|
|
cache = newcache;
|
|
|
|
}
|
|
|
|
|
|
|
|
sign = (BITVECP) cache;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (ISALLTRUE(origval))
|
|
|
|
*penalty = ((float) (SIGLENBIT - sizebitvec(sign))) / (float) (SIGLENBIT + 1);
|
|
|
|
else
|
|
|
|
*penalty = hemdistsign(sign, orig);
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
else
|
|
|
|
*penalty = hemdist(origval, newval);
|
2004-06-01 01:18:12 +08:00
|
|
|
PG_RETURN_POINTER(penalty);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
bool allistrue;
|
|
|
|
BITVEC sign;
|
2007-11-16 09:12:24 +08:00
|
|
|
} CACHESIGN;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
static void
|
2009-06-11 22:49:15 +08:00
|
|
|
fillcache(CACHESIGN *item, TRGM *key)
|
2004-06-01 01:18:12 +08: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;
|
|
|
|
int4 cost;
|
2007-11-16 09:12:24 +08:00
|
|
|
} SPLITCOST;
|
2004-06-01 01:18:12 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
comparecost(const void *a, const void *b)
|
|
|
|
{
|
2011-09-12 02:54:32 +08:00
|
|
|
if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
|
2004-06-01 01:18:12 +08:00
|
|
|
return 0;
|
|
|
|
else
|
2011-09-12 02:54:32 +08:00
|
|
|
return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2007-11-16 09:12:24 +08:00
|
|
|
hemdistcache(CACHESIGN *a, CACHESIGN *b)
|
2004-08-29 13:07:03 +08:00
|
|
|
{
|
|
|
|
if (a->allistrue)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
if (b->allistrue)
|
|
|
|
return 0;
|
|
|
|
else
|
2004-08-29 13:07:03 +08:00
|
|
|
return SIGLENBIT - sizebitvec(b->sign);
|
|
|
|
}
|
|
|
|
else if (b->allistrue)
|
|
|
|
return SIGLENBIT - sizebitvec(a->sign);
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
return hemdistsign(a->sign, b->sign);
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
gtrgm_picksplit(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2004-08-29 13:07:03 +08:00
|
|
|
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
|
|
|
|
OffsetNumber maxoff = entryvec->n - 2;
|
2004-06-01 01:18:12 +08:00
|
|
|
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
|
|
|
|
OffsetNumber k,
|
|
|
|
j;
|
2004-08-29 13:07:03 +08:00
|
|
|
TRGM *datum_l,
|
2004-06-01 01:18:12 +08:00
|
|
|
*datum_r;
|
|
|
|
BITVECP union_l,
|
|
|
|
union_r;
|
|
|
|
int4 size_alpha,
|
|
|
|
size_beta;
|
|
|
|
int4 size_waste,
|
|
|
|
waste = -1;
|
|
|
|
int4 nbytes;
|
|
|
|
OffsetNumber seed_1 = 0,
|
|
|
|
seed_2 = 0;
|
|
|
|
OffsetNumber *left,
|
|
|
|
*right;
|
|
|
|
BITVECP ptr;
|
|
|
|
int i;
|
|
|
|
CACHESIGN *cache;
|
|
|
|
SPLITCOST *costvector;
|
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
/* cache the sign data for each existing item */
|
2004-06-01 01:18:12 +08:00
|
|
|
cache = (CACHESIGN *) palloc(sizeof(CACHESIGN) * (maxoff + 2));
|
2011-10-01 11:54:27 +08:00
|
|
|
for (k = FirstOffsetNumber; k <= maxoff; k = OffsetNumberNext(k))
|
|
|
|
fillcache(&cache[k], GETENTRY(entryvec, k));
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
/* now find the two furthest-apart items */
|
2004-08-29 13:07:03 +08:00
|
|
|
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
|
|
|
|
{
|
|
|
|
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
|
|
|
size_waste = hemdistcache(&(cache[j]), &(cache[k]));
|
|
|
|
if (size_waste > waste)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
waste = size_waste;
|
|
|
|
seed_1 = k;
|
|
|
|
seed_2 = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
/* just in case we didn't make a selection ... */
|
2004-08-29 13:07:03 +08:00
|
|
|
if (seed_1 == 0 || seed_2 == 0)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
seed_1 = 1;
|
|
|
|
seed_2 = 2;
|
|
|
|
}
|
|
|
|
|
2011-10-01 11:54:27 +08:00
|
|
|
/* initialize the result vectors */
|
|
|
|
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
|
|
|
|
v->spl_left = left = (OffsetNumber *) palloc(nbytes);
|
|
|
|
v->spl_right = right = (OffsetNumber *) palloc(nbytes);
|
|
|
|
v->spl_nleft = 0;
|
|
|
|
v->spl_nright = 0;
|
|
|
|
|
2004-06-01 01:18:12 +08:00
|
|
|
/* form initial .. */
|
2004-08-29 13:07:03 +08:00
|
|
|
if (cache[seed_1].allistrue)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
datum_l = (TRGM *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
2004-06-01 01:18:12 +08:00
|
|
|
datum_l->flag = SIGNKEY | ALLISTRUE;
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
datum_l = (TRGM *) palloc(CALCGTSIZE(SIGNKEY, 0));
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY, 0));
|
2004-06-01 01:18:12 +08:00
|
|
|
datum_l->flag = SIGNKEY;
|
|
|
|
memcpy((void *) GETSIGN(datum_l), (void *) cache[seed_1].sign, sizeof(BITVEC));
|
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
if (cache[seed_2].allistrue)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
datum_r = (TRGM *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
|
2004-06-01 01:18:12 +08:00
|
|
|
datum_r->flag = SIGNKEY | ALLISTRUE;
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
datum_r = (TRGM *) palloc(CALCGTSIZE(SIGNKEY, 0));
|
2007-03-01 06:44:38 +08:00
|
|
|
SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY, 0));
|
2004-06-01 01:18:12 +08:00
|
|
|
datum_r->flag = SIGNKEY;
|
|
|
|
memcpy((void *) GETSIGN(datum_r), (void *) cache[seed_2].sign, sizeof(BITVEC));
|
|
|
|
}
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
union_l = GETSIGN(datum_l);
|
|
|
|
union_r = GETSIGN(datum_r);
|
2004-06-01 01:18:12 +08:00
|
|
|
maxoff = OffsetNumberNext(maxoff);
|
|
|
|
fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff));
|
|
|
|
/* sort before ... */
|
|
|
|
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
|
2004-08-29 13:07:03 +08:00
|
|
|
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
costvector[j - 1].pos = j;
|
|
|
|
size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]));
|
2004-08-29 13:07:03 +08:00
|
|
|
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
|
2004-06-01 01:18:12 +08:00
|
|
|
costvector[j - 1].cost = abs(size_alpha - size_beta);
|
|
|
|
}
|
|
|
|
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
for (k = 0; k < maxoff; k++)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
j = costvector[k].pos;
|
2004-08-29 13:07:03 +08:00
|
|
|
if (j == seed_1)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
*left++ = j;
|
|
|
|
v->spl_nleft++;
|
|
|
|
continue;
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else if (j == seed_2)
|
|
|
|
{
|
2004-06-01 01:18:12 +08:00
|
|
|
*right++ = j;
|
|
|
|
v->spl_nright++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (ISALLTRUE(datum_l) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_l) && cache[j].allistrue)
|
|
|
|
size_alpha = 0;
|
2004-06-01 01:18:12 +08:00
|
|
|
else
|
2004-08-29 13:07:03 +08:00
|
|
|
size_alpha = SIGLENBIT - sizebitvec(
|
|
|
|
(cache[j].allistrue) ? GETSIGN(datum_l) : GETSIGN(cache[j].sign)
|
|
|
|
);
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
else
|
|
|
|
size_alpha = hemdistsign(cache[j].sign, GETSIGN(datum_l));
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
if (ISALLTRUE(datum_r) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_r) && cache[j].allistrue)
|
|
|
|
size_beta = 0;
|
2004-06-01 01:18:12 +08:00
|
|
|
else
|
2004-08-29 13:07:03 +08:00
|
|
|
size_beta = SIGLENBIT - sizebitvec(
|
|
|
|
(cache[j].allistrue) ? GETSIGN(datum_r) : GETSIGN(cache[j].sign)
|
|
|
|
);
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
2004-08-29 13:07:03 +08:00
|
|
|
else
|
|
|
|
size_beta = hemdistsign(cache[j].sign, GETSIGN(datum_r));
|
2004-06-01 01:18:12 +08:00
|
|
|
|
2004-08-29 13:07:03 +08:00
|
|
|
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))
|
2004-06-01 01:18:12 +08:00
|
|
|
MemSet((void *) GETSIGN(datum_l), 0xff, sizeof(BITVEC));
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = cache[j].sign;
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
|
|
|
union_l[i] |= ptr[i];
|
2004-06-01 01:18:12 +08:00
|
|
|
}
|
|
|
|
*left++ = j;
|
|
|
|
v->spl_nleft++;
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ISALLTRUE(datum_r) || cache[j].allistrue)
|
|
|
|
{
|
|
|
|
if (!ISALLTRUE(datum_r))
|
2004-06-01 01:18:12 +08:00
|
|
|
MemSet((void *) GETSIGN(datum_r), 0xff, sizeof(BITVEC));
|
2004-08-29 13:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = cache[j].sign;
|
2007-11-16 08:13:02 +08:00
|
|
|
LOOPBYTE
|
|
|
|
union_r[i] |= ptr[i];
|
2004-06-01 01:18:12 +08: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);
|
|
|
|
}
|