-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsp_instr.h
1488 lines (1138 loc) · 46.6 KB
/
sp_instr.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) 2012, 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 _SP_INSTR_H_
#define _SP_INSTR_H_
#include <assert.h>
#include <limits.h>
#include <string.h>
#include <sys/types.h>
#include "field_types.h"
#include "lex_string.h"
#include "my_alloc.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "my_psi_config.h"
#include "my_sys.h"
#include "mysql/components/services/bits/psi_statement_bits.h"
#include "sql/sql_class.h" // Query_arena
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql_string.h"
#include "string_with_len.h"
class Item;
class Item_case_expr;
class Item_trigger_field;
class sp_condition_value;
class sp_handler;
class sp_head;
class sp_pcontext;
class sp_variable;
class Table_ref;
///////////////////////////////////////////////////////////////////////////
// This file contains SP-instruction classes.
///////////////////////////////////////////////////////////////////////////
/**
sp_printable defines an interface which should be implemented if a class wants
report some internal information about its state.
*/
class sp_printable {
public:
virtual void print(const THD *thd, String *str) = 0;
virtual ~sp_printable() = default;
};
/**
An interface for all SP-instructions with destinations that
need to be updated by the SP-optimizer.
*/
class sp_branch_instr {
public:
/**
Update the destination; used by the SP-instruction-optimizer.
@param old_dest current (old) destination (instruction pointer).
@param new_dest new destination (instruction pointer).
*/
virtual void set_destination(uint old_dest, uint new_dest) = 0;
/**
Update all instruction with the given label in the backpatch list to
the specified instruction pointer.
@param dest destination instruction pointer.
*/
virtual void backpatch(uint dest) = 0;
virtual ~sp_branch_instr() = default;
};
///////////////////////////////////////////////////////////////////////////
/**
Base class for every SP-instruction. sp_instr defines interface and provides
base implementation.
*/
class sp_instr : public sp_printable {
public:
sp_instr(uint ip, sp_pcontext *ctx)
: m_arena(nullptr, Query_arena::STMT_INITIALIZED_FOR_SP),
m_marked(false),
m_ip(ip),
m_parsing_ctx(ctx) {}
~sp_instr() override { m_arena.free_items(); }
/**
Execute this instruction
@param thd Thread context
@param[out] nextp index of the next instruction to execute. (For most
instructions this will be the instruction following this
one). Note that this parameter is undefined in case of
errors, use get_cont_dest() to find the continuation
instruction for CONTINUE error handlers.
@return Error status.
*/
virtual bool execute(THD *thd, uint *nextp) = 0;
#ifdef HAVE_PSI_INTERFACE
virtual PSI_statement_info *get_psi_info() = 0;
#endif
uint get_ip() const { return m_ip; }
/**
Get the continuation destination (instruction pointer for the CONTINUE
HANDLER) of this instruction.
@return the continuation destination
*/
virtual uint get_cont_dest() const { return get_ip() + 1; }
sp_pcontext *get_parsing_ctx() const { return m_parsing_ctx; }
protected:
/**
Clear diagnostics area.
@param thd Thread context
*/
void clear_da(THD *thd) const {
thd->get_stmt_da()->reset_diagnostics_area();
thd->get_stmt_da()->reset_condition_info(thd);
}
///////////////////////////////////////////////////////////////////////////
// The following operations are used solely for SP-code-optimizer.
///////////////////////////////////////////////////////////////////////////
public:
/**
Mark this instruction as reachable during optimization and return the
index to the next instruction. Jump instruction will add their
destination to the leads list.
*/
virtual uint opt_mark(sp_head *, List<sp_instr> *leads [[maybe_unused]]) {
m_marked = true;
return get_ip() + 1;
}
/**
Short-cut jumps to jumps during optimization. This is used by the
jump instructions' opt_mark() methods. 'start' is the starting point,
used to prevent the mark sweep from looping for ever. Return the
end destination.
*/
virtual uint opt_shortcut_jump(sp_head *, sp_instr *start [[maybe_unused]]) {
return get_ip();
}
/**
Inform the instruction that it has been moved during optimization.
Most instructions will simply update its index, but jump instructions
must also take care of their destination pointers. Forward jumps get
pushed to the backpatch list 'ibp'.
*/
virtual void opt_move(uint dst, List<sp_branch_instr> *ibp [[maybe_unused]]) {
m_ip = dst;
}
bool opt_is_marked() const { return m_marked; }
virtual SQL_I_List<Item_trigger_field> *get_instr_trig_field_list() {
return nullptr;
}
Query_arena m_arena;
protected:
/// Show if this instruction is reachable within the SP
/// (used by SP-optimizer).
bool m_marked;
/// Instruction pointer.
uint m_ip;
/// Instruction parsing context.
sp_pcontext *m_parsing_ctx;
private:
// Prevent use of copy constructor and assignment operator.
sp_instr(const sp_instr &);
void operator=(sp_instr &);
};
///////////////////////////////////////////////////////////////////////////
/**
sp_lex_instr is a class providing the interface and base implementation
for SP-instructions, whose execution is based on expression evaluation.
sp_lex_instr keeps LEX-object to be able to evaluate the expression.
sp_lex_instr also provides possibility to re-parse the original query
string if for some reason the LEX-object is not valid any longer.
*/
class sp_lex_instr : public sp_instr {
public:
sp_lex_instr(uint ip, sp_pcontext *ctx, LEX *lex, bool is_lex_owner)
: sp_instr(ip, ctx),
m_lex(nullptr),
m_is_lex_owner(false),
m_first_execution(true),
m_prelocking_tables(nullptr),
m_lex_query_tables_own_last(nullptr) {
set_lex(lex, is_lex_owner);
}
~sp_lex_instr() override {
free_lex();
/*
If the instruction is reparsed, m_lex_mem_root was used to allocate
the items, then freeing the memroot, frees the items. Also free the
items allocated on heap as well.
*/
if (alloc_root_inited(&m_lex_mem_root)) m_arena.free_items();
}
/**
Make a few attempts to execute the instruction.
Basically, this operation does the following things:
- install Reprepare_observer to catch metadata changes (if any);
- calls reset_lex_and_exec_core() to execute the instruction;
- if the execution fails due to a change in metadata, re-parse the
instruction's SQL-statement and repeat execution.
@param thd Thread context.
@param[out] nextp Next instruction pointer
@param open_tables Flag to specify if the function should check read
access to tables in LEX's table list and open and
lock them (used in instructions which need to
calculate some expression and don't execute
complete statement).
@return Error status.
*/
bool validate_lex_and_execute_core(THD *thd, uint *nextp, bool open_tables);
SQL_I_List<Item_trigger_field> *get_instr_trig_field_list() override {
return &m_trig_field_list;
}
private:
/**
Prepare LEX and thread for execution of instruction, if requested open
and lock LEX's tables, execute instruction's core function, perform
cleanup afterwards.
@param thd thread context
@param [out] nextp next instruction pointer
@param open_tables if true then check read access to tables in LEX's table
list and open and lock them (used in instructions which
need to calculate some expression and don't execute
complete statement).
@note
We are not saving/restoring some parts of THD which may need this because
we do this once for whole routine execution in sp_head::execute().
@return Error status.
*/
bool reset_lex_and_exec_core(THD *thd, uint *nextp, bool open_tables);
bool execute_expression(THD *thd, uint *nextp);
/**
Parse statement corresponding to this instruction and return a new LEX.
@param thd Thread context.
@param sp The stored program.
@return new LEX-object or NULL in case of failure.
*/
LEX *parse_statement(THD *thd, sp_head *sp);
/**
Set LEX-object.
Previously assigned LEX-object (if any) will be properly cleaned up
and destroyed.
@param lex LEX-object to be used by this instance of sp_lex_instr.
@param is_lex_owner the flag specifying if this instance sp_lex_instr
owns (and thus deletes when needed) passed LEX-object.
*/
void set_lex(LEX *lex, bool is_lex_owner);
/**
Cleanup and destroy assigned LEX-object if needed.
*/
void free_lex();
public:
/////////////////////////////////////////////////////////////////////////
// sp_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool execute(THD *thd, uint *nextp) override {
/*
SP instructions with expressions should clear DA before execution.
Note that sp_instr_stmt will override execute(), but it clears DA
during normal mysql_execute_command().
*/
clear_da(thd);
return validate_lex_and_execute_core(thd, nextp, true);
}
protected:
/////////////////////////////////////////////////////////////////////////
// Interface (virtual) methods.
/////////////////////////////////////////////////////////////////////////
/**
Execute core function of instruction after all preparations
(e.g. setting of proper LEX, saving part of the thread context).
@param thd Thread context.
@param [out] nextp next instruction pointer
@return Error flag.
*/
virtual bool exec_core(THD *thd, uint *nextp) = 0;
/**
@retval false if the object (i.e. LEX-object) is valid and exec_core() can
be just called.
@retval true if the object is not valid any longer, exec_core() can not be
called. The original query string should be re-parsed and a new LEX-object
should be used.
*/
virtual bool is_invalid() const = 0;
/**
Invalidate the object.
*/
virtual void invalidate() = 0;
/**
Return the query string, which can be passed to the parser. I.e. the
operation should return a valid SQL-statement query string.
@param[out] sql_query SQL-statement query string.
*/
virtual void get_query(String *sql_query) const;
/**
Some expressions may be re-parsed as SELECT statements, but need to be
adjusted to another SQL command. This function facilitates that change.
*/
virtual void adjust_sql_command(LEX *) {}
/**
@return the expression query string. This string can not be passed directly
to the parser as it is most likely not a valid SQL-statement.
@note as it can be seen in the get_query() implementation, get_expr_query()
might return EMPTY_CSTR. EMPTY_CSTR means that no query-expression is
available. That happens when class provides different implementation of
get_query(). Strictly speaking, this is a drawback of the current class
hierarchy.
*/
virtual LEX_CSTRING get_expr_query() const { return EMPTY_CSTR; }
/**
Callback function which is called after the statement query string is
successfully parsed, and the thread context has not been switched to the
outer context. The thread context contains new LEX-object corresponding to
the parsed query string.
@param thd Thread context.
@return Error flag.
*/
virtual bool on_after_expr_parsing(THD *thd [[maybe_unused]]) {
return false;
}
/**
Destroy items in the free list before re-parsing the statement query
string (and thus, creating new items).
@param thd Thread context.
*/
virtual void cleanup_before_parsing(THD *thd);
/// LEX-object.
LEX *m_lex;
private:
/**
Mem-root for storing the LEX-tree during reparse. This
mem-root is freed when a reparse is triggered or the stored
routine is dropped.
*/
MEM_ROOT m_lex_mem_root{PSI_NOT_INSTRUMENTED, MEM_ROOT_BLOCK_SIZE};
/**
Indicates whether this sp_lex_instr instance is responsible for
LEX-object deletion.
*/
bool m_is_lex_owner;
/**
Indicates whether exec_core() has not been already called on the current
LEX-object.
*/
bool m_first_execution;
/*****************************************************************************
Support for being able to execute this statement in two modes:
a) inside prelocked mode set by the calling procedure or its ancestor.
b) outside of prelocked mode, when this statement enters/leaves
prelocked mode itself.
*****************************************************************************/
/**
List of additional tables this statement needs to lock when it
enters/leaves prelocked mode on its own.
*/
Table_ref *m_prelocking_tables;
/**
The value m_lex->query_tables_own_last should be set to this when the
statement enters/leaves prelocked mode on its own.
*/
Table_ref **m_lex_query_tables_own_last;
/**
List of all the Item_trigger_field's of instruction.
*/
SQL_I_List<Item_trigger_field> m_trig_field_list;
};
///////////////////////////////////////////////////////////////////////////
/**
sp_instr_stmt represents almost all conventional SQL-statements, which are
supported outside stored programs.
SET-statements, which deal with SP-variable or NEW/OLD trigger pseudo-rows are
not represented by this instruction.
*/
class sp_instr_stmt : public sp_lex_instr {
public:
sp_instr_stmt(uint ip, LEX *lex, LEX_CSTRING query)
: sp_lex_instr(ip, lex->get_sp_current_parsing_ctx(), lex, true),
m_query(query),
m_valid(true) {}
/////////////////////////////////////////////////////////////////////////
// sp_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool execute(THD *thd, uint *nextp) override;
/////////////////////////////////////////////////////////////////////////
// sp_printable implementation.
/////////////////////////////////////////////////////////////////////////
void print(const THD *thd, String *str) override;
/////////////////////////////////////////////////////////////////////////
// sp_lex_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool exec_core(THD *thd, uint *nextp) override;
bool is_invalid() const override { return !m_valid; }
void invalidate() override { m_valid = false; }
void get_query(String *sql_query) const override {
sql_query->append(m_query.str, m_query.length);
}
bool on_after_expr_parsing(THD *) override {
m_valid = true;
return false;
}
private:
/// Complete query of the SQL-statement.
LEX_CSTRING m_query;
/// Specify if the stored LEX-object is up-to-date.
bool m_valid;
#ifdef HAVE_PSI_INTERFACE
public:
PSI_statement_info *get_psi_info() override { return &psi_info; }
static PSI_statement_info psi_info;
#endif
};
///////////////////////////////////////////////////////////////////////////
/**
sp_instr_set represents SET-statements, which deal with SP-variables.
*/
class sp_instr_set : public sp_lex_instr {
public:
sp_instr_set(uint ip, LEX *lex, uint offset, Item *value_item,
LEX_CSTRING value_query, bool is_lex_owner)
: sp_lex_instr(ip, lex->get_sp_current_parsing_ctx(), lex, is_lex_owner),
m_offset(offset),
m_value_item(value_item),
m_value_query(value_query) {}
/////////////////////////////////////////////////////////////////////////
// sp_printable implementation.
/////////////////////////////////////////////////////////////////////////
void print(const THD *thd, String *str) override;
/////////////////////////////////////////////////////////////////////////
// sp_lex_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool exec_core(THD *thd, uint *nextp) override;
bool is_invalid() const override { return m_value_item == nullptr; }
void invalidate() override { m_value_item = nullptr; }
bool on_after_expr_parsing(THD *thd) override {
m_value_item = thd->lex->query_block->single_visible_field();
assert(m_value_item != nullptr);
return false;
}
LEX_CSTRING get_expr_query() const override { return m_value_query; }
void adjust_sql_command(LEX *lex) override {
assert(lex->sql_command == SQLCOM_SELECT);
lex->sql_command = SQLCOM_SET_OPTION;
}
private:
/// Frame offset.
uint m_offset;
/// Value expression item of the SET-statement.
Item *m_value_item;
/// SQL-query corresponding to the value expression.
LEX_CSTRING m_value_query;
#ifdef HAVE_PSI_INTERFACE
public:
static PSI_statement_info psi_info;
PSI_statement_info *get_psi_info() override { return &psi_info; }
#endif
};
///////////////////////////////////////////////////////////////////////////
/**
sp_instr_set_trigger_field represents SET-statements, which deal with NEW/OLD
trigger pseudo-rows.
*/
class sp_instr_set_trigger_field : public sp_lex_instr {
public:
sp_instr_set_trigger_field(uint ip, LEX *lex, LEX_CSTRING trigger_field_name,
Item_trigger_field *trigger_field,
Item *value_item, LEX_CSTRING value_query)
: sp_lex_instr(ip, lex->get_sp_current_parsing_ctx(), lex, true),
m_trigger_field_name(trigger_field_name),
m_trigger_field(trigger_field),
m_value_item(value_item),
m_value_query(value_query) {}
/////////////////////////////////////////////////////////////////////////
// sp_printable implementation.
/////////////////////////////////////////////////////////////////////////
void print(const THD *thd, String *str) override;
/////////////////////////////////////////////////////////////////////////
// sp_lex_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool exec_core(THD *thd, uint *nextp) override;
bool is_invalid() const override { return m_value_item == nullptr; }
void invalidate() override { m_value_item = nullptr; }
bool on_after_expr_parsing(THD *thd) override;
void cleanup_before_parsing(THD *thd) override;
LEX_CSTRING get_expr_query() const override { return m_value_query; }
private:
/// Trigger field name ("field_name" of the "NEW.field_name").
LEX_CSTRING m_trigger_field_name;
/// Item corresponding to the NEW/OLD trigger field.
Item_trigger_field *m_trigger_field;
/// Value expression item of the SET-statement.
Item *m_value_item;
/// SQL-query corresponding to the value expression.
LEX_CSTRING m_value_query;
#ifdef HAVE_PSI_INTERFACE
public:
PSI_statement_info *get_psi_info() override { return &psi_info; }
static PSI_statement_info psi_info;
#endif
};
///////////////////////////////////////////////////////////////////////////
/**
sp_instr_freturn represents RETURN statement in stored functions.
*/
class sp_instr_freturn : public sp_lex_instr {
public:
sp_instr_freturn(uint ip, LEX *lex, Item *expr_item, LEX_CSTRING expr_query,
enum enum_field_types return_field_type)
: sp_lex_instr(ip, lex->get_sp_current_parsing_ctx(), lex, true),
m_expr_item(expr_item),
m_expr_query(expr_query),
m_return_field_type(return_field_type) {}
/////////////////////////////////////////////////////////////////////////
// sp_printable implementation.
/////////////////////////////////////////////////////////////////////////
void print(const THD *thd, String *str) override;
/////////////////////////////////////////////////////////////////////////
// sp_instr implementation.
/////////////////////////////////////////////////////////////////////////
uint opt_mark(sp_head *, List<sp_instr> *) override {
m_marked = true;
return UINT_MAX;
}
/////////////////////////////////////////////////////////////////////////
// sp_lex_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool exec_core(THD *thd, uint *nextp) override;
bool is_invalid() const override { return m_expr_item == nullptr; }
void invalidate() override {
// it's already deleted.
m_expr_item = nullptr;
}
bool on_after_expr_parsing(THD *thd) override {
m_expr_item = thd->lex->query_block->single_visible_field();
assert(m_expr_item != nullptr);
return false;
}
LEX_CSTRING get_expr_query() const override { return m_expr_query; }
void adjust_sql_command(LEX *lex) override {
assert(lex->sql_command == SQLCOM_SELECT);
lex->sql_command = SQLCOM_END;
}
private:
/// RETURN-expression item.
Item *m_expr_item;
/// SQL-query corresponding to the RETURN-expression.
LEX_CSTRING m_expr_query;
/// RETURN-field type code.
enum enum_field_types m_return_field_type;
#ifdef HAVE_PSI_INTERFACE
public:
PSI_statement_info *get_psi_info() override { return &psi_info; }
static PSI_statement_info psi_info;
#endif
};
///////////////////////////////////////////////////////////////////////////
/**
This is base class for all kinds of jump instructions.
@note this is the only class, we directly construct instances of, that has
subclasses. We also redefine sp_instr_jump behavior in those subclasses.
@todo later we will consider introducing a new class, which will be the base
for sp_instr_jump, sp_instr_set_case_expr and sp_instr_jump_case_when.
Something like sp_regular_branch_instr (similar to sp_lex_branch_instr).
*/
class sp_instr_jump : public sp_instr, public sp_branch_instr {
public:
sp_instr_jump(uint ip, sp_pcontext *ctx)
: sp_instr(ip, ctx), m_dest(0), m_optdest(nullptr) {}
sp_instr_jump(uint ip, sp_pcontext *ctx, uint dest)
: sp_instr(ip, ctx), m_dest(dest), m_optdest(nullptr) {}
/////////////////////////////////////////////////////////////////////////
// sp_printable implementation.
/////////////////////////////////////////////////////////////////////////
void print(const THD *thd, String *str) override;
/////////////////////////////////////////////////////////////////////////
// sp_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool execute(THD *, uint *nextp) override {
*nextp = m_dest;
return false;
}
uint opt_mark(sp_head *sp, List<sp_instr> *leads) override;
uint opt_shortcut_jump(sp_head *sp, sp_instr *start) override;
void opt_move(uint dst, List<sp_branch_instr> *ibp) override;
/////////////////////////////////////////////////////////////////////////
// sp_branch_instr implementation.
/////////////////////////////////////////////////////////////////////////
void set_destination(uint old_dest, uint new_dest) override {
if (m_dest == old_dest) m_dest = new_dest;
}
void backpatch(uint dest) override {
/* Calling backpatch twice is a logic flaw in jump resolution. */
assert(m_dest == 0);
m_dest = dest;
}
protected:
/// Where we will go.
uint m_dest;
// The following attribute is used by SP-optimizer.
sp_instr *m_optdest;
#ifdef HAVE_PSI_INTERFACE
public:
PSI_statement_info *get_psi_info() override { return &psi_info; }
static PSI_statement_info psi_info;
#endif
};
///////////////////////////////////////////////////////////////////////////
/**
sp_lex_branch_instr is a base class for SP-instructions, which might perform
conditional jump depending on the value of an SQL-expression.
*/
class sp_lex_branch_instr : public sp_lex_instr, public sp_branch_instr {
protected:
sp_lex_branch_instr(uint ip, sp_pcontext *ctx, LEX *lex, Item *expr_item,
LEX_CSTRING expr_query)
: sp_lex_instr(ip, ctx, lex, true),
m_dest(0),
m_cont_dest(0),
m_optdest(nullptr),
m_cont_optdest(nullptr),
m_expr_item(expr_item),
m_expr_query(expr_query) {}
sp_lex_branch_instr(uint ip, sp_pcontext *ctx, LEX *lex, Item *expr_item,
LEX_CSTRING expr_query, uint dest)
: sp_lex_instr(ip, ctx, lex, true),
m_dest(dest),
m_cont_dest(0),
m_optdest(nullptr),
m_cont_optdest(nullptr),
m_expr_item(expr_item),
m_expr_query(expr_query) {}
public:
void set_cont_dest(uint cont_dest) { m_cont_dest = cont_dest; }
/////////////////////////////////////////////////////////////////////////
// sp_instr implementation.
/////////////////////////////////////////////////////////////////////////
uint opt_mark(sp_head *sp, List<sp_instr> *leads) override;
void opt_move(uint dst, List<sp_branch_instr> *ibp) override;
uint get_cont_dest() const override { return m_cont_dest; }
/////////////////////////////////////////////////////////////////////////
// sp_lex_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool is_invalid() const override { return m_expr_item == nullptr; }
void invalidate() override {
m_expr_item = nullptr; /* it's already deleted. */
}
LEX_CSTRING get_expr_query() const override { return m_expr_query; }
/////////////////////////////////////////////////////////////////////////
// sp_branch_instr implementation.
/////////////////////////////////////////////////////////////////////////
void set_destination(uint old_dest, uint new_dest) override {
if (m_dest == old_dest) m_dest = new_dest;
if (m_cont_dest == old_dest) m_cont_dest = new_dest;
}
void backpatch(uint dest) override {
/* Calling backpatch twice is a logic flaw in jump resolution. */
assert(m_dest == 0);
m_dest = dest;
}
void adjust_sql_command(LEX *lex) override {
assert(lex->sql_command == SQLCOM_SELECT);
lex->sql_command = SQLCOM_END;
}
protected:
/// Where we will go.
uint m_dest;
/// Where continue handlers will go.
uint m_cont_dest;
// The following attributes are used by SP-optimizer.
sp_instr *m_optdest;
sp_instr *m_cont_optdest;
/// Expression item.
Item *m_expr_item;
/// SQL-query corresponding to the expression.
LEX_CSTRING m_expr_query;
};
///////////////////////////////////////////////////////////////////////////
/**
sp_instr_jump_if_not implements SP-instruction, which does the jump if its
SQL-expression is false.
*/
class sp_instr_jump_if_not : public sp_lex_branch_instr {
public:
sp_instr_jump_if_not(uint ip, LEX *lex, Item *expr_item,
LEX_CSTRING expr_query)
: sp_lex_branch_instr(ip, lex->get_sp_current_parsing_ctx(), lex,
expr_item, expr_query) {}
sp_instr_jump_if_not(uint ip, LEX *lex, Item *expr_item,
LEX_CSTRING expr_query, uint dest)
: sp_lex_branch_instr(ip, lex->get_sp_current_parsing_ctx(), lex,
expr_item, expr_query, dest) {}
/////////////////////////////////////////////////////////////////////////
// sp_printable implementation.
/////////////////////////////////////////////////////////////////////////
void print(const THD *thd, String *str) override;
/////////////////////////////////////////////////////////////////////////
// sp_lex_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool exec_core(THD *thd, uint *nextp) override;
bool on_after_expr_parsing(THD *thd) override {
m_expr_item = thd->lex->query_block->single_visible_field();
assert(m_expr_item != nullptr);
return false;
}
#ifdef HAVE_PSI_INTERFACE
public:
PSI_statement_info *get_psi_info() override { return &psi_info; }
static PSI_statement_info psi_info;
#endif
};
///////////////////////////////////////////////////////////////////////////
// Instructions used for the "simple CASE" implementation.
///////////////////////////////////////////////////////////////////////////
/**
sp_instr_set_case_expr is used in the "simple CASE" implementation to evaluate
and store the CASE-expression in the runtime context.
*/
class sp_instr_set_case_expr : public sp_lex_branch_instr {
public:
sp_instr_set_case_expr(uint ip, LEX *lex, uint case_expr_id,
Item *case_expr_item, LEX_CSTRING case_expr_query)
: sp_lex_branch_instr(ip, lex->get_sp_current_parsing_ctx(), lex,
case_expr_item, case_expr_query),
m_case_expr_id(case_expr_id) {}
/////////////////////////////////////////////////////////////////////////
// sp_printable implementation.
/////////////////////////////////////////////////////////////////////////
void print(const THD *thd, String *str) override;
/////////////////////////////////////////////////////////////////////////
// sp_instr implementation.
/////////////////////////////////////////////////////////////////////////
uint opt_mark(sp_head *sp, List<sp_instr> *leads) override;
void opt_move(uint dst, List<sp_branch_instr> *ibp) override;
/////////////////////////////////////////////////////////////////////////
// sp_branch_instr implementation.
/////////////////////////////////////////////////////////////////////////
/*
NOTE: set_destination() and backpatch() are overridden here just because the
m_dest attribute is not used by this class, so there is no need to do
anything about it.
@todo These operations probably should be left as they are (i.e. do not
override them here). The m_dest attribute would be set and not used, but
that should not be a big deal.
@todo This also indicates deficiency of the current SP-istruction class
hierarchy.
*/
void set_destination(uint old_dest, uint new_dest) override {
if (m_cont_dest == old_dest) m_cont_dest = new_dest;
}
void backpatch(uint) override {}
/////////////////////////////////////////////////////////////////////////
// sp_lex_instr implementation.
/////////////////////////////////////////////////////////////////////////
bool exec_core(THD *thd, uint *nextp) override;
bool on_after_expr_parsing(THD *thd) override {
m_expr_item = thd->lex->query_block->single_visible_field();
assert(m_expr_item != nullptr);
return false;
}
private:
/// Identifier (index) of the CASE-expression in the runtime context.
uint m_case_expr_id;
#ifdef HAVE_PSI_INTERFACE
public:
PSI_statement_info *get_psi_info() override { return &psi_info; }
static PSI_statement_info psi_info;
#endif
};