/
sql_lex.h
3396 lines (2944 loc) · 102 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, 2015, Oracle and/or its affiliates.
Copyright (c) 2010, 2017, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 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 Semantic_Analysis Semantic Analysis
*/
#ifndef SQL_LEX_INCLUDED
#define SQL_LEX_INCLUDED
#include "violite.h" /* SSL_type */
#include "sql_trigger.h"
#include "item.h" /* From item_subselect.h: subselect_union_engine */
#include "thr_lock.h" /* thr_lock_type, TL_UNLOCK */
#include "mem_root_array.h"
#include "sql_cmd.h"
#include "sql_alter.h" // Alter_info
#include "sql_window.h"
#include "sql_trigger.h"
/* YACC and LEX Definitions */
/**
A string with metadata.
We'll add more flags here eventually, to know if the string has, e.g.:
- multi-byte characters
- bad byte sequences
- backslash escapes: 'a\nb'
- separator escapes: 'a''b'
and reuse the original query fragments instead of making the string
copy too early, in Lex_input_stream::get_text().
This will allow to avoid unnecessary copying, as well as
create more optimal Item types in sql_yacc.yy
*/
struct Lex_string_with_metadata_st: public LEX_STRING
{
bool m_is_8bit; // True if the string has 8bit characters
public:
void set_8bit(bool is_8bit) { m_is_8bit= is_8bit; }
// Get string repertoire by the 8-bit flag and the character set
uint repertoire(CHARSET_INFO *cs) const
{
return !m_is_8bit && my_charset_is_ascii_based(cs) ?
MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
}
// Get string repertoire by the 8-bit flag, for ASCII-based character sets
uint repertoire() const
{
return !m_is_8bit ? MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
}
};
enum sub_select_type
{
UNSPECIFIED_TYPE,
/* following 3 enums should be as they are*/
UNION_TYPE, INTERSECT_TYPE, EXCEPT_TYPE,
GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
};
enum unit_common_op {OP_MIX, OP_UNION, OP_INTERSECT, OP_EXCEPT};
/* These may not be declared yet */
class Table_ident;
class sql_exchange;
class LEX_COLUMN;
class sp_head;
class sp_name;
class sp_instr;
class sp_pcontext;
class sp_variable;
class st_alter_tablespace;
class partition_info;
class Event_parse_data;
class set_var_base;
class sys_var;
class Item_func_match;
class File_parser;
class Key_part_spec;
class Item_window_func;
struct sql_digest_state;
class With_clause;
#define ALLOC_ROOT_SET 1024
#ifdef MYSQL_SERVER
/*
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();
#endif
/**
used by the parser to store internal variable name
*/
struct sys_var_with_base
{
sys_var *var;
LEX_STRING base_name;
};
struct LEX_TYPE
{
enum enum_field_types type;
char *length, *dec;
CHARSET_INFO *charset;
void set(int t, char *l, char *d, CHARSET_INFO *cs)
{ type= (enum_field_types)t; length= l; dec= d; charset= cs; }
};
#ifdef MYSQL_SERVER
/*
The following hack is needed because mysql_yacc.cc does not define
YYSTYPE before including this file
*/
#ifdef MYSQL_YACC
#define LEX_YYSTYPE void *
#else
#include "lex_symbol.h"
#if MYSQL_LEX
#include "item_func.h" /* Cast_target used in sql_yacc.h */
#include "sql_get_diagnostics.h" /* Types used in sql_yacc.h */
#include "sp_pcontext.h"
#include "sql_yacc.h"
#define LEX_YYSTYPE YYSTYPE *
#else
#define LEX_YYSTYPE void *
#endif
#endif
#endif
// describe/explain types
#define DESCRIBE_NORMAL 1
#define DESCRIBE_EXTENDED 2
/*
This is not within #ifdef because we want "EXPLAIN PARTITIONS ..." to produce
additional "partitions" column even if partitioning is not compiled in.
*/
#define DESCRIBE_PARTITIONS 4
#ifdef MYSQL_SERVER
extern const LEX_STRING null_lex_str;
extern const LEX_STRING empty_lex_str;
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
};
const LEX_STRING sp_data_access_name[]=
{
{ C_STRING_WITH_LEN("") },
{ C_STRING_WITH_LEN("CONTAINS SQL") },
{ C_STRING_WITH_LEN("NO SQL") },
{ C_STRING_WITH_LEN("READS SQL DATA") },
{ C_STRING_WITH_LEN("MODIFIES SQL DATA") }
};
#define DERIVED_SUBQUERY 1
#define DERIVED_VIEW 2
#define DERIVED_WITH 4
enum enum_view_create_mode
{
VIEW_CREATE_NEW, // check that there are not such VIEW/table
VIEW_ALTER, // check that VIEW .frm with such name exists
VIEW_CREATE_OR_REPLACE // check only that there are not such table
};
enum enum_drop_mode
{
DROP_DEFAULT, // mode is not specified
DROP_CASCADE, // CASCADE option
DROP_RESTRICT // RESTRICT option
};
/* Options to add_table_to_list() */
#define TL_OPTION_UPDATING 1
#define TL_OPTION_FORCE_INDEX 2
#define TL_OPTION_IGNORE_LEAVES 4
#define TL_OPTION_ALIAS 8
typedef List<Item> List_item;
typedef Mem_root_array<ORDER*, true> Group_list_ptrs;
/* SERVERS CACHE CHANGES */
typedef struct st_lex_server_options
{
long port;
LEX_STRING server_name, host, db, username, password, scheme, socket, owner;
void reset(LEX_STRING name)
{
server_name= name;
host= db= username= password= scheme= socket= owner= null_lex_str;
port= -1;
}
} LEX_SERVER_OPTIONS;
/**
Structure to hold parameters for CHANGE MASTER, START SLAVE, and STOP SLAVE.
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_MASTER_INFO
{
DYNAMIC_ARRAY repl_ignore_server_ids;
DYNAMIC_ARRAY repl_do_domain_ids;
DYNAMIC_ARRAY repl_ignore_domain_ids;
char *host, *user, *password, *log_file_name;
char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
char *ssl_crl, *ssl_crlpath;
char *relay_log_name;
LEX_STRING connection_name;
/* Value in START SLAVE UNTIL master_gtid_pos=xxx */
LEX_STRING gtid_pos_str;
ulonglong pos;
ulong relay_log_pos;
ulong server_id;
uint port, connect_retry;
float heartbeat_period;
int sql_delay;
/*
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,
repl_do_domain_ids_opt, repl_ignore_domain_ids_opt;
enum {
LEX_GTID_UNCHANGED, LEX_GTID_NO, LEX_GTID_CURRENT_POS, LEX_GTID_SLAVE_POS
} use_gtid_opt;
void init()
{
bzero(this, sizeof(*this));
my_init_dynamic_array(&repl_ignore_server_ids,
sizeof(::server_id), 0, 16, MYF(0));
my_init_dynamic_array(&repl_do_domain_ids,
sizeof(ulong), 0, 16, MYF(0));
my_init_dynamic_array(&repl_ignore_domain_ids,
sizeof(ulong), 0, 16, MYF(0));
sql_delay= -1;
}
void reset(bool is_change_master)
{
if (unlikely(is_change_master))
{
delete_dynamic(&repl_ignore_server_ids);
/* Free all the array elements. */
delete_dynamic(&repl_do_domain_ids);
delete_dynamic(&repl_ignore_domain_ids);
}
host= user= password= log_file_name= ssl_key= ssl_cert= ssl_ca=
ssl_capath= ssl_cipher= relay_log_name= 0;
pos= relay_log_pos= server_id= port= connect_retry= 0;
heartbeat_period= 0;
ssl= ssl_verify_server_cert= heartbeat_opt=
repl_ignore_server_ids_opt= repl_do_domain_ids_opt=
repl_ignore_domain_ids_opt= LEX_MI_UNCHANGED;
gtid_pos_str= null_lex_str;
use_gtid_opt= LEX_GTID_UNCHANGED;
sql_delay= -1;
}
};
typedef struct st_lex_reset_slave
{
bool all;
} LEX_RESET_SLAVE;
enum olap_type
{
UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_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)
class select_result_sink;
/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
class Index_hint : public Sql_alloc
{
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_STRING key_name;
Index_hint (enum index_hint_type type_arg, index_clause_map clause_arg,
char *str, uint length) :
type(type_arg), clause(clause_arg)
{
key_name.str= str;
key_name.length= length;
}
void print(THD *thd, String *str);
};
/*
The state of the lex parsing for selects
master and slaves are pointers to select_lex.
master is pointer to upper level node.
slave is pointer to lower level node
select_lex is a SELECT without union
unit is container of either
- One SELECT
- UNION of selects
select_lex and unit are both inherited form select_lex_node
neighbors are two select_lex or units on the same level
All select describing structures linked with following pointers:
- list of neighbors (next/prev) (prev of first element point to slave
pointer of upper structure)
- For select this is a list of UNION's (or one element list)
- For units this is a list of sub queries for the upper level select
- pointer to master (master), which is
If this is a unit
- pointer to outer select_lex
If this is a select_lex
- pointer to outer unit structure for select
- pointer to slave (slave), which is either:
If this is a unit:
- first SELECT that belong to this unit
If this is a select_lex
- first unit that belong to this SELECT (subquries or derived tables)
- list of all select_lex (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.
If unit contain several selects (UNION now, INTERSECT etc later)
then it have special select_lex called fake_select_lex. It used for
storing global parameters (like ORDER BY, LIMIT) and executing union.
Subqueries used in global ORDER BY clause will be attached to this
fake_select_lex, which will allow them correctly resolve fields of
'upper' UNION and outer selects.
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
fake0
select1 select2 select3
|^^ |^
s||| ||master
l||| |+---------------------------------+
a||| +---------------------------------+|
v|||master slave ||
e||+-------------------------+ ||
V| neighbor | V|
unit1.1<+==================>unit1.2 unit2.1
fake1.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
fake0
select1 select2 select3
in the above picture)
main unit
|^^^^|fake_select_lex
|||||+--------------------------------------------+
||||+--------------------------------------------+|
|||+------------------------------+ ||
||+--------------+ | ||
slave||master | | ||
V| neighbor | neighbor | master|V
select1<========>select2<========>select3 fake0
list of all select_lex 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
*/
/*
Base class for st_select_lex (SELECT_LEX) &
st_select_lex_unit (SELECT_LEX_UNIT)
*/
struct LEX;
class st_select_lex;
class st_select_lex_unit;
class st_select_lex_node {
protected:
st_select_lex_node *next, **prev, /* neighbor list */
*master, *slave, /* vertical links */
*link_next, **link_prev; /* list of whole SELECT_LEX */
public:
ulonglong options;
/*
In sql_cache we store SQL_CACHE flag as specified by user to be
able to restore SELECT statement from internal structures.
*/
enum e_sql_cache { SQL_CACHE_UNSPECIFIED, SQL_NO_CACHE, SQL_CACHE };
e_sql_cache sql_cache;
/*
result of this query can't be cached, bit field, can be :
UNCACHEABLE_DEPENDENT_GENERATED
UNCACHEABLE_DEPENDENT_INJECTED
UNCACHEABLE_RAND
UNCACHEABLE_SIDEEFFECT
UNCACHEABLE_EXPLAIN
UNCACHEABLE_PREPARE
*/
uint8 uncacheable;
enum sub_select_type linkage;
bool no_table_names_allowed; /* used for global order by */
static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
{ return (void*) alloc_root(mem_root, (uint) size); }
static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
static void operator delete(void *ptr, MEM_ROOT *mem_root) {}
// Ensures that at least all members used during cleanup() are initialized.
st_select_lex_node()
: next(NULL), prev(NULL),
master(NULL), slave(NULL),
link_next(NULL), link_prev(NULL),
linkage(UNSPECIFIED_TYPE)
{
}
virtual ~st_select_lex_node() {}
inline st_select_lex_node* get_master() { return master; }
virtual void init_query();
virtual void init_select();
void include_down(st_select_lex_node *upper);
void add_slave(st_select_lex_node *slave_arg);
void include_neighbour(st_select_lex_node *before);
void include_standalone(st_select_lex_node *sel, st_select_lex_node **ref);
void include_global(st_select_lex_node **plink);
void exclude();
void exclude_from_tree();
virtual st_select_lex* outer_select()= 0;
virtual st_select_lex* return_after_parsing()= 0;
virtual bool inc_in_sum_expr();
virtual uint get_in_sum_expr();
virtual TABLE_LIST* get_table_list();
virtual List<Item>* get_item_list();
virtual ulong get_table_join_options();
virtual TABLE_LIST *add_table_to_list(THD *thd, Table_ident *table,
LEX_STRING *alias,
ulong table_options,
thr_lock_type flags= TL_UNLOCK,
enum_mdl_type mdl_type= MDL_SHARED_READ,
List<Index_hint> *hints= 0,
List<String> *partition_names= 0,
LEX_STRING *option= 0);
virtual void set_lock_for_tables(thr_lock_type lock_type) {}
void set_slave(st_select_lex_node *slave_arg) { slave= slave_arg; }
void move_node(st_select_lex_node *where_to_move)
{
if (where_to_move == this)
return;
if (next)
next->prev= prev;
*prev= next;
*where_to_move->prev= this;
next= where_to_move;
}
st_select_lex_node *insert_chain_before(st_select_lex_node **ptr_pos_to_insert,
st_select_lex_node *end_chain_node);
friend class st_select_lex_unit;
friend bool mysql_new_select(LEX *lex, bool move_down, SELECT_LEX *sel);
friend bool mysql_make_view(THD *thd, TABLE_SHARE *share, TABLE_LIST *table,
bool open_view_no_parse);
friend bool mysql_derived_prepare(THD *thd, LEX *lex,
TABLE_LIST *orig_table_list);
friend bool mysql_derived_merge(THD *thd, LEX *lex,
TABLE_LIST *orig_table_list);
friend bool TABLE_LIST::init_derived(THD *thd, bool init_view);
private:
void fast_exclude();
};
typedef class st_select_lex_node SELECT_LEX_NODE;
/*
SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group
SELECT_LEXs
*/
class THD;
class select_result;
class JOIN;
class select_unit;
class Procedure;
class Explain_query;
void delete_explain_query(LEX *lex);
void create_explain_query(LEX *lex, MEM_ROOT *mem_root);
void create_explain_query_if_not_exists(LEX *lex, MEM_ROOT *mem_root);
bool print_explain_for_slow_log(LEX *lex, THD *thd, String *str);
class st_select_lex_unit: public st_select_lex_node {
protected:
TABLE_LIST result_table_list;
select_unit *union_result;
ulonglong found_rows_for_union;
bool saved_error;
public:
// Ensures that at least all members used during cleanup() are initialized.
st_select_lex_unit()
: union_result(NULL), table(NULL), result(NULL),
cleaned(false),
fake_select_lex(NULL)
{
}
TABLE *table; /* temporary table using for appending UNION results */
select_result *result;
bool prepared, // prepare phase already performed for UNION (unit)
optimized, // optimize phase already performed for UNION (unit)
executed, // already executed
cleaned;
bool optimize_started;
// list of fields which points to temporary table for union
List<Item> item_list;
/*
list of types of items inside union (used for union & derived tables)
Item_type_holders from which this list consist may have pointers to Field,
pointers is valid only after preparing SELECTS of this unit and before
any SELECT of this unit execution
*/
List<Item> types;
/**
There is INTERSECT and it is item used in creating temporary
table for it
*/
Item_int *intersect_mark;
/**
Pointer to 'last' select, or pointer to select where we stored
global parameters for union.
If this is a union of multiple selects, the parser puts the global
parameters in fake_select_lex. If the union doesn't use a
temporary table, st_select_lex_unit::prepare() nulls out
fake_select_lex, but saves a copy in saved_fake_select_lex in
order to preserve the global parameters.
If it is not a union, first_select() is the last select.
@return select containing the global parameters
*/
inline st_select_lex *global_parameters()
{
if (fake_select_lex != NULL)
return fake_select_lex;
else if (saved_fake_select_lex != NULL)
return saved_fake_select_lex;
return first_select();
};
//node on which we should return current_select pointer after parsing subquery
st_select_lex *return_to;
/* LIMIT clause runtime counters */
ha_rows select_limit_cnt, offset_limit_cnt;
/* not NULL if unit used in subselect, point to subselect item */
Item_subselect *item;
/*
TABLE_LIST representing this union in the embedding select. Used for
derived tables/views handling.
*/
TABLE_LIST *derived;
bool is_view;
/* With clause attached to this unit (if any) */
With_clause *with_clause;
/* With element where this unit is used as the specification (if any) */
With_element *with_element;
/* thread handler */
THD *thd;
/*
SELECT_LEX for hidden SELECT in onion which process global
ORDER BY and LIMIT
*/
st_select_lex *fake_select_lex;
/**
SELECT_LEX that stores LIMIT and OFFSET for UNION ALL when noq
fake_select_lex is used.
*/
st_select_lex *saved_fake_select_lex;
st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */
bool describe; /* union exec() called for EXPLAIN */
Procedure *last_procedure; /* Pointer to procedure, if such exists */
bool columns_are_renamed;
void init_query();
st_select_lex* outer_select();
st_select_lex* first_select()
{
return reinterpret_cast<st_select_lex*>(slave);
}
void set_with_clause(With_clause *with_cl);
st_select_lex_unit* next_unit()
{
return reinterpret_cast<st_select_lex_unit*>(next);
}
st_select_lex* return_after_parsing() { return return_to; }
void exclude_level();
// void exclude_tree(); // it is not used for long time
bool is_excluded() { return prev == NULL; }
/* UNION methods */
bool prepare(THD *thd, select_result *result, ulong additional_options);
bool optimize();
bool exec();
bool exec_recursive();
bool cleanup();
inline void unclean() { cleaned= 0; }
void reinit_exec_mechanism();
void print(String *str, enum_query_type query_type);
bool add_fake_select_lex(THD *thd);
void init_prepare_fake_select_lex(THD *thd, bool first_execution);
inline bool is_prepared() { return prepared; }
bool change_result(select_result_interceptor *result,
select_result_interceptor *old_result);
void set_limit(st_select_lex *values);
void set_thd(THD *thd_arg) { thd= thd_arg; }
inline bool is_unit_op ();
bool union_needs_tmp_table();
void set_unique_exclude();
friend void lex_start(THD *thd);
friend int subselect_union_engine::exec();
List<Item> *get_column_types(bool for_cursor);
select_unit *get_union_result() { return union_result; }
int save_union_explain(Explain_query *output);
int save_union_explain_part2(Explain_query *output);
unit_common_op common_op();
};
typedef class st_select_lex_unit SELECT_LEX_UNIT;
typedef Bounds_checked_array<Item*> Ref_ptr_array;
/*
Structure which consists of the field and the item which
produces this field.
*/
class Grouping_tmp_field :public Sql_alloc
{
public:
Field *tmp_field;
Item *producing_item;
Grouping_tmp_field(Field *fld, Item *item)
:tmp_field(fld), producing_item(item) {}
};
/*
SELECT_LEX - store information of parsed SELECT statment
*/
class st_select_lex: public st_select_lex_node
{
public:
Name_resolution_context context;
char *db;
Item *where, *having; /* WHERE & HAVING clauses */
Item *prep_where; /* saved WHERE clause for prepared statement processing */
Item *prep_having;/* saved HAVING clause for prepared statement processing */
Item *cond_pushed_into_where; /* condition pushed into the select's WHERE */
Item *cond_pushed_into_having; /* condition pushed into the select's HAVING */
/* Saved values of the WHERE and HAVING clauses*/
Item::cond_result cond_value, having_value;
/* point on lex in which it was created, used in view subquery detection */
LEX *parent_lex;
enum olap_type olap;
/* FROM clause - points to the beginning of the TABLE_LIST::next_local list. */
SQL_I_List<TABLE_LIST> table_list;
/*
GROUP BY clause.
This list may be mutated during optimization (by remove_const()),
so for prepared statements, we keep a copy of the ORDER.next pointers in
group_list_ptrs, and re-establish the original list before each execution.
*/
SQL_I_List<ORDER> group_list;
Group_list_ptrs *group_list_ptrs;
List<Item> item_list; /* list of fields & expressions */
bool is_item_list_lookup;
/*
Usualy it is pointer to ftfunc_list_alloc, but in union used to create fake
select_lex for calling mysql_select under results of union
*/
List<Item_func_match> *ftfunc_list;
List<Item_func_match> ftfunc_list_alloc;
JOIN *join; /* after JOIN::prepare it is pointer to corresponding JOIN */
List<TABLE_LIST> top_join_list; /* join list of the top level */
List<TABLE_LIST> *join_list; /* list for the currently parsed join */
TABLE_LIST *embedding; /* table embedding to the above list */
List<TABLE_LIST> sj_nests; /* Semi-join nests within this join */
/*
Beginning of the list of leaves in a FROM clause, where the leaves
inlcude all base tables including view tables. The tables are connected
by TABLE_LIST::next_leaf, so leaf_tables points to the left-most leaf.
List of all base tables local to a subquery including all view
tables. Unlike 'next_local', this in this list views are *not*
leaves. Created in setup_tables() -> make_leaves_list().
*/
/*
Subqueries that will need to be converted to semi-join nests, including
those converted to jtbm nests. The list is emptied when conversion is done.
*/
List<Item_in_subselect> sj_subselects;
/*
Needed to correctly generate 'PRIMARY' or 'SIMPLE' for select_type column
of EXPLAIN
*/
bool have_merged_subqueries;
List<TABLE_LIST> leaf_tables;
List<TABLE_LIST> leaf_tables_exec;
List<TABLE_LIST> leaf_tables_prep;
enum leaf_list_state {UNINIT, READY, SAVED};
enum leaf_list_state prep_leaf_list_state;
uint insert_tables;
st_select_lex *merged_into; /* select which this select is merged into */
/* (not 0 only for views/derived tables) */
const char *type; /* type of select for EXPLAIN */
SQL_I_List<ORDER> order_list; /* ORDER clause */
SQL_I_List<ORDER> gorder_list;
Item *select_limit, *offset_limit; /* LIMIT clause parameters */
/// Array of pointers to top elements of all_fields list
Ref_ptr_array ref_pointer_array;
/*
number of items in select_list and HAVING clause used to get number
bigger then can be number of entries that will be added to all item
list during split_sum_func
*/
uint select_n_having_items;
uint cond_count; /* number of sargable Items in where/having/on */
uint between_count; /* number of between predicates in where/having/on */
uint max_equal_elems; /* maximal number of elements in multiple equalities */
/*
Number of fields used in select list or where clause of current select
and all inner subselects.
*/
uint select_n_where_fields;
/* reserved for exists 2 in */
uint select_n_reserved;
enum_parsing_place parsing_place; /* where we are parsing expression */
bool with_sum_func; /* sum function indicator */
ulong table_join_options;
uint in_sum_expr;
uint select_number; /* number of select (used for EXPLAIN) */
/*
nest_levels are local to the query or VIEW,
and that view merge procedure does not re-calculate them.
So we also have to remember unit against which we count levels.
*/
SELECT_LEX_UNIT *nest_level_base;
int nest_level; /* nesting level of select */
Item_sum *inner_sum_func_list; /* list of sum func in nested selects */
uint with_wild; /* item list contain '*' */
bool braces; /* SELECT ... UNION (SELECT ... ) <- this braces */
bool automatic_brackets; /* dummy select for INTERSECT precedence */
/* TRUE when having fix field called in processing of this SELECT */
bool having_fix_field;
/* List of references to fields referenced from inner selects */
List<Item_outer_ref> inner_refs_list;
/* Number of Item_sum-derived objects in this SELECT */
uint n_sum_items;
/* Number of Item_sum-derived objects in children and descendant SELECTs */
uint n_child_sum_items;
/* explicit LIMIT clause was used */
bool explicit_limit;
/*
This array is used to note whether we have any candidates for
expression caching in the corresponding clauses
*/
bool expr_cache_may_be_used[PARSING_PLACE_SIZE];
/*
there are subquery in HAVING clause => we can't close tables before
query processing end even if we use temporary table
*/
bool subquery_in_having;
/* TRUE <=> this SELECT is correlated w.r.t. some ancestor select */
bool is_correlated;
/*
This variable is required to ensure proper work of subqueries and
stored procedures. Generally, one should use the states of
Query_arena to determine if it's a statement prepare or first
execution of a stored procedure. However, in case when there was an
error during the first execution of a stored procedure, the SP body
is not expelled from the SP cache. Therefore, a deeply nested
subquery might be left unoptimized. So we need this per-subquery
variable to inidicate the optimization/execution state of every
subquery. Prepared statements work OK in that regard, as in
case of an error during prepare the PS is not created.
*/
bool first_execution;
bool first_natural_join_processing;
bool first_cond_optimization;
/* do not wrap view fields with Item_ref */
bool no_wrap_view_item;
/* exclude this select from check of unique_table() */
bool exclude_from_table_unique_test;
/* index in the select list of the expression currently being fixed */
int cur_pos_in_select_list;
List<udf_func> udf_list; /* udf function calls stack */
/*
This is a copy of the original JOIN USING list that comes from
the parser. The parser :
1. Sets the natural_join of the second TABLE_LIST in the join
and the st_select_lex::prev_join_using.
2. Makes a parent TABLE_LIST and sets its is_natural_join/
join_using_fields members.
3. Uses the wrapper TABLE_LIST as a table in the upper level.
We cannot assign directly to join_using_fields in the parser because
at stage (1.) the parent TABLE_LIST is not constructed yet and
the assignment will override the JOIN USING fields of the lower level
joins on the right.
*/
List<String> *prev_join_using;
/**
The set of those tables whose fields are referenced in the select list of
this select level.
*/
table_map select_list_tables;
/* namp of nesting SELECT visibility (for aggregate functions check) */
nesting_map name_visibility_map;
table_map with_dep;
List<Grouping_tmp_field> grouping_tmp_fields;
/* it is for correct printing SELECT options */
thr_lock_type lock_type;
void init_query();
void init_select();
st_select_lex_unit* master_unit() { return (st_select_lex_unit*) master; }
st_select_lex_unit* first_inner_unit()
{
return (st_select_lex_unit*) slave;
}
st_select_lex* outer_select();
st_select_lex* next_select() { return (st_select_lex*) next; }
st_select_lex* next_select_in_list()
{
return (st_select_lex*) link_next;
}