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.

Referenced by generate_trgm_only(), and iterate_word_similarity().

◆ TRGM_BOUND_RIGHT

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

Definition at line 52 of file trgm_op.c.

Referenced by generate_trgm_only(), and iterate_word_similarity().

◆ 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.

Referenced by iterate_word_similarity(), strict_word_similarity_commutator_op(), strict_word_similarity_op(), word_similarity_commutator_op(), and word_similarity_op().

◆ WORD_SIMILARITY_STRICT

#define WORD_SIMILARITY_STRICT

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

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

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:8725
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

◆ 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.

References cmp(), CMPTRGM, comp_ptrgm(), generate_trgm_only(), i, pos_trgm::index, iterate_word_similarity(), 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().

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

◆ cnt_sml()

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

Definition at line 998 of file trgm_op.c.

References ARRNELEM, CALCSML, CMPTRGM, and GETARR.

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

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  */
1037  return CALCSML(count, len1, inexact ? count : len2);
1038 }
#define CMPTRGM(a, b)
Definition: trgm.h:45
#define ARRNELEM(x)
Definition: trgm.h:108
#define GETARR(x)
Definition: trgm.h:107
float float4
Definition: c.h:491
char trgm[3]
Definition: trgm.h:41
#define CALCSML(count, len1, len2)
Definition: trgm.h:117

◆ comp_ptrgm()

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

Definition at line 424 of file trgm_op.c.

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

Referenced by calc_word_similarity().

425 {
426  const pos_trgm *p1 = (const pos_trgm *) v1;
427  const pos_trgm *p2 = (const pos_trgm *) v2;
428  int cmp;
429 
430  cmp = CMPTRGM(p1->trg, p2->trg);
431  if (cmp != 0)
432  return cmp;
433 
434  if (p1->index < p2->index)
435  return -1;
436  else if (p1->index == p2->index)
437  return 0;
438  else
439  return 1;
440 }
#define CMPTRGM(a, b)
Definition: trgm.h:45
int index
Definition: trgm_op.c:46
trgm trg
Definition: trgm_op.c:45
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742

◆ comp_trgm()

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

Definition at line 161 of file trgm_op.c.

References CMPTRGM.

Referenced by generate_trgm(), and generate_wildcard_trgm().

162 {
163  return CMPTRGM(a, b);
164 }
#define CMPTRGM(a, b)
Definition: trgm.h:45

◆ compact_trigram()

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

Definition at line 198 of file trgm_op.c.

References COMP_LEGACY_CRC32, CPTRGM, FIN_LEGACY_CRC32, and INIT_LEGACY_CRC32.

Referenced by fillTrgm(), and make_trigrams().

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

◆ find_word()

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

Definition at line 171 of file trgm_op.c.

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

Referenced by generate_trgm_only().

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

◆ generate_trgm()

TRGM* generate_trgm ( char *  str,
int  slen 
)

Definition at line 356 of file trgm_op.c.

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

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

357 {
358  TRGM *trg;
359  int len;
360 
361  protect_out_of_mem(slen);
362 
363  trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
364  trg->flag = ARRKEY;
365 
366  len = generate_trgm_only(GETARR(trg), str, slen, NULL);
367  SET_VARSIZE(trg, CALCGTSIZE(ARRKEY, len));
368 
369  if (len == 0)
370  return trg;
371 
372  /*
373  * Make trigrams unique.
374  */
375  if (len > 1)
376  {
377  qsort((void *) GETARR(trg), len, sizeof(trgm), comp_trgm);
378  len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
379  }
380 
381  SET_VARSIZE(trg, CALCGTSIZE(ARRKEY, len));
382 
383  return trg;
384 }
#define ARRKEY
Definition: trgm.h:97
static void protect_out_of_mem(int slen)
Definition: trgm_op.c:339
#define GETARR(x)
Definition: trgm.h:107
Definition: trgm.h:66
#define TRGMHDRSIZE
Definition: trgm.h:73
uint8 flag
Definition: trgm.h:69
char trgm[3]
Definition: trgm.h:41
#define CALCGTSIZE(flag)
Definition: hstore_gist.c:48
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
void * palloc(Size size)
Definition: mcxt.c:949
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define qsort(a, b, c, d)
Definition: port.h:491
static int generate_trgm_only(trgm *trg, char *str, int slen, TrgmBound *bounds)
Definition: trgm_op.c:275
static int comp_trgm(const void *a, const void *b)
Definition: trgm_op.c:161

◆ 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.

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

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

◆ generate_wildcard_trgm()

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

Definition at line 866 of file trgm_op.c.

References ARRKEY, buf, CALCGTSIZE, comp_trgm(), TRGM::flag, get_wildcard_part(), GETARR, 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().

867 {
868  TRGM *trg;
869  char *buf,
870  *buf2;
871  trgm *tptr;
872  int len,
873  charlen,
874  bytelen;
875  const char *eword;
876 
877  protect_out_of_mem(slen);
878 
879  trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
880  trg->flag = ARRKEY;
881  SET_VARSIZE(trg, TRGMHDRSIZE);
882 
883  if (slen + LPADDING + RPADDING < 3 || slen == 0)
884  return trg;
885 
886  tptr = GETARR(trg);
887 
888  /* Allocate a buffer for blank-padded, but not yet case-folded, words */
889  buf = palloc(sizeof(char) * (slen + 4));
890 
891  /*
892  * Extract trigrams from each substring extracted by get_wildcard_part.
893  */
894  eword = str;
895  while ((eword = get_wildcard_part(eword, slen - (eword - str),
896  buf, &bytelen, &charlen)) != NULL)
897  {
898 #ifdef IGNORECASE
899  buf2 = lowerstr_with_len(buf, bytelen);
900  bytelen = strlen(buf2);
901 #else
902  buf2 = buf;
903 #endif
904 
905  /*
906  * count trigrams
907  */
908  tptr = make_trigrams(tptr, buf2, bytelen, charlen);
909 
910 #ifdef IGNORECASE
911  pfree(buf2);
912 #endif
913  }
914 
915  pfree(buf);
916 
917  if ((len = tptr - GETARR(trg)) == 0)
918  return trg;
919 
920  /*
921  * Make trigrams unique.
922  */
923  if (len > 1)
924  {
925  qsort((void *) GETARR(trg), len, sizeof(trgm), comp_trgm);
926  len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
927  }
928 
929  SET_VARSIZE(trg, CALCGTSIZE(ARRKEY, len));
930 
931  return trg;
932 }
char * lowerstr_with_len(const char *str, int len)
Definition: ts_locale.c:252
#define ARRKEY
Definition: trgm.h:97
static void protect_out_of_mem(int slen)
Definition: trgm_op.c:339
static trgm * make_trigrams(trgm *tptr, char *str, int bytelen, int charlen)
Definition: trgm_op.c:223
#define GETARR(x)
Definition: trgm.h:107
void pfree(void *pointer)
Definition: mcxt.c:1056
Definition: trgm.h:66
static char * buf
Definition: pg_test_fsync.c:67
#define TRGMHDRSIZE
Definition: trgm.h:73
uint8 flag
Definition: trgm.h:69
char trgm[3]
Definition: trgm.h:41
#define CALCGTSIZE(flag)
Definition: hstore_gist.c:48
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition: qunique.h:21
#define RPADDING
Definition: trgm.h:17
void * palloc(Size size)
Definition: mcxt.c:949
#define LPADDING
Definition: trgm.h:16
static const char * get_wildcard_part(const char *str, int lenstr, char *buf, int *bytelen, int *charlen)
Definition: trgm_op.c:721
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define qsort(a, b, c, d)
Definition: port.h:491
static int comp_trgm(const void *a, const void *b)
Definition: trgm_op.c:161

◆ 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.

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

Referenced by generate_wildcard_trgm().

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

◆ index_strategy_get_limit()

double index_strategy_get_limit ( StrategyNumber  strategy)

Definition at line 132 of file trgm_op.c.

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

133 {
134  switch (strategy)
135  {
137  return similarity_threshold;
142  default:
143  elog(ERROR, "unrecognized strategy number: %d", strategy);
144  break;
145  }
146 
147  return 0.0; /* keep compiler quiet */
148 }
#define SimilarityStrategyNumber
Definition: trgm.h:30
#define StrictWordSimilarityStrategyNumber
Definition: trgm.h:38
#define ERROR
Definition: elog.h:43
double strict_word_similarity_threshold
Definition: trgm_op.c:21
#define WordSimilarityStrategyNumber
Definition: trgm.h:36
double word_similarity_threshold
Definition: trgm_op.c:20
#define elog(elevel,...)
Definition: elog.h:228
double similarity_threshold
Definition: trgm_op.c:19

◆ 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.

References Assert, CALCSML, i, 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().

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

◆ 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.

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

Referenced by calc_word_similarity().

398 {
399  pos_trgm *result;
400  int i,
401  len = len1 + len2;
402 
403  result = (pos_trgm *) palloc(sizeof(pos_trgm) * len);
404 
405  for (i = 0; i < len1; i++)
406  {
407  memcpy(&result[i].trg, &trg1[i], sizeof(trgm));
408  result[i].index = -1;
409  }
410 
411  for (i = 0; i < len2; i++)
412  {
413  memcpy(&result[i + len1].trg, &trg2[i], sizeof(trgm));
414  result[i + len1].index = i;
415  }
416 
417  return result;
418 }
int index
Definition: trgm_op.c:46
char trgm[3]
Definition: trgm.h:41
void * palloc(Size size)
Definition: mcxt.c:949
int i

◆ make_trigrams()

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

Definition at line 223 of file trgm_op.c.

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

Referenced by generate_trgm_only(), and generate_wildcard_trgm().

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

◆ 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 ( word_similarity  )

◆ PG_FUNCTION_INFO_V1() [6/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity  )

◆ PG_FUNCTION_INFO_V1() [7/16]

PG_FUNCTION_INFO_V1 ( similarity_dist  )

◆ PG_FUNCTION_INFO_V1() [8/16]

PG_FUNCTION_INFO_V1 ( similarity_op  )

◆ PG_FUNCTION_INFO_V1() [9/16]

PG_FUNCTION_INFO_V1 ( word_similarity_op  )

◆ PG_FUNCTION_INFO_V1() [10/16]

PG_FUNCTION_INFO_V1 ( word_similarity_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [11/16]

PG_FUNCTION_INFO_V1 ( word_similarity_dist_op  )

◆ PG_FUNCTION_INFO_V1() [12/16]

PG_FUNCTION_INFO_V1 ( word_similarity_dist_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [13/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_op  )

◆ PG_FUNCTION_INFO_V1() [14/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [15/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_dist_op  )

◆ PG_FUNCTION_INFO_V1() [16/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_dist_commutator_op  )

◆ protect_out_of_mem()

static void protect_out_of_mem ( int  slen)
static

Definition at line 339 of file trgm_op.c.

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

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

340 {
341  if ((Size) (slen / 2) >= (MaxAllocSize / (sizeof(trgm) * 3)) ||
343  ereport(ERROR,
344  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
345  errmsg("out of memory")));
346 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define MaxAllocSize
Definition: memutils.h:40
char trgm[3]
Definition: trgm.h:41
size_t Size
Definition: c.h:467
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1331
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ set_limit()

Datum set_limit ( PG_FUNCTION_ARGS  )

Definition at line 110 of file trgm_op.c.

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

111 {
112  float4 nlimit = PG_GETARG_FLOAT4(0);
113  char *nlimit_str;
114  Oid func_out_oid;
115  bool is_varlena;
116 
117  getTypeOutputInfo(FLOAT4OID, &func_out_oid, &is_varlena);
118 
119  nlimit_str = OidOutputFunctionCall(func_out_oid, Float4GetDatum(nlimit));
120 
121  SetConfigOption("pg_trgm.similarity_threshold", nlimit_str,
123 
125 }
static Datum Float4GetDatum(float4 X)
Definition: postgres.h:681
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
unsigned int Oid
Definition: postgres_ext.h:31
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:7583
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:275
float float4
Definition: c.h:491
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1655
double similarity_threshold
Definition: trgm_op.c:19

◆ show_limit()

Datum show_limit ( PG_FUNCTION_ARGS  )

Definition at line 155 of file trgm_op.c.

References PG_RETURN_FLOAT4, and similarity_threshold.

156 {
158 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
double similarity_threshold
Definition: trgm_op.c:19

◆ show_trgm()

Datum show_trgm ( PG_FUNCTION_ARGS  )

Definition at line 949 of file trgm_op.c.

References 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.

950 {
951  text *in = PG_GETARG_TEXT_PP(0);
952  TRGM *trg;
953  Datum *d;
954  ArrayType *a;
955  trgm *ptr;
956  int i;
957 
959  d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(trg)));
960 
961  for (i = 0, ptr = GETARR(trg); i < ARRNELEM(trg); i++, ptr++)
962  {
963  text *item = (text *) palloc(VARHDRSZ + Max(12, pg_database_encoding_max_length() * 3));
964 
966  {
967  snprintf(VARDATA(item), 12, "0x%06x", trgm2int(ptr));
968  SET_VARSIZE(item, VARHDRSZ + strlen(VARDATA(item)));
969  }
970  else
971  {
972  SET_VARSIZE(item, VARHDRSZ + 3);
973  CPTRGM(VARDATA(item), ptr);
974  }
975  d[i] = PointerGetDatum(item);
976  }
977 
978  a = construct_array(
979  d,
980  ARRNELEM(trg),
981  TEXTOID,
982  -1,
983  false,
984  'i'
985  );
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 
995 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
uint32 trgm2int(trgm *ptr)
Definition: trgm_op.c:935
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define VARDATA(PTR)
Definition: postgres.h:302
#define PointerGetDatum(X)
Definition: postgres.h:556
#define VARHDRSZ
Definition: c.h:562
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
#define ARRNELEM(x)
Definition: trgm.h:108
TRGM * generate_trgm(char *str, int slen)
Definition: trgm_op.c:356
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define GETARR(x)
Definition: trgm.h:107
void pfree(void *pointer)
Definition: mcxt.c:1056
Definition: trgm.h:66
#define ISPRINTABLETRGM(t)
Definition: trgm.h:60
uintptr_t Datum
Definition: postgres.h:367
char trgm[3]
Definition: trgm.h:41
#define Max(x, y)
Definition: c.h:905
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1331
#define DatumGetPointer(X)
Definition: postgres.h:549
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
int i
Definition: c.h:556
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define snprintf
Definition: port.h:192
#define CPTRGM(a, b)
Definition: trgm.h:47

◆ similarity()

Datum similarity ( PG_FUNCTION_ARGS  )

Definition at line 1124 of file trgm_op.c.

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

Referenced by similarity_dist(), and similarity_op().

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 
1142  PG_RETURN_FLOAT4(res);
1143 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
TRGM * generate_trgm(char *str, int slen)
Definition: trgm_op.c:356
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
void pfree(void *pointer)
Definition: mcxt.c:1056
Definition: trgm.h:66
float float4
Definition: c.h:491
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
float4 cnt_sml(TRGM *trg1, TRGM *trg2, bool inexact)
Definition: trgm_op.c:998
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556

◆ similarity_dist()

Datum similarity_dist ( PG_FUNCTION_ARGS  )

Definition at line 1178 of file trgm_op.c.

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

1179 {
1181  PG_GETARG_DATUM(0),
1182  PG_GETARG_DATUM(1)));
1183 
1184  PG_RETURN_FLOAT4(1.0 - res);
1185 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
static float4 DatumGetFloat4(Datum X)
Definition: postgres.h:664
float float4
Definition: c.h:491
Datum similarity(PG_FUNCTION_ARGS)
Definition: trgm_op.c:1124
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617

◆ similarity_op()

Datum similarity_op ( PG_FUNCTION_ARGS  )

Definition at line 1188 of file trgm_op.c.

References DatumGetFloat4(), DirectFunctionCall2, PG_GETARG_DATUM, PG_RETURN_BOOL, similarity(), and similarity_threshold.

1189 {
1191  PG_GETARG_DATUM(0),
1192  PG_GETARG_DATUM(1)));
1193 
1195 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
static float4 DatumGetFloat4(Datum X)
Definition: postgres.h:664
float float4
Definition: c.h:491
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
Datum similarity(PG_FUNCTION_ARGS)
Definition: trgm_op.c:1124
double similarity_threshold
Definition: trgm_op.c:19
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:617

◆ strict_word_similarity()

Datum strict_word_similarity ( PG_FUNCTION_ARGS  )

Definition at line 1162 of file trgm_op.c.

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

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);
1174  PG_RETURN_FLOAT4(res);
1175 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
float float4
Definition: c.h:491
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define WORD_SIMILARITY_STRICT
Definition: trgm_op.c:57
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

◆ strict_word_similarity_commutator_op()

Datum strict_word_similarity_commutator_op ( PG_FUNCTION_ARGS  )

Definition at line 1278 of file trgm_op.c.

References calc_word_similarity(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, strict_word_similarity_threshold, VARDATA_ANY, VARSIZE_ANY_EXHDR, WORD_SIMILARITY_CHECK_ONLY, and WORD_SIMILARITY_STRICT.

1279 {
1280  text *in1 = PG_GETARG_TEXT_PP(0);
1281  text *in2 = PG_GETARG_TEXT_PP(1);
1282  float4 res;
1283 
1285  VARDATA_ANY(in1), VARSIZE_ANY_EXHDR(in1),
1287 
1288  PG_FREE_IF_COPY(in1, 0);
1289  PG_FREE_IF_COPY(in2, 1);
1291 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define WORD_SIMILARITY_CHECK_ONLY
Definition: trgm_op.c:55
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
double strict_word_similarity_threshold
Definition: trgm_op.c:21
float float4
Definition: c.h:491
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define WORD_SIMILARITY_STRICT
Definition: trgm_op.c:57
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

◆ strict_word_similarity_dist_commutator_op()

Datum strict_word_similarity_dist_commutator_op ( PG_FUNCTION_ARGS  )

Definition at line 1310 of file trgm_op.c.

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

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);
1322  PG_RETURN_FLOAT4(1.0 - res);
1323 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
float float4
Definition: c.h:491
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define WORD_SIMILARITY_STRICT
Definition: trgm_op.c:57
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

◆ strict_word_similarity_dist_op()

Datum strict_word_similarity_dist_op ( PG_FUNCTION_ARGS  )

Definition at line 1294 of file trgm_op.c.

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

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);
1306  PG_RETURN_FLOAT4(1.0 - res);
1307 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
float float4
Definition: c.h:491
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define WORD_SIMILARITY_STRICT
Definition: trgm_op.c:57
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

◆ strict_word_similarity_op()

Datum strict_word_similarity_op ( PG_FUNCTION_ARGS  )

Definition at line 1262 of file trgm_op.c.

References calc_word_similarity(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, strict_word_similarity_threshold, VARDATA_ANY, VARSIZE_ANY_EXHDR, WORD_SIMILARITY_CHECK_ONLY, and WORD_SIMILARITY_STRICT.

1263 {
1264  text *in1 = PG_GETARG_TEXT_PP(0);
1265  text *in2 = PG_GETARG_TEXT_PP(1);
1266  float4 res;
1267 
1269  VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2),
1271 
1272  PG_FREE_IF_COPY(in1, 0);
1273  PG_FREE_IF_COPY(in2, 1);
1275 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define WORD_SIMILARITY_CHECK_ONLY
Definition: trgm_op.c:55
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
double strict_word_similarity_threshold
Definition: trgm_op.c:21
float float4
Definition: c.h:491
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define WORD_SIMILARITY_STRICT
Definition: trgm_op.c:57
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

◆ trgm2int()

uint32 trgm2int ( trgm ptr)

Definition at line 935 of file trgm_op.c.

References val.

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

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

◆ trgm_contained_by()

bool trgm_contained_by ( TRGM trg1,
TRGM trg2 
)

Definition at line 1046 of file trgm_op.c.

References ARRNELEM, CMPTRGM, and GETARR.

Referenced by gtrgm_consistent().

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
1076  return true;
1077 }
#define CMPTRGM(a, b)
Definition: trgm.h:45
#define ARRNELEM(x)
Definition: trgm.h:108
#define GETARR(x)
Definition: trgm.h:107
char trgm[3]
Definition: trgm.h:41

◆ trgm_presence_map()

bool* trgm_presence_map ( TRGM query,
TRGM key 
)

Definition at line 1085 of file trgm_op.c.

References ARRNELEM, CMPTRGM, GETARR, i, and palloc0().

Referenced by gtrgm_consistent().

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 
1120  return result;
1121 }
#define CMPTRGM(a, b)
Definition: trgm.h:45
#define ARRNELEM(x)
Definition: trgm.h:108
#define GETARR(x)
Definition: trgm.h:107
void * palloc0(Size size)
Definition: mcxt.c:980
char trgm[3]
Definition: trgm.h:41
int i

◆ word_similarity()

Datum word_similarity ( PG_FUNCTION_ARGS  )

Definition at line 1146 of file trgm_op.c.

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

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);
1158  PG_RETURN_FLOAT4(res);
1159 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
float float4
Definition: c.h:491
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

◆ word_similarity_commutator_op()

Datum word_similarity_commutator_op ( PG_FUNCTION_ARGS  )

Definition at line 1214 of file trgm_op.c.

References calc_word_similarity(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, VARSIZE_ANY_EXHDR, WORD_SIMILARITY_CHECK_ONLY, and word_similarity_threshold.

1215 {
1216  text *in1 = PG_GETARG_TEXT_PP(0);
1217  text *in2 = PG_GETARG_TEXT_PP(1);
1218  float4 res;
1219 
1221  VARDATA_ANY(in1), VARSIZE_ANY_EXHDR(in1),
1223 
1224  PG_FREE_IF_COPY(in1, 0);
1225  PG_FREE_IF_COPY(in2, 1);
1227 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define WORD_SIMILARITY_CHECK_ONLY
Definition: trgm_op.c:55
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
float float4
Definition: c.h:491
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
double word_similarity_threshold
Definition: trgm_op.c:20
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

◆ word_similarity_dist_commutator_op()

Datum word_similarity_dist_commutator_op ( PG_FUNCTION_ARGS  )

Definition at line 1246 of file trgm_op.c.

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

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);
1258  PG_RETURN_FLOAT4(1.0 - res);
1259 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
float float4
Definition: c.h:491
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

◆ word_similarity_dist_op()

Datum word_similarity_dist_op ( PG_FUNCTION_ARGS  )

Definition at line 1230 of file trgm_op.c.

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

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);
1242  PG_RETURN_FLOAT4(1.0 - res);
1243 }
#define PG_RETURN_FLOAT4(x)
Definition: fmgr.h:355
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
float float4
Definition: c.h:491
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

◆ word_similarity_op()

Datum word_similarity_op ( PG_FUNCTION_ARGS  )

Definition at line 1198 of file trgm_op.c.

References calc_word_similarity(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY, VARSIZE_ANY_EXHDR, WORD_SIMILARITY_CHECK_ONLY, and word_similarity_threshold.

1199 {
1200  text *in1 = PG_GETARG_TEXT_PP(0);
1201  text *in2 = PG_GETARG_TEXT_PP(1);
1202  float4 res;
1203 
1205  VARDATA_ANY(in2), VARSIZE_ANY_EXHDR(in2),
1207 
1208  PG_FREE_IF_COPY(in1, 0);
1209  PG_FREE_IF_COPY(in2, 1);
1211 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define WORD_SIMILARITY_CHECK_ONLY
Definition: trgm_op.c:55
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
float float4
Definition: c.h:491
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
double word_similarity_threshold
Definition: trgm_op.c:20
Definition: c.h:556
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition: trgm_op.c:621

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