PostgreSQL Source Code  git master
trgm_gin.c
Go to the documentation of this file.
1 /*
2  * contrib/pg_trgm/trgm_gin.c
3  */
4 #include "postgres.h"
5 
6 #include "access/gin.h"
7 #include "access/stratnum.h"
8 #include "fmgr.h"
9 #include "trgm.h"
10 
16 
17 /*
18  * This function can only be called if a pre-9.1 version of the GIN operator
19  * class definition is present in the catalogs (probably as a consequence
20  * of upgrade-in-place). Cope.
21  */
22 Datum
24 {
25  if (PG_NARGS() == 3)
26  return gin_extract_value_trgm(fcinfo);
27  if (PG_NARGS() == 7)
28  return gin_extract_query_trgm(fcinfo);
29  elog(ERROR, "unexpected number of arguments to gin_extract_trgm");
31 }
32 
33 Datum
35 {
36  text *val = (text *) PG_GETARG_TEXT_PP(0);
37  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
38  Datum *entries = NULL;
39  TRGM *trg;
40  int32 trglen;
41 
42  *nentries = 0;
43 
45  trglen = ARRNELEM(trg);
46 
47  if (trglen > 0)
48  {
49  trgm *ptr;
50  int32 i;
51 
52  *nentries = trglen;
53  entries = (Datum *) palloc(sizeof(Datum) * trglen);
54 
55  ptr = GETARR(trg);
56  for (i = 0; i < trglen; i++)
57  {
58  int32 item = trgm2int(ptr);
59 
60  entries[i] = Int32GetDatum(item);
61  ptr++;
62  }
63  }
64 
65  PG_RETURN_POINTER(entries);
66 }
67 
68 Datum
70 {
71  text *val = (text *) PG_GETARG_TEXT_PP(0);
72  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
73  StrategyNumber strategy = PG_GETARG_UINT16(2);
74 
75  /* bool **pmatch = (bool **) PG_GETARG_POINTER(3); */
76  Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
77 
78  /* bool **nullFlags = (bool **) PG_GETARG_POINTER(5); */
79  int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
80  Datum *entries = NULL;
81  TRGM *trg;
82  int32 trglen;
83  trgm *ptr;
84  TrgmPackedGraph *graph;
85  int32 i;
86 
87  switch (strategy)
88  {
93  break;
95 #ifndef IGNORECASE
96  elog(ERROR, "cannot handle ~~* with case-sensitive trigrams");
97 #endif
98  /* FALL THRU */
99  case LikeStrategyNumber:
100 
101  /*
102  * For wildcard search we extract all the trigrams that every
103  * potentially-matching string must include.
104  */
106  VARSIZE_ANY_EXHDR(val));
107  break;
109 #ifndef IGNORECASE
110  elog(ERROR, "cannot handle ~* with case-sensitive trigrams");
111 #endif
112  /* FALL THRU */
114  trg = createTrgmNFA(val, PG_GET_COLLATION(),
115  &graph, CurrentMemoryContext);
116  if (trg && ARRNELEM(trg) > 0)
117  {
118  /*
119  * Successful regex processing: store NFA-like graph as
120  * extra_data. GIN API requires an array of nentries
121  * Pointers, but we just put the same value in each element.
122  */
123  trglen = ARRNELEM(trg);
124  *extra_data = (Pointer *) palloc(sizeof(Pointer) * trglen);
125  for (i = 0; i < trglen; i++)
126  (*extra_data)[i] = (Pointer) graph;
127  }
128  else
129  {
130  /* No result: have to do full index scan. */
131  *nentries = 0;
132  *searchMode = GIN_SEARCH_MODE_ALL;
133  PG_RETURN_POINTER(entries);
134  }
135  break;
136  default:
137  elog(ERROR, "unrecognized strategy number: %d", strategy);
138  trg = NULL; /* keep compiler quiet */
139  break;
140  }
141 
142  trglen = ARRNELEM(trg);
143  *nentries = trglen;
144 
145  if (trglen > 0)
146  {
147  entries = (Datum *) palloc(sizeof(Datum) * trglen);
148  ptr = GETARR(trg);
149  for (i = 0; i < trglen; i++)
150  {
151  int32 item = trgm2int(ptr);
152 
153  entries[i] = Int32GetDatum(item);
154  ptr++;
155  }
156  }
157 
158  /*
159  * If no trigram was extracted then we have to scan all the index.
160  */
161  if (trglen == 0)
162  *searchMode = GIN_SEARCH_MODE_ALL;
163 
164  PG_RETURN_POINTER(entries);
165 }
166 
167 Datum
169 {
170  bool *check = (bool *) PG_GETARG_POINTER(0);
171  StrategyNumber strategy = PG_GETARG_UINT16(1);
172 
173  /* text *query = PG_GETARG_TEXT_PP(2); */
174  int32 nkeys = PG_GETARG_INT32(3);
175  Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
176  bool *recheck = (bool *) PG_GETARG_POINTER(5);
177  bool res;
178  int32 i,
179  ntrue;
180  double nlimit;
181 
182  /* All cases served by this function are inexact */
183  *recheck = true;
184 
185  switch (strategy)
186  {
190  nlimit = index_strategy_get_limit(strategy);
191 
192  /* Count the matches */
193  ntrue = 0;
194  for (i = 0; i < nkeys; i++)
195  {
196  if (check[i])
197  ntrue++;
198  }
199 
200  /*--------------------
201  * If DIVUNION is defined then similarity formula is:
202  * c / (len1 + len2 - c)
203  * where c is number of common trigrams and it stands as ntrue in
204  * this code. Here we don't know value of len2 but we can assume
205  * that c (ntrue) is a lower bound of len2, so upper bound of
206  * similarity is:
207  * c / (len1 + c - c) => c / len1
208  * If DIVUNION is not defined then similarity formula is:
209  * c / max(len1, len2)
210  * And again, c (ntrue) is a lower bound of len2, but c <= len1
211  * just by definition and, consequently, upper bound of
212  * similarity is just c / len1.
213  * So, independently on DIVUNION the upper bound formula is the same.
214  */
215  res = (nkeys == 0) ? false :
216  (((((float4) ntrue) / ((float4) nkeys))) >= nlimit);
217  break;
218  case ILikeStrategyNumber:
219 #ifndef IGNORECASE
220  elog(ERROR, "cannot handle ~~* with case-sensitive trigrams");
221 #endif
222  /* FALL THRU */
223  case LikeStrategyNumber:
224  /* Check if all extracted trigrams are presented. */
225  res = true;
226  for (i = 0; i < nkeys; i++)
227  {
228  if (!check[i])
229  {
230  res = false;
231  break;
232  }
233  }
234  break;
236 #ifndef IGNORECASE
237  elog(ERROR, "cannot handle ~* with case-sensitive trigrams");
238 #endif
239  /* FALL THRU */
241  if (nkeys < 1)
242  {
243  /* Regex processing gave no result: do full index scan */
244  res = true;
245  }
246  else
247  res = trigramsMatchGraph((TrgmPackedGraph *) extra_data[0],
248  check);
249  break;
250  default:
251  elog(ERROR, "unrecognized strategy number: %d", strategy);
252  res = false; /* keep compiler quiet */
253  break;
254  }
255 
256  PG_RETURN_BOOL(res);
257 }
258 
259 /*
260  * In all cases, GIN_TRUE is at least as favorable to inclusion as
261  * GIN_MAYBE. If no better option is available, simply treat
262  * GIN_MAYBE as if it were GIN_TRUE and apply the same test as the binary
263  * consistent function.
264  */
265 Datum
267 {
269  StrategyNumber strategy = PG_GETARG_UINT16(1);
270 
271  /* text *query = PG_GETARG_TEXT_PP(2); */
272  int32 nkeys = PG_GETARG_INT32(3);
273  Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
275  int32 i,
276  ntrue;
277  bool *boolcheck;
278  double nlimit;
279 
280  switch (strategy)
281  {
285  nlimit = index_strategy_get_limit(strategy);
286 
287  /* Count the matches */
288  ntrue = 0;
289  for (i = 0; i < nkeys; i++)
290  {
291  if (check[i] != GIN_FALSE)
292  ntrue++;
293  }
294 
295  /*
296  * See comment in gin_trgm_consistent() about * upper bound
297  * formula
298  */
299  res = (nkeys == 0)
300  ? GIN_FALSE : (((((float4) ntrue) / ((float4) nkeys)) >= nlimit)
301  ? GIN_MAYBE : GIN_FALSE);
302  break;
303  case ILikeStrategyNumber:
304 #ifndef IGNORECASE
305  elog(ERROR, "cannot handle ~~* with case-sensitive trigrams");
306 #endif
307  /* FALL THRU */
308  case LikeStrategyNumber:
309  /* Check if all extracted trigrams are presented. */
310  res = GIN_MAYBE;
311  for (i = 0; i < nkeys; i++)
312  {
313  if (check[i] == GIN_FALSE)
314  {
315  res = GIN_FALSE;
316  break;
317  }
318  }
319  break;
321 #ifndef IGNORECASE
322  elog(ERROR, "cannot handle ~* with case-sensitive trigrams");
323 #endif
324  /* FALL THRU */
326  if (nkeys < 1)
327  {
328  /* Regex processing gave no result: do full index scan */
329  res = GIN_MAYBE;
330  }
331  else
332  {
333  /*
334  * As trigramsMatchGraph implements a monotonic boolean
335  * function, promoting all GIN_MAYBE keys to GIN_TRUE will
336  * give a conservative result.
337  */
338  boolcheck = (bool *) palloc(sizeof(bool) * nkeys);
339  for (i = 0; i < nkeys; i++)
340  boolcheck[i] = (check[i] != GIN_FALSE);
341  if (!trigramsMatchGraph((TrgmPackedGraph *) extra_data[0],
342  boolcheck))
343  res = GIN_FALSE;
344  pfree(boolcheck);
345  }
346  break;
347  default:
348  elog(ERROR, "unrecognized strategy number: %d", strategy);
349  res = GIN_FALSE; /* keep compiler quiet */
350  break;
351  }
352 
353  /* All cases served by this function are inexact */
354  Assert(res != GIN_TRUE);
356 }
#define GIN_TRUE
Definition: gin.h:60
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check)
Definition: trgm_regexp.c:636
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define RegExpStrategyNumber
Definition: trgm.h:34
TRGM * generate_wildcard_trgm(const char *str, int slen)
Definition: trgm_op.c:866
#define SimilarityStrategyNumber
Definition: trgm.h:30
#define GIN_MAYBE
Definition: gin.h:61
TRGM * createTrgmNFA(text *text_re, Oid collation, TrgmPackedGraph **graph, MemoryContext rcontext)
Definition: trgm_regexp.c:521
#define StrictWordSimilarityStrategyNumber
Definition: trgm.h:38
#define RegExpICaseStrategyNumber
Definition: trgm.h:35
uint16 StrategyNumber
Definition: stratnum.h:22
PG_FUNCTION_INFO_V1(gin_extract_trgm)
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
#define ARRNELEM(x)
Definition: trgm.h:108
#define PG_GET_COLLATION()
Definition: fmgr.h:193
signed int int32
Definition: c.h:347
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define GETARR(x)
Definition: trgm.h:107
#define PG_RETURN_GIN_TERNARY_VALUE(x)
Definition: gin.h:66
Datum gin_trgm_triconsistent(PG_FUNCTION_ARGS)
Definition: trgm_gin.c:266
#define GIN_SEARCH_MODE_ALL
Definition: gin.h:35
void pfree(void *pointer)
Definition: mcxt.c:1056
Datum gin_extract_trgm(PG_FUNCTION_ARGS)
Definition: trgm_gin.c:23
char * Pointer
Definition: c.h:336
#define ERROR
Definition: elog.h:43
Datum gin_extract_query_trgm(PG_FUNCTION_ARGS)
Definition: trgm_gin.c:69
Definition: trgm.h:66
char GinTernaryValue
Definition: gin.h:57
Datum gin_trgm_consistent(PG_FUNCTION_ARGS)
Definition: trgm_gin.c:168
#define LikeStrategyNumber
Definition: trgm.h:32
double index_strategy_get_limit(StrategyNumber strategy)
Definition: trgm_op.c:132
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
uint32 trgm2int(trgm *ptr)
Definition: trgm_op.c:935
float float4
Definition: c.h:491
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
uintptr_t Datum
Definition: postgres.h:367
#define WordSimilarityStrategyNumber
Definition: trgm.h:36
char trgm[3]
Definition: trgm.h:41
Datum gin_extract_value_trgm(PG_FUNCTION_ARGS)
Definition: trgm_gin.c:34
#define Assert(condition)
Definition: c.h:733
TRGM * generate_trgm(char *str, int slen)
Definition: trgm_op.c:356
#define GIN_FALSE
Definition: gin.h:59
#define PG_NARGS()
Definition: fmgr.h:198
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:267
#define Int32GetDatum(X)
Definition: postgres.h:479
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
int i
Definition: c.h:550
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
#define ILikeStrategyNumber
Definition: trgm.h:33
long val
Definition: informix.c:684
#define PG_RETURN_NULL()
Definition: fmgr.h:335