PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
tsquery_op.c File Reference
#include "postgres.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
Include dependency graph for tsquery_op.c:

Go to the source code of this file.

Macros

#define CMPFUNC(NAME, CONDITION)
 

Functions

Datum tsquery_numnode (PG_FUNCTION_ARGS)
 
static QTNodejoin_tsqueries (TSQuery a, TSQuery b, int8 operator, uint16 distance)
 
Datum tsquery_and (PG_FUNCTION_ARGS)
 
Datum tsquery_or (PG_FUNCTION_ARGS)
 
Datum tsquery_phrase_distance (PG_FUNCTION_ARGS)
 
Datum tsquery_phrase (PG_FUNCTION_ARGS)
 
Datum tsquery_not (PG_FUNCTION_ARGS)
 
static int CompareTSQ (TSQuery a, TSQuery b)
 
Datum tsquery_cmp (PG_FUNCTION_ARGS)
 
 CMPFUNC (tsquery_lt, res< 0)
 
 CMPFUNC (tsquery_le, res<=0)
 
 CMPFUNC (tsquery_eq, res==0)
 
 CMPFUNC (tsquery_ge, res >=0)
 
 CMPFUNC (tsquery_gt, res > 0)
 
 CMPFUNC (tsquery_ne, res!=0)
 
TSQuerySign makeTSQuerySign (TSQuery a)
 
static char ** collectTSQueryValues (TSQuery a, int *nvalues_p)
 
static int cmp_string (const void *a, const void *b)
 
static int remove_duplicates (char **strings, int n)
 
Datum tsq_mcontains (PG_FUNCTION_ARGS)
 
Datum tsq_mcontained (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

#define CMPFUNC (   NAME,
  CONDITION 
)
Value:
Datum \
int res = CompareTSQ(a,b); \
PG_RETURN_BOOL( CONDITION ); \
} \
/* keep compiler quiet - no extra ; */ \
extern int no_such_variable
TSQueryData * TSQuery
Definition: ts_type.h:212
#define PG_GETARG_TSQUERY_COPY(n)
Definition: ts_type.h:239
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int CompareTSQ(TSQuery a, TSQuery b)
Definition: tsquery_op.c:188
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158

Definition at line 226 of file tsquery_op.c.

Function Documentation

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

Definition at line 297 of file tsquery_op.c.

Referenced by tsq_mcontains().

298 {
299  const char *sa = *((const char **) a);
300  const char *sb = *((const char **) b);
301 
302  return strcmp(sa, sb);
303 }
CMPFUNC ( tsquery_lt  )
CMPFUNC ( tsquery_le  ,
res<=  0 
)
CMPFUNC ( tsquery_eq  ,
res  = =0 
)
CMPFUNC ( tsquery_ge  ,
res >=  0 
)
CMPFUNC ( tsquery_gt  ,
res  ,
 
)
CMPFUNC ( tsquery_ne  ,
res!  = 0 
)
static char** collectTSQueryValues ( TSQuery  a,
int *  nvalues_p 
)
static

Definition at line 266 of file tsquery_op.c.

References QueryOperand::distance, GETOPERAND, GETQUERY, i, QueryOperand::length, palloc(), QI_VAL, QueryItem::qoperand, TSQueryData::size, QueryItem::type, val, and values.

Referenced by tsq_mcontains().

267 {
268  QueryItem *ptr = GETQUERY(a);
269  char *operand = GETOPERAND(a);
270  char **values;
271  int nvalues = 0;
272  int i;
273 
274  values = (char **) palloc(sizeof(char *) * a->size);
275 
276  for (i = 0; i < a->size; i++)
277  {
278  if (ptr->type == QI_VAL)
279  {
280  int len = ptr->qoperand.length;
281  char *val;
282 
283  val = palloc(len + 1);
284  memcpy(val, operand + ptr->qoperand.distance, len);
285  val[len] = '\0';
286 
287  values[nvalues++] = val;
288  }
289  ptr++;
290  }
291 
292  *nvalues_p = nvalues;
293  return values;
294 }
#define QI_VAL
Definition: ts_type.h:134
uint32 distance
Definition: ts_type.h:158
#define GETQUERY(x)
Definition: _int.h:142
#define GETOPERAND(x)
Definition: ltree.h:118
QueryItemType type
Definition: ts_type.h:195
uint32 length
Definition: ts_type.h:158
static Datum values[MAXATTR]
Definition: bootstrap.c:163
void * palloc(Size size)
Definition: mcxt.c:849
int32 size
Definition: ts_type.h:208
int i
QueryOperand qoperand
Definition: ts_type.h:197
long val
Definition: informix.c:689
static int CompareTSQ ( TSQuery  a,
TSQuery  b 
)
static

Definition at line 188 of file tsquery_op.c.

References GETOPERAND, GETQUERY, QT2QTN(), QTNFree(), QTNodeCompare(), TSQueryData::size, and VARSIZE.

Referenced by tsquery_cmp().

189 {
190  if (a->size != b->size)
191  {
192  return (a->size < b->size) ? -1 : 1;
193  }
194  else if (VARSIZE(a) != VARSIZE(b))
195  {
196  return (VARSIZE(a) < VARSIZE(b)) ? -1 : 1;
197  }
198  else if (a->size != 0)
199  {
200  QTNode *an = QT2QTN(GETQUERY(a), GETOPERAND(a));
201  QTNode *bn = QT2QTN(GETQUERY(b), GETOPERAND(b));
202  int res = QTNodeCompare(an, bn);
203 
204  QTNFree(an);
205  QTNFree(bn);
206 
207  return res;
208  }
209 
210  return 0;
211 }
void QTNFree(QTNode *in)
Definition: tsquery_util.c:63
QTNode * QT2QTN(QueryItem *in, char *operand)
Definition: tsquery_util.c:24
#define VARSIZE(PTR)
Definition: postgres.h:304
#define GETQUERY(x)
Definition: _int.h:142
#define GETOPERAND(x)
Definition: ltree.h:118
int32 size
Definition: ts_type.h:208
int QTNodeCompare(QTNode *an, QTNode *bn)
Definition: tsquery_util.c:96
static QTNode* join_tsqueries ( TSQuery  a,
TSQuery  b,
int8  operator,
uint16  distance 
)
static

Definition at line 31 of file tsquery_op.c.

References QTNode::child, NODE::distance, QueryOperator::distance, QTNode::flags, GETOPERAND, GETQUERY, QTNode::nchild, OP_PHRASE, QueryOperator::oper, palloc0(), QI_OPR, QueryItem::qoperator, QT2QTN(), QTN_NEEDFREE, QueryItem::type, and QTNode::valnode.

Referenced by tsquery_and(), tsquery_or(), and tsquery_phrase_distance().

32 {
33  QTNode *res = (QTNode *) palloc0(sizeof(QTNode));
34 
35  res->flags |= QTN_NEEDFREE;
36 
37  res->valnode = (QueryItem *) palloc0(sizeof(QueryItem));
38  res->valnode->type = QI_OPR;
39  res->valnode->qoperator.oper = operator;
40  if (operator == OP_PHRASE)
41  res->valnode->qoperator.distance = distance;
42 
43  res->child = (QTNode **) palloc0(sizeof(QTNode *) * 2);
44  res->child[0] = QT2QTN(GETQUERY(b), GETOPERAND(b));
45  res->child[1] = QT2QTN(GETQUERY(a), GETOPERAND(a));
46  res->nchild = 2;
47 
48  return res;
49 }
QueryOperator qoperator
Definition: ts_type.h:196
QTNode * QT2QTN(QueryItem *in, char *operand)
Definition: tsquery_util.c:24
int16 distance
Definition: ts_type.h:183
#define GETQUERY(x)
Definition: _int.h:142
#define GETOPERAND(x)
Definition: ltree.h:118
#define QI_OPR
Definition: ts_type.h:135
QueryItemType type
Definition: ts_type.h:195
void * palloc0(Size size)
Definition: mcxt.c:878
#define OP_PHRASE
Definition: ts_type.h:169
struct QTNode ** child
Definition: ts_utils.h:207
#define QTN_NEEDFREE
Definition: ts_utils.h:211
QueryItem * valnode
Definition: ts_utils.h:202
int32 nchild
Definition: ts_utils.h:204
uint32 flags
Definition: ts_utils.h:203
TSQuerySign makeTSQuerySign ( TSQuery  a)

Definition at line 249 of file tsquery_op.c.

References GETQUERY, i, QI_VAL, QueryItem::qoperand, sign, TSQueryData::size, TSQS_SIGLEN, QueryItem::type, and QueryOperand::valcrc.

Referenced by gtsquery_compress(), and gtsquery_consistent().

250 {
251  int i;
252  QueryItem *ptr = GETQUERY(a);
253  TSQuerySign sign = 0;
254 
255  for (i = 0; i < a->size; i++)
256  {
257  if (ptr->type == QI_VAL)
258  sign |= ((TSQuerySign) 1) << (((unsigned int) ptr->qoperand.valcrc) % TSQS_SIGLEN);
259  ptr++;
260  }
261 
262  return sign;
263 }
#define TSQS_SIGLEN
Definition: ts_utils.h:217
#define QI_VAL
Definition: ts_type.h:134
#define GETQUERY(x)
Definition: _int.h:142
char sign
Definition: informix.c:693
uint64 TSQuerySign
Definition: ts_utils.h:215
int32 valcrc
Definition: ts_type.h:151
QueryItemType type
Definition: ts_type.h:195
int32 size
Definition: ts_type.h:208
int i
QueryOperand qoperand
Definition: ts_type.h:197
static int remove_duplicates ( char **  strings,
int  n 
)
static

Definition at line 306 of file tsquery_op.c.

References i.

Referenced by tsq_mcontains().

307 {
308  if (n <= 1)
309  return n;
310  else
311  {
312  int i;
313  char *prev = strings[0];
314  int new_n = 1;
315 
316  for (i = 1; i < n; i++)
317  {
318  if (strcmp(strings[i], prev) != 0)
319  {
320  strings[new_n++] = strings[i];
321  prev = strings[i];
322  }
323  }
324  return new_n;
325  }
326 }
int i
Datum tsq_mcontained ( PG_FUNCTION_ARGS  )

Definition at line 375 of file tsquery_op.c.

References DirectFunctionCall2, PG_GETARG_DATUM, PG_RETURN_DATUM, and tsq_mcontains().

376 {
380  PG_GETARG_DATUM(1),
381  PG_GETARG_DATUM(0)
382  )
383  );
384 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
Datum tsq_mcontains(PG_FUNCTION_ARGS)
Definition: tsquery_op.c:329
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:586
Datum tsq_mcontains ( PG_FUNCTION_ARGS  )

Definition at line 329 of file tsquery_op.c.

References cmp_string(), collectTSQueryValues(), i, PG_GETARG_TSQUERY, PG_RETURN_BOOL, qsort, remove_duplicates(), and result.

Referenced by tsq_mcontained().

330 {
331  TSQuery query = PG_GETARG_TSQUERY(0);
332  TSQuery ex = PG_GETARG_TSQUERY(1);
333  char **query_values;
334  int query_nvalues;
335  char **ex_values;
336  int ex_nvalues;
337  bool result = true;
338 
339  /* Extract the query terms into arrays */
340  query_values = collectTSQueryValues(query, &query_nvalues);
341  ex_values = collectTSQueryValues(ex, &ex_nvalues);
342 
343  /* Sort and remove duplicates from both arrays */
344  qsort(query_values, query_nvalues, sizeof(char *), cmp_string);
345  query_nvalues = remove_duplicates(query_values, query_nvalues);
346  qsort(ex_values, ex_nvalues, sizeof(char *), cmp_string);
347  ex_nvalues = remove_duplicates(ex_values, ex_nvalues);
348 
349  if (ex_nvalues > query_nvalues)
350  result = false;
351  else
352  {
353  int i;
354  int j = 0;
355 
356  for (i = 0; i < ex_nvalues; i++)
357  {
358  for (; j < query_nvalues; j++)
359  {
360  if (strcmp(ex_values[i], query_values[j]) == 0)
361  break;
362  }
363  if (j == query_nvalues)
364  {
365  result = false;
366  break;
367  }
368  }
369  }
370 
371  PG_RETURN_BOOL(result);
372 }
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
return result
Definition: formatting.c:1633
static char ** collectTSQueryValues(TSQuery a, int *nvalues_p)
Definition: tsquery_op.c:266
static int cmp_string(const void *a, const void *b)
Definition: tsquery_op.c:297
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
static int remove_duplicates(char **strings, int n)
Definition: tsquery_op.c:306
int i
#define qsort(a, b, c, d)
Definition: port.h:443
Datum tsquery_and ( PG_FUNCTION_ARGS  )

Definition at line 52 of file tsquery_op.c.

References join_tsqueries(), OP_AND, PG_FREE_IF_COPY, PG_GETARG_TSQUERY_COPY, PG_RETURN_POINTER, PG_RETURN_TSQUERY, QTN2QT(), QTNFree(), and TSQueryData::size.

53 {
56  QTNode *res;
57  TSQuery query;
58 
59  if (a->size == 0)
60  {
61  PG_FREE_IF_COPY(a, 1);
63  }
64  else if (b->size == 0)
65  {
66  PG_FREE_IF_COPY(b, 1);
68  }
69 
70  res = join_tsqueries(a, b, OP_AND, 0);
71 
72  query = QTN2QT(res);
73 
74  QTNFree(res);
75  PG_FREE_IF_COPY(a, 0);
76  PG_FREE_IF_COPY(b, 1);
77 
78  PG_RETURN_TSQUERY(query);
79 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
void QTNFree(QTNode *in)
Definition: tsquery_util.c:63
static QTNode * join_tsqueries(TSQuery a, TSQuery b, int8 operator, uint16 distance)
Definition: tsquery_op.c:31
#define PG_RETURN_TSQUERY(x)
Definition: ts_type.h:240
#define OP_AND
Definition: ts_type.h:167
#define PG_GETARG_TSQUERY_COPY(n)
Definition: ts_type.h:239
TSQuery QTN2QT(QTNode *in)
Definition: tsquery_util.c:362
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
int32 size
Definition: ts_type.h:208
Datum tsquery_cmp ( PG_FUNCTION_ARGS  )

Definition at line 214 of file tsquery_op.c.

References CompareTSQ(), PG_FREE_IF_COPY, PG_GETARG_TSQUERY_COPY, and PG_RETURN_INT32.

215 {
218  int res = CompareTSQ(a, b);
219 
220  PG_FREE_IF_COPY(a, 0);
221  PG_FREE_IF_COPY(b, 1);
222 
223  PG_RETURN_INT32(res);
224 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_TSQUERY_COPY(n)
Definition: ts_type.h:239
static int CompareTSQ(TSQuery a, TSQuery b)
Definition: tsquery_op.c:188
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Datum tsquery_not ( PG_FUNCTION_ARGS  )

Definition at line 158 of file tsquery_op.c.

References QTNode::child, QTNode::flags, GETOPERAND, GETQUERY, QTNode::nchild, OP_NOT, QueryOperator::oper, palloc0(), PG_FREE_IF_COPY, PG_GETARG_TSQUERY_COPY, PG_RETURN_POINTER, QI_OPR, QueryItem::qoperator, QT2QTN(), QTN2QT(), QTN_NEEDFREE, QTNFree(), TSQueryData::size, QueryItem::type, and QTNode::valnode.

159 {
161  QTNode *res;
162  TSQuery query;
163 
164  if (a->size == 0)
166 
167  res = (QTNode *) palloc0(sizeof(QTNode));
168 
169  res->flags |= QTN_NEEDFREE;
170 
171  res->valnode = (QueryItem *) palloc0(sizeof(QueryItem));
172  res->valnode->type = QI_OPR;
173  res->valnode->qoperator.oper = OP_NOT;
174 
175  res->child = (QTNode **) palloc0(sizeof(QTNode *));
176  res->child[0] = QT2QTN(GETQUERY(a), GETOPERAND(a));
177  res->nchild = 1;
178 
179  query = QTN2QT(res);
180 
181  QTNFree(res);
182  PG_FREE_IF_COPY(a, 0);
183 
184  PG_RETURN_POINTER(query);
185 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
QueryOperator qoperator
Definition: ts_type.h:196
void QTNFree(QTNode *in)
Definition: tsquery_util.c:63
QTNode * QT2QTN(QueryItem *in, char *operand)
Definition: tsquery_util.c:24
#define GETQUERY(x)
Definition: _int.h:142
#define GETOPERAND(x)
Definition: ltree.h:118
#define PG_GETARG_TSQUERY_COPY(n)
Definition: ts_type.h:239
TSQuery QTN2QT(QTNode *in)
Definition: tsquery_util.c:362
#define QI_OPR
Definition: ts_type.h:135
QueryItemType type
Definition: ts_type.h:195
void * palloc0(Size size)
Definition: mcxt.c:878
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
struct QTNode ** child
Definition: ts_utils.h:207
#define QTN_NEEDFREE
Definition: ts_utils.h:211
QueryItem * valnode
Definition: ts_utils.h:202
int32 nchild
Definition: ts_utils.h:204
int32 size
Definition: ts_type.h:208
uint32 flags
Definition: ts_utils.h:203
#define OP_NOT
Definition: ts_type.h:166
Datum tsquery_numnode ( PG_FUNCTION_ARGS  )

Definition at line 21 of file tsquery_op.c.

References PG_FREE_IF_COPY, PG_GETARG_TSQUERY, PG_RETURN_INT32, and TSQueryData::size.

22 {
23  TSQuery query = PG_GETARG_TSQUERY(0);
24  int nnode = query->size;
25 
26  PG_FREE_IF_COPY(query, 0);
27  PG_RETURN_INT32(nnode);
28 }
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
int32 size
Definition: ts_type.h:208
Datum tsquery_or ( PG_FUNCTION_ARGS  )

Definition at line 82 of file tsquery_op.c.

References join_tsqueries(), OP_OR, PG_FREE_IF_COPY, PG_GETARG_TSQUERY_COPY, PG_RETURN_POINTER, PG_RETURN_TSQUERY, QTN2QT(), QTNFree(), and TSQueryData::size.

83 {
86  QTNode *res;
87  TSQuery query;
88 
89  if (a->size == 0)
90  {
91  PG_FREE_IF_COPY(a, 1);
93  }
94  else if (b->size == 0)
95  {
96  PG_FREE_IF_COPY(b, 1);
98  }
99 
100  res = join_tsqueries(a, b, OP_OR, 0);
101 
102  query = QTN2QT(res);
103 
104  QTNFree(res);
105  PG_FREE_IF_COPY(a, 0);
106  PG_FREE_IF_COPY(b, 1);
107 
108  PG_RETURN_TSQUERY(query);
109 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
void QTNFree(QTNode *in)
Definition: tsquery_util.c:63
#define OP_OR
Definition: ts_type.h:168
static QTNode * join_tsqueries(TSQuery a, TSQuery b, int8 operator, uint16 distance)
Definition: tsquery_op.c:31
#define PG_RETURN_TSQUERY(x)
Definition: ts_type.h:240
#define PG_GETARG_TSQUERY_COPY(n)
Definition: ts_type.h:239
TSQuery QTN2QT(QTNode *in)
Definition: tsquery_util.c:362
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
int32 size
Definition: ts_type.h:208
Datum tsquery_phrase ( PG_FUNCTION_ARGS  )

Definition at line 148 of file tsquery_op.c.

References DirectFunctionCall3, Int32GetDatum, PG_GETARG_DATUM, PG_RETURN_POINTER, and tsquery_phrase_distance().

149 {
152  PG_GETARG_DATUM(0),
153  PG_GETARG_DATUM(1),
154  Int32GetDatum(1)));
155 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
Datum tsquery_phrase_distance(PG_FUNCTION_ARGS)
Definition: tsquery_op.c:112
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:588
#define Int32GetDatum(X)
Definition: postgres.h:485
Datum tsquery_phrase_distance ( PG_FUNCTION_ARGS  )

Definition at line 112 of file tsquery_op.c.

References NODE::distance, ereport, errcode(), errmsg(), ERROR, join_tsqueries(), MAXENTRYPOS, OP_PHRASE, PG_FREE_IF_COPY, PG_GETARG_INT32, PG_GETARG_TSQUERY_COPY, PG_RETURN_POINTER, PG_RETURN_TSQUERY, QTN2QT(), QTNFree(), and TSQueryData::size.

Referenced by tsquery_phrase().

113 {
116  QTNode *res;
117  TSQuery query;
118  int32 distance = PG_GETARG_INT32(2);
119 
120  if (distance < 0 || distance > MAXENTRYPOS)
121  ereport(ERROR,
122  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
123  errmsg("distance in phrase operator should be non-negative and less than %d",
124  MAXENTRYPOS)));
125  if (a->size == 0)
126  {
127  PG_FREE_IF_COPY(a, 1);
129  }
130  else if (b->size == 0)
131  {
132  PG_FREE_IF_COPY(b, 1);
134  }
135 
136  res = join_tsqueries(a, b, OP_PHRASE, (uint16) distance);
137 
138  query = QTN2QT(res);
139 
140  QTNFree(res);
141  PG_FREE_IF_COPY(a, 0);
142  PG_FREE_IF_COPY(b, 1);
143 
144  PG_RETURN_TSQUERY(query);
145 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
void QTNFree(QTNode *in)
Definition: tsquery_util.c:63
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:256
static QTNode * join_tsqueries(TSQuery a, TSQuery b, int8 operator, uint16 distance)
Definition: tsquery_op.c:31
#define PG_RETURN_TSQUERY(x)
Definition: ts_type.h:240
#define PG_GETARG_TSQUERY_COPY(n)
Definition: ts_type.h:239
unsigned short uint16
Definition: c.h:267
#define ERROR
Definition: elog.h:43
TSQuery QTN2QT(QTNode *in)
Definition: tsquery_util.c:362
#define ereport(elevel, rest)
Definition: elog.h:122
#define OP_PHRASE
Definition: ts_type.h:169
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
int errmsg(const char *fmt,...)
Definition: elog.c:797
int32 size
Definition: ts_type.h:208
#define MAXENTRYPOS
Definition: ts_type.h:85