-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathparse_tree_nodes.h
5745 lines (4576 loc) · 180 KB
/
parse_tree_nodes.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) 2013, 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 */
#ifndef PARSE_TREE_NODES_INCLUDED
#define PARSE_TREE_NODES_INCLUDED
#include <assert.h>
#include <sys/types.h> // TODO: replace with cstdint
#include <bit>
#include <cctype> // std::isspace
#include <cstddef>
#include <memory>
#include "lex_string.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_inttypes.h" // TODO: replace with cstdint
#include "my_list.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "my_thread_local.h"
#include "my_time.h"
#include "mysqld_error.h"
#include "sql/check_stack.h"
#include "sql/enum_query_type.h"
#include "sql/handler.h"
#include "sql/key_spec.h"
#include "sql/mem_root_array.h"
#include "sql/opt_explain.h" // Sql_cmd_explain_other_thread
#include "sql/parse_location.h"
#include "sql/parse_tree_helpers.h" // PT_item_list
#include "sql/parse_tree_node_base.h"
#include "sql/parser_yystype.h"
#include "sql/partition_info.h"
#include "sql/resourcegroups/resource_group_basic_types.h"
#include "sql/resourcegroups/resource_group_sql_cmd.h"
#include "sql/set_var.h"
#include "sql/sp_head.h"
#include "sql/sql_admin.h" // Sql_cmd_shutdown etc.
#include "sql/sql_alter.h"
#include "sql/sql_check_constraint.h" // Sql_check_constraint_spec
#include "sql/sql_cmd_srs.h"
#include "sql/sql_exchange.h"
#include "sql/sql_lex.h" // LEX
#include "sql/sql_list.h"
#include "sql/sql_load.h" // Sql_cmd_load_table
#include "sql/sql_partition_admin.h"
#include "sql/sql_restart_server.h" // Sql_cmd_restart_server
#include "sql/sql_tablespace.h" // Tablespace_options
#include "sql/sql_truncate.h" // Sql_cmd_truncate_table
#include "sql/table.h" // Common_table_expr
#include "sql/tablesample.h"
#include "sql/window_lex.h"
#include "string_with_len.h"
#include "thr_lock.h"
class Item;
class Item_cache;
class Json_table_column;
class PT_column_attr_base;
class PT_field_def_base;
class PT_hint_list;
class PT_insert_values_list;
class PT_part_definition;
class PT_partition;
class PT_subquery;
class PT_type;
class PT_window_list;
class Sql_cmd;
class String;
class THD;
class Window;
class sp_head;
class sp_name;
struct CHARSET_INFO;
/**
@defgroup ptn Parse tree nodes
@ingroup Parser
*/
/**
@defgroup ptn_stmt Nodes representing SQL statements
@ingroup ptn
*/
/**
@defgroup ptn_create_table CREATE TABLE statement
@ingroup ptn_stmt
*/
/**
@defgroup ptn_alter_table ALTER TABLE statement
@ingroup ptn_stmt
*/
/**
@defgroup ptn_create_table_stuff Clauses of CREATE TABLE statement
@ingroup ptn_create_table
*/
/**
@defgroup ptn_partitioning CREATE/ALTER TABLE partitioning-related stuff
@ingroup ptn_create_table ptn_alter_table
*/
/**
@defgroup ptn_part_options Partition options in CREATE/ALTER TABLE
@ingroup ptn_partitioning
*/
/**
@defgroup ptn_create_or_alter_table_options Table options of CREATE/ALTER
TABLE
@anchor ptn_create_or_alter_table_options
@ingroup ptn_create_table ptn_alter_table
*/
/**
@defgroup ptn_col_types Column types in CREATE/ALTER TABLE
@ingroup ptn_create_table ptn_alter_table
*/
/**
@defgroup ptn_col_attrs Column attributes in CREATE/ALTER TABLE
@ingroup ptn_create_table ptn_alter_table
*/
/**
@defgroup ptn_not_gcol_attr Non-generated column attributes in CREATE/ALTER
TABLE
@ingroup ptn_col_attrs ptn_alter_table
*/
/**
Calls contextualize() on every node in the array.
*/
template <class Node_type, class Parse_context_type>
bool contextualize_nodes(Mem_root_array_YY<Node_type *> nodes,
Parse_context_type *pc) {
for (Node_type *i : nodes)
if (i->contextualize(pc)) return true;
return false;
}
/**
Base class for all top-level nodes of SQL statements
@ingroup ptn_stmt
*/
class Parse_tree_root {
Parse_tree_root(const Parse_tree_root &) = delete;
void operator=(const Parse_tree_root &) = delete;
protected:
Parse_tree_root() = default;
explicit Parse_tree_root(const POS &pos) : m_pos(pos) {}
virtual ~Parse_tree_root() = default;
public:
/// Textual location of a token just parsed.
POS m_pos;
virtual Sql_cmd *make_cmd(THD *thd) = 0;
// Return Json parse tree generated by SHOW PARSE_TREE.
virtual std::string get_printable_parse_tree(THD *thd [[maybe_unused]]) {
my_error(ER_NOT_SUPPORTED_YET, MYF(0),
"Parse tree display of this statement");
return "";
}
};
class PT_table_ddl_stmt_base : public Parse_tree_root {
public:
explicit PT_table_ddl_stmt_base(const POS &pos, MEM_ROOT *mem_root)
: Parse_tree_root(pos), m_alter_info(mem_root) {}
~PT_table_ddl_stmt_base() override = 0; // force abstract class
protected:
Alter_info m_alter_info;
};
inline PT_table_ddl_stmt_base::~PT_table_ddl_stmt_base() = default;
/**
Parse context for the table DDL (ALTER TABLE and CREATE TABLE) nodes.
For internal use in the contextualization code.
*/
struct Table_ddl_parse_context final : public Parse_context {
Table_ddl_parse_context(THD *thd_arg, Query_block *select_arg,
Alter_info *alter_info);
HA_CREATE_INFO *const create_info;
Alter_info *const alter_info;
KEY_CREATE_INFO *const key_create_info;
};
/**
Base class for all table DDL (ALTER TABLE and CREATE TABLE) nodes.
*/
typedef Parse_tree_node_tmpl<Table_ddl_parse_context> Table_ddl_node;
class PT_order_expr : public Parse_tree_node, public ORDER {
typedef Parse_tree_node super;
public:
PT_order_expr(const POS &pos, Item *item_arg, enum_order dir) : super(pos) {
item_initial = item_arg;
direction = (dir == ORDER_DESC) ? ORDER_DESC : ORDER_ASC;
}
bool do_contextualize(Parse_context *pc) override;
protected:
void add_json_info(Json_object *obj) override {
obj->add_alias("desc",
create_dom_ptr<Json_boolean>(direction == ORDER_DESC));
}
};
class PT_order_list : public Parse_tree_node {
typedef Parse_tree_node super;
public:
SQL_I_List<ORDER> value;
public:
explicit PT_order_list(const POS &pos) : super(pos) {}
bool do_contextualize(Parse_context *pc) override {
if (super::do_contextualize(pc)) return true;
for (ORDER *o = value.first; o != nullptr; o = o->next) {
if (static_cast<PT_order_expr *>(o)->contextualize(pc)) return true;
}
return false;
}
void push_back(PT_order_expr *order) {
order->used_alias = nullptr;
order->used = 0;
value.link_in_list(order, &order->next);
}
};
class PT_gorder_list : public PT_order_list {
typedef PT_order_list super;
public:
explicit PT_gorder_list(const POS &pos) : super(pos) {}
bool do_contextualize(Parse_context *pc) override {
return super::do_contextualize(pc);
}
};
/**
Represents an element of the WITH list:
WITH [...], [...] SELECT ...,
^ or ^
i.e. a Common Table Expression (CTE, or Query Name in SQL99 terms).
*/
class PT_common_table_expr : public Parse_tree_node {
typedef Parse_tree_node super;
public:
explicit PT_common_table_expr(const POS &pos, const LEX_STRING &name,
const LEX_STRING &subq_text,
uint subq_text_offset, PT_subquery *sn,
const Create_col_name_list *column_names,
MEM_ROOT *mem_root);
/// The name after AS
const LEX_STRING &name() const { return m_name; }
/**
@param thd Thread handler
@param[out] node PT_subquery
@returns a PT_subquery to attach to a table reference for this CTE
*/
bool make_subquery_node(THD *thd, PT_subquery **node);
/**
@param tl Table reference to match
@param in_self If this is a recursive reference
@param[out] found Is set to true/false if matches or not
@returns true if error
*/
bool match_table_ref(Table_ref *tl, bool in_self, bool *found);
/**
@returns true if 'other' is the same instance as 'this'
*/
bool is(const Common_table_expr *other) const {
return other == &m_postparse;
}
void print(const THD *thd, String *str, enum_query_type query_type);
bool do_contextualize(Parse_context *pc) override;
protected:
void add_json_info(Json_object *obj) override;
private:
LEX_STRING m_name;
/// Raw text of query expression (including parentheses)
const LEX_STRING m_subq_text;
/**
Offset in bytes of m_subq_text in original statement which had the WITH
clause.
*/
uint m_subq_text_offset;
/// Parsed version of subq_text
PT_subquery *const m_subq_node;
/// List of explicitly specified column names; if empty, no list.
const Create_col_name_list m_column_names;
/**
A Table_ref representing a CTE needs access to the WITH list
element it derives from. However, in order to:
- limit the members which Table_ref can access
- avoid including this header file everywhere Table_ref needs to
access these members, these members are relocated into a separate inferior
object whose declaration is in table.h, like that of Table_ref. It's
the "postparse" part. Table_ref accesses this inferior object only.
*/
Common_table_expr m_postparse;
friend bool Query_expression::clear_correlated_query_blocks();
};
/**
Represents the WITH list.
WITH [...], [...] SELECT ...,
^^^^^^^^^^^^
*/
class PT_with_list : public Parse_tree_node {
typedef Parse_tree_node super;
public:
/// @param pos Position of this clause in the SQL statement.
/// @param mem_root where interior objects are allocated
explicit PT_with_list(const POS &pos, MEM_ROOT *mem_root)
: super(pos), m_elements(mem_root) {}
bool push_back(PT_common_table_expr *el);
const Mem_root_array<PT_common_table_expr *> &elements() const {
return m_elements;
}
private:
Mem_root_array<PT_common_table_expr *> m_elements;
};
/**
Represents the WITH clause:
WITH [...], [...] SELECT ...,
^^^^^^^^^^^^^^^^^
*/
class PT_with_clause : public Parse_tree_node {
typedef Parse_tree_node super;
public:
PT_with_clause(const POS &pos, const PT_with_list *l, bool r)
: super(pos),
m_list(l),
m_recursive(r),
m_most_inner_in_parsing(nullptr) {}
bool do_contextualize(Parse_context *pc) override;
/**
Looks up a table reference into the list of CTEs.
@param tl Table reference to look up
@param[out] found Is set to true/false if found or not
@returns true if error
*/
bool lookup(Table_ref *tl, PT_common_table_expr **found);
/**
Call this to record in the WITH clause that we are contextualizing the
CTE definition inserted in table reference 'tl'.
@returns information which the caller must provide to
leave_parsing_definition().
*/
const Table_ref *enter_parsing_definition(Table_ref *tl) {
auto old = m_most_inner_in_parsing;
m_most_inner_in_parsing = tl;
return old;
}
void leave_parsing_definition(const Table_ref *old) {
m_most_inner_in_parsing = old;
}
void print(const THD *thd, String *str, enum_query_type query_type);
protected:
void add_json_info(Json_object *obj) override {
obj->add_alias("recursive", create_dom_ptr<Json_boolean>(m_recursive));
}
private:
/// All CTEs of this clause
const PT_with_list *const m_list;
/// True if the user has specified the RECURSIVE keyword.
const bool m_recursive;
/**
The innermost CTE reference which we're parsing at the
moment. Used to detect forward references, loops and recursiveness.
*/
const Table_ref *m_most_inner_in_parsing;
friend bool Query_expression::clear_correlated_query_blocks();
};
class PT_select_item_list : public PT_item_list {
typedef PT_item_list super;
public:
explicit PT_select_item_list(const POS &pos) : super(pos) {}
bool do_contextualize(Parse_context *pc) override;
};
class PT_limit_clause : public Parse_tree_node {
typedef Parse_tree_node super;
Limit_options limit_options;
protected:
void add_json_info(Json_object *obj) override {
obj->add_alias("is_offset_first",
create_dom_ptr<Json_boolean>(limit_options.is_offset_first));
}
public:
PT_limit_clause(const POS &pos, const Limit_options &limit_options_arg)
: super(pos), limit_options(limit_options_arg) {}
bool do_contextualize(Parse_context *pc) override;
friend class PT_query_expression;
};
class PT_cross_join;
class PT_joined_table;
class PT_table_reference : public Parse_tree_node {
public:
explicit PT_table_reference(const POS &pos) : Parse_tree_node(pos) {}
Table_ref *m_table_ref{nullptr};
/**
Lets us build a parse tree top-down, which is necessary due to the
context-dependent nature of the join syntax. This function adds
the @<table_ref@> cross join as the left-most leaf in this join tree
rooted at this node.
@todo: comment on non-join PT_table_reference objects
@param cj This @<table ref@> will be added if it represents a cross join.
@return The new top-level join.
*/
virtual PT_joined_table *add_cross_join(PT_cross_join *cj);
};
class PT_table_factor_table_ident : public PT_table_reference {
typedef PT_table_reference super;
Table_ident *table_ident;
List<String> *opt_use_partition;
const char *const opt_table_alias;
List<Index_hint> *opt_key_definition;
PT_tablesample *opt_tablesample{nullptr};
public:
PT_table_factor_table_ident(const POS &pos, Table_ident *table_ident_arg,
List<String> *opt_use_partition_arg,
const LEX_CSTRING &opt_table_alias_arg,
List<Index_hint> *opt_key_definition_arg,
PT_tablesample *opt_tablesample_arg)
: super(pos),
table_ident(table_ident_arg),
opt_use_partition(opt_use_partition_arg),
opt_table_alias(opt_table_alias_arg.str),
opt_key_definition(opt_key_definition_arg),
opt_tablesample(opt_tablesample_arg) {}
protected:
bool do_contextualize(Parse_context *pc) override;
void add_json_info(Json_object *obj) override;
};
class PT_json_table_column : public Parse_tree_node {
protected:
explicit PT_json_table_column(const POS &pos) : Parse_tree_node(pos) {}
public:
virtual Json_table_column *get_column() = 0;
};
class PT_table_factor_function : public PT_table_reference {
typedef PT_table_reference super;
public:
PT_table_factor_function(const POS &pos, Item *expr, Item *path,
Mem_root_array<PT_json_table_column *> *nested_cols,
const LEX_STRING &table_alias)
: super(pos),
m_expr(expr),
m_path(path),
m_nested_columns(nested_cols),
m_table_alias(table_alias) {}
bool do_contextualize(Parse_context *pc) override;
private:
Item *m_expr;
Item *m_path;
Mem_root_array<PT_json_table_column *> *m_nested_columns;
const LEX_STRING m_table_alias;
};
class PT_table_reference_list_parens : public PT_table_reference {
typedef PT_table_reference super;
Mem_root_array_YY<PT_table_reference *> table_list;
public:
explicit PT_table_reference_list_parens(
const POS &pos, const Mem_root_array_YY<PT_table_reference *> table_list)
: super(pos), table_list(table_list) {}
bool do_contextualize(Parse_context *pc) override;
};
class PT_derived_table : public PT_table_reference {
typedef PT_table_reference super;
public:
PT_derived_table(const POS &pos, bool lateral, PT_subquery *subquery,
const LEX_CSTRING &table_alias,
Create_col_name_list *column_names);
bool do_contextualize(Parse_context *pc) override;
protected:
void add_json_info(Json_object *obj) override;
private:
bool m_lateral;
PT_subquery *m_subquery;
const char *const m_table_alias;
/// List of explicitly specified column names; if empty, no list.
const Create_col_name_list column_names;
};
class PT_table_factor_joined_table : public PT_table_reference {
typedef PT_table_reference super;
public:
PT_table_factor_joined_table(const POS &pos, PT_joined_table *joined_table)
: super(pos), m_joined_table(joined_table) {}
bool do_contextualize(Parse_context *pc) override;
private:
PT_joined_table *m_joined_table;
};
class PT_joined_table : public PT_table_reference {
typedef PT_table_reference super;
protected:
PT_table_reference *m_left_pt_table;
POS m_join_pos;
PT_joined_table_type m_type;
PT_table_reference *m_right_pt_table;
Table_ref *m_left_table_ref{nullptr};
Table_ref *m_right_table_ref{nullptr};
public:
PT_joined_table(const POS &pos, PT_table_reference *tab1_node_arg,
const POS &join_pos_arg, PT_joined_table_type type,
PT_table_reference *tab2_node_arg)
: super(pos),
m_left_pt_table(tab1_node_arg),
m_join_pos(join_pos_arg),
m_type(type),
m_right_pt_table(tab2_node_arg) {
using std::has_single_bit;
static_assert(has_single_bit(unsigned{JTT_INNER}), "not a single bit");
static_assert(has_single_bit(unsigned{JTT_STRAIGHT}), "not a single bit");
static_assert(has_single_bit(unsigned{JTT_NATURAL}), "not a single bit");
static_assert(has_single_bit(unsigned{JTT_LEFT}), "not a single bit");
static_assert(has_single_bit(unsigned{JTT_RIGHT}), "not a single bit");
assert(type == JTT_INNER || type == JTT_STRAIGHT_INNER ||
type == JTT_NATURAL_INNER || type == JTT_NATURAL_LEFT ||
type == JTT_NATURAL_RIGHT || type == JTT_LEFT || type == JTT_RIGHT);
}
/**
Adds the cross join to this join operation. The cross join is nested as
the table reference on the left-hand side.
*/
PT_joined_table *add_cross_join(PT_cross_join *cj) override {
m_left_pt_table = m_left_pt_table->add_cross_join(cj);
return this;
}
/// Adds the table reference as the right-hand side of this join.
void add_rhs(PT_table_reference *table) {
assert(m_right_pt_table == nullptr);
m_right_pt_table = table;
}
bool do_contextualize(Parse_context *pc) override;
/// This class is being inherited, it should thus be abstract.
~PT_joined_table() override = 0;
protected:
bool contextualize_tabs(Parse_context *pc);
void add_json_info(Json_object *obj) override;
};
inline PT_joined_table::~PT_joined_table() = default;
class PT_cross_join : public PT_joined_table {
typedef PT_joined_table super;
public:
PT_cross_join(const POS &pos, PT_table_reference *tab1_node_arg,
const POS &join_pos_arg, PT_joined_table_type Type_arg,
PT_table_reference *tab2_node_arg)
: PT_joined_table(pos, tab1_node_arg, join_pos_arg, Type_arg,
tab2_node_arg) {}
bool do_contextualize(Parse_context *pc) override;
};
class PT_joined_table_on : public PT_joined_table {
typedef PT_joined_table super;
Item *on;
public:
PT_joined_table_on(const POS &pos, PT_table_reference *tab1_node_arg,
const POS &join_pos_arg, PT_joined_table_type type,
PT_table_reference *tab2_node_arg, Item *on_arg)
: super(pos, tab1_node_arg, join_pos_arg, type, tab2_node_arg),
on(on_arg) {}
bool do_contextualize(Parse_context *pc) override;
};
class PT_joined_table_using : public PT_joined_table {
typedef PT_joined_table super;
List<String> *using_fields;
public:
PT_joined_table_using(const POS &pos, PT_table_reference *tab1_node_arg,
const POS &join_pos_arg, PT_joined_table_type type,
PT_table_reference *tab2_node_arg,
List<String> *using_fields_arg)
: super(pos, tab1_node_arg, join_pos_arg, type, tab2_node_arg),
using_fields(using_fields_arg) {}
/// A PT_joined_table_using without a list of columns denotes a natural join.
PT_joined_table_using(const POS &pos, PT_table_reference *tab1_node_arg,
const POS &join_pos_arg, PT_joined_table_type type,
PT_table_reference *tab2_node_arg)
: PT_joined_table_using(pos, tab1_node_arg, join_pos_arg, type,
tab2_node_arg, nullptr) {}
bool do_contextualize(Parse_context *pc) override;
protected:
void add_json_info(Json_object *obj) override;
};
/*
PT_tablesample - parse tree node
Information contained in TABLESAMPLE clause is here.
*/
class PT_tablesample : public Parse_tree_node {
typedef Parse_tree_node super;
public:
tablesample_type m_sampling_type;
Item *m_sample_percentage{nullptr};
PT_tablesample(const POS &pos, tablesample_type tablesample_type_arg,
Item *sample_percentage)
: super(pos),
m_sampling_type(tablesample_type_arg),
m_sample_percentage(sample_percentage) {}
};
class PT_group : public Parse_tree_node {
typedef Parse_tree_node super;
PT_order_list *group_list;
olap_type olap;
protected:
void add_json_info(Json_object *obj) override {
if (olap == ROLLUP_TYPE)
obj->add_alias("olap_options", create_dom_ptr<Json_string>("ROLLUP"));
// Only rollup type supported.
}
public:
PT_group(const POS &pos, PT_order_list *group_list_arg, olap_type olap_arg)
: super(pos), group_list(group_list_arg), olap(olap_arg) {}
bool do_contextualize(Parse_context *pc) override;
};
class PT_order : public Parse_tree_node {
typedef Parse_tree_node super;
public:
PT_order_list *order_list;
explicit PT_order(const POS &pos, PT_order_list *order_list_arg)
: super(pos), order_list(order_list_arg) {}
bool do_contextualize(Parse_context *pc) override;
};
class PT_locking_clause : public Parse_tree_node {
public:
PT_locking_clause(const POS &pos, Lock_strength strength,
Locked_row_action action)
: Parse_tree_node(pos),
m_lock_strength(strength),
m_locked_row_action(action) {}
bool do_contextualize(Parse_context *pc) final;
virtual bool set_lock_for_tables(Parse_context *pc) = 0;
Locked_row_action action() const { return m_locked_row_action; }
protected:
Lock_descriptor get_lock_descriptor() const {
thr_lock_type lock_type = TL_IGNORE;
switch (m_lock_strength) {
case Lock_strength::UPDATE:
lock_type = TL_WRITE;
break;
case Lock_strength::SHARE:
lock_type = TL_READ_WITH_SHARED_LOCKS;
break;
}
return {lock_type, static_cast<thr_locked_row_action>(action())};
}
private:
Lock_strength m_lock_strength;
Locked_row_action m_locked_row_action;
};
class PT_query_block_locking_clause : public PT_locking_clause {
public:
explicit PT_query_block_locking_clause(
const POS &pos, Lock_strength strength,
Locked_row_action action = Locked_row_action::WAIT)
: PT_locking_clause(pos, strength, action) {}
bool set_lock_for_tables(Parse_context *pc) override;
};
class PT_table_locking_clause : public PT_locking_clause {
public:
typedef Mem_root_array_YY<Table_ident *> Table_ident_list;
PT_table_locking_clause(const POS &pos, Lock_strength strength,
Mem_root_array_YY<Table_ident *> tables,
Locked_row_action action)
: PT_locking_clause(pos, strength, action), m_tables(tables) {}
bool set_lock_for_tables(Parse_context *pc) override;
private:
bool raise_error(THD *thd, const Table_ident *name, int error);
bool raise_error(int error);
Table_ident_list m_tables;
};
class PT_locking_clause_list : public Parse_tree_node {
public:
PT_locking_clause_list(const POS &pos, MEM_ROOT *mem_root)
: Parse_tree_node(pos) {
m_locking_clauses.init(mem_root);
}
bool push_back(PT_locking_clause *locking_clause) {
return m_locking_clauses.push_back(locking_clause);
}
bool do_contextualize(Parse_context *pc) override {
for (auto locking_clause : m_locking_clauses)
if (locking_clause->contextualize(pc)) return true;
return false;
}
private:
Mem_root_array_YY<PT_locking_clause *> m_locking_clauses{};
};
class PT_query_expression_body : public Parse_tree_node {
public:
explicit PT_query_expression_body(const POS &pos) : Parse_tree_node(pos) {}
enum Setop_type { NONE, UNION, INTERSECT, EXCEPT };
virtual Setop_type type() const { return NONE; }
virtual bool is_set_operation() const = 0;
/**
True if this query expression can absorb an extraneous order by/limit
clause. The `ORDER BY`/`LIMIT` syntax is mostly consistestent, i.e. a
trailing clause may not refer to the tables in the `<query primary>`, with
one glaring exception:
(...( SELECT ... )...) ORDER BY ...
If the nested query expression doesn't contain `ORDER BY`, the statement
is interpreted as if the `ORDER BY` was absorbed by the innermost query
expression, i.e.:
(...( SELECT ... ORDER BY ... )...)
There is no rewriting of the parse tree nor AST happening here, the
transformation is done by the contextualizer (see
PT_query_expression::contextualize_order_and_limit), which interprets the
parse tree, and builds the AST according to this interpretation. This
interpretation is governed by the following rule: An `ORDER BY` can be
absorbed if none the nested query expressions contains an `ORDER BY` *or*
`LIMIT`. The rule is complex, so here are some examples for illustration:
In these cases the `ORDER BY` *is* absorbed:
( SELECT * FROM t1 ) ORDER BY t1.a;
(( SELECT * FROM t1 )) ORDER BY t1.a;
In these cases the ORDER BY is *not* absorbed:
( SELECT * FROM t1 ORDER BY 1 ) ORDER BY t1.a;
(( SELECT * FROM t1 ) ORDER BY 1 ) ORDER BY t1.a;
( SELECT * FROM t1 LIMIT 1 ) ORDER BY t1.a;
(( SELECT * FROM t1 ) LIMIT 1 ) ORDER BY t1.a;
The same happens with `LIMIT`, obviously, but the optimizer is freeer to
choose when to apply the limit, and there are name no resolution issues
involved.
@param order True if the outer query block has the ORDER BY clause.
@param limit True if the outer query block has the LIMIT clause.
*/
virtual bool can_absorb_order_and_limit(bool order, bool limit) const = 0;
virtual bool has_into_clause() const = 0;
virtual bool has_trailing_into_clause() const = 0;
virtual bool is_table_value_constructor() const = 0;
virtual PT_insert_values_list *get_row_value_list() const = 0;
};
class PT_set_scoped_system_variable : public Parse_tree_node {
typedef Parse_tree_node super;
public:
PT_set_scoped_system_variable(const POS &pos, const POS &var_pos,
const LEX_CSTRING &opt_prefix,
const LEX_CSTRING &name, Item *opt_expr)
: super(pos),
m_varpos(var_pos),
m_opt_prefix{opt_prefix},
m_name{name},
m_opt_expr{opt_expr} {}
bool do_contextualize(Parse_context *pc) override;
private:
const POS m_varpos;
const LEX_CSTRING m_opt_prefix;
const LEX_CSTRING m_name;
Item *m_opt_expr;
};
class PT_option_value_no_option_type : public Parse_tree_node {
protected:
explicit PT_option_value_no_option_type(const POS &pos)
: Parse_tree_node(pos) {}
};
class PT_set_variable : public PT_option_value_no_option_type {
typedef PT_option_value_no_option_type super;
public:
PT_set_variable(const POS &pos, const POS &varpos,
const LEX_CSTRING &opt_prefix, const LEX_CSTRING &name,
const POS &expr_pos, Item *opt_expr)
: super{pos},
m_varpos{varpos},
m_opt_prefix{opt_prefix},
m_name{name},
m_expr_pos{expr_pos},
m_opt_expr{opt_expr} {}
bool do_contextualize(Parse_context *pc) override;
private:
const POS m_varpos;
const LEX_CSTRING m_opt_prefix;
const LEX_CSTRING m_name;
const POS m_expr_pos;
Item *m_opt_expr;
};
class PT_option_value_no_option_type_user_var
: public PT_option_value_no_option_type {
typedef PT_option_value_no_option_type super;
LEX_STRING name;
Item *expr;
public:
PT_option_value_no_option_type_user_var(const POS &pos,
const LEX_STRING &name_arg,
Item *expr_arg)
: super(pos), name(name_arg), expr(expr_arg) {}
bool do_contextualize(Parse_context *pc) override;
};
class PT_set_system_variable : public PT_option_value_no_option_type {
typedef PT_option_value_no_option_type super;
public:
PT_set_system_variable(const POS &pos, enum_var_type scope,
const POS &name_pos, const LEX_CSTRING &opt_prefix,
const LEX_CSTRING &name, Item *opt_expr)
: super(pos),
m_scope{scope},
m_name_pos{name_pos},
m_opt_prefix{opt_prefix},
m_name{name},
m_opt_expr{opt_expr} {}
bool do_contextualize(Parse_context *pc) override;
private:
const enum_var_type m_scope;
const POS m_name_pos;
const LEX_CSTRING m_opt_prefix;
const LEX_CSTRING m_name;
Item *m_opt_expr;
};
class PT_option_value_no_option_type_charset
: public PT_option_value_no_option_type {
typedef PT_option_value_no_option_type super;
const CHARSET_INFO *opt_charset;
public:
PT_option_value_no_option_type_charset(const POS &pos,
const CHARSET_INFO *opt_charset_arg)
: super(pos), opt_charset(opt_charset_arg) {}
bool do_contextualize(Parse_context *pc) override;
};
class PT_option_value_no_option_type_names
: public PT_option_value_no_option_type {
typedef PT_option_value_no_option_type super;
POS m_error_pos;
public:
explicit PT_option_value_no_option_type_names(const POS &pos,
const POS &error_pos)
: super(pos), m_error_pos(error_pos) {}
bool do_contextualize(Parse_context *pc) override;
};
class PT_set_names : public PT_option_value_no_option_type {
typedef PT_option_value_no_option_type super;