postgresql/contrib/pg_trgm/trgm_op.c

309 lines
5.2 KiB
C
Raw Normal View History

#include "trgm.h"
#include <ctype.h>
#include "utils/array.h"
#include "catalog/pg_type.h"
2004-08-29 13:07:03 +08:00
float4 trgm_limit = 0.3;
PG_FUNCTION_INFO_V1(set_limit);
2004-08-29 13:07:03 +08:00
Datum set_limit(PG_FUNCTION_ARGS);
Datum
2004-08-29 13:07:03 +08:00
set_limit(PG_FUNCTION_ARGS)
{
float4 nlimit = PG_GETARG_FLOAT4(0);
if (nlimit < 0 || nlimit > 1.0)
elog(ERROR, "Wrong limit, should be between 0 and 1");
trgm_limit = nlimit;
PG_RETURN_FLOAT4(trgm_limit);
}
PG_FUNCTION_INFO_V1(show_limit);
2004-08-29 13:07:03 +08:00
Datum show_limit(PG_FUNCTION_ARGS);
Datum
2004-08-29 13:07:03 +08:00
show_limit(PG_FUNCTION_ARGS)
{
PG_RETURN_FLOAT4(trgm_limit);
}
2004-08-29 13:07:03 +08:00
#define WORDWAIT 0
#define INWORD 1
static int
2004-08-29 13:07:03 +08:00
comp_trgm(const void *a, const void *b)
{
return CMPTRGM(a, b);
}
static int
2004-08-29 13:07:03 +08:00
unique_array(trgm * a, int len)
{
trgm *curend,
*tmp;
curend = tmp = a;
while (tmp - a < len)
2004-08-29 13:07:03 +08:00
if (CMPTRGM(tmp, curend))
{
curend++;
2004-08-29 13:07:03 +08:00
CPTRGM(curend, tmp);
tmp++;
2004-08-29 13:07:03 +08:00
}
else
tmp++;
2004-08-29 13:07:03 +08:00
return curend + 1 - a;
}
2004-08-29 13:07:03 +08:00
TRGM *
generate_trgm(char *str, int slen)
{
TRGM *trg;
char *buf,
*sptr,
*bufptr;
trgm *tptr;
int state = WORDWAIT;
int wl,
len;
2004-08-29 13:07:03 +08:00
trg = (TRGM *) palloc(TRGMHRDSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
trg->flag = ARRKEY;
trg->len = TRGMHRDSIZE;
2004-08-29 13:07:03 +08:00
if (slen + LPADDING + RPADDING < 3 || slen == 0)
return trg;
tptr = GETARR(trg);
2004-08-29 13:07:03 +08:00
buf = palloc(sizeof(char) * (slen + 4));
sptr = str;
2004-08-29 13:07:03 +08:00
if (LPADDING > 0)
{
*buf = ' ';
2004-08-29 13:07:03 +08:00
if (LPADDING > 1)
*(buf + 1) = ' ';
}
2004-08-29 13:07:03 +08:00
bufptr = buf + LPADDING;
while (sptr - str < slen)
{
if (state == WORDWAIT)
{
if (
#ifdef KEEPONLYALNUM
2004-08-29 13:07:03 +08:00
isalnum((unsigned char) *sptr)
#else
2004-08-29 13:07:03 +08:00
!isspace((unsigned char) *sptr)
#endif
2004-08-29 13:07:03 +08:00
)
{
*bufptr = *sptr; /* start put word in buffer */
bufptr++;
state = INWORD;
2004-08-29 13:07:03 +08:00
if (sptr - str == slen - 1 /* last char */ )
goto gettrg;
}
2004-08-29 13:07:03 +08:00
}
else
{
if (
#ifdef KEEPONLYALNUM
2004-08-29 13:07:03 +08:00
!isalnum((unsigned char) *sptr)
#else
2004-08-29 13:07:03 +08:00
isspace((unsigned char) *sptr)
#endif
2004-08-29 13:07:03 +08:00
)
{
gettrg:
/* word in buffer, so count trigrams */
*bufptr = ' ';
2004-08-29 13:07:03 +08:00
*(bufptr + 1) = ' ';
wl = bufptr - (buf + LPADDING) - 2 + LPADDING + RPADDING;
if (wl <= 0)
{
bufptr = buf + LPADDING;
state = WORDWAIT;
sptr++;
continue;
}
#ifdef IGNORECASE
2004-08-29 13:07:03 +08:00
do
{ /* lower word */
int wwl = bufptr - buf;
bufptr = buf + LPADDING;
while (bufptr - buf < wwl)
{
*bufptr = tolower((unsigned char) *bufptr);
bufptr++;
}
2004-08-29 13:07:03 +08:00
} while (0);
#endif
bufptr = buf;
/* set trigrams */
2004-08-29 13:07:03 +08:00
while (bufptr - buf < wl)
{
CPTRGM(tptr, bufptr);
bufptr++;
tptr++;
}
2004-08-29 13:07:03 +08:00
bufptr = buf + LPADDING;
state = WORDWAIT;
2004-08-29 13:07:03 +08:00
}
else
{
*bufptr = *sptr; /* put in buffer */
bufptr++;
2004-08-29 13:07:03 +08:00
if (sptr - str == slen - 1)
goto gettrg;
}
}
sptr++;
}
pfree(buf);
2004-08-29 13:07:03 +08:00
if ((len = tptr - GETARR(trg)) == 0)
return trg;
2004-08-29 13:07:03 +08:00
if (len > 0)
{
qsort((void *) GETARR(trg), len, sizeof(trgm), comp_trgm);
len = unique_array(GETARR(trg), len);
}
trg->len = CALCGTSIZE(ARRKEY, len);
return trg;
}
PG_FUNCTION_INFO_V1(show_trgm);
2004-08-29 13:07:03 +08:00
Datum show_trgm(PG_FUNCTION_ARGS);
Datum
2004-08-29 13:07:03 +08:00
show_trgm(PG_FUNCTION_ARGS)
{
text *in = PG_GETARG_TEXT_P(0);
TRGM *trg;
Datum *d;
ArrayType *a;
trgm *ptr;
trg = generate_trgm(VARDATA(in), VARSIZE(in) - VARHDRSZ);
2004-08-29 13:07:03 +08:00
d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(trg)));
ptr = GETARR(trg);
2004-08-29 13:07:03 +08:00
while (ptr - GETARR(trg) < ARRNELEM(trg))
{
text *item = (text *) palloc(VARHDRSZ + 3);
VARATT_SIZEP(item) = VARHDRSZ + 3;
CPTRGM(VARDATA(item), ptr);
2004-08-29 13:07:03 +08:00
d[ptr - GETARR(trg)] = PointerGetDatum(item);
ptr++;
}
a = construct_array(
2004-08-29 13:07:03 +08:00
d,
ARRNELEM(trg),
TEXTOID,
-1,
false,
'i'
);
ptr = GETARR(trg);
2004-08-29 13:07:03 +08:00
while (ptr - GETARR(trg) < ARRNELEM(trg))
{
pfree(DatumGetPointer(d[ptr - GETARR(trg)]));
ptr++;
}
2004-08-29 13:07:03 +08:00
pfree(d);
pfree(trg);
2004-08-29 13:07:03 +08:00
PG_FREE_IF_COPY(in, 0);
PG_RETURN_POINTER(a);
}
float4
2004-08-29 13:07:03 +08:00
cnt_sml(TRGM * trg1, TRGM * trg2)
{
trgm *ptr1,
*ptr2;
int count = 0;
int len1,
len2;
ptr1 = GETARR(trg1);
ptr2 = GETARR(trg2);
2004-08-29 13:07:03 +08:00
len1 = ARRNELEM(trg1);
len2 = ARRNELEM(trg2);
2004-08-29 13:07:03 +08:00
while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
{
int res = CMPTRGM(ptr1, ptr2);
if (res < 0)
ptr1++;
2004-08-29 13:07:03 +08:00
else if (res > 0)
ptr2++;
2004-08-29 13:07:03 +08:00
else
{
ptr1++;
ptr2++;
count++;
}
}
#ifdef DIVUNION
2004-08-29 13:07:03 +08:00
return ((((float4) count) / ((float4) (len1 + len2 - count))));
#else
2004-08-29 13:07:03 +08:00
return (((float) count) / ((float) ((len1 > len2) ? len1 : len2)));
#endif
}
PG_FUNCTION_INFO_V1(similarity);
2004-08-29 13:07:03 +08:00
Datum similarity(PG_FUNCTION_ARGS);
Datum
2004-08-29 13:07:03 +08:00
similarity(PG_FUNCTION_ARGS)
{
text *in1 = PG_GETARG_TEXT_P(0);
text *in2 = PG_GETARG_TEXT_P(1);
TRGM *trg1,
*trg2;
float4 res;
trg1 = generate_trgm(VARDATA(in1), VARSIZE(in1) - VARHDRSZ);
trg2 = generate_trgm(VARDATA(in2), VARSIZE(in2) - VARHDRSZ);
2004-08-29 13:07:03 +08:00
res = cnt_sml(trg1, trg2);
pfree(trg1);
pfree(trg2);
2004-08-29 13:07:03 +08:00
PG_FREE_IF_COPY(in1, 0);
PG_FREE_IF_COPY(in2, 1);
PG_RETURN_FLOAT4(res);
}
PG_FUNCTION_INFO_V1(similarity_op);
2004-08-29 13:07:03 +08:00
Datum similarity_op(PG_FUNCTION_ARGS);
Datum
2004-08-29 13:07:03 +08:00
similarity_op(PG_FUNCTION_ARGS)
{
float4 res = DatumGetFloat4(DirectFunctionCall2(
similarity,
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(1)
));
PG_RETURN_BOOL(res >= trgm_limit);
}