ts_utils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include "nodes/pg_list.h"
  15. #include "tsearch/ts_public.h"
  16. #include "tsearch/ts_type.h"
  17. /*
  18. * Common parse definitions for tsvector and tsquery
  19. */
  20. /* tsvector parser support. */
  21. struct TSVectorParseStateData; /* opaque struct in tsvector_parser.c */
  22. typedef struct TSVectorParseStateData *TSVectorParseState;
  23. extern TSVectorParseState init_tsvector_parser(char *input,
  24. bool oprisdelim,
  25. bool is_tsquery);
  26. extern void reset_tsvector_parser(TSVectorParseState state, char *input);
  27. extern bool gettoken_tsvector(TSVectorParseState state,
  28. char **token, int *len,
  29. WordEntryPos **pos, int *poslen,
  30. char **endptr);
  31. extern void close_tsvector_parser(TSVectorParseState state);
  32. /* parse_tsquery */
  33. struct TSQueryParserStateData; /* private in backend/utils/adt/tsquery.c */
  34. typedef struct TSQueryParserStateData *TSQueryParserState;
  35. typedef void (*PushFunction) (Datum opaque, TSQueryParserState state,
  36. char *token, int tokenlen,
  37. int16 tokenweights, /* bitmap as described
  38. * in QueryOperand
  39. * struct */
  40. bool prefix);
  41. extern TSQuery parse_tsquery(char *buf,
  42. PushFunction pushval,
  43. Datum opaque, bool isplain);
  44. /* Functions for use by PushFunction implementations */
  45. extern void pushValue(TSQueryParserState state,
  46. char *strval, int lenval, int16 weight, bool prefix);
  47. extern void pushStop(TSQueryParserState state);
  48. extern void pushOperator(TSQueryParserState state, int8 oper, int16 distance);
  49. /*
  50. * parse plain text and lexize words
  51. */
  52. typedef struct
  53. {
  54. uint16 len;
  55. uint16 nvariant;
  56. union
  57. {
  58. uint16 pos;
  59. /*
  60. * When apos array is used, apos[0] is the number of elements in the
  61. * array (excluding apos[0]), and alen is the allocated size of the
  62. * array.
  63. */
  64. uint16 *apos;
  65. } pos;
  66. uint16 flags; /* currently, only TSL_PREFIX */
  67. char *word;
  68. uint32 alen;
  69. } ParsedWord;
  70. typedef struct
  71. {
  72. ParsedWord *words;
  73. int32 lenwords;
  74. int32 curwords;
  75. int32 pos;
  76. } ParsedText;
  77. extern void parsetext(Oid cfgId, ParsedText *prs, char *buf, int32 buflen);
  78. /*
  79. * headline framework, flow in common to generate:
  80. * 1 parse text with hlparsetext
  81. * 2 parser-specific function to find part
  82. * 3 generateHeadline to generate result text
  83. */
  84. extern void hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query,
  85. char *buf, int32 buflen);
  86. extern text *generateHeadline(HeadlineParsedText *prs);
  87. /*
  88. * TSQuery execution support
  89. *
  90. * TS_execute() executes a tsquery against data that can be represented in
  91. * various forms. The TSExecuteCallback callback function is called to check
  92. * whether a given primitive tsquery value is matched in the data.
  93. */
  94. /*
  95. * struct ExecPhraseData is passed to a TSExecuteCallback function if we need
  96. * lexeme position data (because of a phrase-match operator in the tsquery).
  97. * The callback should fill in position data when it returns true (success).
  98. * If it cannot return position data, it may leave "data" unchanged, but
  99. * then the caller of TS_execute() must pass the TS_EXEC_PHRASE_NO_POS flag
  100. * and must arrange for a later recheck with position data available.
  101. *
  102. * The reported lexeme positions must be sorted and unique. Callers must only
  103. * consult the position bits of the pos array, ie, WEP_GETPOS(data->pos[i]).
  104. * This allows the returned "pos" to point directly to the WordEntryPos
  105. * portion of a tsvector value. If "allocated" is true then the pos array
  106. * is palloc'd workspace and caller may free it when done.
  107. *
  108. * "negate" means that the pos array contains positions where the query does
  109. * not match, rather than positions where it does. "width" is positive when
  110. * the match is wider than one lexeme. Neither of these fields normally need
  111. * to be touched by TSExecuteCallback functions; they are used for
  112. * phrase-search processing within TS_execute.
  113. *
  114. * All fields of the ExecPhraseData struct are initially zeroed by caller.
  115. */
  116. typedef struct ExecPhraseData
  117. {
  118. int npos; /* number of positions reported */
  119. bool allocated; /* pos points to palloc'd data? */
  120. bool negate; /* positions are where query is NOT matched */
  121. WordEntryPos *pos; /* ordered, non-duplicate lexeme positions */
  122. int width; /* width of match in lexemes, less 1 */
  123. } ExecPhraseData;
  124. /*
  125. * Signature for TSQuery lexeme check functions
  126. *
  127. * arg: opaque value passed through from caller of TS_execute
  128. * val: lexeme to test for presence of
  129. * data: to be filled with lexeme positions; NULL if position data not needed
  130. *
  131. * Return TRUE if lexeme is present in data, else FALSE. If data is not
  132. * NULL, it should be filled with lexeme positions, but function can leave
  133. * it as zeroes if position data is not available.
  134. */
  135. typedef bool (*TSExecuteCallback) (void *arg, QueryOperand *val,
  136. ExecPhraseData *data);
  137. /*
  138. * Flag bits for TS_execute
  139. */
  140. #define TS_EXEC_EMPTY (0x00)
  141. /*
  142. * If TS_EXEC_CALC_NOT is not set, then NOT expressions are automatically
  143. * evaluated to be true. Useful in cases where NOT cannot be accurately
  144. * computed (GiST) or it isn't important (ranking). From TS_execute's
  145. * perspective, !CALC_NOT means that the TSExecuteCallback function might
  146. * return false-positive indications of a lexeme's presence.
  147. */
  148. #define TS_EXEC_CALC_NOT (0x01)
  149. /*
  150. * If TS_EXEC_PHRASE_NO_POS is set, allow OP_PHRASE to be executed lossily
  151. * in the absence of position information: a TRUE result indicates that the
  152. * phrase might be present. Without this flag, OP_PHRASE always returns
  153. * false if lexeme position information is not available.
  154. */
  155. #define TS_EXEC_PHRASE_NO_POS (0x02)
  156. /* Obsolete spelling of TS_EXEC_PHRASE_NO_POS: */
  157. #define TS_EXEC_PHRASE_AS_AND TS_EXEC_PHRASE_NO_POS
  158. extern bool TS_execute(QueryItem *curitem, void *arg, uint32 flags,
  159. TSExecuteCallback chkcond);
  160. extern bool tsquery_requires_match(QueryItem *curitem);
  161. /*
  162. * to_ts* - text transformation to tsvector, tsquery
  163. */
  164. extern TSVector make_tsvector(ParsedText *prs);
  165. extern int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix);
  166. extern Datum to_tsvector_byid(PG_FUNCTION_ARGS);
  167. extern Datum to_tsvector(PG_FUNCTION_ARGS);
  168. extern Datum to_tsquery_byid(PG_FUNCTION_ARGS);
  169. extern Datum to_tsquery(PG_FUNCTION_ARGS);
  170. extern Datum plainto_tsquery_byid(PG_FUNCTION_ARGS);
  171. extern Datum plainto_tsquery(PG_FUNCTION_ARGS);
  172. extern Datum phraseto_tsquery_byid(PG_FUNCTION_ARGS);
  173. extern Datum phraseto_tsquery(PG_FUNCTION_ARGS);
  174. /*
  175. * GiST support function
  176. */
  177. extern Datum gtsvector_compress(PG_FUNCTION_ARGS);
  178. extern Datum gtsvector_decompress(PG_FUNCTION_ARGS);
  179. extern Datum gtsvector_consistent(PG_FUNCTION_ARGS);
  180. extern Datum gtsvector_union(PG_FUNCTION_ARGS);
  181. extern Datum gtsvector_same(PG_FUNCTION_ARGS);
  182. extern Datum gtsvector_penalty(PG_FUNCTION_ARGS);
  183. extern Datum gtsvector_picksplit(PG_FUNCTION_ARGS);
  184. extern Datum gtsvector_consistent_oldsig(PG_FUNCTION_ARGS);
  185. /*
  186. * IO functions for pseudotype gtsvector
  187. * used internally in tsvector GiST opclass
  188. */
  189. extern Datum gtsvectorin(PG_FUNCTION_ARGS);
  190. extern Datum gtsvectorout(PG_FUNCTION_ARGS);
  191. /*
  192. * GIN support function
  193. */
  194. extern Datum gin_extract_tsvector(PG_FUNCTION_ARGS);
  195. extern Datum gin_cmp_tslexeme(PG_FUNCTION_ARGS);
  196. extern Datum gin_cmp_prefix(PG_FUNCTION_ARGS);
  197. extern Datum gin_extract_tsquery(PG_FUNCTION_ARGS);
  198. extern Datum gin_tsquery_consistent(PG_FUNCTION_ARGS);
  199. extern Datum gin_tsquery_triconsistent(PG_FUNCTION_ARGS);
  200. extern Datum gin_extract_tsvector_2args(PG_FUNCTION_ARGS);
  201. extern Datum gin_extract_tsquery_5args(PG_FUNCTION_ARGS);
  202. extern Datum gin_tsquery_consistent_6args(PG_FUNCTION_ARGS);
  203. extern Datum gin_extract_tsquery_oldsig(PG_FUNCTION_ARGS);
  204. extern Datum gin_tsquery_consistent_oldsig(PG_FUNCTION_ARGS);
  205. /*
  206. * Possible strategy numbers for indexes
  207. * TSearchStrategyNumber - (tsvector|text) @@ tsquery
  208. * TSearchWithClassStrategyNumber - tsvector @@@ tsquery
  209. */
  210. #define TSearchStrategyNumber 1
  211. #define TSearchWithClassStrategyNumber 2
  212. /*
  213. * TSQuery Utilities
  214. */
  215. extern QueryItem *clean_NOT(QueryItem *ptr, int32 *len);
  216. extern TSQuery cleanup_tsquery_stopwords(TSQuery in);
  217. typedef struct QTNode
  218. {
  219. QueryItem *valnode;
  220. uint32 flags;
  221. int32 nchild;
  222. char *word;
  223. uint32 sign;
  224. struct QTNode **child;
  225. } QTNode;
  226. /* bits in QTNode.flags */
  227. #define QTN_NEEDFREE 0x01
  228. #define QTN_NOCHANGE 0x02
  229. #define QTN_WORDFREE 0x04
  230. typedef uint64 TSQuerySign;
  231. #define TSQS_SIGLEN (sizeof(TSQuerySign)*BITS_PER_BYTE)
  232. #define TSQuerySignGetDatum(X) Int64GetDatum((int64) (X))
  233. #define DatumGetTSQuerySign(X) ((TSQuerySign) DatumGetInt64(X))
  234. #define PG_RETURN_TSQUERYSIGN(X) return TSQuerySignGetDatum(X)
  235. #define PG_GETARG_TSQUERYSIGN(n) DatumGetTSQuerySign(PG_GETARG_DATUM(n))
  236. extern QTNode *QT2QTN(QueryItem *in, char *operand);
  237. extern TSQuery QTN2QT(QTNode *in);
  238. extern void QTNFree(QTNode *in);
  239. extern void QTNSort(QTNode *in);
  240. extern void QTNTernary(QTNode *in);
  241. extern void QTNBinary(QTNode *in);
  242. extern int QTNodeCompare(QTNode *an, QTNode *bn);
  243. extern QTNode *QTNCopy(QTNode *in);
  244. extern void QTNClearFlags(QTNode *in, uint32 flags);
  245. extern bool QTNEq(QTNode *a, QTNode *b);
  246. extern TSQuerySign makeTSQuerySign(TSQuery a);
  247. extern QTNode *findsubquery(QTNode *root, QTNode *ex, QTNode *subs,
  248. bool *isfind);
  249. /*
  250. * TSQuery GiST support
  251. */
  252. extern Datum gtsquery_compress(PG_FUNCTION_ARGS);
  253. extern Datum gtsquery_decompress(PG_FUNCTION_ARGS);
  254. extern Datum gtsquery_consistent(PG_FUNCTION_ARGS);
  255. extern Datum gtsquery_union(PG_FUNCTION_ARGS);
  256. extern Datum gtsquery_same(PG_FUNCTION_ARGS);
  257. extern Datum gtsquery_penalty(PG_FUNCTION_ARGS);
  258. extern Datum gtsquery_picksplit(PG_FUNCTION_ARGS);
  259. extern Datum gtsquery_consistent_oldsig(PG_FUNCTION_ARGS);
  260. /*
  261. * Parser interface to SQL
  262. */
  263. extern Datum ts_token_type_byid(PG_FUNCTION_ARGS);
  264. extern Datum ts_token_type_byname(PG_FUNCTION_ARGS);
  265. extern Datum ts_parse_byid(PG_FUNCTION_ARGS);
  266. extern Datum ts_parse_byname(PG_FUNCTION_ARGS);
  267. /*
  268. * Default word parser
  269. */
  270. extern Datum prsd_start(PG_FUNCTION_ARGS);
  271. extern Datum prsd_nexttoken(PG_FUNCTION_ARGS);
  272. extern Datum prsd_end(PG_FUNCTION_ARGS);
  273. extern Datum prsd_headline(PG_FUNCTION_ARGS);
  274. extern Datum prsd_lextype(PG_FUNCTION_ARGS);
  275. /*
  276. * Dictionary interface to SQL
  277. */
  278. extern Datum ts_lexize(PG_FUNCTION_ARGS);
  279. /*
  280. * Simple built-in dictionary
  281. */
  282. extern Datum dsimple_init(PG_FUNCTION_ARGS);
  283. extern Datum dsimple_lexize(PG_FUNCTION_ARGS);
  284. /*
  285. * Synonym built-in dictionary
  286. */
  287. extern Datum dsynonym_init(PG_FUNCTION_ARGS);
  288. extern Datum dsynonym_lexize(PG_FUNCTION_ARGS);
  289. /*
  290. * ISpell dictionary
  291. */
  292. extern Datum dispell_init(PG_FUNCTION_ARGS);
  293. extern Datum dispell_lexize(PG_FUNCTION_ARGS);
  294. /*
  295. * Thesaurus
  296. */
  297. extern Datum thesaurus_init(PG_FUNCTION_ARGS);
  298. extern Datum thesaurus_lexize(PG_FUNCTION_ARGS);
  299. /*
  300. * headline
  301. */
  302. extern Datum ts_headline_byid_opt(PG_FUNCTION_ARGS);
  303. extern Datum ts_headline_byid(PG_FUNCTION_ARGS);
  304. extern Datum ts_headline(PG_FUNCTION_ARGS);
  305. extern Datum ts_headline_opt(PG_FUNCTION_ARGS);
  306. /*
  307. * current cfg
  308. */
  309. extern Datum get_current_ts_config(PG_FUNCTION_ARGS);
  310. #endif /* _PG_TS_UTILS_H_ */