PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ts_utils.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * ts_utils.h
4  * helper utilities for tsearch
5  *
6  * Copyright (c) 1998-2016, PostgreSQL Global Development Group
7  *
8  * src/include/tsearch/ts_utils.h
9  *
10  *-------------------------------------------------------------------------
11  */
12 #ifndef _PG_TS_UTILS_H_
13 #define _PG_TS_UTILS_H_
14 
15 #include "tsearch/ts_type.h"
16 #include "tsearch/ts_public.h"
17 #include "nodes/pg_list.h"
18 
19 /*
20  * Common parse definitions for tsvector and tsquery
21  */
22 
23 /* tsvector parser support. */
24 
25 struct TSVectorParseStateData; /* opaque struct in tsvector_parser.c */
27 
28 extern TSVectorParseState init_tsvector_parser(char *input,
29  bool oprisdelim,
30  bool is_tsquery);
31 extern void reset_tsvector_parser(TSVectorParseState state, char *input);
32 extern bool gettoken_tsvector(TSVectorParseState state,
33  char **token, int *len,
34  WordEntryPos **pos, int *poslen,
35  char **endptr);
36 extern void close_tsvector_parser(TSVectorParseState state);
37 
38 /* parse_tsquery */
39 
40 struct TSQueryParserStateData; /* private in backend/utils/adt/tsquery.c */
42 
43 typedef void (*PushFunction) (Datum opaque, TSQueryParserState state,
44  char *token, int tokenlen,
45  int16 tokenweights, /* bitmap as described
46  * in QueryOperand
47  * struct */
48  bool prefix);
49 
50 extern TSQuery parse_tsquery(char *buf,
51  PushFunction pushval,
52  Datum opaque, bool isplain);
53 
54 /* Functions for use by PushFunction implementations */
55 extern void pushValue(TSQueryParserState state,
56  char *strval, int lenval, int16 weight, bool prefix);
57 extern void pushStop(TSQueryParserState state);
58 extern void pushOperator(TSQueryParserState state, int8 oper, int16 distance);
59 
60 /*
61  * parse plain text and lexize words
62  */
63 typedef struct
64 {
67  union
68  {
70 
71  /*
72  * When apos array is used, apos[0] is the number of elements in the
73  * array (excluding apos[0]), and alen is the allocated size of the
74  * array.
75  */
77  } pos;
78  uint16 flags; /* currently, only TSL_PREFIX */
79  char *word;
81 } ParsedWord;
82 
83 typedef struct
84 {
89 } ParsedText;
90 
91 extern void parsetext(Oid cfgId, ParsedText *prs, char *buf, int32 buflen);
92 
93 /*
94  * headline framework, flow in common to generate:
95  * 1 parse text with hlparsetext
96  * 2 parser-specific function to find part
97  * 3 generateHeadline to generate result text
98  */
99 
100 extern void hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query,
101  char *buf, int32 buflen);
103 
104 /*
105  * Common check function for tsvector @@ tsquery
106  */
107 typedef struct ExecPhraseData
108 {
109  int npos;
110  bool allocated;
113 
114 extern bool TS_execute(QueryItem *curitem, void *checkval, bool calcnot,
115  bool (*chkcond) (void *, QueryOperand *, ExecPhraseData *));
116 extern bool tsquery_requires_match(QueryItem *curitem);
117 
118 /*
119  * to_ts* - text transformation to tsvector, tsquery
120  */
121 extern TSVector make_tsvector(ParsedText *prs);
122 extern int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix);
123 
132 
133 /*
134  * GiST support function
135  */
136 
145 
146 /*
147  * IO functions for pseudotype gtsvector
148  * used internally in tsvector GiST opclass
149  */
152 
153 /*
154  * GIN support function
155  */
156 
168 
169 /*
170  * Possible strategy numbers for indexes
171  * TSearchStrategyNumber - (tsvector|text) @@ tsquery
172  * TSearchWithClassStrategyNumber - tsvector @@@ tsquery
173  */
174 #define TSearchStrategyNumber 1
175 #define TSearchWithClassStrategyNumber 2
176 
177 /*
178  * TSQuery Utilities
179  */
180 extern QueryItem *clean_NOT(QueryItem *ptr, int32 *len);
182 
183 typedef struct QTNode
184 {
188  char *word;
190  struct QTNode **child;
191 } QTNode;
192 
193 /* bits in QTNode.flags */
194 #define QTN_NEEDFREE 0x01
195 #define QTN_NOCHANGE 0x02
196 #define QTN_WORDFREE 0x04
197 
198 typedef uint64 TSQuerySign;
199 
200 #define TSQS_SIGLEN (sizeof(TSQuerySign)*BITS_PER_BYTE)
201 
202 #define TSQuerySignGetDatum(X) Int64GetDatum((int64) (X))
203 #define DatumGetTSQuerySign(X) ((TSQuerySign) DatumGetInt64(X))
204 #define PG_RETURN_TSQUERYSIGN(X) return TSQuerySignGetDatum(X)
205 #define PG_GETARG_TSQUERYSIGN(n) DatumGetTSQuerySign(PG_GETARG_DATUM(n))
206 
207 
208 extern QTNode *QT2QTN(QueryItem *in, char *operand);
209 extern TSQuery QTN2QT(QTNode *in);
210 extern void QTNFree(QTNode *in);
211 extern void QTNSort(QTNode *in);
212 extern void QTNTernary(QTNode *in);
213 extern void QTNBinary(QTNode *in);
214 extern int QTNodeCompare(QTNode *an, QTNode *bn);
215 extern QTNode *QTNCopy(QTNode *in);
216 extern void QTNClearFlags(QTNode *in, uint32 flags);
217 extern bool QTNEq(QTNode *a, QTNode *b);
218 extern TSQuerySign makeTSQuerySign(TSQuery a);
219 extern QTNode *findsubquery(QTNode *root, QTNode *ex, QTNode *subs,
220  bool *isfind);
221 
222 /*
223  * TSQuery GiST support
224  */
233 
234 /*
235  * Parser interface to SQL
236  */
241 
242 /*
243  * Default word parser
244  */
245 
251 
252 /*
253  * Dictionary interface to SQL
254  */
256 
257 /*
258  * Simple built-in dictionary
259  */
262 
263 /*
264  * Synonym built-in dictionary
265  */
268 
269 /*
270  * ISpell dictionary
271  */
274 
275 /*
276  * Thesaurus
277  */
280 
281 /*
282  * headline
283  */
288 
289 /*
290  * current cfg
291  */
293 
294 #endif /* _PG_TS_UTILS_H_ */
uint16 WordEntryPos
Definition: ts_type.h:63
QTNode * QTNCopy(QTNode *in)
Definition: tsquery_util.c:362
signed short int16
Definition: c.h:252
Datum prsd_headline(PG_FUNCTION_ARGS)
Definition: wparser_def.c:2537
Datum gin_cmp_tslexeme(PG_FUNCTION_ARGS)
Definition: tsginidx.c:25
struct TSQueryParserStateData * TSQueryParserState
Definition: ts_utils.h:41
Datum gtsvector_consistent(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:341
Datum ts_headline_byid_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:289
void pushOperator(TSQueryParserState state, int8 oper, int16 distance)
Definition: tsquery.c:335
QTNode * QT2QTN(QueryItem *in, char *operand)
Definition: tsquery_util.c:21
Datum gin_extract_tsvector(PG_FUNCTION_ARGS)
Definition: tsginidx.c:65
char * word
Definition: ts_utils.h:188
bool QTNEq(QTNode *a, QTNode *b)
Definition: tsquery_util.c:162
QueryItem * clean_NOT(QueryItem *ptr, int32 *len)
struct TSVectorParseStateData * TSVectorParseState
Definition: ts_utils.h:26
Datum gin_extract_tsquery_5args(PG_FUNCTION_ARGS)
Definition: tsginidx.c:377
uint32 sign
Definition: ts_utils.h:189
TSQuery parse_tsquery(char *buf, PushFunction pushval, Datum opaque, bool isplain)
Definition: tsquery.c:611
bool allocated
Definition: ts_utils.h:110
void QTNClearFlags(QTNode *in, uint32 flags)
Definition: tsquery_util.c:397
uint16 * apos
Definition: ts_utils.h:76
uint16 nvariant
Definition: ts_utils.h:66
Datum ts_lexize(PG_FUNCTION_ARGS)
Definition: dict.c:26
Datum gtsquery_consistent_oldsig(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:274
Datum gtsvector_picksplit(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:624
Datum to_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:381
void QTNTernary(QTNode *in)
Definition: tsquery_util.c:180
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)
Definition: tsvector_op.c:1092
Datum ts_parse_byid(PG_FUNCTION_ARGS)
Definition: wparser.c:243
int32 lenwords
Definition: ts_utils.h:86
Datum prsd_lextype(PG_FUNCTION_ARGS)
Definition: wparser_def.c:1967
Datum prsd_nexttoken(PG_FUNCTION_ARGS)
Definition: wparser_def.c:1991
unsigned int Oid
Definition: postgres_ext.h:31
Datum plainto_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:411
Datum gtsquery_consistent(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:52
Datum gtsvector_same(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:442
Datum ts_parse_byname(PG_FUNCTION_ARGS)
Definition: wparser.c:265
int32 curwords
Definition: ts_utils.h:87
void QTNBinary(QTNode *in)
Definition: tsquery_util.c:226
signed int int32
Definition: c.h:253
Datum gtsquery_penalty(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:138
bool tsquery_requires_match(QueryItem *curitem)
Definition: tsvector_op.c:1529
Datum gin_cmp_prefix(PG_FUNCTION_ARGS)
Definition: tsginidx.c:41
Datum phraseto_tsquery(PG_FUNCTION_ARGS)
Definition: to_tsany.c:442
Datum gin_tsquery_consistent(PG_FUNCTION_ARGS)
Definition: tsginidx.c:279
TSVectorParseState init_tsvector_parser(char *input, bool oprisdelim, bool is_tsquery)
void hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int32 buflen)
unsigned short uint16
Definition: c.h:264
Datum to_tsvector_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:216
Datum gtsquery_compress(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:25
Datum get_current_ts_config(PG_FUNCTION_ARGS)
Definition: to_tsany.c:29
TSVector make_tsvector(ParsedText *prs)
Definition: to_tsany.c:145
Datum gtsvectorin(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:95
void pushStop(TSQueryParserState state)
Definition: tsquery.c:420
static char * buf
Definition: pg_test_fsync.c:65
Datum gtsquery_picksplit(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:168
Datum gin_tsquery_triconsistent(PG_FUNCTION_ARGS)
Definition: tsginidx.c:318
uint64 TSQuerySign
Definition: ts_utils.h:198
Datum thesaurus_lexize(PG_FUNCTION_ARGS)
WordEntryPos * pos
Definition: ts_utils.h:111
Datum gtsvector_compress(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:181
unsigned int uint32
Definition: c.h:265
ParsedWord * words
Definition: ts_utils.h:85
uint16 pos
Definition: ts_utils.h:69
Datum dsynonym_lexize(PG_FUNCTION_ARGS)
Definition: dict_synonym.c:209
Datum dispell_init(PG_FUNCTION_ARGS)
Definition: dict_ispell.c:29
TSQuery QTN2QT(QTNode *in)
Definition: tsquery_util.c:334
Datum gtsquery_decompress(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:46
signed char int8
Definition: c.h:251
void parsetext(Oid cfgId, ParsedText *prs, char *buf, int32 buflen)
Datum gin_extract_tsvector_2args(PG_FUNCTION_ARGS)
Definition: tsginidx.c:365
void reset_tsvector_parser(TSVectorParseState state, char *input)
uintptr_t Datum
Definition: postgres.h:374
Datum gin_extract_tsquery(PG_FUNCTION_ARGS)
Definition: tsginidx.c:95
void(* PushFunction)(Datum opaque, TSQueryParserState state, char *token, int tokenlen, int16 tokenweights, bool prefix)
Definition: ts_utils.h:43
Datum to_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:363
void QTNFree(QTNode *in)
Definition: tsquery_util.c:54
bool gettoken_tsvector(TSVectorParseState state, char **token, int *len, WordEntryPos **pos, int *poslen, char **endptr)
Datum gtsvector_penalty(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:542
Datum dsimple_lexize(PG_FUNCTION_ARGS)
Definition: dict_simple.c:74
Datum to_tsvector(PG_FUNCTION_ARGS)
Definition: to_tsany.c:248
Datum gin_tsquery_consistent_oldsig(PG_FUNCTION_ARGS)
Definition: tsginidx.c:411
Datum ts_token_type_byid(PG_FUNCTION_ARGS)
Definition: wparser.c:99
Datum gin_tsquery_consistent_6args(PG_FUNCTION_ARGS)
Definition: tsginidx.c:389
Datum dsimple_init(PG_FUNCTION_ARGS)
Definition: dict_simple.c:29
Datum prsd_end(PG_FUNCTION_ARGS)
Definition: wparser_def.c:2007
Datum phraseto_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:424
bool TS_execute(QueryItem *curitem, void *checkval, bool calcnot, bool(*chkcond)(void *, QueryOperand *, ExecPhraseData *))
Definition: regguts.h:313
Datum dsynonym_init(PG_FUNCTION_ARGS)
Definition: dict_synonym.c:91
uint16 len
Definition: ts_utils.h:65
QTNode * findsubquery(QTNode *root, QTNode *ex, QTNode *subs, bool *isfind)
int32 pos
Definition: ts_utils.h:88
void QTNSort(QTNode *in)
Definition: tsquery_util.c:145
Datum ts_token_type_byname(PG_FUNCTION_ARGS)
Definition: wparser.c:118
Datum plainto_tsquery_byid(PG_FUNCTION_ARGS)
Definition: to_tsany.c:393
Datum gtsvector_decompress(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:272
struct QTNode ** child
Definition: ts_utils.h:190
struct ExecPhraseData ExecPhraseData
int QTNodeCompare(QTNode *an, QTNode *bn)
Definition: tsquery_util.c:86
Datum prsd_start(PG_FUNCTION_ARGS)
Definition: wparser_def.c:1985
Datum gtsquery_union(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:88
uint16 flags
Definition: ts_utils.h:78
uint32 alen
Definition: ts_utils.h:80
QueryItem * valnode
Definition: ts_utils.h:185
TSQuery cleanup_fakeval_and_phrase(TSQuery in)
Datum gtsquery_same(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:106
text * generateHeadline(HeadlineParsedText *prs)
Definition: ts_parse.c:598
int32 nchild
Definition: ts_utils.h:187
Datum gtsvector_consistent_oldsig(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:817
Datum dispell_lexize(PG_FUNCTION_ARGS)
Definition: dict_ispell.c:110
struct QTNode QTNode
Definition: c.h:434
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
void pushValue(TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
Definition: tsquery.c:384
Datum gtsvectorout(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:110
Datum ts_headline_opt(PG_FUNCTION_ARGS)
Definition: wparser.c:356
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:375
Datum thesaurus_init(PG_FUNCTION_ARGS)
TSQuerySign makeTSQuerySign(TSQuery a)
Definition: tsquery_op.c:248
Datum ts_headline(PG_FUNCTION_ARGS)
Definition: wparser.c:347
void close_tsvector_parser(TSVectorParseState state)
Datum gtsvector_union(PG_FUNCTION_ARGS)
Definition: tsgistidx.c:409
uint32 flags
Definition: ts_utils.h:186
Datum gin_extract_tsquery_oldsig(PG_FUNCTION_ARGS)
Definition: tsginidx.c:401
char * word
Definition: ts_utils.h:79
Datum ts_headline_byid(PG_FUNCTION_ARGS)
Definition: wparser.c:338