MySQL 9.3.0
Source Code Documentation
table_function.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef TABLE_FUNCTION_INCLUDED
25#define TABLE_FUNCTION_INCLUDED
26
27#include <assert.h>
28#include <sys/types.h>
29#include <array> // std::array
30
31#include "my_inttypes.h"
32#include "my_table_map.h"
33#include "sql-common/json_dom.h" // Json_wrapper
34#include "sql-common/json_path.h" // Json_path
35#include "sql/create_field.h"
36#include "sql/enum_query_type.h"
37#include "sql/mem_root_array.h"
38#include "sql/psi_memory_key.h" // key_memory_JSON
39#include "sql/sql_const.h" // Item_processor, enum_walk
40#include "sql/sql_list.h" // List
41#include "sql/table.h" // TABLE
42
43class Field;
44class Item;
45class String;
47class THD;
48
49/**
50 Class representing a table function.
51*/
52
54 protected:
55 /// Table function's result table
57 /// Whether the table function was already initialized
58 bool inited;
59
60 public:
61 explicit Table_function() : table(nullptr), inited(false) {}
62
63 virtual ~Table_function() = default;
64 /**
65 Create, but not instantiate the result table
66
67 @param thd thread handler
68 @param options options to create table
69 @param table_alias table's alias
70
71 @returns
72 true on error
73 false on success
74 */
76 const char *table_alias);
77 /**
78 Write current record to the result table and handle overflow to disk
79
80 @returns
81 true on error
82 false on success
83 */
84 bool write_row();
85
86 /**
87 Returns a field with given index
88
89 @param i field's index
90
91 @returns
92 field with given index
93 */
94 Field *get_field(uint i) {
95 assert(i < table->s->fields);
96 return table->field[i];
97 }
98 /**
99 Delete all rows in the table
100 */
101 void empty_table();
102
103 /**
104 Set the default row
105 */
106 void default_row() {}
107 /**
108 Initialize table function
109 @returns
110 true on error
111 false on success
112 */
113 virtual bool init() = 0;
114 /**
115 Initialize table function after the result table has been created
116 @returns
117 true on error
118 false on success
119 */
120 virtual bool init_args();
121 /**
122 Execute the table function - fill the result table
123 @returns
124 true on error
125 false on success
126 */
127 virtual bool fill_result_table() = 0;
128 /**
129 Returns table function's name
130 */
131 virtual const char *func_name() const = 0;
132 /**
133 Return table_map of tables used by the function
134 */
135 virtual table_map used_tables() const { return 0; }
136 /**
137 Print table function
138
139 @param thd thread handler
140 @param str string to print to
141 @param query_type type of the query
142
143 @returns
144 true on error
145 false on success
146 */
147 virtual bool print(const THD *thd, String *str,
148 enum_query_type query_type) const = 0;
149 /**
150 Clean up table function after one execution
151 */
152 void cleanup() { do_cleanup(); }
153
154 /**
155 Destroy table function object after all executions are complete
156 */
157 void destroy() { this->~Table_function(); }
158
159 virtual bool walk(Item_processor processor, enum_walk walk, uchar *arg) = 0;
160
161 /**
162 Fix after tables have been moved from one query_block level to the parent
163 level, e.g by semijoin conversion.
164
165 @param parent_query_block query_block that tables are moved to.
166 @param removed_query_block query_block that tables are moved away from,
167 child of parent_query_block.
168 */
169 void fix_after_pullout(Query_block *parent_query_block,
170 Query_block *removed_query_block);
171
172 private:
173 /**
174 Get the list of fields to create the result table
175 */
177 /**
178 Initialize table function's arguments
179
180 @returns
181 true on error
182 false on success
183 */
184 virtual bool do_init_args() = 0;
186 virtual void do_cleanup() {}
187 virtual void do_fix_after_pullout(Query_block *parent_query_block,
188 Query_block *removed_query_block) = 0;
189};
190
191/****************************************************************************
192 JSON_TABLE function
193****************************************************************************/
194
195/// Type of columns for JSON_TABLE function
196enum class enum_jt_column {
198 JTC_PATH,
201};
202
203/// Types of ON EMPTY/ON ERROR clauses for JSON_TABLE and JSON_VALUE.
204/// @note uint16 enum base limitation is necessary for YYSTYPE.
206 ERROR,
208 DEFAULT,
210};
211
212/**
213 JT_data_source is used as a data source. It's assigned to each NESTED PATH
214 node.
215*/
216
218 public:
219 /// Vector of found values
221 /// Iterator for vector above
223 /// JSON data to seek columns' paths in
225 /// Current m_rowid, used for ORDINALITY columns
227 /**
228 true <=> NESTED PATH associated with this element is producing records.
229 Used to turn off (set to null) sibling NESTED PATHs, when one of them is
230 used to fill result table.
231 */
233
235
236 void cleanup();
237};
238
239/**
240 Reason for skipping a NESTED PATH
241*/
243 JTS_NONE = 0, // NESTED PATH isn't skipped
244 JTS_EOD, // No more data
245 JTS_SIBLING // Skipped due to other sibling NESTED PATH is running
247
248/// Column description for JSON_TABLE function
250 public:
251 /// Column type
253 /// Type of ON ERROR clause
255 /// Type of ON EMPTY clause
257 /// Default value string for ON EMPTY clause
259 /// Parsed JSON for default value of ON MISSING clause
261 /// Default value string for ON ERROR clause
263 /// Parsed JSON string for ON ERROR clause
265 /// List of nested columns, valid only for NESTED PATH
267 /// Nested path
269 /// parsed nested path
271 /// An element in table function's data source array
273 /**
274 Element in table function's data source array to feed data to child
275 nodes. Valid only for NESTED PATH.
276 */
278 /// Next sibling NESTED PATH
280 /// Previous sibling NESTED PATH
282 /// Index of field in the result table
283 int m_field_idx{-1};
284
285 public:
288 Json_on_response_type on_err, Item *error_default,
289 Json_on_response_type on_miss, Item *missing_default)
290 : m_jtc_type(col_type),
291 m_on_error(on_err),
292 m_on_empty(on_miss),
293 m_default_empty_string(missing_default),
294 m_default_error_string(error_default),
298 m_nested_columns(cols),
301 void cleanup() {}
302
303 /**
304 Fill a json table column
305
306 @details Fills a column with data, according to specification in
307 JSON_TABLE. This function handles all kinds of columns:
308 Ordinality) just saves the counter into the column's field
309 Path) extracts value, saves it to the column's field and handles
310 ON ERROR/ON EMPTY clauses
311 Exists) checks the path existence and saves either 1 or 0 into result
312 field
313 Nested path) matches the path expression against data source. If there're
314 matches, this function sets NESTED PATH's iterator over those
315 matches and resets ordinality counter.
316
317 @param[in] table_function the JSON table function
318 @param[out] skip true <=> it's a NESTED PATH node and its path
319 expression didn't return any matches or a
320 previous sibling NESTED PATH clause still producing
321 records, thus all columns of this NESTED PATH node
322 should be skipped
323
324 @returns
325 false column is filled
326 true an error occurred, execution should be stopped
327 */
328 bool fill_column(Table_function_json *table_function, jt_skip_reason *skip);
329};
330
331#define MAX_NESTED_PATH 16
332
334 /// Array of JSON Data Source for each NESTED PATH clause
335 std::array<JT_data_source, MAX_NESTED_PATH> m_jds;
336 /// List of fields for tmp table creation
338 /// Tree of COLUMN clauses
340 /// Array of all columns - the flattened tree above
342 /// JSON_TABLE's alias, for error reporting
343 const char *m_table_alias;
344
345 /** Whether source data has been parsed. */
347 /// JSON_TABLE's data source expression
349
350 public:
351 Table_function_json(const char *alias, Item *a,
353
355 for (uint i = 0; i < m_all_columns.size(); i++)
357 }
358
359 /**
360 Returns function's name
361 */
362 const char *func_name() const override { return "json_table"; }
363
365
366 Item *get_source() { return source; }
367
368 /**
369 Initialize the table function before creation of result table
370
371 @returns
372 true on error
373 false on success
374 */
375 bool init() override;
376
377 /**
378 Execute table function
379
380 @returns
381 true on error
382 false on success
383 */
384 bool fill_result_table() override;
385
386 /**
387 Return table_map of tables used by function's data source
388 */
389 table_map used_tables() const override;
390
391 /**
392 JSON_TABLE printout
393
394 @param thd thread handler
395 @param str string to print to
396 @param query_type type of query
397
398 @returns
399 true on error
400 false on success
401 */
402 bool print(const THD *thd, String *str,
403 enum_query_type query_type) const override;
404
405 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
406
407 private:
408 /**
409 Fill the result table
410
411 @returns
412 true on error
413 false on success
414 */
415 bool fill_json_table();
416
417 /**
418 Initialize columns and lists for json table
419
420 @details This function does several things:
421 1) sets up list of fields (vt_list) for result table creation
422 2) fills array of all columns (m_all_columns) for execution
423 3) for each column that has default ON EMPTY or ON ERROR clauses, checks
424 the value to be proper json and initializes column appropriately
425 4) for each column that involves path, the path is checked to be correct.
426 The function goes recursively, starting from the top NESTED PATH clause
427 and going in the depth-first way, traverses the tree of columns.
428
429 @param nest_idx index of parent's element in the nesting data array
430 @param parent Parent of the NESTED PATH clause being initialized
431
432 @returns
433 false ok
434 true an error occurred
435 */
436 bool init_json_table_col_lists(uint *nest_idx, Json_table_column *parent);
437 /**
438 A helper function which sets all columns under given NESTED PATH column
439 to nullptr. Used to evaluate sibling NESTED PATHS.
440
441 @param root root NESTED PATH column
442 @param [out] last last column which belongs to the given NESTED PATH
443 */
445
446 /**
447 Return list of fields to create result table from
448 */
450 bool do_init_args() override;
451 void do_cleanup() override;
452 void do_fix_after_pullout(Query_block *parent_query_block,
453 Query_block *removed_query_block) override;
454};
455
456/**
457 Print ON EMPTY or ON ERROR clauses.
458
459 @param thd thread handler
460 @param str the string to print to
461 @param query_type formatting options
462 @param on_empty true for ON EMPTY, false for ON ERROR
463 @param response_type the type of the ON ERROR/ON EMPTY response
464 @param default_string the default string in case of DEFAULT type
465*/
466void print_on_empty_or_error(const THD *thd, String *str,
467 enum_query_type query_type, bool on_empty,
468 Json_on_response_type response_type,
469 const Item *default_string);
470#endif /* TABLE_FUNCTION_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:51
Definition: field.h:573
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
JT_data_source is used as a data source.
Definition: table_function.h:217
Json_wrapper_vector v
Vector of found values.
Definition: table_function.h:220
Json_wrapper_vector::iterator it
Iterator for vector above.
Definition: table_function.h:222
void cleanup()
Definition: table_function.cc:784
bool producing_records
true <=> NESTED PATH associated with this element is producing records.
Definition: table_function.h:232
Json_wrapper jdata
JSON data to seek columns' paths in.
Definition: table_function.h:224
JT_data_source()
Definition: table_function.h:234
uint m_rowid
Current m_rowid, used for ORDINALITY columns.
Definition: table_function.h:226
A JSON path expression.
Definition: json_path.h:350
Column description for JSON_TABLE function.
Definition: table_function.h:249
Json_wrapper m_default_empty_json
Parsed JSON for default value of ON MISSING clause.
Definition: table_function.h:260
Item * m_default_empty_string
Default value string for ON EMPTY clause.
Definition: table_function.h:258
List< Json_table_column > * m_nested_columns
List of nested columns, valid only for NESTED PATH.
Definition: table_function.h:266
Item * m_default_error_string
Default value string for ON ERROR clause.
Definition: table_function.h:262
Item * m_path_string
Nested path.
Definition: table_function.h:268
Json_on_response_type m_on_empty
Type of ON EMPTY clause.
Definition: table_function.h:256
Json_table_column(enum_jt_column col_type, Item *path, Json_on_response_type on_err, Item *error_default, Json_on_response_type on_miss, Item *missing_default)
Definition: table_function.h:287
JT_data_source * m_jds_elt
An element in table function's data source array.
Definition: table_function.h:272
Json_wrapper m_default_error_json
Parsed JSON string for ON ERROR clause.
Definition: table_function.h:264
Json_table_column(Item *path, List< Json_table_column > *cols)
Definition: table_function.h:296
bool fill_column(Table_function_json *table_function, jt_skip_reason *skip)
Fill a json table column.
Definition: table_function.cc:339
~Json_table_column()
Definition: table_function.cc:493
Json_on_response_type m_on_error
Type of ON ERROR clause.
Definition: table_function.h:254
int m_field_idx
Index of field in the result table.
Definition: table_function.h:283
enum_jt_column m_jtc_type
Column type.
Definition: table_function.h:252
Json_table_column * m_prev_nested
Previous sibling NESTED PATH.
Definition: table_function.h:281
Json_path m_path_json
parsed nested path
Definition: table_function.h:270
Json_table_column * m_next_nested
Next sibling NESTED PATH.
Definition: table_function.h:279
void cleanup()
Definition: table_function.h:301
Json_table_column(enum_jt_column type)
Definition: table_function.h:286
JT_data_source * m_child_jds_elt
Element in table function's data source array to feed data to child nodes.
Definition: table_function.h:277
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1150
Definition: sql_list.h:494
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:432
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1174
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table_function.h:333
Table_function_json(const char *alias, Item *a, List< Json_table_column > *cols)
Definition: table_function.cc:107
List< Json_table_column > * m_columns
Tree of COLUMN clauses.
Definition: table_function.h:339
List< Create_field > * get_field_list() override
Return list of fields to create result table from.
Definition: table_function.cc:122
~Table_function_json() override
Definition: table_function.h:354
Item * get_source()
Definition: table_function.h:366
bool walk(Item_processor processor, enum_walk walk, uchar *arg) override
Definition: table_function.cc:116
bool fill_result_table() override
Execute table function.
Definition: table_function.cc:611
std::array< JT_data_source, MAX_NESTED_PATH > m_jds
Array of JSON Data Source for each NESTED PATH clause.
Definition: table_function.h:335
void do_cleanup() override
Definition: table_function.cc:772
List< Json_table_column > m_vt_list
List of fields for tmp table creation.
Definition: table_function.h:337
void do_fix_after_pullout(Query_block *parent_query_block, Query_block *removed_query_block) override
Definition: table_function.cc:779
const char * func_name() const override
Returns function's name.
Definition: table_function.h:362
void set_subtree_to_null(Json_table_column *root, Json_table_column **last)
A helper function which sets all columns under given NESTED PATH column to nullptr.
Definition: table_function.cc:322
bool init() override
Initialize the table function before creation of result table.
Definition: table_function.cc:301
const char * m_table_alias
JSON_TABLE's alias, for error reporting.
Definition: table_function.h:343
bool print(const THD *thd, String *str, enum_query_type query_type) const override
JSON_TABLE printout.
Definition: table_function.cc:759
bool is_source_parsed
Whether source data has been parsed.
Definition: table_function.h:346
Item * source
JSON_TABLE's data source expression.
Definition: table_function.h:348
bool do_init_args() override
Check whether given default values can be saved to fields.
Definition: table_function.cc:240
Mem_root_array< Json_table_column * > m_all_columns
Array of all columns - the flattened tree above.
Definition: table_function.h:341
List< Json_table_column > get_columns()
Definition: table_function.h:364
bool fill_json_table()
Fill the result table.
Definition: table_function.cc:555
table_map used_tables() const override
Return table_map of tables used by function's data source.
Definition: table_function.cc:768
bool init_json_table_col_lists(uint *nest_idx, Json_table_column *parent)
Initialize columns and lists for json table.
Definition: table_function.cc:127
Class representing a table function.
Definition: table_function.h:53
virtual void do_fix_after_pullout(Query_block *parent_query_block, Query_block *removed_query_block)=0
Field * get_field(uint i)
Returns a field with given index.
Definition: table_function.h:94
virtual table_map used_tables() const
Return table_map of tables used by the function.
Definition: table_function.h:135
void destroy()
Destroy table function object after all executions are complete.
Definition: table_function.h:157
bool write_row()
Write current record to the result table and handle overflow to disk.
Definition: table_function.cc:72
virtual const char * func_name() const =0
Returns table function's name.
void empty_table()
Delete all rows in the table.
Definition: table_function.cc:85
bool create_result_table(THD *thd, ulonglong options, const char *table_alias)
Create, but not instantiate the result table.
Definition: table_function.cc:63
virtual ~Table_function()=default
Table_function()
Definition: table_function.h:61
virtual void do_cleanup()
Definition: table_function.h:186
virtual bool init()=0
Initialize table function.
virtual bool print(const THD *thd, String *str, enum_query_type query_type) const =0
Print table function.
virtual bool walk(Item_processor processor, enum_walk walk, uchar *arg)=0
virtual bool do_init_args()=0
Initialize table function's arguments.
virtual List< Create_field > * get_field_list()=0
Get the list of fields to create the result table.
virtual bool init_args()
Initialize table function after the result table has been created.
Definition: table_function.cc:90
void fix_after_pullout(Query_block *parent_query_block, Query_block *removed_query_block)
Fix after tables have been moved from one query_block level to the parent level, e....
Definition: table_function.cc:98
void default_row()
Set the default row.
Definition: table_function.h:106
bool inited
Whether the table function was already initialized.
Definition: table_function.h:58
void cleanup()
Clean up table function after one execution.
Definition: table_function.h:152
TABLE * table
Table function's result table.
Definition: table_function.h:56
virtual bool fill_result_table()=0
Execute the table function - fill the result table.
bool setup_table_function(THD *thd)
Setup a table function to use materialization.
Definition: sql_derived.cc:1004
enum_query_type
Query type constants (usable as bitmap flags).
Definition: enum_query_type.h:31
JSON DOM.
This file contains interface support for the JSON path abstraction.
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
uint16_t uint16
Definition: my_inttypes.h:65
uint64_t table_map
Definition: my_table_map.h:30
static char * path
Definition: mysqldump.cc:150
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1084
static size_t skip(size_t pos_start, size_t match_len)
Definition: uri.cc:79
Definition: options.cc:57
PSI_memory_key key_memory_JSON
Definition: psi_memory_key.cc:53
required string type
Definition: replication_group_member_actions.proto:34
File containing constants that can be used throughout the server.
enum_walk
Enumeration for {Item,Query_block[_UNIT],Table_function}walk.
Definition: sql_const.h:289
bool(Item::*)(unsigned char *) Item_processor
Processor type for {Item,Query_block[_UNIT],Table_function}walk.
Definition: sql_const.h:307
Definition: table.h:1425
Field ** field
Definition: table.h:1480
Json_on_response_type
Types of ON EMPTY/ON ERROR clauses for JSON_TABLE and JSON_VALUE.
Definition: table_function.h:205
enum_jt_column
Type of columns for JSON_TABLE function.
Definition: table_function.h:196
jt_skip_reason
Reason for skipping a NESTED PATH.
Definition: table_function.h:242
@ JTS_EOD
Definition: table_function.h:244
@ JTS_SIBLING
Definition: table_function.h:245
@ JTS_NONE
Definition: table_function.h:243
void print_on_empty_or_error(const THD *thd, String *str, enum_query_type query_type, bool on_empty, Json_on_response_type response_type, const Item *default_string)
Print ON EMPTY or ON ERROR clauses.
Definition: table_function.cc:649