MySQL 8.3.0
Source Code Documentation
fts0ast.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2007, 2023, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/fts0ast.h
28 The FTS query parser (AST) abstract syntax tree routines
29
30 Created 2007/03/16/03 Sunny Bains
31 *******************************************************/
32
33#ifndef INNOBASE_FST0AST_H
34#define INNOBASE_FST0AST_H
35
36#include "ha_prototypes.h"
37#include "mem0mem.h"
38
39struct CHARSET_INFO;
40
41/* The type of AST Node */
43 FTS_AST_OPER, /*!< Operator */
44 FTS_AST_NUMB, /*!< Number */
45 FTS_AST_TERM, /*!< Term (or word) */
46 FTS_AST_TEXT, /*!< Text string */
47 FTS_AST_PARSER_PHRASE_LIST, /*!< Phase for plugin parser
48 The difference from text type
49 is that we tokenize text into
50 term list */
51 FTS_AST_LIST, /*!< Expression list */
52 FTS_AST_SUBEXP_LIST /*!< Sub-Expression list */
53};
54
55/* The FTS query operators that we support */
57 FTS_NONE, /*!< No operator */
58
59 FTS_IGNORE, /*!< Ignore rows that contain
60 this word */
61
62 FTS_EXIST, /*!< Include rows that contain
63 this word */
64
65 FTS_NEGATE, /*!< Include rows that contain
66 this word but rank them
67 lower*/
68
69 FTS_INCR_RATING, /*!< Increase the rank for this
70 word*/
71
72 FTS_DECR_RATING, /*!< Decrease the rank for this
73 word*/
74
75 FTS_DISTANCE, /*!< Proximity distance */
76 FTS_IGNORE_SKIP, /*!< Transient node operator
77 signifies that this is a
78 FTS_IGNORE node, and ignored in
79 the first pass of
80 fts_ast_visit() */
81 FTS_EXIST_SKIP /*!< Transient node operator
82 signifies that this ia a
83 FTS_EXIST node, and ignored in
84 the first pass of
85 fts_ast_visit() */
86};
87
88/* Data types used by the FTS parser */
89struct fts_lexer_t;
90struct fts_ast_node_t;
91struct fts_ast_state_t;
92struct fts_ast_string_t;
93
95
96/********************************************************************
97Parse the string using the lexer setup within state.*/
98int fts_parse(
99 /* out: 0 on OK, 1 on error */
100 fts_ast_state_t *state); /*!< in: ast state instance.*/
101
102/********************************************************************
103Create an AST operator node */
105 void *arg, /*!< in: ast state */
106 fts_ast_oper_t oper); /*!< in: ast operator */
107/********************************************************************
108Create an AST term node, makes a copy of ptr */
110 void *arg, /*!< in: ast state */
111 const fts_ast_string_t *ptr); /*!< in: term string */
112/********************************************************************
113Create an AST text node */
115 void *arg, /*!< in: ast state */
116 const fts_ast_string_t *ptr); /*!< in: text string */
117/********************************************************************
118Create an AST expr list node */
120 void *arg, /*!< in: ast state */
121 fts_ast_node_t *expr); /*!< in: ast expr */
122/********************************************************************
123Create a sub-expression list node. This function takes ownership of
124expr and is responsible for deleting it. */
126 /* out: new node */
127 void *arg, /*!< in: ast state instance */
128 fts_ast_node_t *expr); /*!< in: ast expr instance */
129/********************************************************************
130Set the wildcard attribute of a term.*/
131extern void fts_ast_term_set_wildcard(
132 fts_ast_node_t *node); /*!< in: term to change */
133/********************************************************************
134Set the proximity attribute of a text node. */
135void fts_ast_text_set_distance(fts_ast_node_t *node, /*!< in/out: text node */
136 ulint distance); /*!< in: the text proximity
137 distance */
138/** Free a fts_ast_node_t instance.
139 @return next node to free */
141 fts_ast_node_t *node); /*!< in: node to free */
142/********************************************************************
143Add a sub-expression to an AST*/
145 fts_ast_node_t *list, /*!< in: list node instance */
146 fts_ast_node_t *node); /*!< in: (sub) expr to add */
147/********************************************************************
148Print the AST node recursively.*/
149extern void fts_ast_node_print(
150 fts_ast_node_t *node); /*!< in: ast node to print */
151/********************************************************************
152Free node and expr allocations.*/
153extern void fts_ast_state_free(fts_ast_state_t *state); /*!< in: state instance
154 to free */
155/** Check only union operation involved in the node
156@param[in] node ast node to check
157@return true if the node contains only union else false. */
159
160/** Traverse the AST - in-order traversal.
161 @return DB_SUCCESS if all went well */
162[[nodiscard]] dberr_t fts_ast_visit(
163 fts_ast_oper_t oper, /*!< in: FTS operator */
164 fts_ast_node_t *node, /*!< in: instance to traverse*/
165 fts_ast_callback visitor, /*!< in: callback */
166 void *arg, /*!< in: callback arg */
167 bool *has_ignore); /*!< out: whether we encounter
168 and ignored processing an
169 operator, currently we only
170 ignore FTS_IGNORE operator */
171/********************************************************************
172Create a lex instance.*/
173[[nodiscard]] fts_lexer_t *fts_lexer_create(
174 bool boolean_mode, /*!< in: query type */
175 const byte *query, /*!< in: query string */
176 ulint query_len) /*!< in: query string len */
177 MY_ATTRIBUTE((malloc));
178/********************************************************************
179Free an fts_lexer_t instance.*/
180void fts_lexer_free(fts_lexer_t *fts_lexer); /*!< in: lexer instance to
181 free */
182
183/**
184Create an ast string object, with NUL-terminator, so the string
185has one more byte than len
186@param[in] str pointer to string
187@param[in] len length of the string
188@return ast string with NUL-terminator */
190
191/**
192Free an ast string instance
193@param[in,out] ast_str string to free */
195
196/**
197Translate ast string of type FTS_AST_NUMB to unsigned long by strtoul
198@param[in] ast_str string to translate
199@param[in] base the base
200@return translated number */
201ulint fts_ast_string_to_ul(const fts_ast_string_t *ast_str, int base);
202
203/* String of length len.
204We always store the string of length len with a terminating '\0',
205regardless of there is any 0x00 in the string itself */
207 /*!< Pointer to string. */
208 byte *str;
209
210 /*!< Length of the string. */
212};
213
214/* Query term type */
216 fts_ast_string_t *ptr; /*!< Pointer to term string.*/
217 bool wildcard; /*!< true if wild card set.*/
218};
219
220/* Query text type */
222 fts_ast_string_t *ptr; /*!< Pointer to text string.*/
223 ulint distance; /*!< > 0 if proximity distance
224 set */
225};
226
227/* The list of nodes in an expr list */
229 fts_ast_node_t *head; /*!< Children list head */
230 fts_ast_node_t *tail; /*!< Children list tail */
231};
232
233/* FTS AST node to store the term, text, operator and sub-expressions.*/
235 fts_ast_type_t type; /*!< The type of node */
236 fts_ast_text_t text; /*!< Text node */
237 fts_ast_term_t term; /*!< Term node */
238 fts_ast_oper_t oper; /*!< Operator value */
239 fts_ast_list_t list; /*!< Expression list */
240 fts_ast_node_t *next; /*!< Link for expr list */
241 fts_ast_node_t *next_alloc; /*!< For tracking allocations */
242 bool visited; /*!< whether this node is
243 already processed */
245 /* Used by plugin parser */
246 fts_ast_node_t *up_node; /*!< Direct up node */
247 bool go_up; /*!< Flag if go one level up */
248};
249
250/* To track state during parsing */
252 mem_heap_t *heap; /*!< Heap to use for alloc */
253 fts_ast_node_t *root; /*!< If all goes OK, then this
254 will point to the root.*/
255
256 fts_ast_list_t list; /*!< List of nodes allocated */
257
258 fts_lexer_t *lexer; /*!< Lexer callback + arg */
259 CHARSET_INFO *charset; /*!< charset used for
260 tokenization */
261 /* Used by plugin parser */
262 fts_ast_node_t *cur_node; /*!< Current node into which
263 we add new node */
264 int depth; /*!< Depth of parsing state */
265};
266
267/** Create an AST term node, makes a copy of ptr for plugin parser
268 @return node */
270 /*==========i=====================*/
271 void *arg, /*!< in: ast state */
272 const char *ptr, /*!< in: term string */
273 const ulint len); /*!< in: term string length */
274
275/** Create an AST phrase list node for plugin parser
276 @return node */
278 void *arg); /*!< in: ast state */
279
280#ifdef UNIV_DEBUG
282#endif /* UNIV_DEBUG */
283
284#endif /* INNOBASE_FSTS0AST_H */
dberr_t
Definition: db0err.h:38
fts_ast_oper_t
Definition: fts0ast.h:56
@ FTS_IGNORE_SKIP
Transient node operator signifies that this is a FTS_IGNORE node, and ignored in the first pass of ft...
Definition: fts0ast.h:76
@ FTS_EXIST_SKIP
Transient node operator signifies that this ia a FTS_EXIST node, and ignored in the first pass of fts...
Definition: fts0ast.h:81
@ FTS_DISTANCE
Proximity distance.
Definition: fts0ast.h:75
@ FTS_INCR_RATING
Increase the rank for this word.
Definition: fts0ast.h:69
@ FTS_DECR_RATING
Decrease the rank for this word.
Definition: fts0ast.h:72
@ FTS_EXIST
Include rows that contain this word.
Definition: fts0ast.h:62
@ FTS_NONE
No operator.
Definition: fts0ast.h:57
@ FTS_IGNORE
Ignore rows that contain this word.
Definition: fts0ast.h:59
@ FTS_NEGATE
Include rows that contain this word but rank them lower.
Definition: fts0ast.h:65
void fts_ast_string_free(fts_ast_string_t *ast_str)
Free an ast string instance.
Definition: fts0ast.cc:676
fts_lexer_t * fts_lexer_create(bool boolean_mode, const byte *query, ulint query_len)
Definition: fts0pars.cc:1923
void fts_ast_text_set_distance(fts_ast_node_t *node, ulint distance)
in: the text proximity distance
Definition: fts0ast.cc:381
fts_ast_node_t * fts_ast_add_node(fts_ast_node_t *list, fts_ast_node_t *node)
in: (sub) expr to add
Definition: fts0ast.cc:334
void fts_lexer_free(fts_lexer_t *fts_lexer)
in: lexer instance to free
Definition: fts0pars.cc:1953
bool fts_ast_node_check_union(fts_ast_node_t *node)
Check only union operation involved in the node.
Definition: fts0ast.cc:492
fts_ast_node_t * fts_ast_create_node_term_for_parser(void *arg, const char *ptr, const ulint len)
Create an AST term node, makes a copy of ptr for plugin parser.
Definition: fts0ast.cc:161
fts_ast_node_t * fts_ast_create_node_subexp_list(void *arg, fts_ast_node_t *expr)
in: ast expr instance
Definition: fts0ast.cc:261
const char * fts_ast_node_type_get(fts_ast_type_t type)
Definition: fts0ast.cc:693
dberr_t(* fts_ast_callback)(fts_ast_oper_t, fts_ast_node_t *, void *)
Definition: fts0ast.h:94
int fts_parse(fts_ast_state_t *state)
in: ast state instance.
Definition: fts0pars.cc:1984
fts_ast_node_t * fts_ast_free_node(fts_ast_node_t *node)
Free a fts_ast_node_t instance.
Definition: fts0ast.cc:289
ulint fts_ast_string_to_ul(const fts_ast_string_t *ast_str, int base)
Translate ast string of type FTS_AST_NUMB to unsigned long by strtoul.
Definition: fts0ast.cc:688
fts_ast_node_t * fts_ast_create_node_phrase_list(void *arg)
Create an AST phrase list node for plugin parser.
Definition: fts0ast.cc:227
fts_ast_node_t * fts_ast_create_node_list(void *arg, fts_ast_node_t *expr)
in: ast expr
Definition: fts0ast.cc:244
fts_ast_string_t * fts_ast_string_create(const byte *str, ulint len)
Create an ast string object, with NUL-terminator, so the string has one more byte than len.
Definition: fts0ast.cc:655
fts_ast_node_t * fts_ast_create_node_term(void *arg, const fts_ast_string_t *ptr)
in: term string
Definition: fts0ast.cc:97
fts_ast_type_t
Definition: fts0ast.h:42
@ FTS_AST_NUMB
Number.
Definition: fts0ast.h:44
@ FTS_AST_TERM
Term (or word)
Definition: fts0ast.h:45
@ FTS_AST_PARSER_PHRASE_LIST
Phase for plugin parser The difference from text type is that we tokenize text into term list.
Definition: fts0ast.h:47
@ FTS_AST_OPER
Operator.
Definition: fts0ast.h:43
@ FTS_AST_SUBEXP_LIST
Sub-Expression list.
Definition: fts0ast.h:52
@ FTS_AST_TEXT
Text string.
Definition: fts0ast.h:46
@ FTS_AST_LIST
Expression list.
Definition: fts0ast.h:51
void fts_ast_state_free(fts_ast_state_t *state)
in: state instance to free
Definition: fts0ast.cc:396
dberr_t fts_ast_visit(fts_ast_oper_t oper, fts_ast_node_t *node, fts_ast_callback visitor, void *arg, bool *has_ignore)
Traverse the AST - in-order traversal.
Definition: fts0ast.cc:519
void fts_ast_term_set_wildcard(fts_ast_node_t *node)
in: term to change
Definition: fts0ast.cc:361
fts_ast_node_t * fts_ast_create_node_oper(void *arg, fts_ast_oper_t oper)
in: ast operator
Definition: fts0ast.cc:80
fts_ast_node_t * fts_ast_create_node_text(void *arg, const fts_ast_string_t *ptr)
in: text string
Definition: fts0ast.cc:190
void fts_ast_node_print(fts_ast_node_t *node)
in: ast node to print
Definition: fts0ast.cc:484
int fts_lexer(YYSTYPE *, fts_lexer_t *)
Definition: fts0pars.cc:1969
Prototypes for global functions in ha_innodb.cc that are called by InnoDB C code.
#define malloc(A)
Definition: lexyy.cc:914
The memory management.
static char * query
Definition: myisam_ftdump.cc:46
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1065
bool distance(const dd::Spatial_reference_system *srs, const Geometry *g1, const Geometry *g2, double *distance, bool *is_null) noexcept
Computes the distance between two geometries.
Definition: distance.cc:39
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2877
required string type
Definition: replication_group_member_actions.proto:33
Definition: m_ctype.h:422
Definition: fts0ast.h:228
fts_ast_node_t * tail
Children list tail.
Definition: fts0ast.h:230
fts_ast_node_t * head
Children list head.
Definition: fts0ast.h:229
Definition: fts0ast.h:234
fts_ast_oper_t oper
Operator value.
Definition: fts0ast.h:238
fts_ast_type_t type
The type of node.
Definition: fts0ast.h:235
fts_ast_node_t * next
Link for expr list.
Definition: fts0ast.h:240
bool visited
whether this node is already processed
Definition: fts0ast.h:242
bool go_up
Flag if go one level up.
Definition: fts0ast.h:247
fts_ast_list_t list
Expression list.
Definition: fts0ast.h:239
fts_ast_term_t term
Term node.
Definition: fts0ast.h:237
fts_ast_text_t text
Text node.
Definition: fts0ast.h:236
fts_ast_node_t * up_node
Direct up node.
Definition: fts0ast.h:246
fts_ast_node_t * next_alloc
For tracking allocations.
Definition: fts0ast.h:241
trx_t * trx
Definition: fts0ast.h:244
Definition: fts0ast.h:251
fts_ast_list_t list
List of nodes allocated.
Definition: fts0ast.h:256
fts_ast_node_t * root
If all goes OK, then this will point to the root.
Definition: fts0ast.h:253
fts_ast_node_t * cur_node
Current node into which we add new node.
Definition: fts0ast.h:262
int depth
Depth of parsing state.
Definition: fts0ast.h:264
CHARSET_INFO * charset
charset used for tokenization
Definition: fts0ast.h:259
mem_heap_t * heap
Heap to use for alloc.
Definition: fts0ast.h:252
fts_lexer_t * lexer
Lexer callback + arg.
Definition: fts0ast.h:258
Definition: fts0ast.h:206
byte * str
< Pointer to string.
Definition: fts0ast.h:208
ulint len
Definition: fts0ast.h:211
Definition: fts0ast.h:215
bool wildcard
true if wild card set.
Definition: fts0ast.h:217
fts_ast_string_t * ptr
Pointer to term string.
Definition: fts0ast.h:216
Definition: fts0ast.h:221
fts_ast_string_t * ptr
Pointer to text string.
Definition: fts0ast.h:222
ulint distance
> 0 if proximity distance set
Definition: fts0ast.h:223
Definition: fts0pars.cc:109
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:301
Definition: trx0trx.h:683
unsigned long int ulint
Definition: univ.i:405