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 "miscadmin.h"
#include "trgm.h"
#include "tsearch/ts_locale.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)
 
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.3f,
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.6f,
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.5f,
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:4996
@ PGC_USERSET
Definition: guc.h:75
double strict_word_similarity_threshold
Definition: trgm_op.c:23
double word_similarity_threshold
Definition: trgm_op.c:22
double similarity_threshold
Definition: trgm_op.c:21

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

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

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

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

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

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

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

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 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:541
int errcode(int sqlerrcode)
Definition: elog.c:695
#define ereport(elevel,...)
Definition: elog.h:145
#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:1639
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:281
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4158
@ PGC_S_SESSION
Definition: guc.h:122
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2865
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:823
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 954 of file trgm_op.c.

957 {
958  text *in = PG_GETARG_TEXT_PP(0);
959  TRGM *trg;
960  Datum *d;
961  ArrayType *a;
962  trgm *ptr;
963  int i;
964 
966  d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(trg)));
967 
968  for (i = 0, ptr = GETARR(trg); i < ARRNELEM(trg); i++, ptr++)
969  {
970  text *item = (text *) palloc(VARHDRSZ + Max(12, pg_database_encoding_max_length() * 3));
971 
973  {
974  snprintf(VARDATA(item), 12, "0x%06x", trgm2int(ptr));
975  SET_VARSIZE(item, VARHDRSZ + strlen(VARDATA(item)));
976  }
977  else
978  {
979  SET_VARSIZE(item, VARHDRSZ + 3);
980  CPTRGM(VARDATA(item), ptr);
981  }
982  d[i] = PointerGetDatum(item);
983  }
984 
985  a = construct_array_builtin(d, ARRNELEM(trg), TEXTOID);
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_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3338
#define VARHDRSZ
Definition: c.h:628
#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:238
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
uintptr_t Datum
Definition: postgres.h:412
#define VARDATA(PTR)
Definition: postgres.h:316
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:660
#define VARDATA_ANY(PTR)
Definition: postgres.h:362
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:355
Definition: c.h:623
#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:940

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 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:644
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
static float4 DatumGetFloat4(Datum X)
Definition: postgres.h:806
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:626

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

943 {
944  uint32 val = 0;
945 
946  val |= *(((unsigned char *) ptr));
947  val <<= 8;
948  val |= *(((unsigned char *) ptr) + 1);
949  val <<= 8;
950  val |= *(((unsigned char *) ptr) + 2);
951 
unsigned int uint32
Definition: c.h:442
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 18 of file trgm_op.c.

◆ similarity_threshold

double similarity_threshold = 0.3f

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