Index: Makefile =================================================================== RCS file: /projects/cvsroot/pgsql/contrib/pg_trgm/Makefile,v retrieving revision 1.6 diff -c -r1.6 Makefile *** Makefile 9 Feb 2007 17:24:33 -0000 1.6 --- Makefile 22 Feb 2007 13:57:24 -0000 *************** *** 1,7 **** # $PostgreSQL: pgsql/contrib/pg_trgm/Makefile,v 1.6 2007/02/09 17:24:33 petere Exp $ MODULE_big = pg_trgm ! OBJS = trgm_op.o trgm_gist.o DATA_built = pg_trgm.sql DATA = uninstall_pg_trgm.sql --- 1,7 ---- # $PostgreSQL: pgsql/contrib/pg_trgm/Makefile,v 1.6 2007/02/09 17:24:33 petere Exp $ MODULE_big = pg_trgm ! OBJS = trgm_op.o trgm_gist.o trgm_gin.o DATA_built = pg_trgm.sql DATA = uninstall_pg_trgm.sql Index: uninstall_pg_trgm.sql =================================================================== RCS file: /projects/cvsroot/pgsql/contrib/pg_trgm/uninstall_pg_trgm.sql,v retrieving revision 1.2 diff -c -r1.2 uninstall_pg_trgm.sql *** uninstall_pg_trgm.sql 13 Mar 2006 18:04:58 -0000 1.2 --- uninstall_pg_trgm.sql 22 Feb 2007 13:57:24 -0000 *************** *** 20,25 **** --- 20,33 ---- DROP TYPE gtrgm CASCADE; + DROP OPERATOR CLASS gin_trgm_ops USING gin; + + DROP FUNCTION gin_extract_trgm(text, internal); + + DROP FUNCTION gin_extract_trgm(text, internal, internal); + + DROP FUNCTION gin_trgm_consistent(internal, internal, text); + DROP OPERATOR % (text, text); DROP FUNCTION similarity_op(text,text); Index: pg_trgm.sql.in =================================================================== RCS file: /projects/cvsroot/pgsql/contrib/pg_trgm/pg_trgm.sql.in,v retrieving revision 1.2 diff -c -r1.2 pg_trgm.sql.in *** pg_trgm.sql.in 27 Feb 2006 16:09:48 -0000 1.2 --- pg_trgm.sql.in 22 Feb 2007 13:57:24 -0000 *************** *** 36,42 **** JOIN = contjoinsel ); ! --gist key CREATE FUNCTION gtrgm_in(cstring) RETURNS gtrgm AS 'MODULE_PATHNAME' --- 36,42 ---- JOIN = contjoinsel ); ! -- gist key CREATE FUNCTION gtrgm_in(cstring) RETURNS gtrgm AS 'MODULE_PATHNAME' *************** *** 53,59 **** OUTPUT = gtrgm_out ); ! -- support functions CREATE FUNCTION gtrgm_consistent(gtrgm,internal,int4) RETURNS bool AS 'MODULE_PATHNAME' --- 53,59 ---- OUTPUT = gtrgm_out ); ! -- support functions for gist CREATE FUNCTION gtrgm_consistent(gtrgm,internal,int4) RETURNS bool AS 'MODULE_PATHNAME' *************** *** 89,95 **** AS 'MODULE_PATHNAME' LANGUAGE C; ! -- create the operator class CREATE OPERATOR CLASS gist_trgm_ops FOR TYPE text USING gist AS --- 89,95 ---- AS 'MODULE_PATHNAME' LANGUAGE C; ! -- create the operator class for gist CREATE OPERATOR CLASS gist_trgm_ops FOR TYPE text USING gist AS *************** *** 103,107 **** --- 103,133 ---- FUNCTION 7 gtrgm_same (gtrgm, gtrgm, internal), STORAGE gtrgm; + -- support functions for gin + CREATE FUNCTION gin_extract_trgm(text, internal) + RETURNS internal + AS 'MODULE_PATHNAME' + LANGUAGE C; + + CREATE FUNCTION gin_extract_trgm(text, internal, internal) + RETURNS internal + AS 'MODULE_PATHNAME' + LANGUAGE C; + + CREATE FUNCTION gin_trgm_consistent(internal, internal, text) + RETURNS internal + AS 'MODULE_PATHNAME' + LANGUAGE C; + + -- create the operator class for gin + CREATE OPERATOR CLASS gin_trgm_ops + FOR TYPE text USING gin + AS + OPERATOR 1 % (text, text) RECHECK, + FUNCTION 1 btint4cmp (int4, int4), + FUNCTION 2 gin_extract_trgm (text, internal), + FUNCTION 3 gin_extract_trgm (text, internal, internal), + FUNCTION 4 gin_trgm_consistent (internal, internal, text), + STORAGE int4; COMMIT; Index: trgm.h =================================================================== RCS file: /projects/cvsroot/pgsql/contrib/pg_trgm/trgm.h,v retrieving revision 1.5 diff -c -r1.5 trgm.h *** trgm.h 14 Jul 2006 05:28:27 -0000 1.5 --- trgm.h 22 Feb 2007 13:57:24 -0000 *************** *** 28,33 **** --- 28,34 ---- *(((char*)(a))+2) = *(((char*)(b))+2); \ } while(0); + #define TRGMINT(a) ( (*(((char*)(a))+2)<<16)+(*(((char*)(a))+1)<<8)+*(((char*)(a))+0) ) typedef struct { Index: contrib/pg_trgm/trgm_gin.c =================================================================== RCS file: contrib/pg_trgm/trgm_gin.c diff -N contrib/pg_trgm/trgm_gin.c *** /dev/null 1 Jan 1970 00:00:00 -0000 --- contrib/pg_trgm/trgm_gin.c 1 Jan 1970 00:00:00 -0000 *************** *** 0 **** --- 1,77 ---- + #include "trgm.h" + + #include "access/gin.h" + #include "access/itup.h" + #include "access/tuptoaster.h" + #include "storage/bufpage.h" + #include "utils/array.h" + #include "utils/builtins.h" + + PG_FUNCTION_INFO_V1(gin_extract_trgm); + Datum gin_extract_trgm(PG_FUNCTION_ARGS); + + PG_FUNCTION_INFO_V1(gin_trgm_consistent); + Datum gin_trgm_consistent(PG_FUNCTION_ARGS); + + Datum + gin_extract_trgm(PG_FUNCTION_ARGS) + { + text *val = (text *) PG_GETARG_TEXT_P(0); + int32 *nentries = (int32 *) PG_GETARG_POINTER(1); + Datum *entries = NULL; + TRGM *trg; + int4 trglen; + + *nentries = 0; + + trg = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ); + trglen = ARRNELEM(trg); + + if (trglen > 0) + { + trgm *ptr; + int4 i = 0, + item; + + *nentries = (int32) trglen; + entries = (Datum *) palloc(sizeof(Datum) * trglen); + + ptr = GETARR(trg); + while (ptr - GETARR(trg) < ARRNELEM(trg)) + { + item = TRGMINT(ptr); + entries[i++] = Int32GetDatum(item); + + ptr++; + } + } + + PG_RETURN_POINTER(entries); + } + + Datum + gin_trgm_consistent(PG_FUNCTION_ARGS) + { + bool *check = (bool *) PG_GETARG_POINTER(0); + text *query = (text *) PG_GETARG_TEXT_P(2); + bool res = FALSE; + TRGM *trg; + int4 i, + trglen, + ntrue = 0; + + trg = generate_trgm(VARDATA(query), VARSIZE(query) - VARHDRSZ); + trglen = ARRNELEM(trg); + + for (i = 0; i < trglen; i++) + if (check[i]) + ntrue ++; + + #ifdef DIVUNION + res = (trglen == ntrue) ? true : ((((((float4) ntrue) / ((float4) (trglen - ntrue)))) >= trgm_limit) ? true : false); + #else + res = (trglen == 0) ? false : ((((((float4) ntrue) / ((float4) trglen))) >= trgm_limit) ? true : false); + #endif + + PG_RETURN_BOOL(res); + }