MySQL 8.2.0
Source Code Documentation
table.h
Go to the documentation of this file.
1#ifndef TABLE_INCLUDED
2#define TABLE_INCLUDED
3
4/* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <assert.h>
27#include <string.h>
28#include <sys/types.h>
29#include <string>
30
31#include "field_types.h"
32#include "lex_string.h"
33#include "map_helpers.h"
34#include "mem_root_deque.h"
35#include "my_alloc.h"
36#include "my_base.h"
37#include "my_bitmap.h"
38#include "my_compiler.h"
39#include "mysql/binlog/event/table_id.h" // Table_id
40
41#include "my_inttypes.h"
42#include "my_sys.h"
43#include "my_table_map.h"
47#include "sql/dd/types/foreign_key.h" // dd::Foreign_key::enum_rule
48#include "sql/enum_query_type.h" // enum_query_type
49#include "sql/key.h"
50#include "sql/key_spec.h"
51#include "sql/mdl.h" // MDL_wait_for_subgraph
52#include "sql/mem_root_array.h"
53#include "sql/mysqld_cs.h"
54#include "sql/opt_costmodel.h" // Cost_model_table
55#include "sql/partition_info.h"
56#include "sql/record_buffer.h" // Record_buffer
57#include "sql/sql_bitmap.h" // Bitmap
58#include "sql/sql_const.h"
59#include "sql/sql_list.h"
60#include "sql/sql_plist.h"
61#include "sql/sql_plugin_ref.h"
62#include "sql/sql_sort.h" // Sort_result
63#include "thr_lock.h"
64#include "typelib.h"
65
66class Field;
67class Field_longlong;
68
69namespace histograms {
70class Histogram;
71} // namespace histograms
72
75class COND_EQUAL;
76class Field_json;
77/* Structs that defines the TABLE */
78class File_parser;
79class Value_generator;
80class GRANT_TABLE;
81class Handler_share;
82class Index_hint;
83class Item;
84class Item_field;
87class Json_wrapper;
88class Opt_hints_qb;
89class Opt_hints_table;
91class Query_block;
94class SortingIterator;
95class String;
96class THD;
100class Table_ref;
102class Temp_table_param;
103class handler;
104class partition_info;
105enum enum_stats_auto_recalc : int;
106enum Value_generator_source : short;
107enum row_type : int;
108struct AccessPath;
109struct HA_CREATE_INFO;
110struct LEX;
111struct NESTED_JOIN;
113struct TABLE;
114struct TABLE_SHARE;
115struct handlerton;
117using plan_idx = int;
118
119namespace dd {
120class Table;
121class View;
122
123enum class enum_table_type;
124} // namespace dd
126
130
134
136
138
139enum class enum_json_diff_operation;
140
142
144
146
147#define store_record(A, B) \
148 memcpy((A)->B, (A)->record[0], (size_t)(A)->s->reclength)
149#define restore_record(A, B) \
150 memcpy((A)->record[0], (A)->B, (size_t)(A)->s->reclength)
151#define cmp_record(A, B) \
152 memcmp((A)->record[0], (A)->B, (size_t)(A)->s->reclength)
153
154#define tmp_file_prefix "#sql" /**< Prefix for tmp tables */
155#define tmp_file_prefix_length 4
156#define TMP_TABLE_KEY_EXTRA 8
157#define PLACEHOLDER_TABLE_ROW_ESTIMATE 2
158
159/**
160 Enumerate possible types of a table from re-execution
161 standpoint.
162 Table_ref class has a member of this type.
163 At prepared statement prepare, this member is assigned a value
164 as of the current state of the database. Before (re-)execution
165 of a prepared statement, we check that the value recorded at
166 prepare matches the type of the object we obtained from the
167 table definition cache.
168
169 @sa check_and_update_table_version()
170 @sa Execute_observer
171 @sa Prepared_statement::reprepare()
172*/
173
175 /** Initial value set by the parser */
182
183/**
184 Enumerate possible status of a identifier name while determining
185 its validity
186*/
188
189/*************************************************************************/
190
191/**
192 Object_creation_ctx -- interface for creation context of database objects
193 (views, stored routines, events, triggers). Creation context -- is a set
194 of attributes, that should be fixed at the creation time and then be used
195 each time the object is parsed or executed.
196*/
197
199 public:
201
202 void restore_env(THD *thd, Object_creation_ctx *backup_ctx);
203
204 protected:
206 virtual Object_creation_ctx *create_backup_ctx(THD *thd) const = 0;
207 virtual void delete_backup_ctx() = 0;
208
209 virtual void change_env(THD *thd) const = 0;
210
211 public:
212 virtual ~Object_creation_ctx() = default;
213};
214
215/*************************************************************************/
216
217/**
218 Default_object_creation_ctx -- default implementation of
219 Object_creation_ctx.
220*/
221
223 public:
225
227
228 protected:
230
232 const CHARSET_INFO *connection_cl);
233
234 protected:
235 Object_creation_ctx *create_backup_ctx(THD *thd) const override;
236 void delete_backup_ctx() override;
237
238 void change_env(THD *thd) const override;
239
240 protected:
241 /**
242 client_cs stores the value of character_set_client session variable.
243 The only character set attribute is used.
244
245 Client character set is included into query context, because we save
246 query in the original character set, which is client character set. So,
247 in order to parse the query properly we have to switch client character
248 set on parsing.
249 */
251
252 /**
253 connection_cl stores the value of collation_connection session
254 variable. Both character set and collation attributes are used.
255
256 Connection collation is included into query context, because it defines
257 the character set and collation of text literals in internal
258 representation of query (item-objects).
259 */
261};
262
263/**
264 View_creation_ctx -- creation context of view objects.
265*/
266
268 public:
269 static View_creation_ctx *create(THD *thd);
270
271 static View_creation_ctx *create(THD *thd, Table_ref *view);
272
273 private:
275};
276
277/*************************************************************************/
278
279/** Order clause list element */
280
282
283struct ORDER {
284 /// @returns true if item pointer is same as original
285 bool is_item_original() const { return item[0] == item_initial; }
286
287 ORDER *next{nullptr};
288
289 /**
290 The initial ordering expression. Usually substituted during resolving
291 and must not be used during optimization and execution.
292 */
293 Item *item_initial{nullptr}; /* Storage for initial item */
294
295 public:
296 /**
297 Points at the item in the select fields. Note that this means that
298 after resolving, it points into a slice (see JOIN::ref_items),
299 even though the item is not of type Item_ref!
300 */
302
304
306 ORDER_NOT_RELEVANT}; /* Requested direction of ordering */
307 bool in_field_list{false}; /* true if in select field list */
308 /**
309 Tells whether this ORDER element was referenced with an alias or with an
310 expression in the query, and what the alias was:
311 SELECT a AS foo GROUP BY foo: "foo".
312 SELECT a AS foo GROUP BY a: nullptr.
313 */
314 const char *used_alias{nullptr};
315 /**
316 When GROUP BY is implemented with a temporary table (i.e. the table takes
317 care to store only unique group rows, table->group != nullptr), each GROUP
318 BY expression is stored in a column of the table, which is
319 'field_in_tmp_table'.
320 Such field may point into table->record[0] (if we only use it to get its
321 value from a tmp table's row), or into 'buff' (if we use it to do index
322 lookup into the tmp table).
323 */
325 char *buff{nullptr}; /* If tmp-table group */
327 bool is_explicit{false}; /* Whether ASC/DESC is explicitly specified */
328};
329
330/**
331 State information for internal tables grants.
332 This structure is part of the Table_ref, and is updated
333 during the ACL check process.
334 @sa GRANT_INFO
335*/
337 /** True if the internal lookup by schema name was done. */
339 /** Cached internal schema access. */
341 /** True if the internal lookup by table name was done. */
343 /** Cached internal table access. */
345};
346
347/**
348 @brief The current state of the privilege checking process for the current
349 user, SQL statement and SQL object.
350
351 @details The privilege checking process is divided into phases depending on
352 the level of the privilege to be checked and the type of object to be
353 accessed. Due to the mentioned scattering of privilege checking
354 functionality, it is necessary to keep track of the state of the process.
355
356 A GRANT_INFO also serves as a cache of the privilege hash tables. Relevant
357 members are grant_table and version.
358 */
360 GRANT_INFO();
361 /**
362 @brief A copy of the privilege information regarding the current host,
363 database, object and user.
364
365 @details The version of this copy is found in GRANT_INFO::version.
366 */
368 /**
369 @brief Used for cache invalidation when caching privilege information.
370
371 @details The privilege information is stored on disk, with dedicated
372 caches residing in memory: table-level and column-level privileges,
373 respectively, have their own dedicated caches.
374
375 The GRANT_INFO works as a level 1 cache with this member updated to the
376 current value of the global variable @c grant_version (@c static variable
377 in sql_acl.cc). It is updated Whenever the GRANT_INFO is refreshed from
378 the level 2 cache. The level 2 cache is the @c column_priv_hash structure
379 (@c static variable in sql_acl.cc)
380
381 @see grant_version
382 */
383 uint version{0};
384 /**
385 @brief The set of privileges that the current user has fulfilled for a
386 certain host, database, and object.
387
388 @details This field is continually updated throughout the access checking
389 process. In each step the "wanted privilege" is checked against the
390 fulfilled privileges. When/if the intersection of these sets is empty,
391 access is granted.
392
393 The set is implemented as a bitmap, with the bits defined in sql_acl.h.
394 */
395 ulong privilege{0};
396 /** The grant state for internal tables. */
398};
399
407
408/**
409 Category of table found in the table share.
410*/
412 /**
413 Unknown value.
414 */
416
417 /**
418 Temporary table.
419 The table is visible only in the session.
420 Therefore,
421 - FLUSH TABLES WITH READ LOCK
422 - SET GLOBAL READ_ONLY = ON
423 do not apply to this table.
424 Note that LOCK TABLE t FOR READ/WRITE
425 can be used on temporary tables.
426 Temporary tables are not part of the table cache.
427
428 2016-06-14 Contrary to what's written in these comments, the truth is:
429 - tables created by CREATE TEMPORARY TABLE have TABLE_CATEGORY_USER
430 - tables created by create_tmp_table() (internal ones) have
431 TABLE_CATEGORY_TEMPORARY.
432 ha_innodb.cc relies on this observation (so: grep it). If you clean this
433 up, you may also want to look at 'no_tmp_table'; its enum values' meanings
434 have degraded over time: INTERNAL_TMP_TABLE is not used for some internal
435 tmp tables (derived tables). Unification of both enums would be
436 great. Whatever the result, we need to be able to distinguish the two
437 types of temporary tables above, as usage patterns are more restricted for
438 the second type, and allow more optimizations.
439 */
441
442 /**
443 User table.
444 These tables do honor:
445 - LOCK TABLE t FOR READ/WRITE
446 - FLUSH TABLES WITH READ LOCK
447 - SET GLOBAL READ_ONLY = ON
448 User tables are cached in the table cache.
449 */
451
452 /**
453 System table, maintained by the server.
454 These tables do honor:
455 - LOCK TABLE t FOR READ/WRITE
456 - FLUSH TABLES WITH READ LOCK
457 - SET GLOBAL READ_ONLY = ON
458 Typically, writes to system tables are performed by
459 the server implementation, not explicitly be a user.
460 System tables are cached in the table cache.
461 */
463
464 /**
465 Information schema tables.
466 These tables are an interface provided by the system
467 to inspect the system metadata.
468 These tables do *not* honor:
469 - LOCK TABLE t FOR READ/WRITE
470 - FLUSH TABLES WITH READ LOCK
471 - SET GLOBAL READ_ONLY = ON
472 as there is no point in locking explicitly
473 an INFORMATION_SCHEMA table.
474 Nothing is directly written to information schema tables.
475 Note that this value is not used currently,
476 since information schema tables are not shared,
477 but implemented as session specific temporary tables.
478 */
479 /*
480 TODO: Fixing the performance issues of I_S will lead
481 to I_S tables in the table cache, which should use
482 this table type.
483 */
485
486 /**
487 Log tables.
488 These tables are an interface provided by the system
489 to inspect the system logs.
490 These tables do *not* honor:
491 - LOCK TABLE t FOR READ/WRITE
492 - FLUSH TABLES WITH READ LOCK
493 - SET GLOBAL READ_ONLY = ON
494 as there is no point in locking explicitly
495 a LOG table.
496 An example of LOG tables are:
497 - mysql.slow_log
498 - mysql.general_log,
499 which *are* updated even when there is either
500 a GLOBAL READ LOCK or a GLOBAL READ_ONLY in effect.
501 User queries do not write directly to these tables
502 (there are exceptions for log tables).
503 The server implementation perform writes.
504 Log tables are cached in the table cache.
505 */
507
508 /**
509 Performance schema tables.
510 These tables are an interface provided by the system
511 to inspect the system performance data.
512 These tables do *not* honor:
513 - LOCK TABLE t FOR READ/WRITE
514 - FLUSH TABLES WITH READ LOCK
515 - SET GLOBAL READ_ONLY = ON
516 as there is no point in locking explicitly
517 a PERFORMANCE_SCHEMA table.
518 An example of PERFORMANCE_SCHEMA tables are:
519 - performance_schema.*
520 which *are* updated (but not using the handler interface)
521 even when there is either
522 a GLOBAL READ LOCK or a GLOBAL READ_ONLY in effect.
523 User queries do not write directly to these tables
524 (there are exceptions for SETUP_* tables).
525 The server implementation perform writes.
526 Performance tables are cached in the table cache.
527 */
529
530 /**
531 Replication Information Tables.
532 These tables are used to store replication information.
533 These tables do *not* honor:
534 - LOCK TABLE t FOR READ/WRITE
535 - FLUSH TABLES WITH READ LOCK
536 - SET GLOBAL READ_ONLY = ON
537 as there is no point in locking explicitly
538 a Replication Information table.
539 An example of replication tables are:
540 - mysql.slave_master_info
541 - mysql.slave_relay_log_info,
542 which *are* updated even when there is either
543 a GLOBAL READ LOCK or a GLOBAL READ_ONLY in effect.
544 User queries do not write directly to these tables.
545 Replication tables are cached in the table cache.
546 */
548
549 /**
550 Gtid Table.
551 The table is used to store gtids.
552 The table does *not* honor:
553 - LOCK TABLE t FOR READ/WRITE
554 - FLUSH TABLES WITH READ LOCK
555 - SET GLOBAL READ_ONLY = ON
556 as there is no point in locking explicitly
557 a Gtid table.
558 An example of gtid_executed table is:
559 - mysql.gtid_executed,
560 which is updated even when there is either
561 a GLOBAL READ LOCK or a GLOBAL READ_ONLY in effect.
562 Gtid table is cached in the table cache.
563 */
565
566 /**
567 A data dictionary table.
568 Table's with this category will skip checking the
569 TABLE_SHARE versions because these table structures
570 are fixed upon server bootstrap.
571 */
573
574 /**
575 A ACL metadata table.
576 For table in this category we will skip row locks when SQL statement
577 reads them.
578 */
582
583extern ulong refresh_version;
584
589};
590
592 uint count;
594};
595
597 protected:
599 virtual void report_error(uint code, const char *fmt, ...) = 0;
600
601 public:
603 virtual ~Table_check_intact() = default;
604
605 /**
606 Checks whether a table is intact. Should be done *just* after the table has
607 been opened.
608
609 @param[in] thd Thread handle
610 @param[in] table The table to check
611 @param[in] table_def Expected structure of the table (column name
612 and type)
613
614 @retval false OK
615 @retval true There was an error.
616 */
617 bool check(THD *thd, TABLE *table, const TABLE_FIELD_DEF *table_def);
618};
619
620/**
621 Class representing the fact that some thread waits for table
622 share to be flushed. Is used to represent information about
623 such waits in MDL deadlock detector.
624*/
625
630
631 public:
633 uint deadlock_weight_arg)
634 : m_ctx(ctx_arg),
635 m_share(share_arg),
636 m_deadlock_weight(deadlock_weight_arg) {}
637
638 MDL_context *get_ctx() const { return m_ctx; }
639
640 bool accept_visitor(MDL_wait_for_graph_visitor *dvisitor) override;
641
642 uint get_deadlock_weight() const override;
643
644 /**
645 Pointers for participating in the list of waiters for table share.
646 */
649};
650
651typedef I_P_List<
656
660 /**
661 Name of unique key matching FK in parent table, "" if there is no
662 unique key.
663 */
667 /**
668 Arrays with names of referencing columns of the FK.
669 */
672
678
679/**
680 Definition of name for generated keys, owned by TABLE_SHARE
681*/
682struct Key_name {
684};
685
686/**
687 This structure is shared between different table objects. There is one
688 instance of table share per one table in the database.
689*/
690
692 TABLE_SHARE() = default;
693
694 /**
695 Create a new TABLE_SHARE with the given version number.
696 @param version the version of the TABLE_SHARE
697 @param secondary set to true if the TABLE_SHARE represents a table
698 in a secondary storage engine
699 */
700 TABLE_SHARE(unsigned long version, bool secondary)
701 : m_version(version), m_secondary_engine(secondary) {}
702
703 /*
704 Managed collection of refererence-counted snapshots of histograms statistics
705 for the table. TABLE objects acquire/release pointers to histogram
706 statistics from this collection. A new statistics snapshot is inserted when
707 the share is initialized and when histograms are updated/dropped.
708
709 For temporary tables m_histograms should be nullptr since we do not support
710 histograms on temporary tables.
711 */
713
714 /** Category of this table. */
716
718 /**
719 Used to allocate new handler for internal temporary table when the
720 size limitation of the primary storage engine is exceeded.
721 */
723
724 TYPELIB keynames; /* Pointers to keynames */
725 TYPELIB *intervals{nullptr}; /* pointer to interval info */
726 mysql_mutex_t LOCK_ha_data; /* To protect access to ha_data */
727 TABLE_SHARE *next{nullptr}, **prev{nullptr}; /* Link to unused shares */
728 /**
729 Array of table_cache_instances pointers to elements of table caches
730 respresenting this table in each of Table_cache instances.
731 Allocated along with the share itself in alloc_table_share().
732 Each element of the array is protected by Table_cache::m_lock in the
733 corresponding Table_cache. False sharing should not be a problem in
734 this case as elements of this array are supposed to be updated rarely.
735 */
737
738 /* The following is copied to each TABLE on OPEN */
739 Field **field{nullptr};
741 KEY *key_info{nullptr}; /* data of keys defined for the table */
742 uint *blob_field{nullptr}; /* Index to blobs in Field array */
743
744 uchar *default_values{nullptr}; /* row with default values */
745 LEX_STRING comment{nullptr, 0}; /* Comment about table */
746 LEX_STRING compress{nullptr, 0}; /* Compression algorithm */
747 LEX_STRING encrypt_type{nullptr, 0}; /* encryption algorithm */
748
749 /** Secondary storage engine. */
751 /** Secondary engine load status */
752 bool secondary_load{false};
753
755 nullptr}; /* Default charset of string fields */
756
758 /*
759 Key which is used for looking-up table in table cache and in the list
760 of thread's temporary tables. Has the form of:
761 "database_name\0table_name\0" + optional part for temporary tables.
762
763 Note that all three 'table_cache_key', 'db' and 'table_name' members
764 must be set (and be non-zero) for tables in table cache. They also
765 should correspond to each other.
766 To ensure this one can use set_table_cache() methods.
767 */
769 LEX_CSTRING db{nullptr, 0}; /* Pointer to db */
770 LEX_CSTRING table_name{nullptr, 0}; /* Table name (for open) */
771 LEX_STRING path{nullptr, 0}; /* Path to .frm file (from datadir) */
772 LEX_CSTRING normalized_path{nullptr, 0}; /* unpack_filename(path) */
774
777
778 /**
779 The set of indexes that are not disabled for this table. I.e. it excludes
780 indexes disabled by `ALTER TABLE ... DISABLE KEYS`, however it does
781 include invisible indexes. The data dictionary populates this bitmap.
782 */
784
785 /// The set of visible and enabled indexes for this table.
788 ha_rows min_rows{0}, max_rows{0}; /* create information */
789 ulong avg_row_length{0}; /* create information */
790 ulong mysql_version{0}; /* 0 if .frm is created before 5.0 */
791 ulong reclength{0}; /* Recordlength */
792 ulong stored_rec_length{0}; /* Stored record length
793 (no generated-only generated fields) */
795
796 plugin_ref db_plugin{nullptr}; /* storage engine plugin */
797 inline handlerton *db_type() const /* table_type for handler */
798 {
799 // assert(db_plugin);
800 return db_plugin ? plugin_data<handlerton *>(db_plugin) : nullptr;
801 }
802 /**
803 Value of ROW_FORMAT option for the table as provided by user.
804 Can be different from the real row format used by the storage
805 engine. ROW_TYPE_DEFAULT value indicates that no explicit
806 ROW_FORMAT was specified for the table. @sa real_row_type.
807 */
808 enum row_type row_type = {}; // Zero-initialized to ROW_TYPE_DEFAULT
809 /** Real row format used for the table by the storage engine. */
810 enum row_type real_row_type = {}; // Zero-initialized to ROW_TYPE_DEFAULT
812
813 /**
814 Only for internal temporary tables.
815 Count of TABLEs (having this TABLE_SHARE) which have a "handler"
816 (table->file!=nullptr) which is open (ha_open() has been called).
817 */
819
820 /**
821 Only for internal temporary tables.
822 Count of TABLEs (having this TABLE_SHARE) which have opened this table.
823 */
825
826 // Can only be 1,2,4,8 or 16, but use uint32_t since that how it is
827 // represented in InnoDB
828 std::uint32_t key_block_size{0}; /* create key_block_size, if used */
829 uint stats_sample_pages{0}; /* number of pages to sample during
830 stats estimation, if used, otherwise 0. */
832 stats_auto_recalc{}; /* Automatic recalc of stats.
833 Zero-initialized to HA_STATS_AUTO_RECALC_DEFAULT
834 */
836 uint fields{0}; /* Number of fields */
837 uint rec_buff_length{0}; /* Size of table->record[] buffer */
838 uint keys{0}; /* Number of keys defined for the table*/
839 uint key_parts{0}; /* Number of key parts of all keys
840 defined for the table
841 */
842 uint max_key_length{0}; /* Length of the longest key */
843 uint max_unique_length{0}; /* Length of the longest unique key */
845 /**
846 Whether this is a temporary table that already has a UNIQUE index (removing
847 duplicate rows on insert), so that the optimizer does not need to run
848 DISTINCT itself. Also used for INTERSECT and EXCEPT as a fall-back if
849 hashing fails (secondary overflow of in-memory hash table, in which case
850 we revert to de-duplication using the unique key in the output table).
851 */
852 bool is_distinct{false};
853
854 uint null_fields{0}; /* number of null fields */
855 uint blob_fields{0}; /* number of blob fields */
856 uint varchar_fields{0}; /* number of varchar fields */
857 /**
858 For materialized derived tables; @see add_derived_key().
859 'first' means: having the lowest position in key_info.
860 */
862 /**
863 For materialized derived tables: allocated size of key_info array.
864 */
866 /**
867 For materialized derived tables: allocated size of base_key_parts array of
868 all TABLE objects. Used for generated keys.
869 */
871 /**
872 Array of names for generated keys, used for materialized derived tables.
873 Shared among all TABLE objects referring to this table share.
874 */
876 /**
877 Records per key array, used for materialized derived tables.
878 This is a contiguous array, with size given by max_tmp_key_parts.
879 The array is shared with all TABLE objects referring to this table share.
880 */
881 ulong *base_rec_per_key{nullptr};
882 /**
883 Records per key array, float rep., used for materialized derived tables.
884 This is a contiguous array, with size given by max_tmp_key_parts.
885 The array is shared with all TABLE objects referring to this table share.
886 */
888 /**
889 Bitmap with flags representing some of table options/attributes.
890
891 @sa HA_OPTION_PACK_RECORD, HA_OPTION_PACK_KEYS, ...
892
893 @note This is basically copy of HA_CREATE_INFO::table_options bitmap
894 at the time of table opening/usage.
895 */
897 /**
898 Bitmap with flags representing some of table options/attributes which
899 are in use by storage engine.
900
901 @note db_options_in_use is normally copy of db_create_options but can
902 be overridden by SE. E.g. MyISAM does this at handler::open() and
903 handler::info() time.
904 */
906 uint rowid_field_offset{0}; /* Field_nr +1 to rowid field */
907 /* Primary key index number, used in TABLE::key_info[] */
908 uint primary_key{0};
909 uint next_number_index{0}; /* autoincrement key number */
910 uint next_number_key_offset{0}; /* autoinc keypart offset in a key */
911 uint next_number_keypart{0}; /* autoinc keypart number in a key */
912 bool error{false}; /* error during open_table_def() */
914 /// Number of generated fields
915 uint vfields{0};
916 /// Number of fields having the default value generated
918 bool system{false}; /* Set if system table (one record) */
919 bool db_low_byte_first{false}; /* Portable row format */
920 bool crashed{false};
921 bool is_view{false};
922 bool m_open_in_progress{false}; /* True: alloc'ed, false: def opened */
923 mysql::binlog::event::Table_id table_map_id; /* for row-based replication */
924
925 /*
926 Cache for row-based replication table share checks that does not
927 need to be repeated. Possible values are: -1 when cache value is
928 not calculated yet, 0 when table *shall not* be replicated, 1 when
929 table *may* be replicated.
930 */
932
933 /*
934 Storage media to use for this table (unless another storage
935 media has been specified on an individual column - in versions
936 where that is supported)
937 */
939
940 /* Name of the tablespace used for this table */
941 const char *tablespace{nullptr};
942
943 /**
944 Partition meta data. Allocated from TABLE_SHARE::mem_root,
945 created when reading from the dd tables,
946 used as template for each TABLE instance.
947 The reason for having it on the TABLE_SHARE is to be able to reuse the
948 partition_elements containing partition names, values etc. instead of
949 allocating them for each TABLE instance.
950 TODO: Currently it is filled in and then only used for generating
951 the partition_info_str. The plan is to clone/copy/reference each
952 TABLE::part_info instance from it.
953 What is missing before it can be completed:
954 1) The partition expression, currently created only during parsing which
955 also needs the current TABLE instance as context for name resolution etc.
956 2) The partition values, currently the DD stores them as text so it needs
957 to be converted to field images (which is now done by first parsing the
958 value text into an Item, then saving the Item result/value into a field
959 and then finally copy the field image).
960 */
962 // TODO: Remove these four variables:
963 /**
964 Filled in when reading from frm.
965 This can simply be removed when removing the .frm support,
966 since it is already stored in the new DD.
967 */
968 bool auto_partitioned{false};
969 /**
970 Storing the full partitioning clause (PARTITION BY ...) which is used
971 when creating new partition_info object for each new TABLE object by
972 parsing this string.
973 These two will be needed until the missing parts above is fixed.
974 */
975 char *partition_info_str{nullptr};
977
978 /**
979 Cache the checked structure of this table.
980
981 The pointer data is used to describe the structure that
982 a instance of the table must have. Each element of the
983 array specifies a field that must exist on the table.
984
985 The pointer is cached in order to perform the check only
986 once -- when the table is loaded from the disk.
987 */
989
990 /** Main handler's share */
992
993 /** Instrumentation for this table share. */
995
996 /**
997 List of tickets representing threads waiting for the share to be flushed.
998 */
1000
1001 /**
1002 View object holding view definition read from DD. This object is not
1003 cached, and is owned by the table share. We are not able to read it
1004 on demand since we may then get a cache miss while holding LOCK_OPEN.
1005 */
1006 const dd::View *view_object{nullptr};
1007
1008 /**
1009 Data-dictionary object describing explicit temporary table represented
1010 by this share. NULL for other table types (non-temporary tables, internal
1011 temporary tables). This object is owned by TABLE_SHARE and should be
1012 deleted along with it.
1013 */
1015
1016 /// For materialized derived tables; @see add_derived_key().
1018
1019 /**
1020 Arrays with descriptions of foreign keys in which this table participates
1021 as child or parent. We only cache in them information from dd::Table object
1022 which is sufficient for use by prelocking algorithm/to check if table is
1023 referenced by a foreign key.
1024 */
1029
1030 // List of check constraint share instances.
1032
1033 /**
1034 Schema's read only mode - ON (true) or OFF (false). This is filled in
1035 when the share is initialized with meta data from DD. If the schema is
1036 altered, the tables and share are removed. This can be done since
1037 ALTER SCHEMA acquires exclusive meta data locks on the tables in the
1038 schema. We set this only for non-temporary tables. Otherwise, the value
1039 of the member below is 'NOT_SET'.
1040 */
1043
1044 /**
1045 Set share's table cache key and update its db and table name appropriately.
1046
1047 @param key_buff Buffer with already built table cache key to be
1048 referenced from share.
1049 @param key_length Key length.
1050
1051 @note
1052 Since 'key_buff' buffer will be referenced from share it should has same
1053 life-time as share itself.
1054 This method automatically ensures that TABLE_SHARE::table_name/db have
1055 appropriate values by using table cache key as their source.
1056 */
1057
1058 void set_table_cache_key(char *key_buff, size_t key_length) {
1059 table_cache_key.str = key_buff;
1060 table_cache_key.length = key_length;
1061 /*
1062 Let us use the fact that the key is "db/0/table_name/0" + optional
1063 part for temporary tables.
1064 */
1066 db.length = strlen(db.str);
1067 table_name.str = db.str + db.length + 1;
1068 table_name.length = strlen(table_name.str);
1069 }
1070
1071 /**
1072 Set share's table cache key and update its db and table name appropriately.
1073
1074 @param key_buff Buffer to be used as storage for table cache key
1075 (should be at least key_length bytes).
1076 @param key Value for table cache key.
1077 @param key_length Key length.
1078
1079 NOTE
1080 Since 'key_buff' buffer will be used as storage for table cache key
1081 it should has same life-time as share itself.
1082 */
1083
1084 void set_table_cache_key(char *key_buff, const char *key, size_t key_length) {
1085 memcpy(key_buff, key, key_length);
1086 set_table_cache_key(key_buff, key_length);
1087 }
1088
1090
1091 /** Returns the version of this TABLE_SHARE. */
1092 unsigned long version() const { return m_version; }
1093
1094 /**
1095 Set the version of this TABLE_SHARE to zero. This marks the
1096 TABLE_SHARE for automatic removal from the table definition cache
1097 once it is no longer referenced.
1098 */
1099 void clear_version();
1100
1101 /** Is this table share being expelled from the table definition cache? */
1102 bool has_old_version() const { return version() != refresh_version; }
1103
1104 /**
1105 Convert unrelated members of TABLE_SHARE to one enum
1106 representing its type.
1107
1108 @todo perhaps we need to have a member instead of a function.
1109 */
1111 if (is_view) return TABLE_REF_VIEW;
1112 switch (tmp_table) {
1113 case NO_TMP_TABLE:
1114 return TABLE_REF_BASE_TABLE;
1115 case SYSTEM_TMP_TABLE:
1116 return TABLE_REF_I_S_TABLE;
1117 default:
1118 return TABLE_REF_TMP_TABLE;
1119 }
1120 }
1121 /**
1122 Return a table metadata version.
1123 * for base tables and views, we return table_map_id.
1124 It is assigned from a global counter incremented for each
1125 new table loaded into the table definition cache (TDC).
1126 * for temporary tables it's table_map_id again. But for
1127 temporary tables table_map_id is assigned from
1128 thd->query_id. The latter is assigned from a thread local
1129 counter incremented for every new SQL statement. Since
1130 temporary tables are thread-local, each temporary table
1131 gets a unique id.
1132 * for everything else (e.g. information schema tables),
1133 the version id is zero.
1134
1135 This choice of version id is a large compromise
1136 to have a working prepared statement validation in 5.1. In
1137 future version ids will be persistent, as described in WL#4180.
1138
1139 Let's try to explain why and how this limited solution allows
1140 to validate prepared statements.
1141
1142 Firstly, sets (in mathematical sense) of version numbers
1143 never intersect for different table types. Therefore,
1144 version id of a temporary table is never compared with
1145 a version id of a view, and vice versa.
1146
1147 Secondly, for base tables and views, we know that each DDL flushes
1148 the respective share from the TDC. This ensures that whenever
1149 a table is altered or dropped and recreated, it gets a new
1150 version id.
1151 Unfortunately, since elements of the TDC are also flushed on
1152 LRU basis, this choice of version ids leads to false positives.
1153 E.g. when the TDC size is too small, we may have a SELECT
1154 * FROM INFORMATION_SCHEMA.TABLES flush all its elements, which
1155 in turn will lead to a validation error and a subsequent
1156 reprepare of all prepared statements. This is
1157 considered acceptable, since as long as prepared statements are
1158 automatically reprepared, spurious invalidation is only
1159 a performance hit. Besides, no better simple solution exists.
1160
1161 For temporary tables, using thd->query_id ensures that if
1162 a temporary table was altered or recreated, a new version id is
1163 assigned. This suits validation needs very well and will perhaps
1164 never change.
1165
1166 Metadata of information schema tables never changes.
1167 Thus we can safely assume 0 for a good enough version id.
1168
1169 Finally, by taking into account table type, we always
1170 track that a change has taken place when a view is replaced
1171 with a base table, a base table is replaced with a temporary
1172 table and so on.
1173
1174 @retval 0 For schema tables, DD tables and system views.
1175 non-0 For bases tables, views and temporary tables.
1176
1177 @sa Table_ref::is_table_ref_id_equal()
1178 */
1180
1181 /** Determine if the table is missing a PRIMARY KEY. */
1183 assert(primary_key <= MAX_KEY);
1184 return primary_key == MAX_KEY;
1185 }
1186
1187 uint find_first_unused_tmp_key(const Key_map &k);
1188
1189 bool visit_subgraph(Wait_for_flush *waiting_ticket,
1190 MDL_wait_for_graph_visitor *gvisitor);
1191
1192 bool wait_for_old_version(THD *thd, struct timespec *abstime,
1193 uint deadlock_weight);
1194
1195 /**
1196 The set of indexes that the optimizer may use when creating an execution
1197 plan.
1198 */
1199 Key_map usable_indexes(const THD *thd) const;
1200
1201 /** Release resources and free memory occupied by the table share. */
1202 void destroy();
1203
1204 /**
1205 How many TABLE objects use this TABLE_SHARE.
1206 @return the reference count
1207 */
1208 unsigned int ref_count() const {
1209 assert(assert_ref_count_is_locked(this));
1210 return m_ref_count;
1211 }
1212
1213 /**
1214 Increment the reference count by one.
1215 @return the new reference count
1216 */
1217 unsigned int increment_ref_count() {
1218 assert(assert_ref_count_is_locked(this));
1219 assert(!m_open_in_progress);
1220 return ++m_ref_count;
1221 }
1222
1223 /**
1224 Decrement the reference count by one.
1225 @return the new reference count
1226 */
1227 unsigned int decrement_ref_count() {
1228 assert(assert_ref_count_is_locked(this));
1229 assert(!m_open_in_progress);
1230 assert(m_ref_count > 0);
1231 return --m_ref_count;
1232 }
1233
1234 /// Does this TABLE_SHARE represent a table in a primary storage engine?
1235 bool is_primary_engine() const { return !m_secondary_engine; }
1236
1237 /// Does this TABLE_SHARE represent a table in a secondary storage engine?
1239
1240 /**
1241 Does this TABLE_SHARE represent a primary table that has a shadow
1242 copy in a secondary storage engine?
1243 */
1245 return is_primary_engine() && secondary_engine.str != nullptr;
1246 }
1247
1248 /** Returns whether this table is referenced by a foreign key. */
1250
1251 private:
1252 /// How many TABLE objects use this TABLE_SHARE.
1253 unsigned int m_ref_count{0};
1254
1255 /**
1256 TABLE_SHARE version, if changed the TABLE_SHARE must be reopened.
1257 NOTE: The TABLE_SHARE will not be reopened during LOCK TABLES in
1258 close_thread_tables!!!
1259 */
1260 unsigned long m_version{0};
1261
1262 protected: // To allow access from unit tests.
1263 /// Does this TABLE_SHARE represent a table in a secondary storage engine?
1265};
1266
1267/**
1268 Class is used as a BLOB field value storage for
1269 intermediate GROUP_CONCAT results. Used only for
1270 GROUP_CONCAT with DISTINCT or ORDER BY options.
1271 */
1272
1274 private:
1276 /**
1277 Sign that some values were cut
1278 during saving into the storage.
1279 */
1281
1282 public:
1285
1286 void reset() {
1288 truncated_value = false;
1289 }
1290 /**
1291 Function creates duplicate of 'from'
1292 string in 'storage' MEM_ROOT.
1293
1294 @param from string to copy
1295 @param length string length
1296
1297 @retval Pointer to the copied string.
1298 @retval 0 if an error occurred.
1299 */
1300 char *store(const char *from, size_t length) {
1301 return (char *)memdup_root(&storage, from, length);
1302 }
1305 }
1306 bool is_truncated_value() const { return truncated_value; }
1307};
1308
1309/**
1310 Class that represents a single change to a column value in partial
1311 update of a JSON column.
1312*/
1313class Binary_diff final {
1314 /// The offset of the start of the change.
1315 size_t m_offset;
1316
1317 /// The size of the portion that is to be replaced.
1318 size_t m_length;
1319
1320 public:
1321 /**
1322 Create a new Binary_diff object.
1323
1324 @param offset the offset of the beginning of the change
1325 @param length the length of the section that is to be replaced
1326 */
1327 Binary_diff(size_t offset, size_t length)
1329
1330 /// @return the offset of the changed data
1331 size_t offset() const { return m_offset; }
1332
1333 /// @return the length of the changed data
1334 size_t length() const { return m_length; }
1335
1336 /**
1337 Get a pointer to the start of the replacement data.
1338
1339 @param field the column that is updated
1340 @return a pointer to the start of the replacement data
1341 */
1342 const char *new_data(const Field *field) const;
1343
1344 /**
1345 Get a pointer to the start of the old data to be replaced.
1346
1347 @param field the column that is updated
1348 @return a pointer to the start of old data to be replaced.
1349 */
1350 const char *old_data(const Field *field) const;
1351};
1352
1353/**
1354 Vector of Binary_diff objects.
1355
1356 The Binary_diff objects in the vector should be ordered on offset, and none
1357 of the diffs should be overlapping or adjacent.
1358*/
1360
1361/**
1362 Flags for TABLE::m_status (maximum 8 bits).
1363 The flags define the state of the row buffer in TABLE::record[0].
1364*/
1365/**
1366 STATUS_NOT_STARTED is set when table is not accessed yet.
1367 Neither STATUS_NOT_FOUND nor STATUS_NULL_ROW can be set when this flag is set.
1368*/
1369#define STATUS_NOT_STARTED 1
1370/**
1371 Means we were searching for a row and didn't find it. This is used by
1372 storage engines (@see handler::index_read_map()) and the executor, both
1373 when doing an exact row lookup and advancing a scan (no more rows in range).
1374*/
1375#define STATUS_NOT_FOUND 2
1376/// Reserved for use by multi-table update. Means the row has been updated.
1377#define STATUS_UPDATED 16
1378/**
1379 Means that table->null_row is set. This is an artificial NULL-filled row
1380 (one example: in outer join, if no match has been found in inner table).
1381*/
1382#define STATUS_NULL_ROW 32
1383/// Reserved for use by multi-table delete. Means the row has been deleted.
1384#define STATUS_DELETED 64
1385
1386/* Information for one open table */
1388
1389/* Bitmap of table's fields */
1391
1392/*
1393 NOTE: Despite being a struct (for historical reasons), TABLE has
1394 a nontrivial destructor.
1395*/
1396struct TABLE {
1397 TABLE_SHARE *s{nullptr};
1398 handler *file{nullptr};
1399 TABLE *next{nullptr}, *prev{nullptr};
1400
1401 private:
1402 /**
1403 Links for the lists of used/unused TABLE objects for the particular
1404 table in the specific instance of Table_cache (in other words for
1405 specific Table_cache_element object).
1406 Declared as private to avoid direct manipulation with those objects.
1407 One should use methods of I_P_List template instead.
1408 */
1409 TABLE *cache_next{nullptr}, **cache_prev{nullptr};
1410
1411 /*
1412 Give Table_cache_element access to the above two members to allow
1413 using them for linking TABLE objects in a list.
1414 */
1416
1417 public:
1418 // Pointer to the histograms available on the table.
1419 // Protected in the same way as the pointer to the share.
1421
1422 /**
1423 A bitmap marking the hidden generated columns that exists for functional
1424 indexes.
1425 */
1427 /**
1428 The current session using this table object.
1429 Should be NULL when object is not in use.
1430 For an internal temporary table, it is NULL when the table is closed.
1431 Used for two purposes:
1432 - Signal that the object is in use, and by which session.
1433 - Pass the thread handler to storage handlers.
1434 The field should NOT be used as a general THD reference, instead use
1435 a passed THD reference, or, if there is no such, current_thd.
1436 The reason for this is that we cannot guarantee the field is not NULL.
1437 */
1438 THD *in_use{nullptr};
1439 Field **field{nullptr}; /* Pointer to fields */
1440 /// Count of hidden fields, if internal temporary table; 0 otherwise.
1442
1443 uchar *record[2]{nullptr, nullptr}; /* Pointer to records */
1444 uchar *write_row_record{nullptr}; /* Used as optimisation in
1445 THD::write_row */
1446 uchar *insert_values{nullptr}; /* used by INSERT ... UPDATE */
1447
1448 /// Buffer for use in multi-row reads. Initially empty.
1450
1451 /*
1452 Map of keys that can be used to retrieve all data from this table
1453 needed by the query without reading the row.
1454 */
1457
1458 /* Merge keys are all keys that had a column referred to in the query */
1460
1461 /*
1462 possible_quick_keys is a superset of quick_keys to use with EXPLAIN of
1463 JOIN-less commands (single-table UPDATE and DELETE).
1464
1465 When explaining regular JOINs, we use JOIN_TAB::keys to output the
1466 "possible_keys" column value. However, it is not available for
1467 single-table UPDATE and DELETE commands, since they don't use JOIN
1468 optimizer at the top level. OTOH they directly use the range optimizer,
1469 that collects all keys usable for range access here.
1470 */
1472
1473 /*
1474 A set of keys that can be used in the query that references this
1475 table.
1476
1477 All indexes disabled on the table's TABLE_SHARE (see TABLE::s) will be
1478 subtracted from this set upon instantiation. Thus for any TABLE t it holds
1479 that t.keys_in_use_for_query is a subset of t.s.keys_in_use. Generally we
1480 must not introduce any new keys here (see setup_tables).
1481
1482 The set is implemented as a bitmap.
1483 */
1485 /* Map of keys that can be used to calculate GROUP BY without sorting */
1487 /* Map of keys that can be used to calculate ORDER BY without sorting */
1489 KEY *key_info{nullptr}; /* data of keys defined for the table */
1490 /**
1491 Key part array for generated keys, used for materialized derived tables.
1492 This is a contiguous array, with size given by s->max_tmp_key_parts.
1493 */
1495
1496 Field *next_number_field{nullptr}; /* Set if next_number is activated */
1497 Field *found_next_number_field{nullptr}; /* Set on open */
1498 /// Pointer to generated columns
1499 Field **vfield{nullptr};
1500 /// Pointer to fields having the default value generated
1502 /// Field used by unique constraint
1504 // ----------------------------------------------------------------------
1505 // The next few members are used if this (temporary) file is used solely for
1506 // the materialization/computation of an INTERSECT or EXCEPT set operation
1507 // (in addition to hash_field above used to detect duplicate rows). For
1508 // INTERSECT and EXCEPT, we always use the hash field and compute the shape
1509 // of the result set using m_set_counter. The latter is a hidden field
1510 // located between the hash field and the row proper, only present for
1511 // INTERSECT or EXCEPT materialized in a temporary result table. The
1512 // materialized table has no duplicate rows, relying instead of the embedded
1513 // counter to produce the correct number of duplicates with ALL semantics. If
1514 // we have distinct semantics, we squash duplicates. This all happens in the
1515 // reading step of the tmp table (TableScanIterator::Read),
1516 // cf. m_last_operation_is_distinct. For explanation if the logic of the set
1517 // counter, see MaterializeIterator<Profiler>::MaterializeOperand.
1518 //
1519
1520 /// A priori unlimited. We pass this on to TableScanIterator at construction
1521 /// time, q.v., to limit the number of rows out of an EXCEPT or INTERSECT.
1522 /// For these set operations, we do not know enough to enforce the limit at
1523 /// materialize time (as for UNION): only when reading the rows with
1524 /// TableScanIterator do we check the counters.
1525 /// @todo: Ideally, this limit should be communicated to TableScanIterator in
1526 /// some other way.
1528
1529 private:
1530 /// The set counter. It points to the field in the materialized table
1531 /// holding the counter used to compute INTERSECT and EXCEPT, in record[0].
1532 /// For EXCEPT [DISTINCT | ALL] and INTERSECT DISTINCT this is a simple 64
1533 /// bits counter. For INTERSECT ALL, it is subdivided into two sub counters
1534 /// cf. class HalfCounter, cf. MaterializeOperand. See set_counter().
1536
1537 /// If m_set_counter is set: true if last block has DISTINCT semantics,
1538 /// either because it is marked as such, or because we have computed this
1539 /// to give an equivalent answer. If false, we have ALL semantics.
1540 /// It will be true if any DISTINCT is given in the merged N-ary set
1541 /// operation. See is_distinct().
1543 /// If false, de-duplication happens via an index on this table, if
1544 /// true, via an in-memory hash table, in which case we do not need to use
1545 /// any index.
1547
1548 public:
1558
1559 private:
1560 /// Holds the set operation type
1562
1563 public:
1564 /// Test if this tmp table stores the result of a UNION set operation or
1565 /// a single table.
1566 /// @return true if so, else false.
1567 bool is_union_or_table() const { return m_set_counter == nullptr; }
1568
1569 void set_hashing(bool use_hashing) {
1570 m_deduplicate_with_hashing = use_hashing;
1571 }
1572
1574
1575 /// Returns the set operation type
1577 if (m_set_op_type == SOT_NONE) {
1578 assert(is_union_or_table()); // EXCEPT and INTERSECT are already set up
1580 }
1581 return m_set_op_type;
1582 }
1583
1584 bool is_intersect() const {
1585 return m_set_op_type == SOT_INTERSECT_ALL ||
1587 }
1588
1589 bool is_except() const {
1590 return m_set_op_type == SOT_EXCEPT_ALL ||
1592 }
1593
1595 /**
1596 Initialize the set counter field pointer and the type of set operation
1597 *other than UNION*.
1598 @param set_counter the field in the materialized table that holds the
1599 counter we use to compute intersect or except
1600 @param except if true, EXCEPT, else INTERSECT
1601 @param distinct if true, the set operation is DISTINCT, else ALL
1602 */
1603 void set_set_op(Field_longlong *set_counter, bool except, bool distinct) {
1606 assert(m_set_op_type == SOT_NONE);
1607 m_set_op_type = except
1608 ? (distinct ? SOT_EXCEPT_DISTINCT : SOT_EXCEPT_ALL)
1610 }
1611
1613 //
1614 // end of INTERSECT and EXCEPT specific members
1615 // ----------------------------------------------------------------------
1616
1617 Field *fts_doc_id_field{nullptr}; /* Set if FTS_DOC_ID field is present */
1618
1619 /* Table's triggers, 0 if there are no of them */
1621 Table_ref *pos_in_table_list{nullptr}; /* Element referring to this table */
1622 /* Position in thd->locked_table_list under LOCK TABLES */
1624 ORDER *group{nullptr};
1625 const char *alias{nullptr}; ///< alias or table name
1626 uchar *null_flags{nullptr}; ///< Pointer to the null flags of record[0]
1628 nullptr}; ///< Saved null_flags while null_row is true
1629
1630 /* containers */
1632 /*
1633 Bitmap of fields that one or more query condition refers to. Only
1634 used if optimizer_condition_fanout_filter is turned 'on'.
1635 Currently, only the WHERE clause and ON clause of inner joins is
1636 taken into account but not ON conditions of outer joins.
1637 Furthermore, HAVING conditions apply to groups and are therefore
1638 not useful as table condition filters.
1639 */
1641
1642 /**
1643 Bitmap of table fields (columns), which are explicitly set in the
1644 INSERT INTO statement. It is declared here to avoid memory allocation
1645 on MEM_ROOT).
1646
1647 @sa fields_set_during_insert.
1648 */
1650
1651 /**
1652 The read set contains the set of columns that the execution engine needs to
1653 process the query. In particular, it is used to tell the storage engine
1654 which columns are needed. For virtual generated columns, the underlying base
1655 columns are also added, since they are required in order to calculate the
1656 virtual generated columns.
1657
1658 Internal operations in the execution engine that need to move rows between
1659 buffers, such as aggregation, sorting, hash join and set operations, should
1660 rather use read_set_internal, since the virtual generated columns have
1661 already been calculated when the row was read from the storage engine.
1662
1663 Set during resolving; every field that gets resolved, sets its own bit
1664 in the read set. In some cases, we switch the read set around during
1665 various phases; note that it is a pointer.
1666
1667 In addition, for binary logging purposes, the bitmaps are set according
1668 to the settings of @@binlog_row_image. Therefore, for logging purposes,
1669 some additional fields, to those specified by the optimizer, may be
1670 flagged in the read and write sets.
1671 @c TABLE::mark_columns_per_binlog_row_image for additional details.
1672 */
1674
1676
1677 /**
1678 A bitmap of fields that are explicitly referenced by the query. This is
1679 mostly the same as read_set, but it does not include base columns of
1680 referenced virtual generated columns unless the base columns are referenced
1681 explicitly in the query.
1682
1683 This is the read set that should be used for determining which columns to
1684 store in join buffers, aggregation buffers, sort buffers, or similar
1685 operations internal to the execution engine. Both because it is unnecessary
1686 to store the implicitly read base columns in the buffer, since they won't
1687 ever be read out of the buffer anyways, and because the base columns may not
1688 even be possible to read, if a covering index scan is used and the index
1689 only contains the virtual column and not all its base columns.
1690 */
1692
1693 /**
1694 A pointer to the bitmap of table fields (columns), which are explicitly set
1695 in the INSERT INTO statement.
1696
1697 fields_set_during_insert points to def_fields_set_during_insert
1698 for base (non-temporary) tables. In other cases, it is NULL.
1699 Triggers can not be defined for temporary tables, so this bitmap does not
1700 matter for temporary tables.
1701
1702 @sa def_fields_set_during_insert.
1703 */
1705
1706 /*
1707 The ID of the query that opened and is using this table. Has different
1708 meanings depending on the table type.
1709
1710 Temporary tables:
1711
1712 table->query_id is set to thd->query_id for the duration of a statement
1713 and is reset to 0 once it is closed by the same statement. A non-zero
1714 table->query_id means that a statement is using the table even if it's
1715 not the current statement (table is in use by some outer statement).
1716
1717 Non-temporary tables:
1718
1719 Under pre-locked or LOCK TABLES mode: query_id is set to thd->query_id
1720 for the duration of a statement and is reset to 0 once it is closed by
1721 the same statement. A non-zero query_id is used to control which tables
1722 in the list of pre-opened and locked tables are actually being used.
1723 */
1725
1726 /*
1727 For each key that has quick_keys.is_set(key) == true: estimate of #records
1728 and max #key parts that range access would use.
1729 */
1731
1732 /* Bitmaps of key parts that =const for the entire join. */
1734
1737
1738 /*
1739 Estimate of number of records that satisfy SARGable part of the table
1740 condition, or table->file->records if no SARGable condition could be
1741 constructed.
1742 This value is used by join optimizer as an estimate of number of records
1743 that will pass the table condition (condition that depends on fields of
1744 this table and constants)
1745 */
1747
1748 uint lock_position{0}; /* Position in MYSQL_LOCK.table */
1749 uint lock_data_start{0}; /* Start pos. in MYSQL_LOCK.locks */
1750 uint lock_count{0}; /* Number of locks */
1751 uint db_stat{0}; /* mode of file as in handler.h */
1752 int current_lock{0}; /* Type of lock on table */
1753
1754 // List of table check constraints.
1756
1757 private:
1758 /**
1759 If true, this table is inner w.r.t. some outer join operation, all columns
1760 are nullable (in the query), and null_row may be true.
1761 */
1762 bool nullable{false};
1763
1764 uint8 m_status{0}; /* What's in record[0] */
1765 public:
1766 /*
1767 If true, the current table row is considered to have all columns set to
1768 NULL, including columns declared as "not null" (see nullable).
1769 @todo make it private, currently join buffering changes it through a pointer
1770 */
1771 bool null_row{false};
1772
1773 bool copy_blobs{false}; /* copy_blobs when storing */
1774
1775 /*
1776 TODO: Each of the following flags take up 8 bits. They can just as easily
1777 be put into one single unsigned long and instead of taking up 18
1778 bytes, it would take up 4.
1779 */
1780 bool force_index{false};
1781
1782 /**
1783 Flag set when the statement contains FORCE INDEX FOR ORDER BY
1784 See Table_ref::process_index_hints().
1785 */
1787
1788 /**
1789 Flag set when the statement contains FORCE INDEX FOR GROUP BY
1790 See Table_ref::process_index_hints().
1791 */
1793 bool const_table{false};
1794 /// True if writes to this table should not write rows and just write keys.
1795 bool no_rows{false};
1796
1797 /**
1798 If set, the optimizer has found that row retrieval should access index
1799 tree only.
1800 */
1801 bool key_read{false};
1802 /**
1803 Certain statements which need the full row, set this to ban index-only
1804 access.
1805 */
1806 bool no_keyread{false};
1807 /**
1808 If set, indicate that the table is not replicated by the server.
1809 */
1810 bool no_replicate{false};
1811 /* To signal that the table is associated with a HANDLER statement */
1812 bool open_by_handler{false};
1813 /**
1814 To indicate that value of the auto_increment field was provided
1815 explicitly by the user or from some other source (e.g. in case of
1816 INSERT ... SELECT, ALTER TABLE or LOAD DATA) and not as default
1817 or result of conversion from NULL value.
1818
1819 @note Since auto_increment fields are always non-NULL we can't find
1820 out using methods of Field class if 0 value stored in such field
1821 was provided explicitly or is result of applying default/conversion
1822 from NULL value. In the former case no new auto_increment value
1823 needs to be generated in MODE_NO_AUTO_VALUE_ON_ZERO mode, while
1824 the latter cases require new value generation. Hence the need
1825 for this flag.
1826 @note Used only in the MODE_NO_AUTO_VALUE_ON_ZERO mode and only
1827 by handler::write_row().
1828 */
1830 bool alias_name_used{false}; /* true if table_name is alias */
1831 bool get_fields_in_item_tree{false}; /* Signal to fix_field */
1832
1833 private:
1834 /**
1835 This TABLE object is invalid and cannot be reused. TABLE object might have
1836 inconsistent info or handler might not allow some operations.
1837
1838 For example, TABLE might have inconsistent info about partitioning.
1839 We also use this flag to avoid calling handler::reset() for partitioned
1840 InnoDB tables after in-place ALTER TABLE API commit phase and to force
1841 closing table after REPAIR TABLE has failed during its prepare phase as
1842 well.
1843
1844 @note This member can be set only by thread that owns/has opened the
1845 table and while holding its THD::LOCK_thd_data lock.
1846 It can be read without locking by this owner thread, or by some other
1847 thread concurrently after acquiring owner's THD::LOCK_thd_data.
1848
1849 @note The TABLE will not be reopened under LOCK TABLES in
1850 close_thread_tables().
1851 */
1852 bool m_invalid_dict{false};
1853
1854 /**
1855 This TABLE object is invalid and cannot be reused as it has outdated
1856 rec_per_key and handler stats.
1857
1858 @note This member is protected from concurrent access to it by lock of
1859 Table Cache's partition to which this TABLE object belongs,
1860 */
1861 bool m_invalid_stats{false};
1862
1863 /**
1864 For tmp tables. true <=> tmp table has been instantiated.
1865 Also indicates that table was successfully opened since
1866 we immediately delete tmp tables which we fail to open.
1867 */
1868 bool created{false};
1869
1870 public:
1871 /// For a materializable derived or SJ table: true if has been materialized
1872 bool materialized{false};
1873 struct /* field connections */
1874 {
1875 class JOIN_TAB *join_tab{nullptr};
1876 class QEP_TAB *qep_tab{nullptr};
1877 thr_lock_type lock_type{TL_UNLOCK}; /* How table is used */
1879 /*
1880 true <=> range optimizer found that there is no rows satisfying
1881 table conditions.
1882 */
1883 bool impossible_range{false};
1885
1886 /**
1887 @todo This member should not be declared in-line. That makes it
1888 impossible for any function that does memory allocation to take a const
1889 reference to a TABLE object.
1890 */
1892 /**
1893 Initialized in Item_func_group_concat::setup for appropriate
1894 temporary table if GROUP_CONCAT is used with ORDER BY | DISTINCT
1895 and BLOB field count > 0.
1896 */
1898
1899 /**
1900 Not owned by the TABLE; used only from filesort_free_buffers().
1901 See comments on SortingIterator::CleanupAfterQuery().
1902 */
1905
1906 /**
1907 The result of applying a unique operation (by row ID) to the table, if done.
1908 In particular, this is done in some forms of index merge.
1909 */
1911 partition_info *part_info{nullptr}; /* Partition related information */
1912 /* If true, all partitions have been pruned away */
1915
1916 private:
1917 /// Cost model object for operations on this table
1919#ifndef NDEBUG
1920 /**
1921 Internal tmp table sequential number. Increased in the order of
1922 creation. Used for debugging purposes when many tmp tables are used
1923 during execution (e.g several windows with window functions)
1924 */
1926#endif
1927 public:
1928 void reset();
1929 void init(THD *thd, Table_ref *tl);
1930 bool init_tmp_table(THD *thd, TABLE_SHARE *share, MEM_ROOT *m_root,
1931 CHARSET_INFO *charset, const char *alias, Field **fld,
1932 uint *blob_fld, bool is_virtual);
1933 bool fill_item_list(mem_root_deque<Item *> *item_list) const;
1934 void clear_column_bitmaps(void);
1935 void prepare_for_position(void);
1936
1939 uint key_parts = 0) const;
1941 void mark_auto_increment_column(void);
1942 void mark_columns_needed_for_update(THD *thd, bool mark_binlog_columns);
1946 void mark_generated_columns(bool is_update);
1947 void mark_gcol_in_maps(const Field *field);
1948 void mark_check_constraint_columns(bool is_update);
1949 void column_bitmaps_set(MY_BITMAP *read_set_arg, MY_BITMAP *write_set_arg);
1950 inline void column_bitmaps_set_no_signal(MY_BITMAP *read_set_arg,
1951 MY_BITMAP *write_set_arg) {
1952 read_set = read_set_arg;
1953 write_set = write_set_arg;
1954 }
1955 inline void use_all_columns() {
1957 }
1961 }
1962 void invalidate_dict();
1963 void invalidate_stats();
1964 /**
1965 @note Can be called by thread owning table without additional locking, and
1966 by any other thread which has acquired owner's THD::LOCK_thd_data lock.
1967 */
1968 inline bool has_invalid_dict() const {
1969 assert(assert_invalid_dict_is_locked(this));
1970 return !db_stat || m_invalid_dict;
1971 }
1972 /// @note Can be called by thread owning Table_cache::m_lock
1973 inline bool has_invalid_stats() {
1974 assert(assert_invalid_stats_is_locked(this));
1975 return m_invalid_stats;
1976 }
1977 /// @returns first non-hidden column
1979 /// @returns count of visible fields
1981 bool alloc_tmp_keys(uint new_key_count, uint new_key_part_count,
1982 bool modify_share);
1983 bool add_tmp_key(Field_map *key_parts, bool invisible, bool modify_share);
1984 void move_tmp_key(int old_idx, bool modify_share);
1985 void drop_unused_tmp_keys(bool modify_share);
1986
1987 void set_keyread(bool flag);
1988
1989 /**
1990 Check whether the given index has a virtual generated columns.
1991
1992 @param index_no the given index to check
1993
1994 @returns true if if index is defined over at least one virtual generated
1995 column
1996 */
1997 inline bool index_contains_some_virtual_gcol(uint index_no) const {
1998 assert(index_no < s->keys);
1999 return key_info[index_no].flags & HA_VIRTUAL_GEN_KEY;
2000 }
2001 void update_const_key_parts(Item *conds);
2002
2003 bool check_read_removal(uint index);
2004
2005 ptrdiff_t default_values_offset() const {
2006 return (ptrdiff_t)(s->default_values - record[0]);
2007 }
2008
2009 /// @returns true if a storage engine handler object is assigned to table
2010 bool has_storage_handler() const { return file != nullptr; }
2011
2012 /// Set storage handler for temporary table
2014 // Ensure consistent call order
2015 assert((file == nullptr && file_arg != nullptr) ||
2016 (file != nullptr && file_arg == nullptr));
2017 assert(!is_created());
2018 assert(file_arg->inited == handler::NONE);
2019 file = file_arg;
2020 }
2021 /// Return true if table is instantiated, and false otherwise.
2022 bool is_created() const { return created; }
2023
2024 /**
2025 Set the table as "created", and enable flags in storage engine
2026 that could not be enabled without an instantiated table.
2027 */
2028 void set_created();
2029 /**
2030 Set the contents of table to be "deleted", ie "not created", after having
2031 deleted the contents.
2032 */
2033 void set_deleted() { created = materialized = false; }
2034 /// Set table as nullable, ie it is inner wrt some outer join
2035 void set_nullable() { nullable = true; }
2036
2037 /// Return whether table is nullable
2038 bool is_nullable() const { return nullable; }
2039
2040 /// @return true if table contains one or more generated columns
2041 bool has_gcol() const { return vfield; }
2042
2043 /**
2044 Life cycle of the row buffer is as follows:
2045 - The initial state is "not started".
2046 - When reading a row through the storage engine handler, the status is set
2047 as "has row" or "no row", depending on whether a row was found or not.
2048 The "not started" state is cleared, as well as the "null row" state,
2049 the updated state and the deleted state.
2050 - When making a row available in record[0], make sure to update row status
2051 similarly to how the storage engine handler does it.
2052 - If a NULL-extended row is needed in join execution, the "null row" state
2053 is set. Note that this can be combined with "has row" if a row was read
2054 but condition on it was evaluated to false (happens for single-row
2055 lookup), or "no row" if no more rows could be read.
2056 Note also that for the "null row" state, the NULL bits inside the
2057 row are set to one, so the row inside the row buffer is no longer usable,
2058 unless the NULL bits are saved in a separate buffer.
2059 - The "is updated" and "is deleted" states are set when row is updated or
2060 deleted, respectively.
2061 */
2062 /// Set status for row buffer as "not started"
2065 null_row = false;
2066 }
2067
2068 /// @return true if a row operation has been done
2069 bool is_started() const { return !(m_status & STATUS_NOT_STARTED); }
2070
2071 /// Set status for row buffer: contains row
2073 m_status = 0;
2074 null_row = false;
2075 }
2076
2077 /**
2078 Set status for row buffer: contains no row. This is set when
2079 - A lookup operation finds no row
2080 - A scan operation scans past the last row of the range.
2081 - An error in generating key values before calling storage engine.
2082 */
2083 void set_no_row() {
2085 null_row = false;
2086 }
2087
2088 /**
2089 Set "row found" status from handler result
2090
2091 @param status 0 if row was found, <> 0 if row was not found
2092 */
2095 null_row = false;
2096 }
2097
2098 /**
2099 Set current row as "null row", for use in null-complemented outer join.
2100 The row buffer may or may not contain a valid row.
2101 set_null_row() and reset_null_row() are used by the join executor to
2102 signal the presence or absence of a NULL-extended row for an outer joined
2103 table. Null rows may also be used to specify rows that are all NULL in
2104 grouing operations.
2105 @note this is a destructive operation since the NULL value bit vector
2106 is overwritten. Caching operations must be aware of this.
2107 */
2109 null_row = true;
2111 if (s->null_bytes > 0) memset(null_flags, 255, s->null_bytes);
2112 }
2113
2114 /// Clear "null row" status for the current row
2116 null_row = false;
2117 m_status &= ~STATUS_NULL_ROW;
2118 }
2119
2120 /// Set "updated" property for the current row
2122 assert(is_started() && has_row());
2124 }
2125
2126 /// Set "deleted" property for the current row
2128 assert(is_started() && has_row());
2130 }
2131
2132 /// @return true if there is a row in row buffer
2133 bool has_row() const { return !(m_status & STATUS_NOT_FOUND); }
2134
2135 /// @return true if current row is null-extended
2136 bool has_null_row() const { return null_row; }
2137
2138 /// @return true if current row has been updated (multi-table update)
2139 bool has_updated_row() const { return m_status & STATUS_UPDATED; }
2140
2141 /// @return true if current row has been deleted (multi-table delete)
2142 bool has_deleted_row() const { return m_status & STATUS_DELETED; }
2143
2144 /// Save the NULL flags of the current row into the designated buffer.
2145 /// This should be done before null-complementing a table accessed
2146 /// with EQRefIterator or a const table, as they need to be able to
2147 /// restore the original contents of the record buffer before
2148 /// reading the next row. This is necessary because of their special
2149 /// code for avoiding table access if the same row should be
2150 /// accessed by the next read.
2152 if (s->null_bytes > 0) memcpy(null_flags_saved, null_flags, s->null_bytes);
2153 }
2154
2155 /// Restore the NULL flags of the current row from the designated buffer
2157 if (s->null_bytes > 0) memcpy(null_flags, null_flags_saved, s->null_bytes);
2158 }
2159
2160 /// Empties internal temporary table (deletes rows, closes scan)
2161 bool empty_result_table();
2162
2163 /**
2164 Initialize the optimizer cost model.
2165
2166 This function should be called each time a new query is started.
2167
2168 @param cost_model_server the main cost model object for the query
2169 */
2170 void init_cost_model(const Cost_model_server *cost_model_server) {
2171 m_cost_model.init(cost_model_server, this);
2172 }
2173
2174 /**
2175 Return the cost model object for this table.
2176 */
2177 const Cost_model_table *cost_model() const { return &m_cost_model; }
2178
2179 /**
2180 Bind all the table's value generator columns in all the forms:
2181 stored/virtual GC, default expressions and checked constraints.
2182
2183 @details When a table is opened from the dictionary, the Value Generator
2184 expressions are bound during opening (see fix_value_generator_fields()).
2185 After query execution, Item::cleanup() is called on them
2186 (see cleanup_value_generator_items()). When the table is opened from the
2187 table cache, the Value Generetor(s) need to be bound again and this
2188 function does that.
2189 */
2191
2192 /**
2193 Clean any state in items associated with generated columns to be ready for
2194 the next statement.
2195 */
2197
2198#ifndef NDEBUG
2199 void set_tmp_table_seq_id(uint arg) { tmp_table_seq_id = arg; }
2200#endif
2201 /**
2202 Update covering keys depending on max read key length.
2203
2204 Update available covering keys for the table, based on a constrained field
2205 and the identified covering prefix keys: If the matched part of field is
2206 longer than the index prefix,
2207 the prefix index cannot be used as a covering index.
2208
2209 @param[in] field Pointer to field object
2210 @param[in] key_read_length Max read key length
2211 @param[in] covering_prefix_keys Covering prefix keys
2212 */
2213 void update_covering_prefix_keys(Field *field, uint16 key_read_length,
2214 Key_map *covering_prefix_keys);
2215
2216 /**
2217 Returns the primary engine handler for the table.
2218 If none exist, nullptr is returned.
2219 */
2221
2222 private:
2223 /**
2224 Bitmap that tells which columns are eligible for partial update in an
2225 update statement.
2226
2227 The bitmap is lazily allocated in the TABLE's mem_root when
2228 #mark_column_for_partial_update() is called.
2229 */
2231
2232 /**
2233 Object which contains execution time state used for partial update
2234 of JSON columns.
2235
2236 It is allocated in the execution mem_root by #setup_partial_update() if
2237 there are columns that have been marked as eligible for partial update.
2238 */
2240
2241 /**
2242 This flag decides whether or not we should log the drop temporary table
2243 command.
2244 */
2246
2247 public:
2248 /**
2249 Does this table have any columns that can be updated using partial update
2250 in the current row?
2251
2252 @return whether any columns in the current row can be updated using partial
2253 update
2254 */
2255 bool has_binary_diff_columns() const;
2256
2257 /**
2258 Get the list of binary diffs that have been collected for a given column in
2259 the current row, or `nullptr` if partial update cannot be used for that
2260 column.
2261
2262 @param field the column to get binary diffs for
2263 @return the list of binary diffs for the column, or `nullptr` if the column
2264 cannot be updated using partial update
2265 */
2266 const Binary_diff_vector *get_binary_diffs(const Field *field) const;
2267
2268 /**
2269 Mark a given column as one that can potentially be updated using
2270 partial update during execution of an update statement.
2271
2272 Whether it is actually updated using partial update, is not
2273 determined until execution time, since that depends both on the
2274 data that is in the column and the new data that is written to the
2275 column.
2276
2277 This function should be called during preparation of an update
2278 statement.
2279
2280 @param field a column which is eligible for partial update
2281 @retval false on success
2282 @retval true on out-of-memory
2283 */
2285
2286 /**
2287 Has this column been marked for partial update?
2288
2289 Note that this only tells if the column satisfies the syntactical
2290 requirements for being partially updated. Use #is_binary_diff_enabled() or
2291 #is_logical_diff_enabled() instead to see if partial update should be used
2292 on the column.
2293
2294 @param field the column to check
2295 @return whether the column has been marked for partial update
2296 */
2297 bool is_marked_for_partial_update(const Field *field) const;
2298
2299 /**
2300 Does this table have any columns that were marked with
2301 #mark_column_for_partial_update()?
2302
2303 Note that this only tells if any of the columns satisfy the syntactical
2304 requirements for being partially updated. Use
2305 #has_binary_diff_columns(), #is_binary_diff_enabled() or
2306 #is_logical_diff_enabled() instead to see if partial update should be used
2307 on a column.
2308 */
2310
2311 /**
2312 Enable partial update of JSON columns in this table. It is only
2313 enabled for the columns that have previously been marked for
2314 partial update using #mark_column_for_partial_update().
2315
2316 @param logical_diffs should logical JSON diffs be collected in addition
2317 to the physical binary diffs?
2318
2319 This function should be called once per statement execution, when
2320 the update statement is optimized.
2321
2322 @retval false on success
2323 @retval true on out-of-memory
2324 */
2325 bool setup_partial_update(bool logical_diffs);
2326
2327 /**
2328 @see setup_partial_update(bool)
2329
2330 This is a wrapper that auto-computes the value of the parameter
2331 logical_diffs.
2332
2333 @retval false on success
2334 @retval true on out-of-memory
2335 */
2336 bool setup_partial_update();
2337
2338 /**
2339 Add a binary diff for a column that is updated using partial update.
2340
2341 @param field the column that is being updated
2342 @param offset the offset of the changed portion
2343 @param length the length of the changed portion
2344
2345 @retval false on success
2346 @retval true on out-of-memory
2347 */
2348 bool add_binary_diff(const Field *field, size_t offset, size_t length);
2349
2350 /**
2351 Clear the diffs that have been collected for partial update of
2352 JSON columns, and re-enable partial update for any columns where
2353 partial update was temporarily disabled for the current row.
2354 Should be called between each row that is updated.
2355 */
2357
2358 /**
2359 Clean up state used for partial update of JSON columns.
2360
2361 This function should be called at the end of each statement
2362 execution.
2363 */
2365
2366 /**
2367 Temporarily disable collection of binary diffs for a column in the current
2368 row.
2369
2370 This function is called during execution to disable partial update of a
2371 column that was previously marked as eligible for partial update with
2372 #mark_column_for_partial_update() during preparation.
2373
2374 Partial update of this column will be re-enabled when we go to the next
2375 row.
2376
2377 @param field the column to stop collecting binary diffs for
2378 */
2380
2381 /**
2382 Temporarily disable collection of Json_diff objects describing the
2383 logical changes of a JSON column in the current row.
2384
2385 Collection of logical JSON diffs is re-enabled when we go to the next row.
2386
2387 @param field the column to stop collecting logical JSON diffs for
2388 */
2390
2391 /**
2392 Get a buffer that can be used to hold the partially updated column value
2393 while performing partial update.
2394 */
2396
2397 /**
2398 Add a logical JSON diff describing a logical change to a JSON column in
2399 partial update.
2400
2401 @param field the column that is updated
2402 @param path the JSON path that is changed
2403 @param operation the operation to perform
2404 @param new_value the new value in the path
2405
2406 @throws std::bad_alloc if memory cannot be allocated
2407 */
2409 enum_json_diff_operation operation,
2410 const Json_wrapper *new_value);
2411
2412 /**
2413 Get the list of JSON diffs that have been collected for a given column in
2414 the current row, or `nullptr` if partial update cannot be used for that
2415 column.
2416
2417 @param field the column to get JSON diffs for
2418 @return the list of JSON diffs for the column, or `nullptr` if the column
2419 cannot be updated using partial update
2420 */
2422
2423 /**
2424 Is partial update using binary diffs enabled on this JSON column?
2425
2426 @param field the column to check
2427 @return whether the column can be updated with binary diffs
2428 */
2429 bool is_binary_diff_enabled(const Field *field) const;
2430
2431 /**
2432 Is partial update using logical diffs enabled on this JSON column?
2433
2434 @param field the column to check
2435 @return whether the column can be updated with JSON diffs
2436 */
2437 bool is_logical_diff_enabled(const Field *field) const;
2438
2439 /**
2440 Virtual fields of type BLOB have a flag m_keep_old_value. This flag is set
2441 to false for all such fields in this table.
2442 */
2444
2445 /**
2446 Set the variable should_binlog_drop_if_temp_flag, so that
2447 the logging of temporary tables can be decided.
2448
2449 @param should_binlog the value to set flag should_binlog_drop_if_temp_flag
2450 */
2451 void set_binlog_drop_if_temp(bool should_binlog);
2452
2453 /**
2454 @return whether should_binlog_drop_if_temp_flag flag is
2455 set or not
2456 */
2457 bool should_binlog_drop_if_temp(void) const;
2458
2459 /**
2460 Find the histogram for the given field index.
2461
2462 @param field_index The index of the field we want to find a histogram for.
2463
2464 @retval nullptr if no histogram is found.
2465 @retval Pointer to a histogram if one is found.
2466 */
2467 const histograms::Histogram *find_histogram(uint field_index) const;
2468};
2469
2470static inline void empty_record(TABLE *table) {
2471 restore_record(table, s->default_values);
2472 if (table->s->null_bytes > 0)
2473 memset(table->null_flags, 255, table->s->null_bytes);
2474}
2475
2476#define MY_I_S_MAYBE_NULL 1
2477#define MY_I_S_UNSIGNED 2
2478
2480 /**
2481 This is used as column name.
2482 */
2483 const char *field_name;
2484 /**
2485 For string-type columns, this is the maximum number of
2486 characters. Otherwise, it is the 'display-length' for the column.
2487 For the data type MYSQL_TYPE_DATETIME this field specifies the
2488 number of digits in the fractional part of time value.
2489 */
2491 /**
2492 This denotes data type for the column. For the most part, there seems to
2493 be one entry in the enum for each SQL data type, although there seem to
2494 be a number of additional entries in the enum.
2495 */
2498 /**
2499 This is used to set column attributes. By default, columns are @c NOT
2500 @c NULL and @c SIGNED, and you can deviate from the default
2501 by setting the appropriate flags. You can use either one of the flags
2502 @c MY_I_S_MAYBE_NULL and @c MY_I_S_UNSIGNED or
2503 combine them using the bitwise or operator @c |. Both flags are
2504 defined in table.h.
2505 */
2506 uint field_flags; // Field attributes (maybe_null, signed, unsigned etc.)
2507 const char *old_name;
2508 uint open_method; // Not used
2509};
2510
2512 const char *table_name;
2514 /* Fill table with data */
2515 int (*fill_table)(THD *thd, Table_ref *tables, Item *cond);
2516 /* Handle fields for old SHOW */
2517 int (*old_format)(THD *thd, ST_SCHEMA_TABLE *schema_table);
2518 int (*process_table)(THD *thd, Table_ref *tables, TABLE *table, bool res,
2521};
2522
2523/**
2524 Strategy for how to process a view or derived table (merge or materialization)
2525*/
2531
2532#define VIEW_SUID_INVOKER 0
2533#define VIEW_SUID_DEFINER 1
2534#define VIEW_SUID_DEFAULT 2
2535
2536/* view WITH CHECK OPTION parameter options */
2537#define VIEW_CHECK_NONE 0
2538#define VIEW_CHECK_LOCAL 1
2539#define VIEW_CHECK_CASCADED 2
2540
2541/* result of view WITH CHECK OPTION parameter check */
2542#define VIEW_CHECK_OK 0
2543#define VIEW_CHECK_ERROR 1
2544#define VIEW_CHECK_SKIP 2
2545
2546/** The threshold size a blob field buffer before it is freed */
2547#define MAX_TDC_BLOB_SIZE 65536
2548
2549/**
2550 Struct that describes an expression selected from a derived table or view.
2551*/
2553 /**
2554 Points to an item that represents the expression.
2555 If the item is determined to be unused, the pointer is set to NULL.
2556 */
2558 /// Name of selected expression
2559 const char *name;
2560};
2561
2562/*
2563 Column reference of a NATURAL/USING join. Since column references in
2564 joins can be both from views and stored tables, may point to either a
2565 Field (for tables), or a Field_translator (for views).
2566*/
2567
2569 public:
2570 Field_translator *view_field; /* Column reference of merge view. */
2571 Item_field *table_field; /* Column reference of table or temp view. */
2572 Table_ref *table_ref; /* Original base table/view reference. */
2573 /*
2574 True if a common join column of two NATURAL/USING join operands. Notice
2575 that when we have a hierarchy of nested NATURAL/USING joins, a column can
2576 be common at some level of nesting but it may not be common at higher
2577 levels of nesting. Thus this flag may change depending on at which level
2578 we are looking at some column.
2579 */
2581
2582 public:
2584 Natural_join_column(Item_field *field_param, Table_ref *tab);
2585 const char *name();
2586 Item *create_item(THD *thd);
2587 Field *field();
2588 const char *table_name();
2589 const char *db_name();
2590 GRANT_INFO *grant();
2591};
2592
2593/**
2594 This is generic enum. It may be reused in the ACL statements
2595 for clauses that can map to the values defined in this enum.
2596*/
2598 UNCHANGED, /* The clause is not specified */
2599 DEFAULT, /* Default value of clause is specified */
2600 YES, /* Value that maps to True is specified */
2601 NO /* Value that maps to False is specified */
2602};
2603
2604struct LEX_MFA {
2611 /*
2612 The following flags are indicators for the SQL syntax used while
2613 parsing CREATE/ALTER user. While other members are self-explanatory,
2614 'uses_authentication_string_clause' signifies if the password is in
2615 hash form (if the var was set to true) or not.
2616 */
2621 /* flag set during CREATE USER .. INITIAL AUTHENTICATION BY */
2623 /* flag set during ALTER USER .. ADD nth FACTOR */
2625 /* flag set during ALTER USER .. MODIFY nth FACTOR */
2627 /* flag set during ALTER USER .. DROP nth FACTOR */
2629 /*
2630 flag used during authentication and to decide if server should
2631 be in sandbox mode or not
2632 */
2634 /* flag set during ALTER USER .. nth FACTOR UNREGISTER */
2636 /* flag set during ALTER USER .. INITIATE REGISTRATION */
2638 /* flag set during ALTER USER .. FINISH REGISTRATION */
2640
2642 void reset() {
2644 auth = NULL_CSTR;
2648 nth_factor = 1;
2652 has_password_generator = false;
2653 passwordless = false;
2654 add_factor = false;
2655 drop_factor = false;
2656 modify_factor = false;
2657 requires_registration = false;
2658 unregister = false;
2659 init_registration = false;
2660 finish_registration = false;
2661 }
2662 void copy(LEX_MFA *m, MEM_ROOT *alloc);
2663};
2664
2665/*
2666 This structure holds the specifications relating to
2667 ALTER user ... PASSWORD EXPIRE ...
2668*/
2686 /* Holds the specification of 'PASSWORD REQUIRE CURRENT' clause. */
2688 void cleanup() {
2694 account_locked = false;
2706 }
2707};
2708
2709/*
2710 This structure holds the specifications related to
2711 mysql user and the associated auth details.
2712*/
2713struct LEX_USER {
2721 /* restrict MFA methods to atmost 3 authentication plugins */
2725
2726 void init() {
2727 user = NULL_CSTR;
2728 host = NULL_CSTR;
2730 uses_replace_clause = false;
2732 discard_old_password = false;
2749 mfa_list.clear();
2750 with_initial_auth = false;
2751 }
2752
2754
2755 bool add_mfa_identifications(LEX_MFA *factor2, LEX_MFA *factor3 = nullptr);
2756
2757 /*
2758 Allocates the memory in the THD mem pool and initialize the members of
2759 this struct. It is preferable to use this method to create a LEX_USER
2760 rather allocating the memory in the THD and initializing the members
2761 explicitly.
2762 */
2763 static LEX_USER *alloc(THD *thd);
2764 static LEX_USER *alloc(THD *thd, LEX_STRING *user, LEX_STRING *host);
2765 /*
2766 Initialize the members of this struct. It is preferable to use this method
2767 to initialize a LEX_USER rather initializing the members explicitly.
2768 */
2769 static LEX_USER *init(LEX_USER *to_init, THD *thd, LEX_STRING *user,
2770 LEX_STRING *host);
2771};
2772
2773/**
2774 Derive type of metadata lock to be requested for table used by a DML
2775 statement from the type of THR_LOCK lock requested for this table.
2776*/
2777
2778inline enum enum_mdl_type mdl_type_for_dml(enum thr_lock_type lock_type) {
2779 return lock_type >= TL_WRITE_ALLOW_WRITE
2783}
2784
2785/**
2786 Type of table which can be open for an element of table list.
2787*/
2788
2794
2795/**
2796 This structure is used to keep info about possible key for the result table
2797 of a derived table/view.
2798 The 'referenced_by' is the table map of tables to which this possible
2799 key corresponds.
2800 The 'used_field' is a map of fields of which this key consists of.
2801 See also the comment for the Table_ref::update_derived_keys function.
2802*/
2803
2805 public:
2809};
2810
2811class Table_function;
2812/*
2813 Table reference in the FROM clause.
2814
2815 These table references can be of several types that correspond to
2816 different SQL elements. Below we list all types of TABLE_LISTs with
2817 the necessary conditions to determine when a Table_ref instance
2818 belongs to a certain type.
2819
2820 1) table (Table_ref::view == NULL)
2821 - base table
2822 (Table_ref::derived == NULL)
2823 - subquery - Table_ref::table is a temp table
2824 (Table_ref::derived != NULL)
2825 - information schema table
2826 (Table_ref::schema_table != NULL)
2827 NOTICE: for schema tables Table_ref::field_translation may be != NULL
2828 2) view (Table_ref::view != NULL)
2829 - merge (Table_ref::effective_algorithm == VIEW_ALGORITHM_MERGE)
2830 also (Table_ref::field_translation != NULL)
2831 - temptable(Table_ref::effective_algorithm == VIEW_ALGORITHM_TEMPTABLE)
2832 also (Table_ref::field_translation == NULL)
2833 3) nested table reference (Table_ref::nested_join != NULL)
2834 - table sequence - e.g. (t1, t2, t3)
2835 TODO: how to distinguish from a JOIN?
2836 - general JOIN
2837 TODO: how to distinguish from a table sequence?
2838 - NATURAL JOIN
2839 (Table_ref::natural_join != NULL)
2840 - JOIN ... USING
2841 (Table_ref::join_using_fields != NULL)
2842 - semi-join
2843 ;
2844*/
2845
2847 public:
2848 Table_ref() = default;
2849
2850 /**
2851 Only to be used by legacy code that temporarily needs a Table_ref,
2852 more specifically: Query_result_create::binlog_show_create_table().
2853 */
2854 explicit Table_ref(TABLE *table_arg) : table(table_arg) {}
2855
2856 /// Constructor that can be used when the strings are null terminated.
2857 Table_ref(const char *db_name, const char *table_name,
2858 enum thr_lock_type lock_type)
2859 : Table_ref(db_name, strlen(db_name), table_name, strlen(table_name),
2860 table_name, lock_type) {}
2861
2862 /**
2863 Creates a Table_ref object with pre-allocated strings for database,
2864 table and alias.
2865 */
2866 Table_ref(TABLE *table_arg, const char *db_name_arg, size_t db_length_arg,
2867 const char *table_name_arg, size_t table_name_length_arg,
2868 const char *alias_arg, enum thr_lock_type lock_type_arg)
2869 : db(db_name_arg),
2870 table_name(table_name_arg),
2871 alias(alias_arg),
2872 m_map(1),
2873 table(table_arg),
2874 m_lock_descriptor{lock_type_arg},
2875 db_length(db_length_arg),
2876 table_name_length(table_name_length_arg) {
2879 }
2880
2881 /// Constructor that can be used when the strings are null terminated.
2882 Table_ref(const char *db_name, const char *table_name, const char *alias,
2883 enum thr_lock_type lock_type)
2884 : Table_ref(db_name, strlen(db_name), table_name, strlen(table_name),
2885 alias, lock_type) {}
2886
2887 /**
2888 This constructor can be used when a Table_ref is needed for an
2889 existing temporary table. These typically have very long table names, since
2890 it is a fully qualified path. For this reason, the table is set to the
2891 alias. The database name is left blank. The lock descriptor is set to
2892 TL_READ.
2893 */
2894 Table_ref(TABLE *table_arg, const char *alias_arg)
2895 : db(""),
2896 table_name(alias_arg),
2897 alias(alias_arg),
2898 m_map(1),
2899 table(table_arg),
2901 db_length(0),
2902 table_name_length(strlen(alias_arg)) {
2905 }
2906
2907 /**
2908 Sets an explicit enum_mdl_type value, without initializing
2909 m_lock_descriptor.
2910 */
2911 Table_ref(TABLE *table_arg, const char *alias_arg, enum_mdl_type mdl_type)
2912 : db(table_arg->s->db.str),
2913 table_name(table_arg->s->table_name.str),
2914 alias(alias_arg),
2915 m_map(1),
2916 table(table_arg),
2917 db_length(table_arg->s->db.length),
2918 table_name_length(table_arg->s->table_name.length) {
2921 }
2922
2923 Table_ref(const char *db_name, const char *table_name_arg,
2924 enum thr_lock_type lock_type_arg,
2925 enum enum_mdl_type mdl_request_type)
2926 : db(db_name),
2927 table_name(table_name_arg),
2928 alias(table_name_arg),
2929 m_map(1),
2930 m_lock_descriptor{lock_type_arg},
2931 db_length(strlen(db_name)),
2932 table_name_length(strlen(table_name_arg)) {
2935 mdl_request.set_type(mdl_request_type);
2936 }
2937
2938 Table_ref(const char *db_name, size_t db_length_arg,
2939 const char *table_name_arg, size_t table_name_length_arg,
2940 enum thr_lock_type lock_type_arg,
2941 enum enum_mdl_type mdl_request_type)
2942 : db(db_name),
2943 table_name(table_name_arg),
2944 alias(table_name_arg),
2945 m_map(1),
2946 m_lock_descriptor{lock_type_arg},
2947 db_length(db_length_arg),
2948 table_name_length(table_name_length_arg) {
2951 mdl_request.set_type(mdl_request_type);
2952 }
2953
2954 Table_ref(const char *db_name, size_t db_length_arg,
2955 const char *table_name_arg, size_t table_name_length_arg,
2956 enum thr_lock_type lock_type_arg)
2957 : db(db_name),
2958 table_name(table_name_arg),
2959 alias(table_name_arg),
2960 m_map(1),
2961 m_lock_descriptor{lock_type_arg},
2962 db_length(db_length_arg),
2963 table_name_length(table_name_length_arg) {}
2964
2965 /**
2966 Sets an explicit enum_mdl_type value, without initializing
2967 m_lock_descriptor.
2968 */
2969 Table_ref(const char *db_name, size_t db_length_arg,
2970 const char *table_name_arg, size_t table_name_length_arg,
2971 const char *alias_arg, enum enum_mdl_type mdl_request_type)
2972 : db(db_name),
2973 table_name(table_name_arg),
2974 alias(alias_arg),
2975 m_map(1),
2976 db_length(db_length_arg),
2977 table_name_length(table_name_length_arg) {
2980 mdl_request.set_type(mdl_request_type);
2981 }
2982
2983 Table_ref(const char *db_name, size_t db_length_arg,
2984 const char *table_name_arg, size_t table_name_length_arg,
2985 const char *alias_arg, enum thr_lock_type lock_type_arg,
2986 enum enum_mdl_type mdl_request_type)
2987 : db(db_name),
2988 table_name(table_name_arg),
2989 alias(alias_arg),
2990 m_map(1),
2991 m_lock_descriptor{lock_type_arg},
2992 db_length(db_length_arg),
2993 table_name_length(table_name_length_arg) {
2996 mdl_request.set_type(mdl_request_type);
2997 }
2998
2999 Table_ref(const char *db_name_arg, size_t db_length_arg,
3000 const char *table_name_arg, size_t table_name_length_arg,
3001 const char *alias_arg, enum thr_lock_type lock_type_arg)
3002 : db(db_name_arg),
3003 table_name(table_name_arg),
3004 alias(alias_arg),
3005 m_map(1),
3006 m_lock_descriptor{lock_type_arg},
3007 db_length(db_length_arg),
3008 table_name_length(table_name_length_arg) {
3011 }
3012
3013 /// Create a Table_ref object representing a nested join
3014 static Table_ref *new_nested_join(MEM_ROOT *allocator, const char *alias,
3016 mem_root_deque<Table_ref *> *belongs_to,
3017 Query_block *select);
3019 Item *join_cond() const { return m_join_cond; }
3020 void set_join_cond(Item *val) {
3021 // If optimization has started, it's too late to change m_join_cond.
3022 assert(m_join_cond_optim == nullptr || m_join_cond_optim == (Item *)1);
3023 m_join_cond = val;
3024 }
3027 /*
3028 Either we are setting to "empty", or there must pre-exist a
3029 permanent condition.
3030 */
3031 assert(cond == nullptr || cond == (Item *)1 || m_join_cond != nullptr);
3032 m_join_cond_optim = cond;
3033 }
3035
3036 /// @returns true if semi-join nest
3037 bool is_sj_nest() const { return m_is_sj_or_aj_nest && !m_join_cond; }
3038 /// @returns true if anti-join nest
3039 bool is_aj_nest() const { return m_is_sj_or_aj_nest && m_join_cond; }
3040 /// @returns true if anti/semi-join nest
3041 bool is_sj_or_aj_nest() const { return m_is_sj_or_aj_nest; }
3042 /// Makes the next a semi/antijoin nest
3044 assert(!m_is_sj_or_aj_nest);
3045 m_is_sj_or_aj_nest = true;
3046 }
3047
3048 /// Merge tables from a query block into a nested join structure
3050
3051 /// Reset table
3052 void reset();
3053
3054 /// Evaluate the check option of a view
3055 int view_check_option(THD *thd) const;
3056
3057 /// Produce a textual identification of this object
3058 void print(const THD *thd, String *str, enum_query_type query_type) const;
3059
3060 /// Check which single table inside a view that matches a table map
3061 bool check_single_table(Table_ref **table_ref, table_map map);
3062
3063 /// Allocate a buffer for inserted column values
3065
3067 /**
3068 Retrieve the last (right-most) leaf in a nested join tree with
3069 respect to name resolution.
3070
3071
3072 Given that 'this' is a nested table reference, recursively walk
3073 down the right-most children of 'this' until we reach a leaf
3074 table reference with respect to name resolution.
3075
3076 The right-most child of a nested table reference is the first
3077 element in the list of children because the children are inserted
3078 in reverse order.
3079
3080 @return
3081 - If 'this' is a nested table reference - the right-most child
3082 of the tree rooted in 'this',
3083 - else - 'this'
3084 */
3086 bool is_leaf_for_name_resolution() const;
3087
3088 /// Return the outermost view this table belongs to, or itself
3089 inline const Table_ref *top_table() const {
3090 return belong_to_view ? belong_to_view : this;
3091 }
3092
3094 return const_cast<Table_ref *>(
3095 const_cast<const Table_ref *>(this)->top_table());
3096 }
3097
3098 /// Prepare check option for a view
3099 bool prepare_check_option(THD *thd, bool is_cascaded = false);
3100
3101 /// Merge WHERE condition of view or derived table into outer query
3102 bool merge_where(THD *thd);
3103
3104 /// Prepare replace filter for a view (used for REPLACE command)
3105 bool prepare_replace_filter(THD *thd);
3106
3107 /// Return true if this represents a named view
3108 bool is_view() const { return view != nullptr; }
3109
3110 /// Return true if this represents a derived table (an unnamed view)
3111 bool is_derived() const { return derived != nullptr && view == nullptr; }
3112
3113 /// Return true if this represents a named view or a derived table
3114 bool is_view_or_derived() const { return derived != nullptr; }
3115
3116 /// Return true if this represents a table function
3117 bool is_table_function() const { return table_function != nullptr; }
3118 /**
3119 @returns true if this is a recursive reference inside the definition of a
3120 recursive CTE.
3121 @note that it starts its existence as a dummy derived table, until the
3122 end of resolution when it's not a derived table anymore, just a reference
3123 to the materialized temporary table. Whereas a non-recursive
3124 reference to the recursive CTE is a derived table.
3125 */
3127
3128 /// @returns true if this is a base table (permanent or temporary)
3129 bool is_base_table() const {
3130 return !(is_view_or_derived() || is_table_function() ||
3132 }
3133 /**
3134 @see is_recursive_reference().
3135 @returns true if error
3136 */
3138
3139 /**
3140 @returns true for a table that represents an optimizer internal table,
3141 is a derived table, a recursive reference, a table function.
3142 Internal tables are only visible inside a query expression, and is hence
3143 not visible in any schema, or need any kind of privilege checking.
3144 */
3145 bool is_internal() const {
3147 }
3148
3149 /**
3150 @returns true for a table that is a placeholder, ie a derived table,
3151 a view, a recursive reference, a table function or a schema table.
3152 A table is also considered to be a placeholder if it does not have a
3153 TABLE object for some other reason.
3154 */
3155 bool is_placeholder() const {
3157 is_table_function() || schema_table || table == nullptr;
3158 }
3159
3160 /// Return true if view or derived table and can be merged
3161 bool is_mergeable() const;
3162
3163 /**
3164 Checks if this is a table that contains zero rows or one row, and that can
3165 be materialized during optimization.
3166
3167 @returns true if materializable table contains one or zero rows, and
3168 materialization during optimization is permitted
3169
3170 Returning true, if the hypergraph optimizer is not active, implies that the
3171 table is materialized during optimization, so it need not be optimized
3172 during execution. The hypergraph optimizer does not care about const tables,
3173 so such tables are not executed during optimization time when it is active.
3174 */
3175 bool materializable_is_const() const;
3176
3177 /// Return true if this is a derived table or view that is merged
3179
3180 /// Set table to be merged
3181 void set_merged() {
3184 }
3185
3186 /// Return true if this is a materializable derived table/view
3189 }
3190
3191 /// Set table to be materialized
3193 // @todo We should do this only once, but currently we cannot:
3194 // assert(effective_algorithm == VIEW_ALGORITHM_UNDEFINED);
3197 }
3198
3199 /// Return true if table is updatable
3200 bool is_updatable() const { return m_updatable; }
3201
3202 /// Set table as updatable. (per default, a table is non-updatable)
3203 void set_updatable() { m_updatable = true; }
3204
3205 /// Return true if table is insertable-into
3206 bool is_insertable() const { return m_insertable; }
3207
3208 /// Set table as insertable-into. (per default, a table is not insertable)
3209 void set_insertable() { m_insertable = true; }
3210
3211 /// Return true if table is being updated
3212 bool is_updated() const { return m_updated; }
3213
3214 /// Set table and all referencing views as being updated
3216 for (Table_ref *tr = this; tr != nullptr; tr = tr->referencing_view)
3217 tr->m_updated = true;
3218 }
3219
3220 /// Return true if table is being inserted into
3221 bool is_inserted() const { return m_inserted; }
3222
3223 /// Set table and all referencing views as being inserted into
3225 for (Table_ref *tr = this; tr != nullptr; tr = tr->referencing_view)
3226 tr->m_inserted = true;
3227 }
3228
3229 /// Return true if table is being deleted from
3230 bool is_deleted() const { return m_deleted; }
3231
3232 /// Set table and all referencing views as being deleted from
3234 for (Table_ref *tr = this; tr != nullptr; tr = tr->referencing_view)
3235 tr->m_deleted = true;
3236 }
3237
3238 /// Set table as full-text search (default is not fulltext searched)
3240
3241 /// Returns true if a MATCH function references this table.
3243
3244 /// Is this table only available in an external storage engine?
3245 bool is_external() const;
3246
3247 /**
3248 Set table as readonly, ie it is neither updatable, insertable nor
3249 deletable during this statement.
3250 */
3252 m_updatable = false;
3253 m_insertable = false;
3254 }
3255
3256 /**
3257 Return true if this is a view or derived table that is defined over
3258 more than one base table, and false otherwise.
3259 */
3260 bool is_multiple_tables() const {
3261 if (is_view_or_derived()) {
3262 assert(is_merged()); // Cannot be a materialized view
3263 return leaf_tables_count() > 1;
3264 } else {
3265 assert(nested_join == nullptr); // Must be a base table
3266 return false;
3267 }
3268 }
3269
3270 /// Return no. of base tables a merged view or derived table is defined over.
3271 uint leaf_tables_count() const;
3272
3273 /// Return first leaf table of a base table or a view/derived table
3275 Table_ref *tr = this;
3276 while (tr->merge_underlying_list) tr = tr->merge_underlying_list;
3277 return tr;
3278 }
3279
3280 /// Return any leaf table that is not an inner table of an outer join
3281 /// @todo WL#6570 with prepare-once, replace with first_leaf_table()
3282 /// when WL#6059 is merged in (it really converts RIGHT JOIN to
3283 /// LEFT JOIN so the first leaf is part of a LEFT JOIN,
3284 /// guaranteed).
3286 Table_ref *tr = this;
3287 while (tr->merge_underlying_list) {
3288 tr = tr->merge_underlying_list;
3289 /*
3290 "while" is used, however, an "if" might be sufficient since there is
3291 no more than one inner table in a join nest (with outer_join true).
3292 */
3293 while (tr->outer_join) tr = tr->next_local;
3294 }
3295 return tr;
3296 }
3297 /**
3298 Set the LEX object of a view (will also define this as a view).
3299 @note: The value 1 is used to indicate a view but without a valid
3300 query object. Use only if the LEX object is not going to
3301 be used in later processing.
3302 */
3303 void set_view_query(LEX *lex) { view = lex; }
3304
3305 /// Return the valid LEX object for a view.
3306 LEX *view_query() const {
3307 assert(view != nullptr && view != (LEX *)1);
3308 return view;
3309 }
3310
3311 /**
3312 Set the query expression of a derived table or view.
3313 (Will also define this as a derived table, unless it is a named view.)
3314 */
3316 derived = query_expr;
3317 }
3318
3319 /// Return the query expression of a derived table or view.
3321 assert(derived);
3322 return derived;
3323 }
3324
3325 /// Resolve a derived table or view reference
3326 bool resolve_derived(THD *thd, bool apply_semijoin);
3327
3328 /// Optimize the query expression representing a derived table/view
3329 bool optimize_derived(THD *thd);
3330
3331 /// Create result table for a materialized derived table/view
3332 bool create_materialized_table(THD *thd);
3333
3334 /// Materialize derived table
3335 bool materialize_derived(THD *thd);
3336
3337 /// Check if we can push outer where condition to this derived table
3339
3340 /// Return the number of hidden fields added for the temporary table
3341 /// created for this derived table.
3343
3344 /// Prepare security context for a view
3345 bool prepare_security(THD *thd);
3346
3349
3350 /**
3351 Compiles the tagged hints list and fills up TABLE::keys_in_use_for_query,
3352 TABLE::keys_in_use_for_group_by, TABLE::keys_in_use_for_order_by,
3353 TABLE::force_index and TABLE::covering_keys.
3354 */
3355 bool process_index_hints(const THD *thd, TABLE *table);
3356
3357 /**
3358 Compare the version of metadata from the previous execution
3359 (if any) with values obtained from the current table
3360 definition cache element.
3361
3362 @sa check_and_update_table_version()
3363 */
3365 return (m_table_ref_type == s->get_table_ref_type() &&
3367 }
3368
3369 /**
3370 Record the value of metadata version of the corresponding
3371 table definition cache element in this parse tree node.
3372
3373 @sa check_and_update_table_version()
3374 */
3377 }
3378
3379 void set_table_ref_id(enum_table_ref_type table_ref_type_arg,
3380 ulonglong table_ref_version_arg) {
3381 m_table_ref_type = table_ref_type_arg;
3382 m_table_ref_version = table_ref_version_arg;
3383 }
3384
3385 /**
3386 If a derived table, returns query block id of first underlying query block.
3387 Zero if not derived.
3388 */
3389 uint query_block_id() const;
3390
3391 /**
3392 This is for showing in EXPLAIN.
3393 If a derived table, returns query block id of first underlying query block
3394 of first materialized Table_ref instance. Zero if not derived.
3395 */
3396 uint query_block_id_for_explain() const;
3397
3398 /**
3399 @brief Returns the name of the database that the referenced table belongs
3400 to.
3401 */
3402 const char *get_db_name() const { return db; }
3403
3404 /**
3405 @brief Returns the name of the table that this Table_ref represents.
3406
3407 @details The unqualified table name or view name for a table or view,
3408 respectively.
3409 */
3410 const char *get_table_name() const { return table_name; }
3412 ha_rows fallback_estimate = PLACEHOLDER_TABLE_ROW_ESTIMATE);
3413 bool update_derived_keys(THD *, Field *, Item **, uint, bool *);
3414 bool generate_keys();
3415
3416 /// Setup a derived table to use materialization
3419
3420 /// Setup a table function to use materialization
3421 bool setup_table_function(THD *thd);
3422
3423 bool create_field_translation(THD *thd);
3424
3425 /**
3426 @brief Returns the outer join nest that this Table_ref belongs to, if
3427 any.
3428
3429 @details There are two kinds of join nests, outer-join nests and semi-join
3430 nests. This function returns non-NULL in the following cases:
3431 @li 1. If this table/nest is embedded in a nest and this nest IS NOT a
3432 semi-join nest. (In other words, it is an outer-join nest.)
3433 @li 2. If this table/nest is embedded in a nest and this nest IS a
3434 semi-join nest, but this semi-join nest is embedded in another
3435 nest. (This other nest will be an outer-join nest, since all inner
3436 joined nested semi-join nests have been merged in
3437 @c simplify_joins() ).
3438 Note: This function assumes that @c simplify_joins() has been performed.
3439 Before that, join nests will be present for all types of join.
3440
3441 @return outer join nest, or NULL if none.
3442 */
3443
3445 if (!embedding) return nullptr;
3446 if (embedding->is_sj_nest()) return embedding->embedding;
3447 return embedding;
3448 }
3449 /**
3450 Return true if this table is an inner table of some outer join.
3451
3452 Examine all the embedding join nests of the table.
3453 @note This function works also before redundant join nests have been
3454 eliminated.
3455
3456 @return true if table is an inner table of some outer join, false otherwise.
3457 */
3458
3460 if (outer_join) return true;
3461 for (Table_ref *emb = embedding; emb; emb = emb->embedding) {
3462 if (emb->outer_join) return true;
3463 }
3464 return false;
3465 }
3466
3467 /**
3468 Return the base table entry of an updatable table.
3469 In DELETE and UPDATE, a view used as a target table must be mergeable,
3470 updatable and defined over a single table.
3471 */
3473 const Table_ref *tbl = this;
3474 assert(tbl->is_updatable() && !tbl->is_multiple_tables());
3475 while (tbl->is_view_or_derived()) {
3476 tbl = tbl->merge_underlying_list;
3477 assert(tbl->is_updatable() && !tbl->is_multiple_tables());
3478 }
3479 return tbl;
3480 }
3481
3483 return const_cast<Table_ref *>(
3484 static_cast<const Table_ref *>(this)->updatable_base_table());
3485 }
3486
3487 /**
3488 Mark that there is a NATURAL JOIN or JOIN ... USING between two tables.
3489
3490 This function marks that table b should be joined with a either via
3491 a NATURAL JOIN or via JOIN ... USING. Both join types are special
3492 cases of each other, so we treat them together. The function
3493 setup_conds() creates a list of equal condition between all fields
3494 of the same name for NATURAL JOIN or the fields in
3495 Table_ref::join_using_fields for JOIN ... USING.
3496 The list of equality conditions is stored
3497 either in b->join_cond(), or in JOIN::conds, depending on whether there
3498 was an outer join.
3499
3500 EXAMPLE
3501 @verbatim
3502 SELECT * FROM t1 NATURAL LEFT JOIN t2
3503 <=>
3504 SELECT * FROM t1 LEFT JOIN t2 ON (t1.i=t2.i and t1.j=t2.j ... )
3505
3506 SELECT * FROM t1 NATURAL JOIN t2 WHERE <some_cond>
3507 <=>
3508 SELECT * FROM t1, t2 WHERE (t1.i=t2.i and t1.j=t2.j and <some_cond>)
3509
3510 SELECT * FROM t1 JOIN t2 USING(j) WHERE <some_cond>
3511 <=>
3512 SELECT * FROM t1, t2 WHERE (t1.j=t2.j and <some_cond>)
3513 @endverbatim
3514
3515 @param b Right join argument.
3516 */
3518
3519 /**
3520 Set granted privileges for a table.
3521
3522 Can be used when generating temporary tables that are also used in
3523 resolver process, such as when generating a UNION table
3524
3525 @param privilege Privileges granted for this table.
3526 */
3527 void set_privileges(ulong privilege) { grant.privilege |= privilege; }
3528
3529 bool save_properties();
3530 void restore_properties();
3531
3532 /*
3533 List of tables local to a subquery or the top-level SELECT (used by
3534 SQL_I_List). Considers views as leaves (unlike 'next_leaf' below).
3535 Created at parse time in Query_block::add_table_to_list() ->
3536 table_list.link_in_list().
3537 */
3539 /* link in a global list of all queries tables */
3540 Table_ref *next_global{nullptr}, **prev_global{nullptr};
3541 const char *db{nullptr}, *table_name{nullptr}, *alias{nullptr};
3542 /*
3543 Target tablespace name: When creating or altering tables, this
3544 member points to the tablespace_name in the HA_CREATE_INFO struct.
3545 */
3547 char *option{nullptr}; /* Used by cache index */
3548
3549 /** Table level optimizer hints for this table. */
3551 /* Hints for query block of this table. */
3553
3554 void set_lock(const Lock_descriptor &descriptor) {
3555 m_lock_descriptor = descriptor;
3556 }
3557
3559
3561
3562 private:
3563 /**
3564 The members below must be kept aligned so that (1 << m_tableno) == m_map.
3565 A table that takes part in a join operation must be assigned a unique
3566 table number.
3567 */
3568 uint m_tableno{0}; ///< Table number within query block
3569 table_map m_map{0}; ///< Table map, derived from m_tableno
3570 /**
3571 If this table or join nest is the Y in "X [LEFT] JOIN Y ON C", this
3572 member points to C. May also be generated from JOIN ... USING clause.
3573 It may be modified only by permanent transformations (permanent = done
3574 once for all executions of a prepared statement).
3575 */
3578
3579 public:
3580 /*
3581 (Valid only for semi-join nests) Bitmap of tables that are within the
3582 semi-join (this is different from bitmap of all nest's children because
3583 tables that were pulled out of the semi-join nest remain listed as
3584 nest's children).
3585 */
3587
3588 /*
3589 During parsing - left operand of NATURAL/USING join where 'this' is
3590 the right operand. After parsing (this->natural_join == this) iff
3591 'this' represents a NATURAL or USING join operation. Thus after
3592 parsing 'this' is a NATURAL/USING join iff (natural_join != NULL).
3593 */
3595 /*
3596 True if 'this' represents a nested join that is a NATURAL JOIN.
3597 For one of the operands of 'this', the member 'natural_join' points
3598 to the other operand of 'this'.
3599 */
3600 bool is_natural_join{false};
3601 /* Field names in a USING clause for JOIN ... USING. */
3603 /*
3604 Explicitly store the result columns of either a NATURAL/USING join or
3605 an operand of such a join.
3606 */
3608 /* true if join_columns contains all columns of this table reference. */
3610
3611 /*
3612 List of nodes in a nested join tree, that should be considered as
3613 leaves with respect to name resolution. The leaves are: views,
3614 top-most nodes representing NATURAL/USING joins, subqueries, and
3615 base tables. All of these Table_ref instances contain a
3616 materialized list of columns. The list is local to a subquery.
3617 */
3619 /* Index names in a "... JOIN ... USE/IGNORE INDEX ..." clause. */
3621 TABLE *table{nullptr}; /* opened table */
3623 table_id{}; /* table id (from binlog) for opened table */
3624 /*
3625 Query_result for derived table to pass it from table creation to table
3626 filling procedure
3627 */
3629 /*
3630 Reference from aux_tables to local list entry of main select of
3631 multi-delete statement:
3632 delete t1 from t2,t1 where t1.a<'B' and t2.b=t1.b;
3633 here it will be reference of first occurrence of t1 to second (as you
3634 can see this lists can't be merged)
3635 */
3637
3638 /*
3639 Holds the function used as the table function
3640 */
3642
3643 /**
3644 If we've previously made an access path for “derived”, it is cached here.
3645 This is useful if we need to plan the query block twice (the hypergraph
3646 optimizer can do so, with and without in2exists predicates), both saving
3647 work and avoiding issues when we try to throw away the old items_to_copy
3648 for a new (identical) one.
3649 */
3651
3652 private:
3653 /**
3654 This field is set to non-null for derived tables and views. It points
3655 to the Query_expression representing the derived table/view.
3656 E.g. for a query
3657 @verbatim SELECT * FROM (SELECT a FROM t1) b @endverbatim
3658 */
3659 Query_expression *derived{nullptr}; /* Query_expression of derived table */
3660
3661 /// If non-NULL, the CTE which this table is derived from.
3663 /**
3664 If the user has specified column names with the syntaxes "table name
3665 parenthesis column names":
3666 WITH qn(column names) AS (select...)
3667 or
3668 FROM (select...) dt(column names)
3669 or
3670 CREATE VIEW v(column_names) AS ...
3671 then this points to the list of column names. NULL otherwise.
3672 */
3674
3675 public:
3676 ST_SCHEMA_TABLE *schema_table{nullptr}; /* Information_schema table */
3678 /*
3679 True when the view field translation table is used to convert
3680 schema table fields for backwards compatibility with SHOW command.
3681 */
3683 /* link to query_block where this table was used */
3685
3686 private:
3687 LEX *view{nullptr}; /* link on VIEW lex for merging */
3688
3689 public:
3690 /// Array of selected expressions from a derived table or view.
3692
3693 /// pointer to element after last one in translation table above
3695 /*
3696 List (based on next_local) of underlying tables of this view. I.e. it
3697 does not include the tables of subqueries used in the view. Is set only
3698 for merged views.
3699 */
3701 /*
3702 - 0 for base tables
3703 - in case of the view it is the list of all (not only underlying
3704 tables but also used in subquery ones) tables of the view.
3705 */
3707 /* most upper view this table belongs to */
3709 /*
3710 The view directly referencing this table
3711 (non-zero only for merged underlying tables of a view).
3712 */
3714 /* Ptr to parent MERGE table list item. See top comment in ha_myisammrg.cc */
3716 /*
3717 Security context (non-zero only for tables which belong
3718 to view with SQL SECURITY DEFINER)
3719 */
3721 /*
3722 This view security context (non-zero only for views with
3723 SQL SECURITY DEFINER)
3724 */
3726 /*
3727 List of all base tables local to a subquery including all view
3728 tables. Unlike 'next_local', this in this list views are *not*
3729 leaves. Created in setup_tables() -> make_leaf_tables().
3730 */
3732 Item *derived_where_cond{nullptr}; ///< WHERE condition from derived table
3733 Item *check_option{nullptr}; ///< WITH CHECK OPTION condition
3734 Item *replace_filter{nullptr}; ///< Filter for REPLACE command
3735 LEX_STRING select_stmt{nullptr, 0}; ///< text of (CREATE/SELECT) statement
3736 LEX_STRING source{nullptr, 0}; ///< source of CREATE VIEW
3737 LEX_STRING timestamp{nullptr, 0}; ///< GMT time stamp of last operation
3738 LEX_USER definer; ///< definer of view
3739 /**
3740 @note: This field is currently not reliable when read from dictionary:
3741 If an underlying view is changed, updatable_view is not changed,
3742 due to lack of dependency checking in dictionary implementation.
3743 Prefer to use is_updatable() during preparation and optimization.
3744 */
3745 ulonglong updatable_view{0}; ///< VIEW can be updated
3746 /**
3747 @brief The declared algorithm, if this is a view.
3748 @details One of
3749 - VIEW_ALGORITHM_UNDEFINED
3750 - VIEW_ALGORITHM_TEMPTABLE
3751 - VIEW_ALGORITHM_MERGE
3752 @todo Replace with an enum
3753 */
3755 ulonglong view_suid{0}; ///< view is suid (true by default)
3756 ulonglong with_check{0}; ///< WITH CHECK OPTION
3757
3758 private:
3759 /// The view algorithm that is actually used, if this is a view.
3762
3763 public:
3765
3766 public:
3767 /// True if right argument of LEFT JOIN; false in other cases (i.e. if left
3768 /// argument of LEFT JOIN, if argument of INNER JOIN; RIGHT JOINs are
3769 /// converted to LEFT JOIN during contextualization).
3770 bool outer_join{false};
3771 /// True if was originally the left argument of a RIGHT JOIN, before we
3772 /// made it the right argument of a LEFT JOIN.
3774 uint shared{0}; /* Used in multi-upd */
3775 size_t db_length{0};
3777
3778 private:
3779 /// True if VIEW/TABLE is updatable, based on analysis of query (SQL rules).
3780 bool m_updatable{false};
3781 /// True if VIEW/TABLE is insertable, based on analysis of query (SQL rules).
3782 bool m_insertable{false};
3783 /// True if table is target of UPDATE statement, or updated in IODKU stmt.
3784 bool m_updated{false};
3785 /// True if table is target of INSERT statement.
3786 bool m_inserted{false};
3787 /// True if table is target of DELETE statement, or deleted in REPLACE stmt.
3788 bool m_deleted{false};
3789 bool m_fulltext_searched{false}; ///< True if fulltext searched
3790 public:
3791 bool straight{false}; /* optimize with prev table */
3792 /**
3793 True for tables and views being changed in a data change statement.
3794 Also true for tables subject to a SELECT ... FOR UPDATE.
3795 Also used by replication to filter out statements that can be ignored,
3796 especially important for multi-table UPDATE and DELETE.
3797 */
3798 bool updating{false};
3799 /// preload only non-leaf nodes (IS THIS USED???)
3800 bool ignore_leaves{false};
3801 /**
3802 The set of tables in the query block that this table depends on.
3803 Can be set due to outer join, join order hints or NOT EXISTS relationship.
3804 */
3806 /// The outer tables that an outer join's join condition depends on
3808 /**
3809 Is non-NULL if this table reference is a nested join, ie it represents
3810 the inner tables of an outer join, the tables contained in the
3811 parentheses of an inner join (eliminated during resolving), the tables
3812 referenced in a derived table or view, in a semi-join nest, the tables
3813 from the subquery.
3814 */
3816 /// The nested join containing this table reference.
3818 /// The join list immediately containing this table reference
3820 /// stop PS caching
3821 bool cacheable_table{false};
3822 /**
3823 Specifies which kind of table should be open for this element
3824 of table list.
3825 */
3827 /* true if this merged view contain auto_increment field */
3829 /// true <=> VIEW CHECK OPTION condition is processed (also for prep. stmts)
3831 /// true <=> Filter condition is processed
3833
3835 char timestamp_buffer[20]{0}; /* buffer for timestamp (19+1) */
3836 /*
3837 This Table_ref object is just placeholder for prelocking, it will be
3838 used for implicit LOCK TABLES only and won't be used in real statement.
3839 */
3841 /**
3842 Indicates that if Table_ref object corresponds to the table/view
3843 which requires special handling.
3844 */
3845 enum {
3846 /* Normal open. */
3848 /* Associate a table share only if the the table exists. */
3850 /*
3851 Associate a table share only if the the table exists.
3852 Also upgrade metadata lock to exclusive if table doesn't exist.
3853 */
3855 /* Don't associate a table share. */
3856 OPEN_STUB
3857 } open_strategy{OPEN_NORMAL};
3859 /** true if an alias for this table was specified in the SQL. */
3860 bool is_alias{false};
3861 /** true if the table is referred to in the statement using a fully
3862 qualified name (@<db_name@>.@<table_name@>).
3863 */
3864 bool is_fqtn{false};
3865 /**
3866 If true, this table is a derived (materialized) table which was created
3867 from a scalar subquery, cf.
3868 Query_block::transform_scalar_subqueries_to_join_with_derived
3869 */
3871
3872 /* View creation context. */
3873
3875
3876 /*
3877 Attributes to save/load view creation context in/from frm-file.
3878
3879 They are required only to be able to use existing parser to load
3880 view-definition file. As soon as the parser parsed the file, view
3881 creation context is initialized and the attributes become redundant.
3882
3883 These attributes MUST NOT be used for any purposes but the parsing.
3884 */
3885
3888
3889 /*
3890 View definition (SELECT-statement) in the UTF-form.
3891 */
3892
3894
3895 // True, If this is a system view
3896 bool is_system_view{false};
3897
3898 /*
3899 Set to 'true' if this is a DD table being opened in the context of a
3900 dictionary operation. Note that when 'false', this may still be a DD
3901 table when opened in a non-DD context, e.g. as part of an I_S view
3902 query.
3903 */
3904 bool is_dd_ctx_table{false};
3905
3906 /* End of view definition context. */
3907
3908 /* List of possible keys. Valid only for materialized derived tables/views. */
3910
3911 /**
3912 Indicates what triggers we need to pre-load for this Table_ref
3913 when opening an associated TABLE. This is filled after
3914 the parsed tree is created.
3915 */
3918
3920
3921 /// if true, EXPLAIN can't explain view due to insufficient rights.
3922 bool view_no_explain{false};
3923
3924 /* List to carry partition names from PARTITION (...) clause in statement */
3926
3927 /// Set table number
3929 assert(tableno < MAX_TABLES);
3931 m_map = (table_map)1 << tableno;
3932 }
3933 /// Return table number
3934 uint tableno() const { return m_tableno; }
3935
3936 /// Return table map derived from table number
3937 table_map map() const {
3938 assert(((table_map)1 << m_tableno) == m_map);
3939 return m_map;
3940 }
3941
3942 /// If non-NULL, the CTE which this table is derived from.
3945 /// @see m_derived_column_names
3948 }
3951 }
3952
3953 private:
3954 /*
3955 A group of members set and used only during JOIN::optimize().
3956 */
3957 /**
3958 Optimized copy of m_join_cond (valid for one single
3959 execution). Initialized by Query_block::get_optimizable_conditions().
3960 */
3962
3963 public:
3964 COND_EQUAL *cond_equal{nullptr}; ///< Used with outer join
3965 /// true <=> this table is a const one and was optimized away.
3966
3967 bool optimized_away{false};
3968 /**
3969 true <=> all possible keys for a derived table were collected and
3970 could be re-used while statement re-execution.
3971 */
3972
3974
3975 private:
3976 /// If a recursive reference inside the definition of a CTE.
3978 // End of group for optimization
3979
3980 /** See comments for set_metadata_id() */
3982 /** See comments for TABLE_SHARE::get_table_ref_version() */
3984
3985 /*
3986 All members whose names are suffixed with "_saved" are duplicated in
3987 class TABLE but actually belong in this class. They are saved from class
3988 TABLE when preparing a statement and restored when executing the statement.
3989 They are not required for a regular (non-prepared) statement.
3990 */
3996 bool nullable_saved{false};
4004};
4005
4006/*
4007 Iterator over the fields of a generic table reference.
4008*/
4009
4011 public:
4012 virtual ~Field_iterator() = default;
4013 virtual void set(Table_ref *) = 0;
4014 virtual void next() = 0;
4015 virtual bool end_of_fields() = 0; /* Return 1 at end of list */
4016 virtual const char *name() = 0;
4017 virtual Item *create_item(THD *) = 0;
4018 virtual Field *field() = 0;
4019};
4020
4021/*
4022 Iterator over the fields of a base table, view with temporary
4023 table, or subquery.
4024*/
4025
4028
4029 public:
4031 void set(Table_ref *table) override { ptr = table->table->field; }
4032 void set_table(TABLE *table) { ptr = table->field; }
4033 void next() override { ptr++; }
4034 bool end_of_fields() override { return *ptr == nullptr; }
4035 const char *name() override;
4036 Item *create_item(THD *thd) override;
4037 Field *field() override { return *ptr; }
4038};
4039
4040/**
4041 Iterator over the fields of a merged derived table or view.
4042*/
4043
4047
4048 public:
4050 void set(Table_ref *table) override;
4051 void next() override { ptr++; }
4052 bool end_of_fields() override { return ptr == array_end; }
4053 const char *name() override;
4054 Item *create_item(THD *thd) override;
4055 Item **item_ptr() { return &ptr->item; }
4056 Field *field() override { return nullptr; }
4057 inline Item *item() { return ptr->item; }
4059};
4060
4061/*
4062 Field_iterator interface to the list of materialized fields of a
4063 NATURAL/USING join.
4064*/
4065
4069
4070 public:
4072 ~Field_iterator_natural_join() override = default;
4073 void set(Table_ref *table) override;
4074 void next() override;
4075 bool end_of_fields() override { return !cur_column_ref; }
4076 const char *name() override { return cur_column_ref->name(); }
4077 Item *create_item(THD *thd) override {
4078 return cur_column_ref->create_item(thd);
4079 }
4080 Field *field() override { return cur_column_ref->field(); }
4082};
4083
4084/**
4085 Generic iterator over the fields of an arbitrary table reference.
4086
4087 This class unifies the various ways of iterating over the columns
4088 of a table reference depending on the type of SQL entity it
4089 represents. If such an entity represents a nested table reference,
4090 this iterator encapsulates the iteration over the columns of the
4091 members of the table reference.
4092
4093 The implementation assumes that all underlying NATURAL/USING table
4094 references already contain their result columns and are linked into
4095 the list Table_ref::next_name_resolution_table.
4096*/
4097
4104 void set_field_iterator();
4105
4106 public:
4108 void set(Table_ref *table) override;
4109 void next() override;
4110 bool end_of_fields() override {
4111 return (table_ref == last_leaf && field_it->end_of_fields());
4112 }
4113 const char *name() override { return field_it->name(); }
4114 const char *get_table_name();
4115 const char *get_db_name();
4116 GRANT_INFO *grant();
4117 Item *create_item(THD *thd) override { return field_it->create_item(thd); }
4118 Field *field() override { return field_it->field(); }
4120 Table_ref *parent_table_ref);
4122};
4123
4126 char *db, *table;
4128};
4129
4131 MY_BITMAP *bitmap) {
4132 my_bitmap_map *old = bitmap->bitmap;
4133 bitmap->bitmap = table->s->all_set.bitmap; // does not repoint last_word_ptr
4134 return old;
4135}
4136
4137static inline void tmp_restore_column_map(MY_BITMAP *bitmap,
4138 my_bitmap_map *old) {
4139 bitmap->bitmap = old;
4140}
4141
4142/* The following is only needed for debugging */
4143
4145 [[maybe_unused]],
4146 MY_BITMAP *bitmap
4147 [[maybe_unused]]) {
4148#ifndef NDEBUG
4149 return tmp_use_all_columns(table, bitmap);
4150#else
4151 return nullptr;
4152#endif
4153}
4154
4155static inline void dbug_tmp_restore_column_map(MY_BITMAP *bitmap
4156 [[maybe_unused]],
4157 my_bitmap_map *old
4158 [[maybe_unused]]) {
4159#ifndef NDEBUG
4160 tmp_restore_column_map(bitmap, old);
4161#endif
4162}
4163
4164/*
4165 Variant of the above : handle both read and write sets.
4166 Provide for the possibility of the read set being the same as the write set
4167*/
4168static inline void dbug_tmp_use_all_columns(
4169 TABLE *table [[maybe_unused]], my_bitmap_map **save [[maybe_unused]],
4170 MY_BITMAP *read_set [[maybe_unused]],
4171 MY_BITMAP *write_set [[maybe_unused]]) {
4172#ifndef NDEBUG
4173 save[0] = read_set->bitmap;
4174 save[1] = write_set->bitmap;
4175 (void)tmp_use_all_columns(table, read_set);
4176 (void)tmp_use_all_columns(table, write_set);
4177#endif
4178}
4179
4181 MY_BITMAP *read_set [[maybe_unused]], MY_BITMAP *write_set [[maybe_unused]],
4182 my_bitmap_map **old [[maybe_unused]]) {
4183#ifndef NDEBUG
4184 tmp_restore_column_map(read_set, old[0]);
4185 tmp_restore_column_map(write_set, old[1]);
4186#endif
4187}
4188
4189void init_mdl_requests(Table_ref *table_list);
4190
4191/**
4192 Unpacks the definition of a value generator in all its forms: generated
4193 column, default expression or checked constraint.
4194 The function parses the text definition of this expression, resolves its
4195 items and runs validation and calculates the base_columns_map which is used
4196 for tracking the columns the expression depends on.
4197
4198 @param[in] thd Thread handler
4199 @param[in] table Table having the value generator to be unpacked
4200 @param[in,out] val_generator Contains the expression in string format, and,
4201 if successful will be replaced by the parser
4202 with a new one having the unpacked expression.
4203 @param[in] source Source of value generator(a generated column,
4204 a regular column with generated default value or
4205 a check constraint).
4206 @param[in] source_name Name of the source (generated column, a regular
4207 column with generated default value or a check
4208 constraint).
4209 @param[in] field The column the value generator depends on. Can
4210 be null for checked constraints which do not
4211 depend on a single column.
4212 @param[in] is_create_table Indicates that table is opened as part
4213 of CREATE or ALTER and does not yet exist in SE
4214 @param[out] error_reported updated flag for the caller that no other error
4215 messages are to be generated.
4216
4217 @retval true Failure.
4218 @retval false Success.
4219*/
4220
4222 Value_generator **val_generator,
4224 const char *source_name, Field *field,
4225 bool is_create_table, bool *error_reported);
4226
4227/**
4228 Unpack the partition expression. Parse the partition expression
4229 to produce an Item.
4230
4231 @param[in] thd Thread handler
4232 @param[in] outparam Table object
4233 @param[in] share TABLE_SHARE object
4234 @param[in] engine_type Engine type of the partitions.
4235 @param[in] is_create_table Indicates that table is opened as part of
4236 CREATE or ALTER and does not yet exist in SE
4237
4238 @retval true Failure.
4239 @retval false Success.
4240*/
4241
4242bool unpack_partition_info(THD *thd, TABLE *outparam, TABLE_SHARE *share,
4243 handlerton *engine_type, bool is_create_table);
4244
4245int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias,
4246 uint db_stat, uint prgflag, uint ha_open_flags,
4247 TABLE *outparam, bool is_create_table,
4248 const dd::Table *table_def_param);
4249TABLE_SHARE *alloc_table_share(const char *db, const char *table_name,
4250 const char *key, size_t key_length,
4251 bool open_secondary);
4252void init_tmp_table_share(THD *thd, TABLE_SHARE *share, const char *key,
4253 size_t key_length, const char *table_name,
4254 const char *path, MEM_ROOT *mem_root);
4255void free_table_share(TABLE_SHARE *share);
4257Ident_name_check check_db_name(const char *name, size_t length);
4259 bool preserve_lettercase);
4260bool check_column_name(const char *name);
4261Ident_name_check check_table_name(const char *name, size_t length);
4262int rename_file_ext(const char *from, const char *to, const char *ext);
4263char *get_field(MEM_ROOT *mem, Field *field);
4264bool get_field(MEM_ROOT *mem, Field *field, class String *res);
4265
4266int closefrm(TABLE *table, bool free_share);
4267void free_blobs(TABLE *table);
4269int set_zone(int nr, int min_zone, int max_zone);
4270void append_unescaped(String *res, const char *pos, size_t length);
4271char *fn_rext(char *name);
4273 const LEX_CSTRING &name);
4274
4275/* performance schema */
4277
4280
4281/* information schema */
4283
4284/* mysql schema name and DD ID */
4286static const uint MYSQL_SCHEMA_DD_ID = 1;
4287
4288/* mysql tablespace name and DD ID */
4290static const uint MYSQL_TABLESPACE_DD_ID = 1;
4291
4292/* replication's tables */
4296
4297inline bool is_infoschema_db(const char *name, size_t len) {
4298 return (
4301}
4302
4303inline bool is_infoschema_db(const char *name) {
4305}
4306
4307inline bool is_perfschema_db(const char *name, size_t len) {
4308 return (PERFORMANCE_SCHEMA_DB_NAME.length == len &&
4310 name));
4311}
4312
4313inline bool is_perfschema_db(const char *name) {
4315 name);
4316}
4317
4318/**
4319 Check if the table belongs to the P_S, excluding setup and threads tables.
4320
4321 @note Performance Schema tables must be accessible independently of the
4322 LOCK TABLE mode. This function is needed to handle the special case
4323 of P_S tables being used under LOCK TABLE mode.
4324*/
4325inline bool belongs_to_p_s(Table_ref *tl) {
4326 return (!strcmp("performance_schema", tl->db) &&
4327 strcmp(tl->table_name, "threads") &&
4328 strstr(tl->table_name, "setup_") == nullptr);
4329}
4330
4331/**
4332 return true if the table was created explicitly.
4333*/
4335 const char *name = table->s->table_name.str;
4337}
4338
4339bool is_simple_order(ORDER *order);
4340
4341uint add_pk_parts_to_sk(KEY *sk, uint sk_n, KEY *pk, uint pk_n,
4342 TABLE_SHARE *share, handler *handler_file,
4343 uint *usable_parts, bool use_extended_sk);
4344void setup_key_part_field(TABLE_SHARE *share, handler *handler_file,
4345 uint primary_key_n, KEY *keyinfo, uint key_n,
4346 uint key_part_n, uint *usable_parts,
4347 bool part_of_key_not_extended);
4348
4349const uchar *get_field_name(const uchar *arg, size_t *length);
4350
4351void repoint_field_to_record(TABLE *table, uchar *old_rec, uchar *new_rec);
4354 uint active_index = MAX_KEY);
4355
4356/**
4357 Check if a Table_ref instance represents a pre-opened temporary table.
4358*/
4359
4360inline bool is_temporary_table(const Table_ref *tl) {
4361 if (tl->is_view() || tl->schema_table) return false;
4362
4363 if (!tl->table) return false;
4364
4365 /*
4366 NOTE: 'table->s' might be NULL for specially constructed TABLE
4367 instances. See SHOW TRIGGERS for example.
4368 */
4369
4370 if (!tl->table->s) return false;
4371
4372 return tl->table->s->tmp_table != NO_TMP_TABLE;
4373}
4374
4375/**
4376 After parsing, a Common Table Expression is accessed through a
4377 Table_ref. This class contains all information about the CTE which the
4378 Table_ref needs.
4379
4380 @note that before and during parsing, the CTE is described by a
4381 PT_common_table_expr.
4382*/
4384 public:
4387 TABLE *clone_tmp_table(THD *thd, Table_ref *tl);
4389 /// Remove one table reference.
4390 void remove_table(Table_ref *tr);
4391 /// Empties the materialized CTE and informs all of its clones.
4392 bool clear_all_references();
4393 /**
4394 All references to this CTE in the statement, except those inside the
4395 query expression defining this CTE.
4396 In other words, all non-recursive references.
4397 */
4399 /// True if it's a recursive CTE
4401 /**
4402 List of all TABLE_LISTSs reading/writing to the tmp table created to
4403 materialize this CTE. Due to shared materialization, only the first one
4404 has a TABLE generated by create_tmp_table(); other ones have a TABLE
4405 generated by open_table_from_share().
4406 */
4408 /// Name of the WITH block. Used only for EXPLAIN FORMAT=tree.
4410};
4411
4412/**
4413 This iterates on those references to a derived table / view / CTE which are
4414 materialized. If a recursive CTE, this includes recursive references.
4415 Upon construction it is passed a non-recursive materialized reference
4416 to the derived table (Table_ref*).
4417 For a CTE it may return more than one reference; for a derived table or a
4418 view, there is only one (as references to a same view are treated as
4419 independent objects).
4420 References are returned as TABLE*.
4421*/
4423 Table_ref *const start; ///< The reference provided in construction.
4424 size_t ref_idx{0}; ///< Current index in cte->tmp_tables
4425 bool m_is_first{true}; ///< True when at first reference in list
4426 public:
4427 explicit Derived_refs_iterator(Table_ref *start_arg) : start(start_arg) {}
4430 m_is_first = ref_idx == 0;
4431 // Derived tables and views have a single reference.
4432 if (cte == nullptr) {
4433 return ref_idx++ == 0 ? start->table : nullptr;
4434 }
4435 /*
4436 CTEs may have multiple references. Return the next one, but notice that
4437 some references may have been deleted.
4438 */
4439 while (ref_idx < cte->tmp_tables.size()) {
4440 TABLE *table = cte->tmp_tables[ref_idx++]->table;
4441 if (table != nullptr) return table;
4442 }
4443 return nullptr;
4444 }
4445 void rewind() {
4446 ref_idx = 0;
4447 m_is_first = true;
4448 }
4449 /// @returns true if the last get_next() returned the first element.
4450 bool is_first() const {
4451 // Call after get_next() has been called:
4452 assert(ref_idx > 0);
4453 return m_is_first;
4454 }
4455};
4456
4457/**
4458 RAII class to reset TABLE::autoinc_field_has_explicit_non_null_value after
4459 processing individual row in INSERT or LOAD DATA statements.
4460*/
4462 public:
4464 : m_table(table) {}
4467 }
4468
4469 private:
4471};
4472
4473// Whether we can ask the storage engine for the row ID of the last row read.
4474//
4475// Some operations needs a row ID to operate correctly (i.e. weedout). Normally,
4476// the row ID is provided by the storage engine by calling handler::position().
4477// But there are cases when position() should not be called:
4478//
4479// 1. If we have a const table (rows are fetched during optimization), we
4480// should not call position().
4481// 2. If we have a NULL-complemented row, calling position() would give a
4482// random row ID back, as there has not been any row read.
4483//
4484// Operations that needs the row ID must also check the value of
4485// QEP_TAB::rowid_status to see whether they actually need a row ID.
4486// See QEP_TAB::rowid_status for more details.
4487inline bool can_call_position(const TABLE *table) {
4488 return !table->const_table && !(table->is_nullable() && table->null_row);
4489}
4490
4491//////////////////////////////////////////////////////////////////////////
4492
4493/*
4494 NOTE:
4495 These structures are added to read .frm file in upgrade scenario.
4496
4497 They should not be used any where else in the code.
4498 They will be removed in future release.
4499 Any new code should not be added in this section.
4500*/
4501
4502/**
4503 These members were removed from TABLE_SHARE as they are not used in
4504 in the code. open_binary_frm() uses these members while reading
4505 .frm files.
4506*/
4508 public:
4511 null_field_first(false),
4512 stored_fields(0),
4514 frm_version(0),
4515 fieldnames() {}
4516
4519 uint stored_fields; /* Number of stored fields
4520 (i.e. without generated-only ones) */
4521
4522 enum utype {
4535 EMPTY_VAL, // EMPTY_VAL rather than EMPTY since EMPTY can conflict with
4536 // system headers.
4548 GENERATED_FIELD = 128
4550
4551 /**
4552 For shares representing views File_parser object with view
4553 definition read from .FRM file.
4554 */
4557 TYPELIB fieldnames; /* Pointer to fieldnames */
4558};
4559
4560/**
4561 Create TABLE_SHARE from .frm file.
4562
4563 FRM_context object is used to store the value removed from
4564 TABLE_SHARE. These values are used only for .frm file parsing.
4565
4566 @param[in] thd Thread handle.
4567 @param[in] path Path of the frm file.
4568 @param[out] share TABLE_SHARE to be populated.
4569 @param[out] frm_context FRM_context object.
4570 @param[in] db Database name.
4571 @param[in] table Table name.
4572 @param[in] is_fix_view_cols_and_deps Fix view column data, table
4573 and routine dependency.
4574
4575 @retval 0 ON SUCCESS
4576 @retval -1 ON FAILURE
4577 @retval -2 ON LESS SEVER FAILURE (see read_frm_file)
4578*/
4579int create_table_share_for_upgrade(THD *thd, const char *path,
4580 TABLE_SHARE *share, FRM_context *frm_context,
4581 const char *db, const char *table,
4582 bool is_fix_view_cols_and_deps);
4583//////////////////////////////////////////////////////////////////////////
4584
4585/**
4586 Create a copy of the key_info from TABLE_SHARE object to TABLE object.
4587
4588 Wherever prefix key is present, allocate a new Field object, having its
4589 field_length set to the prefix key length, and point the table's matching
4590 key_part->field to this new Field object.
4591
4592 This ensures that unpack_partition_info() reads the correct prefix length of
4593 partitioned fields
4594
4595 @param table Table for which key_info is to be allocated
4596 @param root MEM_ROOT in which to allocate key_info
4597
4598 @retval false Success
4599 @retval true Failed to allocate memory for table.key_info in root
4600*/
4601
4603
4604#endif /* TABLE_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
int64 query_id_t
Definition: binlog.h:71
Per internal schema ACL access rules.
Definition: auth_common.h:143
Per internal table ACL access rules.
Definition: auth_common.h:105
RAII class to reset TABLE::autoinc_field_has_explicit_non_null_value after processing individual row ...
Definition: table.h:4461
Autoinc_field_has_explicit_non_null_value_reset_guard(TABLE *table)
Definition: table.h:4463
~Autoinc_field_has_explicit_non_null_value_reset_guard()
Definition: table.h:4465
Class that represents a single change to a column value in partial update of a JSON column.
Definition: table.h:1313
const char * old_data(const Field *field) const
Get a pointer to the start of the old data to be replaced.
Definition: table.cc:7715
size_t length() const
Definition: table.h:1334
size_t m_length
The size of the portion that is to be replaced.
Definition: table.h:1318
const char * new_data(const Field *field) const
Get a pointer to the start of the replacement data.
Definition: table.cc:7706
Binary_diff(size_t offset, size_t length)
Create a new Binary_diff object.
Definition: table.h:1327
size_t m_offset
The offset of the start of the change.
Definition: table.h:1315
size_t offset() const
Definition: table.h:1331
Definition: sql_bitmap.h:153
Class is used as a BLOB field value storage for intermediate GROUP_CONCAT results.
Definition: table.h:1273
bool truncated_value
Sign that some values were cut during saving into the storage.
Definition: table.h:1280
Blob_mem_storage()
Definition: table.cc:4090
void reset()
Definition: table.h:1286
void set_truncated_value(bool is_truncated_value)
Definition: table.h:1303
MEM_ROOT storage
Definition: table.h:1275
~Blob_mem_storage()
Definition: table.cc:4094
bool is_truncated_value() const
Definition: table.h:1306
char * store(const char *from, size_t length)
Function creates duplicate of 'from' string in 'storage' MEM_ROOT.
Definition: table.h:1300
Definition: item_cmpfunc.h:2703
After parsing, a Common Table Expression is accessed through a Table_ref.
Definition: table.h:4383
Common_table_expr(MEM_ROOT *mem_root)
Definition: table.h:4385
LEX_STRING name
Name of the WITH block. Used only for EXPLAIN FORMAT=tree.
Definition: table.h:4409
TABLE * clone_tmp_table(THD *thd, Table_ref *tl)
Produces, from the first tmp TABLE object, a clone TABLE object for Table_ref 'tl',...
Definition: sql_derived.cc:169
bool clear_all_references()
Empties the materialized CTE and informs all of its clones.
Definition: sql_union.cc:1592
bool recursive
True if it's a recursive CTE.
Definition: table.h:4400
Mem_root_array< Table_ref * > references
All references to this CTE in the statement, except those inside the query expression defining this C...
Definition: table.h:4398
void remove_table(Table_ref *tr)
Remove one table reference.
Definition: sql_derived.cc:252
bool substitute_recursive_reference(THD *thd, Query_block *sl)
Replaces the recursive reference in query block 'sl' with a clone of the first tmp table.
Definition: sql_derived.cc:239
Mem_root_array< Table_ref * > tmp_tables
List of all TABLE_LISTSs reading/writing to the tmp table created to materialize this CTE.
Definition: table.h:4407
API for getting cost estimates for server operations that are not directly related to a table object.
Definition: opt_costmodel.h:53
API for getting cost estimates for operations on table data.
Definition: opt_costmodel.h:241
void init(const Cost_model_server *cost_model_server, const TABLE *table)
Initializes the cost model object.
Definition: opt_costmodel.cc:67
Default_object_creation_ctx – default implementation of Object_creation_ctx.
Definition: table.h:222
const CHARSET_INFO * get_client_cs()
Definition: table.h:224
void change_env(THD *thd) const override
Definition: table.cc:218
void delete_backup_ctx() override
Definition: table.cc:216
Default_object_creation_ctx(THD *thd)
Definition: table.cc:203
const CHARSET_INFO * get_connection_cl()
Definition: table.h:226
Object_creation_ctx * create_backup_ctx(THD *thd) const override
Definition: table.cc:211
const CHARSET_INFO * m_connection_cl
connection_cl stores the value of collation_connection session variable.
Definition: table.h:260
const CHARSET_INFO * m_client_cs
client_cs stores the value of character_set_client session variable.
Definition: table.h:250
This structure is used to keep info about possible key for the result table of a derived table/view.
Definition: table.h:2804
table_map referenced_by
Definition: table.h:2806
Field_map used_fields
Definition: table.h:2807
uint key_part_count