| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 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/fts0sql.cc |
| 29 |
Full Text Search functionality. |
| 30 |
|
| 31 |
Created 2007-03-27 Sunny Bains |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <sys/types.h> |
| 35 |
|
| 36 |
#include "dict0dd.h" |
| 37 |
#include "dict0dict.h" |
| 38 |
#include "fts0priv.h" |
| 39 |
#include "fts0types.h" |
| 40 |
#include "pars0pars.h" |
| 41 |
#include "que0que.h" |
| 42 |
#include "trx0roll.h" |
| 43 |
|
| 44 |
#include <algorithm> |
| 45 |
#include <string> |
| 46 |
#include "current_thd.h" |
| 47 |
|
| 48 |
/** SQL statements for creating the ancillary FTS tables. */ |
| 49 |
|
| 50 |
/** Preamble to all SQL statements. */ |
| 51 |
static const char *fts_sql_begin = "PROCEDURE P() IS\n"; |
| 52 |
|
| 53 |
/** Postamble to non-committing SQL statements. */ |
| 54 |
static const char *fts_sql_end = |
| 55 |
"\n" |
| 56 |
"END;\n"; |
| 57 |
|
| 58 |
/** Get the table id. |
| 59 |
@return number of bytes written */ |
| 60 |
int fts_get_table_id( |
| 61 |
const fts_table_t *fts_table, /*!< in: FTS Auxiliary table */ |
| 62 |
char *table_id) /*!< out: table id, must be at least |
| 63 |
FTS_AUX_MIN_TABLE_ID_LENGTH bytes |
| 64 |
long */ |
| 65 |
{ |
| 66 |
int len; |
| 67 |
|
| 68 |
ut_a(fts_table->table != nullptr); |
| 69 |
|
| 70 |
switch (fts_table->type) { |
| 71 |
case FTS_COMMON_TABLE: |
| 72 |
len = fts_write_object_id(fts_table->table_id, table_id); |
| 73 |
break; |
| 74 |
|
| 75 |
case FTS_INDEX_TABLE: |
| 76 |
|
| 77 |
len = fts_write_object_id(fts_table->table_id, table_id); |
| 78 |
|
| 79 |
table_id[len] = '_'; |
| 80 |
++len; |
| 81 |
table_id += len; |
| 82 |
|
| 83 |
len += fts_write_object_id(fts_table->index_id, table_id); |
| 84 |
break; |
| 85 |
|
| 86 |
default: |
| 87 |
ut_error; |
| 88 |
} |
| 89 |
|
| 90 |
ut_a(len >= 16); |
| 91 |
ut_a(len < FTS_AUX_MIN_TABLE_ID_LENGTH); |
| 92 |
|
| 93 |
return (len); |
| 94 |
} |
| 95 |
|
| 96 |
/** Construct the prefix name of an FTS table. |
| 97 |
@param[in] fts_table Auxiliary FTS table |
| 98 |
@param[in] is_5_7 true if we need 5.7 compatible name |
| 99 |
@return own: table name, must be freed with ut::free() */ |
| 100 |
static char *fts_get_table_name_prefix_low(const fts_table_t *fts_table, |
| 101 |
bool is_5_7) { |
| 102 |
int len; |
| 103 |
const char *slash; |
| 104 |
char *prefix_name; |
| 105 |
int dbname_len = 0; |
| 106 |
int prefix_name_len; |
| 107 |
char table_id[FTS_AUX_MIN_TABLE_ID_LENGTH]; |
| 108 |
|
| 109 |
slash = static_cast<const char *>( |
| 110 |
memchr(fts_table->parent, '/', strlen(fts_table->parent))); |
| 111 |
|
| 112 |
if (slash) { |
| 113 |
/* Print up to and including the separator. */ |
| 114 |
dbname_len = static_cast<int>(slash - fts_table->parent) + 1; |
| 115 |
} |
| 116 |
|
| 117 |
len = fts_get_table_id(fts_table, table_id); |
| 118 |
|
| 119 |
prefix_name_len = dbname_len + 4 + len + 1; |
| 120 |
|
| 121 |
prefix_name = static_cast<char *>( |
| 122 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, prefix_name_len)); |
| 123 |
|
| 124 |
len = sprintf(prefix_name, "%.*s%s%s", dbname_len, fts_table->parent, |
| 125 |
is_5_7 ? FTS_PREFIX_5_7 : FTS_PREFIX, table_id); |
| 126 |
|
| 127 |
ut_a(len > 0); |
| 128 |
ut_a(len == prefix_name_len - 1); |
| 129 |
|
| 130 |
return (prefix_name); |
| 131 |
} |
| 132 |
|
| 133 |
/** Construct the prefix name of an FTS table. |
| 134 |
@return own: table name, must be freed with ut::free() */ |
| 135 |
char *fts_get_table_name_prefix( |
| 136 |
const fts_table_t *fts_table) /*!< in: Auxiliary table type */ |
| 137 |
{ |
| 138 |
return (fts_get_table_name_prefix_low(fts_table, false)); |
| 139 |
} |
| 140 |
|
| 141 |
/** Construct the prefix name of an FTS table in 5.7 compatible name |
| 142 |
@param[in] fts_table Auxiliary FTS table |
| 143 |
@return own: table name, must be freed with ut::free() */ |
| 144 |
char *fts_get_table_name_prefix_5_7(const fts_table_t *fts_table) { |
| 145 |
return (fts_get_table_name_prefix_low(fts_table, true)); |
| 146 |
} |
| 147 |
|
| 148 |
/** Construct the name of an ancillary FTS table for the given table. |
| 149 |
Caller must allocate enough memory(usually size of MAX_FULL_NAME_LEN) |
| 150 |
for param 'table_name' |
| 151 |
@param[in] fts_table FTS Aux table |
| 152 |
@param[in,out] table_name aux table name |
| 153 |
@param[in] is_5_7 true if we need 5.7 compatible name */ |
| 154 |
static void fts_get_table_name_low(const fts_table_t *fts_table, |
| 155 |
char *table_name, bool is_5_7) { |
| 156 |
int len; |
| 157 |
char *prefix_name; |
| 158 |
|
| 159 |
prefix_name = is_5_7 ? fts_get_table_name_prefix_5_7(fts_table) |
| 160 |
: fts_get_table_name_prefix(fts_table); |
| 161 |
|
| 162 |
len = sprintf(table_name, "%s_%s", prefix_name, fts_table->suffix); |
| 163 |
|
| 164 |
ut_a(len > 0); |
| 165 |
ut_a(strlen(prefix_name) + 1 + strlen(fts_table->suffix) == |
| 166 |
static_cast<uint>(len)); |
| 167 |
|
| 168 |
ut::free(prefix_name); |
| 169 |
} |
| 170 |
|
| 171 |
/** Construct the name of an ancillary FTS table for the given table. |
| 172 |
Caller must allocate enough memory(usually size of MAX_FULL_NAME_LEN) |
| 173 |
for param 'table_name'. */ |
| 174 |
void fts_get_table_name(const fts_table_t *fts_table, |
| 175 |
/*!< in: Auxiliary table type */ |
| 176 |
char *table_name) |
| 177 |
/*!< in/out: aux table name */ |
| 178 |
{ |
| 179 |
fts_get_table_name_low(fts_table, table_name, false); |
| 180 |
} |
| 181 |
|
| 182 |
/** Construct the name of an ancillary FTS table for the given table in |
| 183 |
5.7 compatible format. Caller must allocate enough memory(usually size |
| 184 |
of MAX_FULL_NAME_LEN) for param 'table_name' |
| 185 |
@param[in] fts_table Auxiliary table object |
| 186 |
@param[in,out] table_name aux table name */ |
| 187 |
void fts_get_table_name_5_7(const fts_table_t *fts_table, char *table_name) { |
| 188 |
fts_get_table_name_low(fts_table, table_name, true); |
| 189 |
} |
| 190 |
|
| 191 |
/** Parse an SQL string. |
| 192 |
@return query graph */ |
| 193 |
que_t *fts_parse_sql( |
| 194 |
fts_table_t *fts_table, /*!< in: FTS auxiliarry table info */ |
| 195 |
pars_info_t *info, /*!< in: info struct, or NULL */ |
| 196 |
const char *sql) /*!< in: SQL string to evaluate */ |
| 197 |
{ |
| 198 |
char *str; |
| 199 |
que_t *graph; |
| 200 |
dict_table_t *aux_table = nullptr; |
| 201 |
MDL_ticket *mdl = nullptr; |
| 202 |
THD *thd = current_thd; |
| 203 |
|
| 204 |
str = ut_str3cat(fts_sql_begin, sql, fts_sql_end); |
| 205 |
|
| 206 |
/* To open this table in advance, in case it has to be opened |
| 207 |
in pars_sql where pars_mutex is held. This is because holding |
| 208 |
a mutex to open a table which may access InnoDB is not safe */ |
| 209 |
if (fts_table != nullptr) { |
| 210 |
char table_name[MAX_FULL_NAME_LEN]; |
| 211 |
|
| 212 |
fts_get_table_name(fts_table, table_name); |
| 213 |
|
| 214 |
aux_table = dd_table_open_on_name_in_mem(table_name, false); |
| 215 |
DBUG_EXECUTE_IF( |
| 216 |
"force_evict_fts_aux_table_and_reload", if (aux_table != nullptr) { |
| 217 |
dict_sys_mutex_enter(); |
| 218 |
dd_table_close(aux_table, nullptr, nullptr, true); |
| 219 |
dict_table_remove_from_cache(aux_table); |
| 220 |
dict_sys_mutex_exit(); |
| 221 |
aux_table = nullptr; |
| 222 |
}); |
| 223 |
|
| 224 |
if (aux_table == nullptr) { |
| 225 |
aux_table = dd_table_open_on_name(thd, &mdl, table_name, false, |
| 226 |
DICT_ERR_IGNORE_NONE); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/* The InnoDB SQL parser is not re-entrant. */ |
| 231 |
mutex_enter(&pars_mutex); |
| 232 |
|
| 233 |
graph = pars_sql(info, str); |
| 234 |
ut_a(graph); |
| 235 |
|
| 236 |
mutex_exit(&pars_mutex); |
| 237 |
|
| 238 |
if (aux_table != nullptr) { |
| 239 |
dd_table_close(aux_table, thd, &mdl, false); |
| 240 |
} |
| 241 |
|
| 242 |
ut::free(str); |
| 243 |
|
| 244 |
return (graph); |
| 245 |
} |
| 246 |
|
| 247 |
/** Evaluate an SQL query graph. |
| 248 |
@return DB_SUCCESS or error code */ |
| 249 |
dberr_t fts_eval_sql(trx_t *trx, /*!< in: transaction */ |
| 250 |
que_t *graph) /*!< in: Query graph to evaluate */ |
| 251 |
{ |
| 252 |
graph->trx = trx; |
| 253 |
graph->fork_type = QUE_FORK_MYSQL_INTERFACE; |
| 254 |
|
| 255 |
auto thr = que_fork_start_command(graph); |
| 256 |
ut_a(thr); |
| 257 |
|
| 258 |
que_run_threads(thr); |
| 259 |
|
| 260 |
return (trx->error_state); |
| 261 |
} |
| 262 |
|
| 263 |
/** Construct the column specification part of the SQL string for selecting the |
| 264 |
indexed FTS columns for the given table. Adds the necessary bound |
| 265 |
ids to the given 'info' and returns the SQL string. Examples: |
| 266 |
|
| 267 |
One indexed column named "text": |
| 268 |
|
| 269 |
"$sel0", |
| 270 |
info/ids: sel0 -> "text" |
| 271 |
|
| 272 |
Two indexed columns named "subject" and "content": |
| 273 |
|
| 274 |
"$sel0, $sel1", |
| 275 |
info/ids: sel0 -> "subject", sel1 -> "content", |
| 276 |
@return heap-allocated WHERE string */ |
| 277 |
const char *fts_get_select_columns_str( |
| 278 |
dict_index_t *index, /*!< in: index */ |
| 279 |
pars_info_t *info, /*!< in/out: parser info */ |
| 280 |
mem_heap_t *heap) /*!< in: memory heap */ |
| 281 |
{ |
| 282 |
ulint i; |
| 283 |
const char *str = ""; |
| 284 |
|
| 285 |
for (i = 0; i < index->n_user_defined_cols; i++) { |
| 286 |
char *sel_str; |
| 287 |
|
| 288 |
dict_field_t *field = index->get_field(i); |
| 289 |
|
| 290 |
sel_str = mem_heap_printf(heap, "sel%lu", (ulong)i); |
| 291 |
|
| 292 |
/* Set copy_name to true since it's dynamic. */ |
| 293 |
pars_info_bind_id(info, true, sel_str, field->name); |
| 294 |
|
| 295 |
str = mem_heap_printf(heap, "%s%s$%s", str, (*str) ? ", " : "", sel_str); |
| 296 |
} |
| 297 |
|
| 298 |
return (str); |
| 299 |
} |
| 300 |
|
| 301 |
/** Commit a transaction. |
| 302 |
@return DB_SUCCESS or error code */ |
| 303 |
dberr_t fts_sql_commit(trx_t *trx) /*!< in: transaction */ |
| 304 |
{ |
| 305 |
dberr_t error; |
| 306 |
|
| 307 |
error = trx_commit_for_mysql(trx); |
| 308 |
|
| 309 |
/* Commit should always succeed */ |
| 310 |
ut_a(error == DB_SUCCESS); |
| 311 |
|
| 312 |
return (DB_SUCCESS); |
| 313 |
} |
| 314 |
|
| 315 |
/** Rollback a transaction. |
| 316 |
@return DB_SUCCESS or error code */ |
| 317 |
dberr_t fts_sql_rollback(trx_t *trx) /*!< in: transaction */ |
| 318 |
{ |
| 319 |
return (trx_rollback_to_savepoint(trx, nullptr)); |
| 320 |
} |