| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2013, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file fts/fts0plugin.cc |
| 29 |
Full Text Search plugin support. |
| 30 |
|
| 31 |
Created 2013/06/04 Shaohua Wang |
| 32 |
***********************************************************************/ |
| 33 |
|
| 34 |
#include "fts0plugin.h" |
| 35 |
#include "ft_global.h" |
| 36 |
#include "fts0ast.h" |
| 37 |
#include "fts0tokenize.h" |
| 38 |
#include "mysql/plugin.h" |
| 39 |
|
| 40 |
/** FTS default parser init |
| 41 |
@return 0 */ |
| 42 |
static int fts_default_parser_init( |
| 43 |
MYSQL_FTPARSER_PARAM *param |
| 44 |
[[maybe_unused]]) /*!< in: plugin parser param */ |
| 45 |
{ |
| 46 |
return (0); |
| 47 |
} |
| 48 |
|
| 49 |
/** FTS default parser deinit |
| 50 |
@return 0 */ |
| 51 |
static int fts_default_parser_deinit( |
| 52 |
MYSQL_FTPARSER_PARAM *param |
| 53 |
[[maybe_unused]]) /*!< in: plugin parser param */ |
| 54 |
{ |
| 55 |
return (0); |
| 56 |
} |
| 57 |
|
| 58 |
/** FTS default parser parse from ft_static.c in MYISAM. |
| 59 |
@return 0 if parse successfully, or return non-zero */ |
| 60 |
static int fts_default_parser_parse( |
| 61 |
MYSQL_FTPARSER_PARAM *param) /*!< in: plugin parser param */ |
| 62 |
{ |
| 63 |
return (param->mysql_parse(param, param->doc, param->length)); |
| 64 |
} |
| 65 |
|
| 66 |
/* FTS default parser from ft_static.c in MYISAM. */ |
| 67 |
struct st_mysql_ftparser fts_default_parser = { |
| 68 |
MYSQL_FTPARSER_INTERFACE_VERSION, fts_default_parser_parse, |
| 69 |
fts_default_parser_init, fts_default_parser_deinit}; |
| 70 |
|
| 71 |
/** Get a operator node from token boolean info |
| 72 |
@return node */ |
| 73 |
static fts_ast_node_t *fts_query_get_oper_node( |
| 74 |
MYSQL_FTPARSER_BOOLEAN_INFO *info, /*!< in: token info */ |
| 75 |
fts_ast_state_t *state) /*!< in/out: query parse state*/ |
| 76 |
{ |
| 77 |
fts_ast_node_t *oper_node = nullptr; |
| 78 |
|
| 79 |
if (info->yesno > 0) { |
| 80 |
oper_node = fts_ast_create_node_oper(state, FTS_EXIST); |
| 81 |
} else if (info->yesno < 0) { |
| 82 |
oper_node = fts_ast_create_node_oper(state, FTS_IGNORE); |
| 83 |
} else if (info->weight_adjust > 0) { |
| 84 |
oper_node = fts_ast_create_node_oper(state, FTS_INCR_RATING); |
| 85 |
} else if (info->weight_adjust < 0) { |
| 86 |
oper_node = fts_ast_create_node_oper(state, FTS_DECR_RATING); |
| 87 |
} else if (info->wasign > 0) { |
| 88 |
oper_node = fts_ast_create_node_oper(state, FTS_NEGATE); |
| 89 |
} |
| 90 |
|
| 91 |
return (oper_node); |
| 92 |
} |
| 93 |
|
| 94 |
/** FTS plugin parser 'myql_add_word' callback function for query parse. |
| 95 |
Refer to 'MYSQL_FTPARSER_PARAM' for more detail. |
| 96 |
Note: |
| 97 |
a. Parse logic refers to 'ftb_query_add_word' from ft_boolean_search.c in |
| 98 |
MYISAM; b. Parse node or tree refers to fts0pars.y. |
| 99 |
@return 0 if add successfully, or return non-zero. */ |
| 100 |
static int fts_query_add_word_for_parser( |
| 101 |
MYSQL_FTPARSER_PARAM *param, /*!< in: parser param */ |
| 102 |
char *word, /*!< in: token */ |
| 103 |
int word_len, /*!< in: token length */ |
| 104 |
MYSQL_FTPARSER_BOOLEAN_INFO *info) /*!< in: token info */ |
| 105 |
{ |
| 106 |
fts_ast_state_t *state = static_cast<fts_ast_state_t *>(param->mysql_ftparam); |
| 107 |
fts_ast_node_t *cur_node = state->cur_node; |
| 108 |
fts_ast_node_t *oper_node = nullptr; |
| 109 |
fts_ast_node_t *term_node = nullptr; |
| 110 |
fts_ast_node_t *node = nullptr; |
| 111 |
|
| 112 |
switch (info->type) { |
| 113 |
case FT_TOKEN_STOPWORD: |
| 114 |
/* We only handler stopword in phrase */ |
| 115 |
if (cur_node->type != FTS_AST_PARSER_PHRASE_LIST) { |
| 116 |
break; |
| 117 |
} |
| 118 |
[[fallthrough]]; |
| 119 |
|
| 120 |
case FT_TOKEN_WORD: |
| 121 |
term_node = fts_ast_create_node_term_for_parser(state, word, word_len); |
| 122 |
|
| 123 |
if (info->trunc) { |
| 124 |
fts_ast_term_set_wildcard(term_node); |
| 125 |
} |
| 126 |
|
| 127 |
if (cur_node->type == FTS_AST_PARSER_PHRASE_LIST) { |
| 128 |
/* Ignore operator inside phrase */ |
| 129 |
fts_ast_add_node(cur_node, term_node); |
| 130 |
} else { |
| 131 |
ut_ad(cur_node->type == FTS_AST_LIST || |
| 132 |
cur_node->type == FTS_AST_SUBEXP_LIST); |
| 133 |
oper_node = fts_query_get_oper_node(info, state); |
| 134 |
|
| 135 |
if (oper_node) { |
| 136 |
node = fts_ast_create_node_list(state, oper_node); |
| 137 |
fts_ast_add_node(node, term_node); |
| 138 |
fts_ast_add_node(cur_node, node); |
| 139 |
} else { |
| 140 |
fts_ast_add_node(cur_node, term_node); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
break; |
| 145 |
|
| 146 |
case FT_TOKEN_LEFT_PAREN: |
| 147 |
/* Check parse error */ |
| 148 |
if (cur_node->type != FTS_AST_LIST && |
| 149 |
cur_node->type != FTS_AST_SUBEXP_LIST) { |
| 150 |
return (1); |
| 151 |
} |
| 152 |
|
| 153 |
/* Set operator */ |
| 154 |
oper_node = fts_query_get_oper_node(info, state); |
| 155 |
if (oper_node != nullptr) { |
| 156 |
node = fts_ast_create_node_list(state, oper_node); |
| 157 |
fts_ast_add_node(cur_node, node); |
| 158 |
node->go_up = true; |
| 159 |
node->up_node = cur_node; |
| 160 |
cur_node = node; |
| 161 |
} |
| 162 |
|
| 163 |
if (info->quot) { |
| 164 |
/* Phrase node */ |
| 165 |
node = fts_ast_create_node_phrase_list(state); |
| 166 |
} else { |
| 167 |
/* Subexp list node */ |
| 168 |
node = fts_ast_create_node_subexp_list(state, nullptr); |
| 169 |
} |
| 170 |
|
| 171 |
fts_ast_add_node(cur_node, node); |
| 172 |
|
| 173 |
node->up_node = cur_node; |
| 174 |
state->cur_node = node; |
| 175 |
state->depth += 1; |
| 176 |
|
| 177 |
break; |
| 178 |
|
| 179 |
case FT_TOKEN_RIGHT_PAREN: |
| 180 |
info->quot = nullptr; |
| 181 |
|
| 182 |
if (cur_node->up_node != nullptr) { |
| 183 |
cur_node = cur_node->up_node; |
| 184 |
|
| 185 |
if (cur_node->go_up) { |
| 186 |
ut_a(cur_node->up_node && !(cur_node->up_node->go_up)); |
| 187 |
cur_node = cur_node->up_node; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
state->cur_node = cur_node; |
| 192 |
|
| 193 |
if (state->depth > 0) { |
| 194 |
state->depth--; |
| 195 |
} else { |
| 196 |
/* Parentheses mismatch */ |
| 197 |
return (1); |
| 198 |
} |
| 199 |
|
| 200 |
break; |
| 201 |
|
| 202 |
case FT_TOKEN_EOF: |
| 203 |
default: |
| 204 |
break; |
| 205 |
} |
| 206 |
|
| 207 |
return (0); |
| 208 |
} |
| 209 |
|
| 210 |
/** FTS plugin parser 'myql_parser' callback function for query parse. |
| 211 |
Refer to 'MYSQL_FTPARSER_PARAM' for more detail. |
| 212 |
@return 0 if parse successfully */ |
| 213 |
static int fts_parse_query_internal( |
| 214 |
MYSQL_FTPARSER_PARAM *param, /*!< in: parser param */ |
| 215 |
char *query, /*!< in: query string */ |
| 216 |
int len) /*!< in: query length */ |
| 217 |
{ |
| 218 |
MYSQL_FTPARSER_BOOLEAN_INFO info; |
| 219 |
const CHARSET_INFO *cs = param->cs; |
| 220 |
uchar **start = reinterpret_cast<uchar **>(&query); |
| 221 |
uchar *end = reinterpret_cast<uchar *>(query + len); |
| 222 |
FT_WORD w = {nullptr, 0, 0}; |
| 223 |
|
| 224 |
info.prev = ' '; |
| 225 |
info.quot = nullptr; |
| 226 |
memset(&w, 0, sizeof(w)); |
| 227 |
/* Note: We don't handle simple parser mode here, |
| 228 |
but user supplied plugin parser should handler it. */ |
| 229 |
while (fts_get_word(cs, start, end, &w, &info)) { |
| 230 |
int ret = param->mysql_add_word(param, reinterpret_cast<char *>(w.pos), |
| 231 |
w.len, &info); |
| 232 |
if (ret) { |
| 233 |
return (ret); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
return (0); |
| 238 |
} |
| 239 |
|
| 240 |
/** fts parse query by plugin parser. |
| 241 |
@return 0 if parse successfully, or return non-zero. */ |
| 242 |
int fts_parse_by_parser(bool mode, /*!< in: parse boolean mode */ |
| 243 |
uchar *query_str, /*!< in: query string */ |
| 244 |
ulint query_len, /*!< in: query string length */ |
| 245 |
st_mysql_ftparser *parser, /*!< in: fts plugin parser */ |
| 246 |
fts_ast_state_t *state) /*!< in/out: parser state */ |
| 247 |
{ |
| 248 |
MYSQL_FTPARSER_PARAM param; |
| 249 |
int ret; |
| 250 |
|
| 251 |
ut_ad(parser); |
| 252 |
|
| 253 |
/* Initial parser param */ |
| 254 |
param.mysql_parse = fts_parse_query_internal; |
| 255 |
param.mysql_add_word = fts_query_add_word_for_parser; |
| 256 |
param.mysql_ftparam = static_cast<void *>(state); |
| 257 |
param.cs = state->charset; |
| 258 |
param.doc = reinterpret_cast<char *>(query_str); |
| 259 |
param.length = static_cast<int>(query_len); |
| 260 |
param.flags = 0; |
| 261 |
param.mode = |
| 262 |
mode ? MYSQL_FTPARSER_FULL_BOOLEAN_INFO : MYSQL_FTPARSER_SIMPLE_MODE; |
| 263 |
|
| 264 |
PARSER_INIT(parser, ¶m); |
| 265 |
ret = parser->parse(¶m); |
| 266 |
PARSER_DEINIT(parser, ¶m); |
| 267 |
|
| 268 |
return (ret | state->depth); |
| 269 |
} |