-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsql_lex.h
5200 lines (4373 loc) · 172 KB
/
sql_lex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2000, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@defgroup GROUP_PARSER Parser
@{
*/
#ifndef SQL_LEX_INCLUDED
#define SQL_LEX_INCLUDED
#include <string.h>
#include <sys/types.h> // TODO: replace with cstdint
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <functional>
#include <map>
#include <memory>
#include <new>
#include <string>
#include <utility>
#include "lex_string.h"
#include "map_helpers.h"
#include "mem_root_deque.h"
#include "memory_debugging.h"
#include "my_alloc.h" // Destroy_only
#include "my_base.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h" // TODO: replace with cstdint
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "my_table_map.h"
#include "my_thread_local.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/service_mysql_alloc.h" // my_free
#include "mysql/strings/m_ctype.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "prealloced_array.h" // Prealloced_array
#include "sql/dd/info_schema/table_stats.h" // dd::info_schema::Table_stati...
#include "sql/dd/info_schema/tablespace_stats.h" // dd::info_schema::Tablesp...
#include "sql/enum_query_type.h"
#include "sql/handler.h"
#include "sql/item.h" // Name_resolution_context
#include "sql/item_subselect.h" // Subquery_strategy
#include "sql/iterators/row_iterator.h"
#include "sql/join_optimizer/access_path.h"
#include "sql/join_optimizer/materialize_path_parameters.h"
#include "sql/key_spec.h" // KEY_CREATE_INFO
#include "sql/mdl.h"
#include "sql/mem_root_array.h" // Mem_root_array
#include "sql/parse_location.h"
#include "sql/parse_tree_node_base.h" // enum_parsing_context
#include "sql/parser_yystype.h"
#include "sql/query_options.h" // OPTION_NO_CONST_TABLES
#include "sql/query_term.h"
#include "sql/set_var.h"
#include "sql/sql_array.h"
#include "sql/sql_connect.h" // USER_RESOURCES
#include "sql/sql_const.h"
#include "sql/sql_data_change.h" // enum_duplicates
#include "sql/sql_error.h" // warn_on_deprecated_charset
#include "sql/sql_list.h"
#include "sql/sql_plugin_ref.h"
#include "sql/sql_servers.h" // Server_options
#include "sql/sql_udf.h" // Item_udftype
#include "sql/table.h" // Table_ref
#include "sql/thr_malloc.h"
#include "sql/trigger_def.h" // enum_trigger_action_time_type
#include "sql/visible_fields.h"
#include "sql_string.h"
#include "string_with_len.h"
#include "strings/sql_chars.h"
#include "thr_lock.h" // thr_lock_type
#include "violite.h" // SSL_type
class Alter_info;
class Event_parse_data;
class Field;
class Item_cond;
class Item_func_get_system_var;
class Item_func_match;
class Item_func_set_user_var;
class Item_rollup_group_item;
class Item_rollup_sum_switcher;
class Item_sum;
class JOIN;
class Opt_hints_global;
class Opt_hints_qb;
class PT_subquery;
class PT_with_clause;
class Parse_tree_root;
class Protocol;
class Query_result;
class Query_result_interceptor;
class Query_result_union;
class Query_block;
class Query_expression;
class Select_lex_visitor;
class Sql_cmd;
class THD;
class Value_generator;
class Window;
class partition_info;
class sp_head;
class sp_name;
class sp_pcontext;
struct LEX;
struct NESTED_JOIN;
struct PSI_digest_locker;
struct sql_digest_state;
union Lexer_yystype;
struct Lifted_expressions_map;
const size_t INITIAL_LEX_PLUGIN_LIST_SIZE = 16;
constexpr const int MAX_SELECT_NESTING{sizeof(nesting_map) * 8 - 1};
/*
There are 8 different type of table access so there is no more than
combinations 2^8 = 256:
. STMT_READS_TRANS_TABLE
. STMT_READS_NON_TRANS_TABLE
. STMT_READS_TEMP_TRANS_TABLE
. STMT_READS_TEMP_NON_TRANS_TABLE
. STMT_WRITES_TRANS_TABLE
. STMT_WRITES_NON_TRANS_TABLE
. STMT_WRITES_TEMP_TRANS_TABLE
. STMT_WRITES_TEMP_NON_TRANS_TABLE
The unsafe conditions for each combination is represented within a byte
and stores the status of the option --binlog-direct-non-trans-updates,
whether the trx-cache is empty or not, and whether the isolation level
is lower than ISO_REPEATABLE_READ:
. option (OFF/ON)
. trx-cache (empty/not empty)
. isolation (>= ISO_REPEATABLE_READ / < ISO_REPEATABLE_READ)
bits 0 : . OFF, . empty, . >= ISO_REPEATABLE_READ
bits 1 : . OFF, . empty, . < ISO_REPEATABLE_READ
bits 2 : . OFF, . not empty, . >= ISO_REPEATABLE_READ
bits 3 : . OFF, . not empty, . < ISO_REPEATABLE_READ
bits 4 : . ON, . empty, . >= ISO_REPEATABLE_READ
bits 5 : . ON, . empty, . < ISO_REPEATABLE_READ
bits 6 : . ON, . not empty, . >= ISO_REPEATABLE_READ
bits 7 : . ON, . not empty, . < ISO_REPEATABLE_READ
*/
extern uint binlog_unsafe_map[256];
/*
Initializes the array with unsafe combinations and its respective
conditions.
*/
void binlog_unsafe_map_init();
/*
If we encounter a diagnostics statement (GET DIAGNOSTICS, or e.g.
the old SHOW WARNINGS|ERRORS, or "diagnostics variables" such as
@@warning_count | @@error_count, we'll set some hints so this
information is not lost. DA_KEEP_UNSPECIFIED is used in LEX constructor to
avoid leaving variables uninitialized.
*/
enum enum_keep_diagnostics {
DA_KEEP_NOTHING = 0, /**< keep nothing */
DA_KEEP_DIAGNOSTICS, /**< keep the diagnostics area */
DA_KEEP_COUNTS, /**< keep \@warning_count / \@error_count */
DA_KEEP_PARSE_ERROR, /**< keep diagnostics area after parse error */
DA_KEEP_UNSPECIFIED /**< keep semantics is unspecified */
};
enum enum_sp_suid_behaviour {
SP_IS_DEFAULT_SUID = 0,
SP_IS_NOT_SUID,
SP_IS_SUID
};
enum enum_sp_data_access {
SP_DEFAULT_ACCESS = 0,
SP_CONTAINS_SQL,
SP_NO_SQL,
SP_READS_SQL_DATA,
SP_MODIFIES_SQL_DATA
};
/**
enum_sp_type defines type codes of stored programs.
@note these codes are used when dealing with the mysql.routines system table,
so they must not be changed.
@note the following macros were used previously for the same purpose. Now they
are used for ACL only.
*/
enum class enum_sp_type {
FUNCTION = 1,
PROCEDURE,
TRIGGER,
EVENT,
LIBRARY,
/*
Must always be the last one.
Denotes an error condition.
*/
INVALID_SP_TYPE
};
inline enum_sp_type to_sp_type(longlong val) {
if (val >= static_cast<longlong>(enum_sp_type::FUNCTION) &&
val < static_cast<longlong>(enum_sp_type::INVALID_SP_TYPE))
return static_cast<enum_sp_type>(val);
else
return enum_sp_type::INVALID_SP_TYPE;
}
inline longlong to_longlong(enum_sp_type val) {
return static_cast<longlong>(val);
}
inline uint to_uint(enum_sp_type val) { return static_cast<uint>(val); }
/*
Values for the type enum. This reflects the order of the enum declaration
in the CREATE TABLE command. These values are used to enumerate object types
for the ACL statements.
These values were also used for enumerating stored program types. However, now
enum_sp_type should be used for that instead of them.
*/
#define TYPE_ENUM_FUNCTION 1
#define TYPE_ENUM_PROCEDURE 2
#define TYPE_ENUM_TRIGGER 3
#define TYPE_ENUM_PROXY 4
#define TYPE_ENUM_LIBRARY 5
#define TYPE_ENUM_INVALID 6
enum class Acl_type {
TABLE = 0,
FUNCTION = TYPE_ENUM_FUNCTION,
PROCEDURE = TYPE_ENUM_PROCEDURE,
LIBRARY = TYPE_ENUM_LIBRARY,
INVALID_TYPE = TYPE_ENUM_INVALID
};
Acl_type lex_type_to_acl_type(ulong lex_type);
enum_sp_type acl_type_to_enum_sp_type(Acl_type type);
Acl_type enum_sp_type_to_acl_type(enum_sp_type type);
const LEX_CSTRING sp_data_access_name[] = {
{STRING_WITH_LEN("")},
{STRING_WITH_LEN("CONTAINS SQL")},
{STRING_WITH_LEN("NO SQL")},
{STRING_WITH_LEN("READS SQL DATA")},
{STRING_WITH_LEN("MODIFIES SQL DATA")}};
enum class enum_view_create_mode {
VIEW_CREATE_NEW, // check that there are not such VIEW/table
VIEW_ALTER, // check that VIEW with such name exists
VIEW_CREATE_OR_REPLACE // check only that there are not such table
};
enum class enum_alter_user_attribute {
ALTER_USER_COMMENT_NOT_USED, // No user metadata ALTER in the AST
ALTER_USER_COMMENT, // A text comment is expected
ALTER_USER_ATTRIBUTE // A JSON object is expected
};
/* Options to add_table_to_list() */
#define TL_OPTION_UPDATING 0x01
#define TL_OPTION_IGNORE_LEAVES 0x02
#define TL_OPTION_ALIAS 0x04
/* Structure for db & table in sql_yacc */
class Table_function;
class Table_ident {
public:
LEX_CSTRING db;
LEX_CSTRING table;
Query_expression *sel;
Table_function *table_function;
Table_ident(Protocol *protocol, const LEX_CSTRING &db_arg,
const LEX_CSTRING &table_arg, bool force);
Table_ident(const LEX_CSTRING &db_arg, const LEX_CSTRING &table_arg)
: db(db_arg), table(table_arg), sel(nullptr), table_function(nullptr) {}
Table_ident(const LEX_CSTRING &table_arg)
: table(table_arg), sel(nullptr), table_function(nullptr) {
db = NULL_CSTR;
}
/**
This constructor is used only for the case when we create a derived
table. A derived table has no name and doesn't belong to any database.
Later, if there was an alias specified for the table, it will be set
by add_table_to_list.
*/
Table_ident(Query_expression *s) : sel(s), table_function(nullptr) {
db = EMPTY_CSTR; /* a subject to casedn_str */
table = EMPTY_CSTR;
}
/*
This constructor is used only for the case when we create a table function.
It has no name and doesn't belong to any database as it exists only
during query execution. Later, if there was an alias specified for the
table, it will be set by add_table_to_list.
*/
Table_ident(LEX_CSTRING &table_arg, Table_function *table_func_arg)
: table(table_arg), sel(nullptr), table_function(table_func_arg) {
/* We must have a table name here as this is used with add_table_to_list */
db = EMPTY_CSTR; /* a subject to casedn_str */
}
// True if we can tell from syntax that this is a table function.
bool is_table_function() const { return (table_function != nullptr); }
// True if we can tell from syntax that this is an unnamed derived table.
bool is_derived_table() const { return sel; }
void change_db(const char *db_name) {
db.str = db_name;
db.length = strlen(db_name);
}
};
using List_item = mem_root_deque<Item *>;
using Group_list_ptrs = Mem_root_array<ORDER *>;
/**
Structure to hold parameters for CHANGE REPLICATION SOURCE, START REPLICA, and
STOP REPLICA.
Remark: this should not be confused with Master_info (and perhaps
would better be renamed to st_lex_replication_info). Some fields,
e.g., delay, are saved in Relay_log_info, not in Master_info.
*/
struct LEX_SOURCE_INFO {
/*
The array of IGNORE_SERVER_IDS has a preallocation, and is not expected
to grow to any significant size, so no instrumentation.
*/
LEX_SOURCE_INFO() : repl_ignore_server_ids(PSI_NOT_INSTRUMENTED) {
initialize();
}
char *host, *user, *password, *log_file_name, *bind_addr, *network_namespace;
uint port, connect_retry;
float heartbeat_period;
int sql_delay;
ulonglong pos;
ulong server_id, retry_count;
char *gtid;
char *view_id;
const char *channel; // identifier similar to database name
enum {
UNTIL_SQL_BEFORE_GTIDS = 0,
UNTIL_SQL_AFTER_GTIDS
} gtid_until_condition;
bool until_after_gaps;
bool replica_until;
bool for_channel;
/*
Enum is used for making it possible to detect if the user
changed variable or if it should be left at old value
*/
enum {
LEX_MI_UNCHANGED = 0,
LEX_MI_DISABLE,
LEX_MI_ENABLE
} ssl,
ssl_verify_server_cert, heartbeat_opt, repl_ignore_server_ids_opt,
retry_count_opt, auto_position, port_opt, get_public_key,
m_source_connection_auto_failover, m_gtid_only;
char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
char *ssl_crl, *ssl_crlpath, *tls_version;
/*
Ciphersuites used for TLS 1.3 communication with the master server.
*/
enum enum_tls_ciphersuites {
UNSPECIFIED = 0,
SPECIFIED_NULL,
SPECIFIED_STRING
};
enum enum_tls_ciphersuites tls_ciphersuites;
char *tls_ciphersuites_string;
char *public_key_path;
char *relay_log_name;
ulong relay_log_pos;
char *compression_algorithm;
uint zstd_compression_level;
Prealloced_array<ulong, 2> repl_ignore_server_ids;
/**
Flag that is set to `true` whenever `PRIVILEGE_CHECKS_USER` is set to `NULL`
as a part of a `CHANGE REPLICATION SOURCE TO` statement.
*/
bool privilege_checks_none;
/**
Username and hostname parts of the `PRIVILEGE_CHECKS_USER`, when it's set to
a user.
*/
const char *privilege_checks_username, *privilege_checks_hostname;
/**
Flag indicating if row format should be enforced for this channel event
stream.
*/
int require_row_format;
/**
Identifies what is the slave policy on primary keys in tables.
If set to STREAM it just replicates the value of sql_require_primary_key.
If set to ON it fails when the source tries to replicate a table creation
or alter operation that does not have a primary key.
If set to OFF it does not enforce any policies on the channel for primary
keys.
*/
enum {
LEX_MI_PK_CHECK_UNCHANGED = 0,
LEX_MI_PK_CHECK_STREAM = 1,
LEX_MI_PK_CHECK_ON = 2,
LEX_MI_PK_CHECK_OFF = 3,
LEX_MI_PK_CHECK_GENERATE = 4
} require_table_primary_key_check;
enum {
LEX_MI_ANONYMOUS_TO_GTID_UNCHANGED = 0,
LEX_MI_ANONYMOUS_TO_GTID_OFF,
LEX_MI_ANONYMOUS_TO_GTID_LOCAL,
LEX_MI_ANONYMOUS_TO_GTID_UUID
} assign_gtids_to_anonymous_transactions_type;
const char *assign_gtids_to_anonymous_transactions_manual_uuid{nullptr};
/// Initializes everything to zero/NULL/empty.
void initialize();
/// Sets all fields to their "unspecified" value.
void set_unspecified();
private:
// Not copyable or assignable.
LEX_SOURCE_INFO(const LEX_SOURCE_INFO &);
LEX_SOURCE_INFO &operator=(const LEX_SOURCE_INFO &);
};
struct LEX_RESET_REPLICA {
bool all;
};
enum sub_select_type {
UNSPECIFIED_TYPE,
GLOBAL_OPTIONS_TYPE,
DERIVED_TABLE_TYPE
};
/*
String names used to print a statement with index hints.
Keep in sync with index_hint_type.
*/
extern const char *index_hint_type_name[];
typedef uchar index_clause_map;
/*
Bits in index_clause_map : one for each possible FOR clause in
USE/FORCE/IGNORE INDEX index hint specification
*/
#define INDEX_HINT_MASK_JOIN (1)
#define INDEX_HINT_MASK_GROUP (1 << 1)
#define INDEX_HINT_MASK_ORDER (1 << 2)
#define INDEX_HINT_MASK_ALL \
(INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | INDEX_HINT_MASK_ORDER)
/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
class Index_hint {
public:
/* The type of the hint : USE/FORCE/IGNORE */
enum index_hint_type type;
/* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
index_clause_map clause;
/*
The index name. Empty (str=NULL) name represents an empty list
USE INDEX () clause
*/
LEX_CSTRING key_name;
Index_hint(const char *str, uint length) {
key_name.str = str;
key_name.length = length;
}
void print(const THD *thd, String *str);
};
/*
Class Query_expression represents a query expression.
Class Query_block represents a query block.
In addition to what is explained below, the query block(s) of a query
expression is contained in a tree expressing the nesting of set operations,
cf. query_term.h
A query expression contains one or more query blocks (more than one means
that the query expression contains one or more set operations - UNION,
INTERSECT or EXCEPT - unless the query blocks are used to describe
subqueries). These classes are connected as follows: both classes have a
master, a slave, a next and a prev field. For class Query_block, master and
slave connect to objects of type Query_expression, whereas for class
Query_expression, they connect to Query_block. master is pointer to outer
node. slave is pointer to the first inner node.
neighbors are two Query_block or Query_expression objects on
the same level.
The structures are linked with the following pointers:
- list of neighbors (next/prev) (prev of first element point to slave
pointer of outer structure)
- For Query_block, this is a list of query blocks.
- For Query_expression, this is a list of subqueries.
- pointer to outer node (master), which is
If this is Query_expression
- pointer to outer query_block.
If this is Query_block
- pointer to outer Query_expression.
- pointer to inner objects (slave), which is either:
If this is an Query_expression:
- first query block that belong to this query expression.
If this is an Query_block
- first query expression that belong to this query block (subqueries).
- list of all Query_block objects (link_next/link_prev)
This is to be used for things like derived tables creation, where we
go through this list and create the derived tables.
In addition to the above mentioned link, the query's tree structure is
represented by the member m_query_term, see query_term.h
For example for following query:
select *
from table1
where table1.field IN (select * from table1_1_1 union
select * from table1_1_2)
union
select *
from table2
where table2.field=(select (select f1 from table2_1_1_1_1
where table2_1_1_1_1.f2=table2_1_1.f3)
from table2_1_1
where table2_1_1.f1=table2.f2)
union
select * from table3;
we will have following structure:
select1: (select * from table1 ...)
select2: (select * from table2 ...)
select3: (select * from table3)
select1.1.1: (select * from table1_1_1)
...
main unit
select1 select2 select3
|^^ |^
s||| ||master
l||| |+---------------------------------+
a||| +---------------------------------+|
v|||master slave ||
e||+-------------------------+ ||
V| neighbor | V|
unit1.1<+==================>unit1.2 unit2.1
select1.1.1 select 1.1.2 select1.2.1 select2.1.1
|^
||
V|
unit2.1.1.1
select2.1.1.1.1
relation in main unit will be following:
(bigger picture for:
main unit
select1 select2 select3
in the above picture)
main unit
|^^^
||||
||||
|||+------------------------------+
||+--------------+ |
slave||master | |
V| neighbor | neighbor |
select1<========>select2<========>select3
list of all query_block will be following (as it will be constructed by
parser):
select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
|
+---------------------------------------------------------------------+
|
+->select1.1.1->select1.1.2
*/
/**
This class represents a query expression (one query block or
several query blocks combined with UNION).
*/
class Query_expression {
/**
Intrusive double-linked list of all query expressions
immediately contained within the same query block.
*/
Query_expression *next;
Query_expression **prev;
/**
The query block wherein this query expression is contained,
NULL if the query block is the outer-most one.
*/
Query_block *master;
/// The first query block in this query expression.
Query_block *slave;
// The query set operation structure, see doc for Query_term.
Query_term *m_query_term{nullptr};
public:
/// Getter for m_query_term, q.v.
Query_term *query_term() const { return m_query_term; }
/// Setter for m_query_term, q.v.
void set_query_term(Query_term *qt) { m_query_term = qt; }
/// Convenience method to avoid down casting, i.e. interpret m_query_term
/// as a Query_term_set_op.
/// @retval a non-null node iff !is_simple
/// @retval nullptr if is_simple() holds.
Query_term_set_op *set_operation() const {
return is_simple() ? nullptr : down_cast<Query_term_set_op *>(m_query_term);
}
/// Return the query block iff !is_simple() holds
Query_block *non_simple_result_query_block() const {
if (is_simple())
return nullptr;
else
return m_query_term->query_block();
}
bool is_leaf_block(Query_block *qb);
Query_term *find_blocks_query_term(const Query_block *qb) const {
for (auto qt : query_terms<>()) {
if (qt->query_block() == qb) return qt;
}
return nullptr;
}
/**
Return iterator object over query terms rooted in m_query_term,
using either post order visiting (default) or pre order,
optionally skipping leaf nodes (query blocks corresponding to SELECTs or
table constructors). By default, we visit all nodes.
Usage: for (auto qt : query_terms<..>() { ... }
E.g.
for (auto qt : query_terms<>()) { } Visit all nodes, post order
for (auto qt : query_terms<QTC_PRE_ORDER, false>()) { }
Skip leaves, pre order
@tparam order == QTC_POST_ORDER if post order traversal is desired;default
== QTC_PRE_ORDER pre-order traversal
@tparam visit_leaves == VL_VISIT_LEAVES: if we want the traversal to include
leaf nodes i.e. the SELECTs or table constructors
== VL_SKIP_LEAVES: leaves will be skipped
@returns iterator object
*/
template <Visit_order order = QTC_POST_ORDER,
Visit_leaves visit_leaves = VL_VISIT_LEAVES>
Query_terms<order, visit_leaves> query_terms() const {
return Query_terms<order, visit_leaves>(m_query_term);
}
/**
Return the Query_block of the last query term in a n-ary set
operation that is the right side of the last DISTINCT set operation in that
n_ary set operation:
E.e. for
A UNION B UNION ALL C,
B's block will be returned. If no DISTINCT is present or not a set
operation, return nullptr.
@returns query block of last distinct right operand
*/
Query_block *last_distinct() const {
auto const setop = down_cast<Query_term_set_op *>(m_query_term);
if (setop->last_distinct() > 0)
return setop->child(setop->last_distinct())->query_block();
else
return nullptr;
}
bool has_top_level_distinct() const {
if (is_simple()) return false;
return down_cast<Query_term_set_op *>(m_query_term)->last_distinct() > 0;
}
private:
/**
Marker for subqueries in WHERE, HAVING, ORDER BY, GROUP BY and
SELECT item lists.
Must be read/written when holding LOCK_query_plan.
See Item_subselect::explain_subquery_checker
*/
enum_parsing_context explain_marker;
bool prepared; ///< All query blocks in query expression are prepared
bool optimized; ///< All query blocks in query expression are optimized
bool executed; ///< Query expression has been executed
/// Object to which the result for this query expression is sent.
/// Not used if we materialize directly into a parent query expression's
/// result table (see optimize()).
Query_result *m_query_result;
/**
An iterator you can read from to get all records for this query.
May be nullptr even after create_access_paths(), or in the case of an
unfinished materialization (see optimize()).
*/
unique_ptr_destroy_only<RowIterator> m_root_iterator;
AccessPath *m_root_access_path = nullptr;
/**
If there is an unfinished materialization (see optimize()),
contains one element for each operand (query block) in this query
expression.
*/
Mem_root_array<MaterializePathParameters::Operand> m_operands;
private:
/**
Convert the executor structures to a set of access paths, storing the result
in m_root_access_path.
*/
void create_access_paths(THD *thd);
public:
/**
result of this query can't be cached, bit field, can be :
UNCACHEABLE_DEPENDENT
UNCACHEABLE_RAND
UNCACHEABLE_SIDEEFFECT
*/
uint8 uncacheable;
explicit Query_expression(enum_parsing_context parsing_context);
/// @return true for a query expression without UNION/INTERSECT/EXCEPT or
/// multi-level ORDER, i.e. we have a "simple table".
bool is_simple() const { return m_query_term->term_type() == QT_QUERY_BLOCK; }
/// Values for Query_expression::cleaned
enum enum_clean_state {
UC_DIRTY, ///< Unit isn't cleaned
UC_PART_CLEAN, ///< Unit were cleaned, except JOIN and JOIN_TABs were
///< kept for possible EXPLAIN
UC_CLEAN ///< Unit completely cleaned, all underlying JOINs were
///< freed
};
enum_clean_state cleaned; ///< cleanliness state
public:
/**
Return the query block holding the top level ORDER BY, LIMIT and OFFSET.
If the query is not a set operation (UNION, INTERSECT or EXCEPT, and the
query expression has no multi-level ORDER BY/LIMIT, this represents the
single query block of the query itself, cf. documentation for class
Query_term.
@return query block containing the global parameters
*/
inline Query_block *global_parameters() const {
return query_term()->query_block();
}
/* LIMIT clause runtime counters */
ha_rows select_limit_cnt, offset_limit_cnt;
/* For IN/EXISTS predicates, we may not push down LIMIT 1 safely if true*/
bool m_contains_except_all{false};
/// Points to subquery if this query expression is used in one, otherwise NULL
Item_subselect *item;
/**
The WITH clause which is the first part of this query expression. NULL if
none.
*/
PT_with_clause *m_with_clause;
/**
If this query expression is underlying of a derived table, the derived
table. NULL if none.
*/
Table_ref *derived_table;
/**
First query block (in this UNION) which references the CTE.
NULL if not the query expression of a recursive CTE.
*/
Query_block *first_recursive;
/**
If 'this' is body of lateral derived table:
map of tables in the same FROM clause as this derived table, and to which
the derived table's body makes references.
In pre-resolution stages, this is OUTER_REF_TABLE_BIT, just to indicate
that this has LATERAL; after resolution, which has found references in the
body, this is the proper map (with no PSEUDO_TABLE_BITS anymore).
*/
table_map m_lateral_deps;
/**
This query expression represents a scalar subquery and we need a run-time
check that the cardinality doesn't exceed 1.
*/
bool m_reject_multiple_rows{false};
/// @return true if query expression can be merged into an outer query
bool is_mergeable() const;
/// @return true if query expression is recommended to be merged
bool merge_heuristic(const LEX *lex) const;
/// @return the query block this query expression belongs to as subquery
Query_block *outer_query_block() const { return master; }
/// @return the first query block inside this query expression
Query_block *first_query_block() const { return slave; }
/// @return the next query expression within same query block (next subquery)
Query_expression *next_query_expression() const { return next; }
/// @return the query result object in use for this query expression
Query_result *query_result() const { return m_query_result; }
RowIterator *root_iterator() const { return m_root_iterator.get(); }
unique_ptr_destroy_only<RowIterator> release_root_iterator() {
return std::move(m_root_iterator);
}
AccessPath *root_access_path() const { return m_root_access_path; }
// Asks each query block to switch to an access path with in2exists
// conditions removed (if they were ever added).
// See JOIN::change_to_access_path_without_in2exists().
void change_to_access_path_without_in2exists(THD *thd);
void clear_root_access_path() {
m_root_access_path = nullptr;
m_root_iterator.reset();
}
/**
Ensures that there are iterators created for the access paths created
by optimize(), even if it is not a top-level Query_expression.
If there are already iterators, it is a no-op. optimize() must have
been called earlier.
The use case for this is if we have a query block that's not top-level,
but we figure out after the fact that we wanted to run it anyway.
The typical case would be that we notice that the query block can return
at most one row (a so-called const table), and want to run it during
optimization.
*/
bool force_create_iterators(THD *thd);
/**
Creates iterators for the access paths created by optimize(). Usually called
on a top-level Query_expression, but can also be called on non-top level
expressions from force_create_iterators(). See force_create_iterators() for
details.
*/
bool create_iterators(THD *thd);
/// See optimize().
bool unfinished_materialization() const { return !m_operands.empty(); }
/// See optimize().
Mem_root_array<MaterializePathParameters::Operand>
release_query_blocks_to_materialize() {
return std::move(m_operands);
}
/// Set new query result object for this query expression
void set_query_result(Query_result *res) { m_query_result = res; }
/**
Whether there is a chance that optimize() is capable of materializing
directly into a result table if given one. Note that even if this function
returns true, optimize() can choose later not to do so, since it depends
on information (in particular, whether the query blocks can run under
the iterator executor or not) that is not available before optimize time.
TODO(sgunders): Now that all query blocks can run under the iterator
executor, the above may no longer be true. This needs investigation.
*/
bool can_materialize_directly_into_result() const;
bool prepare(THD *thd, Query_result *result,
mem_root_deque<Item *> *insert_field_list,
ulonglong added_options, ulonglong removed_options);
/**
If and only if materialize_destination is non-nullptr, it means that the
caller intends to materialize our result into the given table. If it is
advantageous (in particular, if this query expression is a UNION DISTINCT),
optimize() will not create an iterator by itself, but rather do an
unfinished materialize. This means that it will collect iterators for
all the query blocks and prepare them for materializing into the given
table, but not actually create a root iterator for this query expression;
the caller is responsible for calling release_query_blocks_to_materialize()
and creating the iterator itself.
Even if materialize_destination is non-nullptr, this function may choose
to make a regular iterator. The caller is responsible for checking
unfinished_materialization() if it has given a non-nullptr table.
@param thd Thread handle.
@param materialize_destination What table to try to materialize into,
or nullptr if the caller does not intend to materialize the result.
@param finalize_access_paths Relevant for the hypergraph optimizer only.
If false, the given access paths will _not_ be finalized, so you cannot
create iterators from it before finalize() is called (see
FinalizePlanForQueryBlock()), and create_iterators must also be false.
This is relevant only if you are potentially optimizing multiple times
(see change_to_access_path_without_in2exists()), since you are only
allowed to finalize a query block once. "Fake" query blocks (see
query_term.h) are always finalized.
*/
bool optimize(THD *thd, TABLE *materialize_destination,
bool finalize_access_paths);
/**
For any non-finalized query block, finalize it so that we are allowed to
create iterators. Must be called after the final access path is chosen
(ie., after any calls to change_to_access_path_without_in2exists()).
*/
bool finalize(THD *thd);
#ifndef NDEBUG
void DebugPrintQueryPlan(THD *thd, const char *keyword) const;
#endif
/**
Do everything that would be needed before running Init() on the root
iterator. In particular, clear out data from previous execution iterations,
if needed.
*/
bool ClearForExecution();
bool ExecuteIteratorQuery(THD *thd);
bool execute(THD *thd);
bool explain(THD *explain_thd, const THD *query_thd);
bool explain_query_term(THD *explain_thd, const THD *query_thd,
Query_term *qt);
void cleanup(bool full);
/**
Destroy contained objects, in particular temporary tables which may
have their own mem_roots.
*/
void destroy();
void print(const THD *thd, String *str, enum_query_type query_type);
bool accept(Select_lex_visitor *visitor);