PostgreSQL Source Code git master
trgm_op.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "catalog/pg_collation_d.h"
#include "catalog/pg_type.h"
#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
#include "tsearch/ts_locale.h"
#include "utils/formatting.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_crc.h"
Include dependency graph for trgm_op.c:

Go to the source code of this file.

Data Structures

struct  pos_trgm
 

Macros

#define TRGM_BOUND_LEFT   0x01 /* trigram is left bound of word */
 
#define TRGM_BOUND_RIGHT   0x02 /* trigram is right bound of word */
 
#define WORD_SIMILARITY_CHECK_ONLY
 
#define WORD_SIMILARITY_STRICT
 

Typedefs

typedef uint8 TrgmBound
 

Functions

 PG_FUNCTION_INFO_V1 (set_limit)
 
 PG_FUNCTION_INFO_V1 (show_limit)
 
 PG_FUNCTION_INFO_V1 (show_trgm)
 
 PG_FUNCTION_INFO_V1 (similarity)
 
 PG_FUNCTION_INFO_V1 (word_similarity)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity)
 
 PG_FUNCTION_INFO_V1 (similarity_dist)
 
 PG_FUNCTION_INFO_V1 (similarity_op)
 
 PG_FUNCTION_INFO_V1 (word_similarity_op)
 
 PG_FUNCTION_INFO_V1 (word_similarity_commutator_op)
 
 PG_FUNCTION_INFO_V1 (word_similarity_dist_op)
 
 PG_FUNCTION_INFO_V1 (word_similarity_dist_commutator_op)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity_op)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity_commutator_op)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity_dist_op)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity_dist_commutator_op)
 
void _PG_init (void)
 
Datum set_limit (PG_FUNCTION_ARGS)
 
double index_strategy_get_limit (StrategyNumber strategy)
 
Datum show_limit (PG_FUNCTION_ARGS)
 
static int comp_trgm (const void *a, const void *b)
 
static char * find_word (char *str, int lenstr, char **endword, int *charlen)
 
void compact_trigram (trgm *tptr, char *str, int bytelen)
 
static trgmmake_trigrams (trgm *tptr, char *str, int bytelen, int charlen)
 
static int generate_trgm_only (trgm *trg, char *str, int slen, TrgmBound *bounds)
 
static void protect_out_of_mem (int slen)
 
TRGMgenerate_trgm (char *str, int slen)
 
static pos_trgmmake_positional_trgm (trgm *trg1, int len1, trgm *trg2, int len2)
 
static int comp_ptrgm (const void *v1, const void *v2)
 
static float4 iterate_word_similarity (int *trg2indexes, bool *found, int ulen1, int len2, int len, uint8 flags, TrgmBound *bounds)
 
static float4 calc_word_similarity (char *str1, int slen1, char *str2, int slen2, uint8 flags)
 
static const char * get_wildcard_part (const char *str, int lenstr, char *buf, int *bytelen, int *charlen)
 
TRGMgenerate_wildcard_trgm (const char *str, int slen)
 
uint32 trgm2int (trgm *ptr)
 
Datum show_trgm (PG_FUNCTION_ARGS)
 
float4 cnt_sml (TRGM *trg1, TRGM *trg2, bool inexact)
 
bool trgm_contained_by (TRGM *trg1, TRGM *trg2)
 
bool * trgm_presence_map (TRGM *query, TRGM *key)
 
Datum similarity (PG_FUNCTION_ARGS)
 
Datum word_similarity (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity (PG_FUNCTION_ARGS)
 
Datum similarity_dist (PG_FUNCTION_ARGS)
 
Datum similarity_op (PG_FUNCTION_ARGS)
 
Datum word_similarity_op (PG_FUNCTION_ARGS)
 
Datum word_similarity_commutator_op (PG_FUNCTION_ARGS)
 
Datum word_similarity_dist_op (PG_FUNCTION_ARGS)
 
Datum word_similarity_dist_commutator_op (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity_op (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity_commutator_op (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity_dist_op (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity_dist_commutator_op (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
double similarity_threshold = 0.3f
 
double word_similarity_threshold = 0.6f
 
double strict_word_similarity_threshold = 0.5f
 

Macro Definition Documentation

◆ TRGM_BOUND_LEFT

#define TRGM_BOUND_LEFT   0x01 /* trigram is left bound of word */

Definition at line 54 of file trgm_op.c.

◆ TRGM_BOUND_RIGHT

#define TRGM_BOUND_RIGHT   0x02 /* trigram is right bound of word */

Definition at line 55 of file trgm_op.c.

◆ WORD_SIMILARITY_CHECK_ONLY

#define WORD_SIMILARITY_CHECK_ONLY
Value:
0x01 /* only check existence of similar
* search pattern in text */

Definition at line 58 of file trgm_op.c.

◆ WORD_SIMILARITY_STRICT

#define WORD_SIMILARITY_STRICT
Value:
0x02 /* force bounds of extent to match
* word bounds */

Definition at line 59 of file trgm_op.c.

Typedef Documentation

◆ TrgmBound

typedef uint8 TrgmBound

Definition at line 53 of file trgm_op.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 65 of file trgm_op.c.

68{
69 /* Define custom GUC variables. */
70 DefineCustomRealVariable("pg_trgm.similarity_threshold",
71 "Sets the threshold used by the % operator.",
72 "Valid range is 0.0 .. 1.0.",
74 0.3f,
75 0.0,
76 1.0,
78 0,
79 NULL,
80 NULL,
81 NULL);
82 DefineCustomRealVariable("pg_trgm.word_similarity_threshold",
83 "Sets the threshold used by the <% operator.",
84 "Valid range is 0.0 .. 1.0.",
86 0.6f,
87 0.0,
88 1.0,
90 0,
91 NULL,
92 NULL,
93 NULL);
94 DefineCustomRealVariable("pg_trgm.strict_word_similarity_threshold",
95 "Sets the threshold used by the <<% operator.",
96 "Valid range is 0.0 .. 1.0.",
98 0.5f,
99 0.0,
100 1.0,
102 0,
103 NULL,
104 NULL,
105 NULL);
106
void DefineCustomRealVariable(const char *name, const char *short_desc, const char *long_desc, double *valueAddr, double bootValue, double minValue, double maxValue, GucContext context, int flags, GucRealCheckHook check_hook, GucRealAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:5188
@ PGC_USERSET
Definition: guc.h:79
double strict_word_similarity_threshold
Definition: trgm_op.c:26
double word_similarity_threshold
Definition: trgm_op.c:25
double similarity_threshold
Definition: trgm_op.c:24

References DefineCustomRealVariable(), MarkGUCPrefixReserved(), PGC_USERSET, similarity_threshold, strict_word_similarity_threshold, and word_similarity_threshold.

◆ calc_word_similarity()

static float4 calc_word_similarity ( char *  str1,
int  slen1,
char *  str2,
int  slen2,
uint8  flags 
)
static

Definition at line 624 of file trgm_op.c.

628{
629 bool *found;
630 pos_trgm *ptrg;
631 trgm *trg1;
632 trgm *trg2;
633 int len1,
634 len2,
635 len,
636 i,
637 j,
638 ulen1;
639 int *trg2indexes;
640 float4 result;
641 TrgmBound *bounds;
642
643 protect_out_of_mem(slen1 + slen2);
644
645 /* Make positional trigrams */
646 trg1 = (trgm *) palloc(sizeof(trgm) * (slen1 / 2 + 1) * 3);
647 trg2 = (trgm *) palloc(sizeof(trgm) * (slen2 / 2 + 1) * 3);
648 if (flags & WORD_SIMILARITY_STRICT)
649 bounds = (TrgmBound *) palloc0(sizeof(TrgmBound) * (slen2 / 2 + 1) * 3);
650 else
651 bounds = NULL;
652
653 len1 = generate_trgm_only(trg1, str1, slen1, NULL);
654 len2 = generate_trgm_only(trg2, str2, slen2, bounds);
655
656 ptrg = make_positional_trgm(trg1, len1, trg2, len2);
657 len = len1 + len2;
658 qsort(ptrg, len, sizeof(pos_trgm), comp_ptrgm);
659
660 pfree(trg1);
661 pfree(trg2);
662
663 /*
664 * Merge positional trigrams array: enumerate each trigram and find its
665 * presence in required word.
666 */
667 trg2indexes = (int *) palloc(sizeof(int) * len2);
668 found = (bool *) palloc0(sizeof(bool) * len);
669
670 ulen1 = 0;
671 j = 0;
672 for (i = 0; i < len; i++)
673 {
674 if (i > 0)
675 {
676 int cmp = CMPTRGM(ptrg[i - 1].trg, ptrg[i].trg);
677
678 if (cmp != 0)
679 {
680 if (found[j])
681 ulen1++;
682 j++;
683 }
684 }
685
686 if (ptrg[i].index >= 0)
687 {
688 trg2indexes[ptrg[i].index] = j;
689 }
690 else
691 {
692 found[j] = true;
693 }
694 }
695 if (found[j])
696 ulen1++;
697
698 /* Run iterative procedure to find maximum similarity with word */
699 result = iterate_word_similarity(trg2indexes, found, ulen1, len2, len,
700 flags, bounds);
701
702 pfree(trg2indexes);
703 pfree(found);
704 pfree(ptrg);
705
float float4
Definition: c.h:586
int j
Definition: isn.c:73
int i
Definition: isn.c:72
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
void * palloc(Size size)
Definition: mcxt.c:1317
const void size_t len
#define qsort(a, b, c, d)
Definition: port.h:475
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
Definition: type.h:96
int index
Definition: trgm_op.c:49
#define CMPTRGM(a, b)
Definition: trgm.h:45
char trgm[3]
Definition: trgm.h:41
static float4 iterate_word_similarity(int *trg2indexes, bool *found, int ulen1, int len2, int len, uint8 flags, TrgmBound *bounds)
Definition: trgm_op.c:456
uint8 TrgmBound
Definition: trgm_op.c:53
static void protect_out_of_mem(int slen)
Definition: trgm_op.c:342
static pos_trgm * make_positional_trgm(trgm *trg1, int len1, trgm *trg2, int len2)
Definition: trgm_op.c:400
static int comp_ptrgm(const void *v1, const void *v2)
Definition: trgm_op.c:427
#define WORD_SIMILARITY_STRICT
Definition: trgm_op.c:59
static int generate_trgm_only(trgm *trg, char *str, int slen, TrgmBound *bounds)
Definition: trgm_op.c:278

References cmp(), CMPTRGM, comp_ptrgm(), generate_trgm_only(), i, pos_trgm::index, iterate_word_similarity(), j, len, make_positional_trgm(), palloc(), palloc0(), pfree(), protect_out_of_mem(), qsort, and WORD_SIMILARITY_STRICT.

Referenced by strict_word_similarity(), strict_word_similarity_commutator_op(), strict_word_similarity_dist_commutator_op(), strict_word_similarity_dist_op(), strict_word_similarity_op(), word_similarity(), word_similarity_commutator_op(), word_similarity_dist_commutator_op(), word_similarity_dist_op(), and word_similarity_op().

◆ cnt_sml()

float4 cnt_sml ( TRGM trg1,
TRGM trg2,
bool  inexact 
)

Definition at line 994 of file trgm_op.c.

997{
998 trgm *ptr1,
999 *ptr2;
1000 int count = 0;
1001 int len1,
1002 len2;
1003
1004 ptr1 = GETARR(trg1);
1005 ptr2 = GETARR(trg2);
1006
1007 len1 = ARRNELEM(trg1);
1008 len2 = ARRNELEM(trg2);
1009
1010 /* explicit test is needed to avoid 0/0 division when both lengths are 0 */
1011 if (len1 <= 0 || len2 <= 0)
1012 return (float4) 0.0;
1013
1014 while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1015 {
1016 int res = CMPTRGM(ptr1, ptr2);
1017
1018 if (res < 0)
1019 ptr1++;
1020 else if (res > 0)
1021 ptr2++;
1022 else
1023 {
1024 ptr1++;
1025 ptr2++;
1026 count++;
1027 }
1028 }
1029
1030 /*
1031 * If inexact then len2 is equal to count, because we don't know actual
1032 * length of second string in inexact search and we can assume that count
1033 * is a lower bound of len2.
1034 */
#define ARRNELEM(x)
Definition: trgm.h:101
#define GETARR(x)
Definition: trgm.h:100

References ARRNELEM, CALCSML, CMPTRGM, GETARR, and res.

Referenced by gtrgm_consistent(), gtrgm_distance(), and similarity().

◆ comp_ptrgm()

static int comp_ptrgm ( const void *  v1,
const void *  v2 
)
static

Definition at line 427 of file trgm_op.c.

430{
431 const pos_trgm *p1 = (const pos_trgm *) v1;
432 const pos_trgm *p2 = (const pos_trgm *) v2;
433 int cmp;
434
435 cmp = CMPTRGM(p1->trg, p2->trg);
436 if (cmp != 0)
437 return cmp;
438
trgm trg
Definition: trgm_op.c:48

References cmp(), CMPTRGM, pos_trgm::index, p2, pg_cmp_s32(), and pos_trgm::trg.

Referenced by calc_word_similarity().

◆ comp_trgm()

static int comp_trgm ( const void *  a,
const void *  b 
)
static

Definition at line 164 of file trgm_op.c.

167{

References a, b, and CMPTRGM.

Referenced by generate_trgm(), and generate_wildcard_trgm().

◆ compact_trigram()

void compact_trigram ( trgm tptr,
char *  str,
int  bytelen 
)

Definition at line 201 of file trgm_op.c.

204{
205 if (bytelen == 3)
206 {
207 CPTRGM(tptr, str);
208 }
209 else
210 {
212
214 COMP_LEGACY_CRC32(crc, str, bytelen);
216
217 /*
218 * use only 3 upper bytes from crc, hope, it's good enough hashing
219 */
220 CPTRGM(tptr, &crc);
const char * str
return crc
uint32 pg_crc32
Definition: pg_crc.h:37
#define INIT_LEGACY_CRC32(crc)
Definition: pg_crc.h:79
#define COMP_LEGACY_CRC32(crc, data, len)
Definition: pg_crc.h:81
#define FIN_LEGACY_CRC32(crc)
Definition: pg_crc.h:80
#define CPTRGM(a, b)
Definition: trgm.h:47

References COMP_LEGACY_CRC32, CPTRGM, crc, FIN_LEGACY_CRC32, INIT_LEGACY_CRC32, and str.

Referenced by fillTrgm(), and make_trigrams().

◆ find_word()

static char * find_word ( char *  str,
int  lenstr,
char **  endword,
int *  charlen 
)
static

Definition at line 174 of file trgm_op.c.

177{
178 char *beginword = str;
179
180 while (beginword - str < lenstr && !ISWORDCHR(beginword))
181 beginword += pg_mblen(beginword);
182
183 if (beginword - str >= lenstr)
184 return NULL;
185
186 *endword = beginword;
187 *charlen = 0;
188 while (*endword - str < lenstr && ISWORDCHR(*endword))
189 {
190 *endword += pg_mblen(*endword);
191 (*charlen)++;
192 }
193
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1023
#define ISWORDCHR(c)
Definition: trgm.h:53

References ISWORDCHR, pg_mblen(), and str.

Referenced by generate_trgm_only().

◆ generate_trgm()

TRGM * generate_trgm ( char *  str,
int  slen 
)

Definition at line 359 of file trgm_op.c.

362{
363 TRGM *trg;
364 int len;
365
366 protect_out_of_mem(slen);
367
368 trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
369 trg->flag = ARRKEY;
370
371 len = generate_trgm_only(GETARR(trg), str, slen, NULL);
373
374 if (len == 0)
375 return trg;
376
377 /*
378 * Make trigrams unique.
379 */
380 if (len > 1)
381 {
382 qsort(GETARR(trg), len, sizeof(trgm), comp_trgm);
383 len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
384 }
385
387
#define CALCGTSIZE(flag, siglen)
Definition: hstore_gist.c:60
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
Definition: trgm.h:61
uint8 flag
Definition: trgm.h:63
#define ARRKEY
Definition: trgm.h:90
#define TRGMHDRSIZE
Definition: trgm.h:67
static int comp_trgm(const void *a, const void *b)
Definition: trgm_op.c:164
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References ARRKEY, CALCGTSIZE, comp_trgm(), TRGM::flag, generate_trgm_only(), GETARR, len, palloc(), protect_out_of_mem(), qsort, qunique(), SET_VARSIZE, str, and TRGMHDRSIZE.

Referenced by gin_extract_query_trgm(), gin_extract_value_trgm(), gtrgm_compress(), gtrgm_consistent(), gtrgm_distance(), show_trgm(), and similarity().

◆ generate_trgm_only()

static int generate_trgm_only ( trgm trg,
char *  str,
int  slen,
TrgmBound bounds 
)
static

Definition at line 278 of file trgm_op.c.

281{
282 trgm *tptr;
283 char *buf;
284 int charlen,
285 bytelen;
286 char *bword,
287 *eword;
288
289 if (slen + LPADDING + RPADDING < 3 || slen == 0)
290 return 0;
291
292 tptr = trg;
293
294 /* Allocate a buffer for case-folded, blank-padded words */
295 buf = (char *) palloc(slen * pg_database_encoding_max_length() + 4);
296
297 if (LPADDING > 0)
298 {
299 *buf = ' ';
300 if (LPADDING > 1)
301 *(buf + 1) = ' ';
302 }
303
304 eword = str;
305 while ((bword = find_word(eword, slen - (eword - str), &eword, &charlen)) != NULL)
306 {
307#ifdef IGNORECASE
308 bword = str_tolower(bword, eword - bword, DEFAULT_COLLATION_OID);
309 bytelen = strlen(bword);
310#else
311 bytelen = eword - bword;
312#endif
313
314 memcpy(buf + LPADDING, bword, bytelen);
315
316#ifdef IGNORECASE
317 pfree(bword);
318#endif
319
320 buf[LPADDING + bytelen] = ' ';
321 buf[LPADDING + bytelen + 1] = ' ';
322
323 /* Calculate trigrams marking their bounds if needed */
324 if (bounds)
325 bounds[tptr - trg] |= TRGM_BOUND_LEFT;
326 tptr = make_trigrams(tptr, buf, bytelen + LPADDING + RPADDING,
327 charlen + LPADDING + RPADDING);
328 if (bounds)
329 bounds[tptr - trg - 1] |= TRGM_BOUND_RIGHT;
330 }
331
332 pfree(buf);
333
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
Definition: formatting.c:1637
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1546
static char * buf
Definition: pg_test_fsync.c:72
#define RPADDING
Definition: trgm.h:17
#define LPADDING
Definition: trgm.h:16
static char * find_word(char *str, int lenstr, char **endword, int *charlen)
Definition: trgm_op.c:174
#define TRGM_BOUND_RIGHT
Definition: trgm_op.c:55
#define TRGM_BOUND_LEFT
Definition: trgm_op.c:54
static trgm * make_trigrams(trgm *tptr, char *str, int bytelen, int charlen)
Definition: trgm_op.c:226

References buf, find_word(), LPADDING, make_trigrams(), palloc(), pfree(), pg_database_encoding_max_length(), RPADDING, str, str_tolower(), TRGM_BOUND_LEFT, and TRGM_BOUND_RIGHT.

Referenced by calc_word_similarity(), and generate_trgm().

◆ generate_wildcard_trgm()

TRGM * generate_wildcard_trgm ( const char *  str,
int  slen 
)

Definition at line 869 of file trgm_op.c.

872{
873 TRGM *trg;
874 char *buf,
875 *buf2;
876 trgm *tptr;
877 int len,
878 charlen,
879 bytelen;
880 const char *eword;
881
882 protect_out_of_mem(slen);
883
884 trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
885 trg->flag = ARRKEY;
887
888 if (slen + LPADDING + RPADDING < 3 || slen == 0)
889 return trg;
890
891 tptr = GETARR(trg);
892
893 /* Allocate a buffer for blank-padded, but not yet case-folded, words */
894 buf = palloc(sizeof(char) * (slen + 4));
895
896 /*
897 * Extract trigrams from each substring extracted by get_wildcard_part.
898 */
899 eword = str;
900 while ((eword = get_wildcard_part(eword, slen - (eword - str),
901 buf, &bytelen, &charlen)) != NULL)
902 {
903#ifdef IGNORECASE
904 buf2 = str_tolower(buf, bytelen, DEFAULT_COLLATION_OID);
905 bytelen = strlen(buf2);
906#else
907 buf2 = buf;
908#endif
909
910 /*
911 * count trigrams
912 */
913 tptr = make_trigrams(tptr, buf2, bytelen, charlen);
914
915#ifdef IGNORECASE
916 pfree(buf2);
917#endif
918 }
919
920 pfree(buf);
921
922 if ((len = tptr - GETARR(trg)) == 0)
923 return trg;
924
925 /*
926 * Make trigrams unique.
927 */
928 if (len > 1)
929 {
930 qsort(GETARR(trg), len, sizeof(trgm), comp_trgm);
931 len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
932 }
933
935
static const char * get_wildcard_part(const char *str, int lenstr, char *buf, int *bytelen, int *charlen)
Definition: trgm_op.c:724

References ARRKEY, buf, CALCGTSIZE, comp_trgm(), TRGM::flag, get_wildcard_part(), GETARR, len, LPADDING, make_trigrams(), palloc(), pfree(), protect_out_of_mem(), qsort, qunique(), RPADDING, SET_VARSIZE, str, str_tolower(), and TRGMHDRSIZE.

Referenced by gin_extract_query_trgm(), and gtrgm_consistent().

◆ get_wildcard_part()

static const char * get_wildcard_part ( const char *  str,
int  lenstr,
char *  buf,
int *  bytelen,
int *  charlen 
)
static

Definition at line 724 of file trgm_op.c.

728{
729 const char *beginword = str;
730 const char *endword;
731 char *s = buf;
732 bool in_leading_wildcard_meta = false;
733 bool in_trailing_wildcard_meta = false;
734 bool in_escape = false;
735 int clen;
736
737 /*
738 * Find the first word character, remembering whether preceding character
739 * was wildcard meta-character. Note that the in_escape state persists
740 * from this loop to the next one, since we may exit at a word character
741 * that is in_escape.
742 */
743 while (beginword - str < lenstr)
744 {
745 if (in_escape)
746 {
747 if (ISWORDCHR(beginword))
748 break;
749 in_escape = false;
750 in_leading_wildcard_meta = false;
751 }
752 else
753 {
754 if (ISESCAPECHAR(beginword))
755 in_escape = true;
756 else if (ISWILDCARDCHAR(beginword))
757 in_leading_wildcard_meta = true;
758 else if (ISWORDCHR(beginword))
759 break;
760 else
761 in_leading_wildcard_meta = false;
762 }
763 beginword += pg_mblen(beginword);
764 }
765
766 /*
767 * Handle string end.
768 */
769 if (beginword - str >= lenstr)
770 return NULL;
771
772 /*
773 * Add left padding spaces if preceding character wasn't wildcard
774 * meta-character.
775 */
776 *charlen = 0;
777 if (!in_leading_wildcard_meta)
778 {
779 if (LPADDING > 0)
780 {
781 *s++ = ' ';
782 (*charlen)++;
783 if (LPADDING > 1)
784 {
785 *s++ = ' ';
786 (*charlen)++;
787 }
788 }
789 }
790
791 /*
792 * Copy data into buf until wildcard meta-character, non-word character or
793 * string boundary. Strip escapes during copy.
794 */
795 endword = beginword;
796 while (endword - str < lenstr)
797 {
798 clen = pg_mblen(endword);
799 if (in_escape)
800 {
801 if (ISWORDCHR(endword))
802 {
803 memcpy(s, endword, clen);
804 (*charlen)++;
805 s += clen;
806 }
807 else
808 {
809 /*
810 * Back up endword to the escape character when stopping at an
811 * escaped char, so that subsequent get_wildcard_part will
812 * restart from the escape character. We assume here that
813 * escape chars are single-byte.
814 */
815 endword--;
816 break;
817 }
818 in_escape = false;
819 }
820 else
821 {
822 if (ISESCAPECHAR(endword))
823 in_escape = true;
824 else if (ISWILDCARDCHAR(endword))
825 {
826 in_trailing_wildcard_meta = true;
827 break;
828 }
829 else if (ISWORDCHR(endword))
830 {
831 memcpy(s, endword, clen);
832 (*charlen)++;
833 s += clen;
834 }
835 else
836 break;
837 }
838 endword += clen;
839 }
840
841 /*
842 * Add right padding spaces if next character isn't wildcard
843 * meta-character.
844 */
845 if (!in_trailing_wildcard_meta)
846 {
847 if (RPADDING > 0)
848 {
849 *s++ = ' ';
850 (*charlen)++;
851 if (RPADDING > 1)
852 {
853 *s++ = ' ';
854 (*charlen)++;
855 }
856 }
857 }
858
859 *bytelen = s - buf;
#define ISESCAPECHAR(x)
Definition: trgm.h:57
#define ISWILDCARDCHAR(x)
Definition: trgm.h:58

References buf, ISESCAPECHAR, ISWILDCARDCHAR, ISWORDCHR, LPADDING, pg_mblen(), RPADDING, and str.

Referenced by generate_wildcard_trgm().

◆ index_strategy_get_limit()

double index_strategy_get_limit ( StrategyNumber  strategy)

Definition at line 135 of file trgm_op.c.

138{
139 switch (strategy)
140 {
147 default:
148 elog(ERROR, "unrecognized strategy number: %d", strategy);
149 break;
150 }
151
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define WordSimilarityStrategyNumber
Definition: trgm.h:35
#define StrictWordSimilarityStrategyNumber
Definition: trgm.h:37
#define SimilarityStrategyNumber
Definition: trgm.h:29

References elog, ERROR, similarity_threshold, SimilarityStrategyNumber, strict_word_similarity_threshold, StrictWordSimilarityStrategyNumber, word_similarity_threshold, and WordSimilarityStrategyNumber.

Referenced by gin_trgm_consistent(), gin_trgm_triconsistent(), and gtrgm_consistent().

◆ iterate_word_similarity()

static float4 iterate_word_similarity ( int *  trg2indexes,
bool *  found,
int  ulen1,
int  len2,
int  len,
uint8  flags,
TrgmBound bounds 
)
static

Definition at line 456 of file trgm_op.c.

465{
466 int *lastpos,
467 i,
468 ulen2 = 0,
469 count = 0,
470 upper = -1,
471 lower;
472 float4 smlr_cur,
473 smlr_max = 0.0f;
474 double threshold;
475
476 Assert(bounds || !(flags & WORD_SIMILARITY_STRICT));
477
478 /* Select appropriate threshold */
479 threshold = (flags & WORD_SIMILARITY_STRICT) ?
482
483 /*
484 * Consider first trigram as initial lower bound for strict word
485 * similarity, or initialize it later with first trigram present for plain
486 * word similarity.
487 */
488 lower = (flags & WORD_SIMILARITY_STRICT) ? 0 : -1;
489
490 /* Memorise last position of each trigram */
491 lastpos = (int *) palloc(sizeof(int) * len);
492 memset(lastpos, -1, sizeof(int) * len);
493
494 for (i = 0; i < len2; i++)
495 {
496 int trgindex;
497
499
500 /* Get index of next trigram */
501 trgindex = trg2indexes[i];
502
503 /* Update last position of this trigram */
504 if (lower >= 0 || found[trgindex])
505 {
506 if (lastpos[trgindex] < 0)
507 {
508 ulen2++;
509 if (found[trgindex])
510 count++;
511 }
512 lastpos[trgindex] = i;
513 }
514
515 /*
516 * Adjust upper bound if trigram is upper bound of word for strict
517 * word similarity, or if trigram is present in required substring for
518 * plain word similarity
519 */
520 if ((flags & WORD_SIMILARITY_STRICT) ? (bounds[i] & TRGM_BOUND_RIGHT)
521 : found[trgindex])
522 {
523 int prev_lower,
524 tmp_ulen2,
525 tmp_lower,
526 tmp_count;
527
528 upper = i;
529 if (lower == -1)
530 {
531 lower = i;
532 ulen2 = 1;
533 }
534
535 smlr_cur = CALCSML(count, ulen1, ulen2);
536
537 /* Also try to adjust lower bound for greater similarity */
538 tmp_count = count;
539 tmp_ulen2 = ulen2;
540 prev_lower = lower;
541 for (tmp_lower = lower; tmp_lower <= upper; tmp_lower++)
542 {
543 float smlr_tmp;
544 int tmp_trgindex;
545
546 /*
547 * Adjust lower bound only if trigram is lower bound of word
548 * for strict word similarity, or consider every trigram as
549 * lower bound for plain word similarity.
550 */
551 if (!(flags & WORD_SIMILARITY_STRICT)
552 || (bounds[tmp_lower] & TRGM_BOUND_LEFT))
553 {
554 smlr_tmp = CALCSML(tmp_count, ulen1, tmp_ulen2);
555 if (smlr_tmp > smlr_cur)
556 {
557 smlr_cur = smlr_tmp;
558 ulen2 = tmp_ulen2;
559 lower = tmp_lower;
560 count = tmp_count;
561 }
562
563 /*
564 * If we only check that word similarity is greater than
565 * threshold we do not need to calculate a maximum
566 * similarity.
567 */
568 if ((flags & WORD_SIMILARITY_CHECK_ONLY)
569 && smlr_cur >= threshold)
570 break;
571 }
572
573 tmp_trgindex = trg2indexes[tmp_lower];
574 if (lastpos[tmp_trgindex] == tmp_lower)
575 {
576 tmp_ulen2--;
577 if (found[tmp_trgindex])
578 tmp_count--;
579 }
580 }
581
582 smlr_max = Max(smlr_max, smlr_cur);
583
584 /*
585 * if we only check that word similarity is greater than threshold
586 * we do not need to calculate a maximum similarity.
587 */
588 if ((flags & WORD_SIMILARITY_CHECK_ONLY) && smlr_max >= threshold)
589 break;
590
591 for (tmp_lower = prev_lower; tmp_lower < lower; tmp_lower++)
592 {
593 int tmp_trgindex;
594
595 tmp_trgindex = trg2indexes[tmp_lower];
596 if (lastpos[tmp_trgindex] == tmp_lower)
597 lastpos[tmp_trgindex] = -1;
598 }
599 }
600 }
601
602 pfree(lastpos);
603
#define Max(x, y)
Definition: c.h:955
#define Assert(condition)
Definition: c.h:815
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:80
#define CALCSML(count, len1, len2)
Definition: trgm.h:110
#define WORD_SIMILARITY_CHECK_ONLY
Definition: trgm_op.c:58

References Assert, CALCSML, CHECK_FOR_INTERRUPTS, i, len, lower(), Max, palloc(), pfree(), strict_word_similarity_threshold, TRGM_BOUND_LEFT, TRGM_BOUND_RIGHT, upper(), WORD_SIMILARITY_CHECK_ONLY, WORD_SIMILARITY_STRICT, and word_similarity_threshold.

Referenced by calc_word_similarity().

◆ make_positional_trgm()

static pos_trgm * make_positional_trgm ( trgm trg1,
int  len1,
trgm trg2,
int  len2 
)
static

Definition at line 400 of file trgm_op.c.

403{
404 pos_trgm *result;
405 int i,
406 len = len1 + len2;
407
408 result = (pos_trgm *) palloc(sizeof(pos_trgm) * len);
409
410 for (i = 0; i < len1; i++)
411 {
412 memcpy(&result[i].trg, &trg1[i], sizeof(trgm));
413 result[i].index = -1;
414 }
415
416 for (i = 0; i < len2; i++)
417 {
418 memcpy(&result[i + len1].trg, &trg2[i], sizeof(trgm));
419 result[i + len1].index = i;
420 }
421

References i, pos_trgm::index, len, and palloc().

Referenced by calc_word_similarity().

◆ make_trigrams()

static trgm * make_trigrams ( trgm tptr,
char *  str,
int  bytelen,
int  charlen 
)
static

Definition at line 226 of file trgm_op.c.

229{
230 char *ptr = str;
231
232 if (charlen < 3)
233 return tptr;
234
235 if (bytelen > charlen)
236 {
237 /* Find multibyte character boundaries and apply compact_trigram */
238 int lenfirst = pg_mblen(str),
239 lenmiddle = pg_mblen(str + lenfirst),
240 lenlast = pg_mblen(str + lenfirst + lenmiddle);
241
242 while ((ptr - str) + lenfirst + lenmiddle + lenlast <= bytelen)
243 {
244 compact_trigram(tptr, ptr, lenfirst + lenmiddle + lenlast);
245
246 ptr += lenfirst;
247 tptr++;
248
249 lenfirst = lenmiddle;
250 lenmiddle = lenlast;
251 lenlast = pg_mblen(ptr + lenfirst + lenmiddle);
252 }
253 }
254 else
255 {
256 /* Fast path when there are no multibyte characters */
257 Assert(bytelen == charlen);
258
259 while (ptr - str < bytelen - 2 /* number of trigrams = strlen - 2 */ )
260 {
261 CPTRGM(tptr, ptr);
262 ptr++;
263 tptr++;
264 }
265 }
266
void compact_trigram(trgm *tptr, char *str, int bytelen)
Definition: trgm_op.c:201

References Assert, compact_trigram(), CPTRGM, pg_mblen(), and str.

Referenced by generate_trgm_only(), and generate_wildcard_trgm().

◆ PG_FUNCTION_INFO_V1() [1/16]

PG_FUNCTION_INFO_V1 ( set_limit  )

◆ PG_FUNCTION_INFO_V1() [2/16]

PG_FUNCTION_INFO_V1 ( show_limit  )

◆ PG_FUNCTION_INFO_V1() [3/16]

PG_FUNCTION_INFO_V1 ( show_trgm  )

◆ PG_FUNCTION_INFO_V1() [4/16]

PG_FUNCTION_INFO_V1 ( similarity  )

◆ PG_FUNCTION_INFO_V1() [5/16]

PG_FUNCTION_INFO_V1 ( similarity_dist  )

◆ PG_FUNCTION_INFO_V1() [6/16]

PG_FUNCTION_INFO_V1 ( similarity_op  )

◆ PG_FUNCTION_INFO_V1() [7/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity  )

◆ PG_FUNCTION_INFO_V1() [8/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [9/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_dist_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [10/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_dist_op  )

◆ PG_FUNCTION_INFO_V1() [11/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_op  )

◆ PG_FUNCTION_INFO_V1() [12/16]

PG_FUNCTION_INFO_V1 ( word_similarity  )

◆ PG_FUNCTION_INFO_V1() [13/16]

PG_FUNCTION_INFO_V1 ( word_similarity_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [14/16]

PG_FUNCTION_INFO_V1 ( word_similarity_dist_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [15/16]

PG_FUNCTION_INFO_V1 ( word_similarity_dist_op  )

◆ PG_FUNCTION_INFO_V1() [16/16]

PG_FUNCTION_INFO_V1 ( word_similarity_op  )

◆ protect_out_of_mem()

static void protect_out_of_mem ( int  slen)
static

Definition at line 342 of file trgm_op.c.

345{
346 if ((Size) (slen / 2) >= (MaxAllocSize / (sizeof(trgm) * 3)) ||
349 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
size_t Size
Definition: c.h:562
int errcode(int sqlerrcode)
Definition: elog.c:853
#define ereport(elevel,...)
Definition: elog.h:149
#define MaxAllocSize
Definition: fe_memutils.h:22

References ereport, errcode(), errmsg(), ERROR, MaxAllocSize, and pg_database_encoding_max_length().

Referenced by calc_word_similarity(), generate_trgm(), and generate_wildcard_trgm().

◆ set_limit()

Datum set_limit ( PG_FUNCTION_ARGS  )

Definition at line 113 of file trgm_op.c.

116{
117 float4 nlimit = PG_GETARG_FLOAT4(0);
118 char *nlimit_str;
119 Oid func_out_oid;
120 bool is_varlena;
121
122 getTypeOutputInfo(FLOAT4OID, &func_out_oid, &is_varlena);
123
124 nlimit_str = OidOutputFunctionCall(func_out_oid, Float4GetDatum(nlimit));
125
126 SetConfigOption("pg_trgm.similarity_threshold", nlimit_str,
128
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:281
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4332
@ PGC_S_SESSION
Definition: guc.h:126
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2934
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:480
unsigned int Oid
Definition: postgres_ext.h:32

References Float4GetDatum(), getTypeOutputInfo(), OidOutputFunctionCall(), PG_GETARG_FLOAT4, PG_RETURN_FLOAT4, PGC_S_SESSION, PGC_USERSET, SetConfigOption(), and similarity_threshold.

◆ show_limit()

Datum show_limit ( PG_FUNCTION_ARGS  )

Definition at line 158 of file trgm_op.c.

161{

References PG_RETURN_FLOAT4, and similarity_threshold.

◆ show_trgm()

Datum show_trgm ( PG_FUNCTION_ARGS  )

Definition at line 952 of file trgm_op.c.

955{
956 text *in = PG_GETARG_TEXT_PP(0);
957 TRGM *trg;
958 Datum *d;
959 ArrayType *a;
960 trgm *ptr;
961 int i;
962
964 d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(trg)));
965
966 for (i = 0, ptr = GETARR(trg); i < ARRNELEM(trg); i++, ptr++)
967 {
969
971 {
972 snprintf(VARDATA(item), 12, "0x%06x", trgm2int(ptr));
973 SET_VARSIZE(item, VARHDRSZ + strlen(VARDATA(item)));
974 }
975 else
976 {
977 SET_VARSIZE(item, VARHDRSZ + 3);
978 CPTRGM(VARDATA(item), ptr);
979 }
980 d[i] = PointerGetDatum(item);
981 }
982
983 a = construct_array_builtin(d, ARRNELEM(trg), TEXTOID);
984
985 for (i = 0; i < ARRNELEM(trg); i++)
987
988 pfree(d);
989 pfree(trg);
990 PG_FREE_IF_COPY(in, 0);
991
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3381
#define VARHDRSZ
Definition: c.h:649
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
int a
Definition: isn.c:68
#define snprintf
Definition: port.h:239
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
Definition: c.h:644
#define ISPRINTABLETRGM(t)
Definition: trgm.h:55
TRGM * generate_trgm(char *str, int slen)
Definition: trgm_op.c:359
uint32 trgm2int(trgm *ptr)
Definition: trgm_op.c:938
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References a, ARRNELEM, construct_array_builtin(), CPTRGM, DatumGetPointer(), generate_trgm(), GETARR, i, ISPRINTABLETRGM, Max, palloc(), pfree(), pg_database_encoding_max_length(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_POINTER, PointerGetDatum(), SET_VARSIZE, snprintf, trgm2int(), VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

◆ similarity()

Datum similarity ( PG_FUNCTION_ARGS  )

Definition at line 1120 of file trgm_op.c.

1123{
1124 text *in1 = PG_GETARG_TEXT_PP(0);
1125 text *in2 = PG_GETARG_TEXT_PP(1);
1126 TRGM *trg1,
1127 *trg2;
1128 float4 res;
1129
1130 trg1 = generate_trgm(VARDATA_ANY(in1), VARSIZE_ANY_EXHDR(in1));
1131 trg2 = generate_trgm(VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2));
1132
1133 res = cnt_sml(trg1, trg2, false);
1134
1135 pfree(trg1);
1136 pfree(trg2);
1137 PG_FREE_IF_COPY(in1, 0);
1138 PG_FREE_IF_COPY(in2, 1);
1139
float4 cnt_sml(TRGM *trg1, TRGM *trg2, bool inexact)
Definition: trgm_op.c:994

References cnt_sml(), generate_trgm(), pfree(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_FLOAT4, res, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by similarity_dist(), and similarity_op().

◆ similarity_dist()

Datum similarity_dist ( PG_FUNCTION_ARGS  )

Definition at line 1174 of file trgm_op.c.

1177{
1179 PG_GETARG_DATUM(0),
1180 PG_GETARG_DATUM(1)));
1181
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
static float4 DatumGetFloat4(Datum X)
Definition: postgres.h:463
Datum similarity(PG_FUNCTION_ARGS)
Definition: trgm_op.c:1120

References DatumGetFloat4(), DirectFunctionCall2, PG_GETARG_DATUM, PG_RETURN_FLOAT4, res, and similarity().

◆ similarity_op()

◆ strict_word_similarity()

Datum strict_word_similarity ( PG_FUNCTION_ARGS  )

Definition at line 1158 of file trgm_op.c.

1161{
1162 text *in1 = PG_GETARG_TEXT_PP(0);
1163 text *in2 = PG_GETARG_TEXT_PP(1);
1164 float4 res;
1165
1167 VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2),
1169
1170 PG_FREE_IF_COPY(in1, 0);
1171 PG_FREE_IF_COPY(in2, 1);
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:624

References calc_word_similarity(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_FLOAT4, res, VARDATA_ANY, VARSIZE_ANY_EXHDR, and WORD_SIMILARITY_STRICT.

◆ strict_word_similarity_commutator_op()

◆ strict_word_similarity_dist_commutator_op()

Datum strict_word_similarity_dist_commutator_op ( PG_FUNCTION_ARGS  )

◆ strict_word_similarity_dist_op()

Datum strict_word_similarity_dist_op ( PG_FUNCTION_ARGS  )

◆ strict_word_similarity_op()

◆ trgm2int()

uint32 trgm2int ( trgm ptr)

Definition at line 938 of file trgm_op.c.

941{
942 uint32 val = 0;
943
944 val |= *(((unsigned char *) ptr));
945 val <<= 8;
946 val |= *(((unsigned char *) ptr) + 1);
947 val <<= 8;
948 val |= *(((unsigned char *) ptr) + 2);
949
uint32_t uint32
Definition: c.h:488
long val
Definition: informix.c:689

References val.

Referenced by gin_extract_query_trgm(), gin_extract_value_trgm(), and show_trgm().

◆ trgm_contained_by()

bool trgm_contained_by ( TRGM trg1,
TRGM trg2 
)

Definition at line 1042 of file trgm_op.c.

1045{
1046 trgm *ptr1,
1047 *ptr2;
1048 int len1,
1049 len2;
1050
1051 ptr1 = GETARR(trg1);
1052 ptr2 = GETARR(trg2);
1053
1054 len1 = ARRNELEM(trg1);
1055 len2 = ARRNELEM(trg2);
1056
1057 while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1058 {
1059 int res = CMPTRGM(ptr1, ptr2);
1060
1061 if (res < 0)
1062 return false;
1063 else if (res > 0)
1064 ptr2++;
1065 else
1066 {
1067 ptr1++;
1068 ptr2++;
1069 }
1070 }
1071 if (ptr1 - GETARR(trg1) < len1)
1072 return false;
1073 else

References ARRNELEM, CMPTRGM, GETARR, and res.

Referenced by gtrgm_consistent().

◆ trgm_presence_map()

bool * trgm_presence_map ( TRGM query,
TRGM key 
)

Definition at line 1081 of file trgm_op.c.

1084{
1085 bool *result;
1086 trgm *ptrq = GETARR(query),
1087 *ptrk = GETARR(key);
1088 int lenq = ARRNELEM(query),
1089 lenk = ARRNELEM(key),
1090 i;
1091
1092 result = (bool *) palloc0(lenq * sizeof(bool));
1093
1094 /* for each query trigram, do a binary search in the key array */
1095 for (i = 0; i < lenq; i++)
1096 {
1097 int lo = 0;
1098 int hi = lenk;
1099
1100 while (lo < hi)
1101 {
1102 int mid = (lo + hi) / 2;
1103 int res = CMPTRGM(ptrq, ptrk + mid);
1104
1105 if (res < 0)
1106 hi = mid;
1107 else if (res > 0)
1108 lo = mid + 1;
1109 else
1110 {
1111 result[i] = true;
1112 break;
1113 }
1114 }
1115 ptrq++;
1116 }
1117

References ARRNELEM, CMPTRGM, GETARR, i, sort-test::key, palloc0(), and res.

Referenced by gtrgm_consistent().

◆ word_similarity()

Datum word_similarity ( PG_FUNCTION_ARGS  )

Definition at line 1142 of file trgm_op.c.

1145{
1146 text *in1 = PG_GETARG_TEXT_PP(0);
1147 text *in2 = PG_GETARG_TEXT_PP(1);
1148 float4 res;
1149
1151 VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2),
1152 0);
1153
1154 PG_FREE_IF_COPY(in1, 0);
1155 PG_FREE_IF_COPY(in2, 1);

References calc_word_similarity(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_FLOAT4, res, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ word_similarity_commutator_op()

◆ word_similarity_dist_commutator_op()

Datum word_similarity_dist_commutator_op ( PG_FUNCTION_ARGS  )

Definition at line 1242 of file trgm_op.c.

1245{
1246 text *in1 = PG_GETARG_TEXT_PP(0);
1247 text *in2 = PG_GETARG_TEXT_PP(1);
1248 float4 res;
1249
1251 VARDATA_ANY(in1), VARSIZE_ANY_EXHDR(in1),
1252 0);
1253
1254 PG_FREE_IF_COPY(in1, 0);
1255 PG_FREE_IF_COPY(in2, 1);

References calc_word_similarity(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_FLOAT4, res, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ word_similarity_dist_op()

Datum word_similarity_dist_op ( PG_FUNCTION_ARGS  )

Definition at line 1226 of file trgm_op.c.

1229{
1230 text *in1 = PG_GETARG_TEXT_PP(0);
1231 text *in2 = PG_GETARG_TEXT_PP(1);
1232 float4 res;
1233
1235 VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2),
1236 0);
1237
1238 PG_FREE_IF_COPY(in1, 0);
1239 PG_FREE_IF_COPY(in2, 1);

References calc_word_similarity(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_FLOAT4, res, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ word_similarity_op()

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 21 of file trgm_op.c.

◆ similarity_threshold

double similarity_threshold = 0.3f

Definition at line 24 of file trgm_op.c.

Referenced by _PG_init(), index_strategy_get_limit(), set_limit(), show_limit(), and similarity_op().

◆ strict_word_similarity_threshold

double strict_word_similarity_threshold = 0.5f

◆ word_similarity_threshold

double word_similarity_threshold = 0.6f