PostgreSQL Source Code  git master
trgm_op.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "catalog/pg_type.h"
#include "lib/qunique.h"
#include "trgm.h"
#include "tsearch/ts_locale.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

void _PG_init (void)
 
 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)
 
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)
 
booltrgm_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 51 of file trgm_op.c.

◆ TRGM_BOUND_RIGHT

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

Definition at line 52 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 55 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 56 of file trgm_op.c.

Typedef Documentation

◆ TrgmBound

typedef uint8 TrgmBound

Definition at line 50 of file trgm_op.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 62 of file trgm_op.c.

65 {
66  /* Define custom GUC variables. */
67  DefineCustomRealVariable("pg_trgm.similarity_threshold",
68  "Sets the threshold used by the % operator.",
69  "Valid range is 0.0 .. 1.0.",
71  0.3,
72  0.0,
73  1.0,
75  0,
76  NULL,
77  NULL,
78  NULL);
79  DefineCustomRealVariable("pg_trgm.word_similarity_threshold",
80  "Sets the threshold used by the <% operator.",
81  "Valid range is 0.0 .. 1.0.",
83  0.6,
84  0.0,
85  1.0,
87  0,
88  NULL,
89  NULL,
90  NULL);
91  DefineCustomRealVariable("pg_trgm.strict_word_similarity_threshold",
92  "Sets the threshold used by the <<% operator.",
93  "Valid range is 0.0 .. 1.0.",
95  0.5,
96  0.0,
97  1.0,
99  0,
100  NULL,
101  NULL,
102  NULL);
103 
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:9264
@ PGC_USERSET
Definition: guc.h:76
double strict_word_similarity_threshold
Definition: trgm_op.c:21
double word_similarity_threshold
Definition: trgm_op.c:20
double similarity_threshold
Definition: trgm_op.c:19

References DefineCustomRealVariable(), EmitWarningsOnPlaceholders(), 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 621 of file trgm_op.c.

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

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 996 of file trgm_op.c.

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

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 424 of file trgm_op.c.

427 {
428  const pos_trgm *p1 = (const pos_trgm *) v1;
429  const pos_trgm *p2 = (const pos_trgm *) v2;
430  int cmp;
431 
432  cmp = CMPTRGM(p1->trg, p2->trg);
433  if (cmp != 0)
434  return cmp;
435 
436  if (p1->index < p2->index)
437  return -1;
438  else if (p1->index == p2->index)
439  return 0;
440  else
trgm trg
Definition: trgm_op.c:45

References cmp(), CMPTRGM, pos_trgm::index, p2, 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 161 of file trgm_op.c.

164 {

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 198 of file trgm_op.c.

201 {
202  if (bytelen == 3)
203  {
204  CPTRGM(tptr, str);
205  }
206  else
207  {
208  pg_crc32 crc;
209 
211  COMP_LEGACY_CRC32(crc, str, bytelen);
213 
214  /*
215  * use only 3 upper bytes from crc, hope, it's good enough hashing
216  */
217  CPTRGM(tptr, &crc);
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:48

References COMP_LEGACY_CRC32, CPTRGM, crc, FIN_LEGACY_CRC32, INIT_LEGACY_CRC32, and generate_unaccent_rules::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 171 of file trgm_op.c.

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

References ISWORDCHR, pg_mblen(), and generate_unaccent_rules::str.

Referenced by generate_trgm_only().

◆ generate_trgm()

TRGM* generate_trgm ( char *  str,
int  slen 
)

Definition at line 356 of file trgm_op.c.

359 {
360  TRGM *trg;
361  int len;
362 
363  protect_out_of_mem(slen);
364 
365  trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
366  trg->flag = ARRKEY;
367 
368  len = generate_trgm_only(GETARR(trg), str, slen, NULL);
370 
371  if (len == 0)
372  return trg;
373 
374  /*
375  * Make trigrams unique.
376  */
377  if (len > 1)
378  {
379  qsort((void *) GETARR(trg), len, sizeof(trgm), comp_trgm);
380  len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
381  }
382 
384 
#define CALCGTSIZE(flag, siglen)
Definition: hstore_gist.c:59
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
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:67
uint8 flag
Definition: trgm.h:69
#define ARRKEY
Definition: trgm.h:96
#define TRGMHDRSIZE
Definition: trgm.h:73
static int comp_trgm(const void *a, const void *b)
Definition: trgm_op.c:161

References ARRKEY, CALCGTSIZE, comp_trgm(), TRGM::flag, generate_trgm_only(), GETARR, len, palloc(), protect_out_of_mem(), qsort, qunique(), SET_VARSIZE, generate_unaccent_rules::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 275 of file trgm_op.c.

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

References buf, find_word(), lowerstr_with_len(), LPADDING, make_trigrams(), palloc(), pfree(), pg_database_encoding_max_length(), RPADDING, generate_unaccent_rules::str, 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 866 of file trgm_op.c.

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

References ARRKEY, buf, CALCGTSIZE, comp_trgm(), TRGM::flag, get_wildcard_part(), GETARR, len, lowerstr_with_len(), LPADDING, make_trigrams(), palloc(), pfree(), protect_out_of_mem(), qsort, qunique(), RPADDING, SET_VARSIZE, generate_unaccent_rules::str, 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 721 of file trgm_op.c.

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

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

Referenced by generate_wildcard_trgm().

◆ index_strategy_get_limit()

double index_strategy_get_limit ( StrategyNumber  strategy)

Definition at line 132 of file trgm_op.c.

135 {
136  switch (strategy)
137  {
139  return similarity_threshold;
144  default:
145  elog(ERROR, "unrecognized strategy number: %d", strategy);
146  break;
147  }
148 
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define WordSimilarityStrategyNumber
Definition: trgm.h:36
#define StrictWordSimilarityStrategyNumber
Definition: trgm.h:38
#define SimilarityStrategyNumber
Definition: trgm.h:30

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 457 of file trgm_op.c.

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

References Assert(), CALCSML, 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 397 of file trgm_op.c.

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

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 223 of file trgm_op.c.

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

References Assert(), compact_trigram(), CPTRGM, pg_mblen(), and generate_unaccent_rules::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 339 of file trgm_op.c.

342 {
343  if ((Size) (slen / 2) >= (MaxAllocSize / (sizeof(trgm) * 3)) ||
345  ereport(ERROR,
346  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
size_t Size
Definition: c.h:540
int errcode(int sqlerrcode)
Definition: elog.c:693
#define ereport(elevel,...)
Definition: elog.h:143
#define MaxAllocSize
Definition: memutils.h:40

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 110 of file trgm_op.c.

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

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 155 of file trgm_op.c.

158 {

References PG_RETURN_FLOAT4, and similarity_threshold.

◆ show_trgm()

Datum show_trgm ( PG_FUNCTION_ARGS  )

Definition at line 949 of file trgm_op.c.

952 {
953  text *in = PG_GETARG_TEXT_PP(0);
954  TRGM *trg;
955  Datum *d;
956  ArrayType *a;
957  trgm *ptr;
958  int i;
959 
961  d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(trg)));
962 
963  for (i = 0, ptr = GETARR(trg); i < ARRNELEM(trg); i++, ptr++)
964  {
965  text *item = (text *) palloc(VARHDRSZ + Max(12, pg_database_encoding_max_length() * 3));
966 
968  {
969  snprintf(VARDATA(item), 12, "0x%06x", trgm2int(ptr));
970  SET_VARSIZE(item, VARHDRSZ + strlen(VARDATA(item)));
971  }
972  else
973  {
974  SET_VARSIZE(item, VARHDRSZ + 3);
975  CPTRGM(VARDATA(item), ptr);
976  }
977  d[i] = PointerGetDatum(item);
978  }
979 
980  a = construct_array(d,
981  ARRNELEM(trg),
982  TEXTOID,
983  -1,
984  false,
985  TYPALIGN_INT);
986 
987  for (i = 0; i < ARRNELEM(trg); i++)
988  pfree(DatumGetPointer(d[i]));
989 
990  pfree(d);
991  pfree(trg);
992  PG_FREE_IF_COPY(in, 0);
993 
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3319
#define VARHDRSZ
Definition: c.h:627
#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:69
#define snprintf
Definition: port.h:225
uintptr_t Datum
Definition: postgres.h:411
#define DatumGetPointer(X)
Definition: postgres.h:593
#define VARDATA(PTR)
Definition: postgres.h:315
#define VARDATA_ANY(PTR)
Definition: postgres.h:361
#define PointerGetDatum(X)
Definition: postgres.h:600
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:354
Definition: c.h:622
#define ISPRINTABLETRGM(t)
Definition: trgm.h:61
TRGM * generate_trgm(char *str, int slen)
Definition: trgm_op.c:356
uint32 trgm2int(trgm *ptr)
Definition: trgm_op.c:935

References a, ARRNELEM, construct_array(), 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 1122 of file trgm_op.c.

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

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 1176 of file trgm_op.c.

1179 {
1181  PG_GETARG_DATUM(0),
1182  PG_GETARG_DATUM(1)));
1183 
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:633
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
static float4 DatumGetFloat4(Datum X)
Definition: postgres.h:708
Datum similarity(PG_FUNCTION_ARGS)
Definition: trgm_op.c:1122

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 1160 of file trgm_op.c.

1163 {
1164  text *in1 = PG_GETARG_TEXT_PP(0);
1165  text *in2 = PG_GETARG_TEXT_PP(1);
1166  float4 res;
1167 
1169  VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2),
1171 
1172  PG_FREE_IF_COPY(in1, 0);
1173  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:621

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  )

Definition at line 1308 of file trgm_op.c.

1311 {
1312  text *in1 = PG_GETARG_TEXT_PP(0);
1313  text *in2 = PG_GETARG_TEXT_PP(1);
1314  float4 res;
1315 
1317  VARDATA_ANY(in1), VARSIZE_ANY_EXHDR(in1),
1319 
1320  PG_FREE_IF_COPY(in1, 0);
1321  PG_FREE_IF_COPY(in2, 1);

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_dist_op()

Datum strict_word_similarity_dist_op ( PG_FUNCTION_ARGS  )

Definition at line 1292 of file trgm_op.c.

1295 {
1296  text *in1 = PG_GETARG_TEXT_PP(0);
1297  text *in2 = PG_GETARG_TEXT_PP(1);
1298  float4 res;
1299 
1301  VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2),
1303 
1304  PG_FREE_IF_COPY(in1, 0);
1305  PG_FREE_IF_COPY(in2, 1);

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_op()

◆ trgm2int()

uint32 trgm2int ( trgm ptr)

Definition at line 935 of file trgm_op.c.

938 {
939  uint32 val = 0;
940 
941  val |= *(((unsigned char *) ptr));
942  val <<= 8;
943  val |= *(((unsigned char *) ptr) + 1);
944  val <<= 8;
945  val |= *(((unsigned char *) ptr) + 2);
946 
unsigned int uint32
Definition: c.h:441
long val
Definition: informix.c:664

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 1044 of file trgm_op.c.

1047 {
1048  trgm *ptr1,
1049  *ptr2;
1050  int len1,
1051  len2;
1052 
1053  ptr1 = GETARR(trg1);
1054  ptr2 = GETARR(trg2);
1055 
1056  len1 = ARRNELEM(trg1);
1057  len2 = ARRNELEM(trg2);
1058 
1059  while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1060  {
1061  int res = CMPTRGM(ptr1, ptr2);
1062 
1063  if (res < 0)
1064  return false;
1065  else if (res > 0)
1066  ptr2++;
1067  else
1068  {
1069  ptr1++;
1070  ptr2++;
1071  }
1072  }
1073  if (ptr1 - GETARR(trg1) < len1)
1074  return false;
1075  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 1083 of file trgm_op.c.

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

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 1144 of file trgm_op.c.

1147 {
1148  text *in1 = PG_GETARG_TEXT_PP(0);
1149  text *in2 = PG_GETARG_TEXT_PP(1);
1150  float4 res;
1151 
1153  VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2),
1154  0);
1155 
1156  PG_FREE_IF_COPY(in1, 0);
1157  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()

Datum word_similarity_commutator_op ( PG_FUNCTION_ARGS  )

◆ word_similarity_dist_commutator_op()

Datum word_similarity_dist_commutator_op ( PG_FUNCTION_ARGS  )

Definition at line 1244 of file trgm_op.c.

1247 {
1248  text *in1 = PG_GETARG_TEXT_PP(0);
1249  text *in2 = PG_GETARG_TEXT_PP(1);
1250  float4 res;
1251 
1253  VARDATA_ANY(in1), VARSIZE_ANY_EXHDR(in1),
1254  0);
1255 
1256  PG_FREE_IF_COPY(in1, 0);
1257  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 1228 of file trgm_op.c.

1231 {
1232  text *in1 = PG_GETARG_TEXT_PP(0);
1233  text *in2 = PG_GETARG_TEXT_PP(1);
1234  float4 res;
1235 
1237  VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2),
1238  0);
1239 
1240  PG_FREE_IF_COPY(in1, 0);
1241  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()

Datum word_similarity_op ( PG_FUNCTION_ARGS  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 16 of file trgm_op.c.

◆ similarity_threshold

double similarity_threshold = 0.3f

Definition at line 19 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