File: sp_instr.h

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (1489 lines) | stat: -rw-r--r-- 47,747 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
/* Copyright (c) 2012, 2025, 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 "m_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"

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);

  /**
    (Re-)parse the query corresponding to this instruction and return a new
    LEX-object.

    @param thd  Thread context.
    @param sp   The stored program.

    @return new LEX-object or NULL in case of failure.
  */
  LEX *parse_expr(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
};

///////////////////////////////////////////////////////////////////////////

/**
  sp_instr_jump_case_when instruction is used in the "simple CASE"
  implementation. It's a jump instruction with the following condition:
    (CASE-expression = WHEN-expression)
  CASE-expression is retrieved from sp_rcontext;
  WHEN-expression is kept by this instruction.
*/
class sp_instr_jump_case_when : public sp_lex_branch_instr {
 public:
  sp_instr_jump_case_when(uint ip, LEX *lex, int case_expr_id,
                          Item *when_expr_item, LEX_CSTRING when_expr_query)
      : sp_lex_branch_instr(ip, lex->get_sp_current_parsing_ctx(), lex,
                            when_expr_item, when_expr_query),
        m_case_expr_id(case_expr_id) {}

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *thd, String *str) override;

  /////////////////////////////////////////////////////////////////////////
  // sp_lex_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool exec_core(THD *thd, uint *nextp) override;

  void invalidate() override {
    // Items should be already deleted in lex-keeper.
    m_case_expr_item = nullptr;
    m_eq_item = nullptr;
    m_expr_item = nullptr;  // it's a WHEN-expression.
  }

  /**
    Build CASE-expression item tree:
      Item_func_eq(case-expression, when-i-expression)

    This function is used for the following form of CASE statement:
      CASE case-expression
        WHEN when-1-expression THEN ...
        WHEN when-2-expression THEN ...
        ...
        WHEN when-n-expression THEN ...
      END CASE

    The thing is that after the parsing we have an item (item tree) for the
    case-expression and for each when-expression. Here we build jump
    conditions: expressions like (case-expression = when-i-expression).

    @param thd  Thread context.

    @return Error flag.
  */
  bool on_after_expr_parsing(THD *thd) override;

 private:
  /// Identifier (index) of the CASE-expression in the runtime context.
  int m_case_expr_id;

  /// Item representing the CASE-expression.
  Item_case_expr *m_case_expr_item;

  /**
    Item corresponding to the main item of the jump-condition-expression:
    it's the equal function (=) in the (case-expression = when-i-expression)
    expression.
  */
  Item *m_eq_item;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////
// SQL-condition handler instructions.
///////////////////////////////////////////////////////////////////////////

class sp_instr_hpush_jump : public sp_instr_jump {
 public:
  sp_instr_hpush_jump(uint ip, sp_pcontext *ctx, sp_handler *handler);

  ~sp_instr_hpush_jump() override;

  void add_condition(sp_condition_value *condition_value);

  sp_handler *get_handler() { return m_handler; }

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *thd, String *str) override;

  /////////////////////////////////////////////////////////////////////////
  // sp_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool execute(THD *thd, uint *nextp) override;

  uint opt_mark(sp_head *sp, List<sp_instr> *leads) override;

  /** Override sp_instr_jump's shortcut; we stop here. */
  uint opt_shortcut_jump(sp_head *, sp_instr *) override { return get_ip(); }

  /////////////////////////////////////////////////////////////////////////
  // sp_branch_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  void backpatch(uint dest) override {
    assert(!m_dest || !m_opt_hpop);
    if (!m_dest)
      m_dest = dest;
    else
      m_opt_hpop = dest;
  }

 private:
  /// Handler.
  sp_handler *m_handler;

  /// hpop marking end of handler scope.
  uint m_opt_hpop;

  // This attribute is needed for SHOW PROCEDURE CODE only (i.e. it's needed in
  // debug version only). It's used in print().
  uint m_frame;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////

class sp_instr_hpop : public sp_instr {
 public:
  sp_instr_hpop(uint ip, sp_pcontext *ctx) : sp_instr(ip, ctx) {}

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *, String *str) override {
    str->append(STRING_WITH_LEN("hpop"));
  }

  /////////////////////////////////////////////////////////////////////////
  // sp_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool execute(THD *thd, uint *nextp) override;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////

class sp_instr_hreturn : public sp_instr_jump {
 public:
  sp_instr_hreturn(uint ip, sp_pcontext *ctx);

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *thd, String *str) override;

  /////////////////////////////////////////////////////////////////////////
  // sp_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool execute(THD *thd, uint *nextp) override;

  /** Override sp_instr_jump's shortcut; we stop here. */
  uint opt_shortcut_jump(sp_head *, sp_instr *) override { return get_ip(); }

  uint opt_mark(sp_head *sp, List<sp_instr> *leads) override;

 private:
  // This attribute is needed for SHOW PROCEDURE CODE only (i.e. it's needed in
  // debug version only). It's used in print().
  uint m_frame;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////
// Cursor implementation.
///////////////////////////////////////////////////////////////////////////

/**
  sp_instr_cpush corresponds to DECLARE CURSOR, implements DECLARE CURSOR and
  OPEN.

  This is the most important instruction in cursor implementation. It is created
  and added to sp_head when DECLARE CURSOR is being parsed. The arena of this
  instruction contains LEX-object for the cursor's SELECT-statement.

  This instruction is actually used to open the cursor.

  execute() operation "implements" DECLARE CURSOR statement -- it merely pushes
  a new cursor object into the stack in sp_rcontext object.

  exec_core() operation implements OPEN statement. It is important to implement
  OPEN statement in this instruction, because OPEN may lead to re-parsing of the
  SELECT-statement. So, the original Arena and parsing context must be used.
*/
class sp_instr_cpush : public sp_lex_instr {
 public:
  sp_instr_cpush(uint ip, sp_pcontext *ctx, LEX *cursor_lex,
                 LEX_CSTRING cursor_query, int cursor_idx)
      : sp_lex_instr(ip, ctx, cursor_lex, true),
        m_cursor_query(cursor_query),
        m_valid(true),
        m_cursor_idx(cursor_idx) {
    /*
      Cursors cause queries to depend on external state, so they are
      noncacheable.
    */
    cursor_lex->safe_to_cache_query = false;
  }

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *thd, String *str) override;

  /////////////////////////////////////////////////////////////////////////
  // sp_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool execute(THD *thd, uint *nextp) 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_cursor_query.str, m_cursor_query.length);
  }

  bool on_after_expr_parsing(THD *) override {
    m_valid = true;
    return false;
  }

 private:
  /// This attribute keeps the cursor SELECT statement.
  LEX_CSTRING m_cursor_query;

  /// Flag if the LEX-object of this instruction is valid or not.
  /// The LEX-object is not valid when metadata have changed.
  bool m_valid;

  /// Used to identify the cursor in the sp_rcontext.
  int m_cursor_idx;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////

/**
  sp_instr_cpop instruction is added at the end of BEGIN..END block.
  It's used to remove declared cursors so that they are not visible any longer.
*/
class sp_instr_cpop : public sp_instr {
 public:
  sp_instr_cpop(uint ip, sp_pcontext *ctx, uint count)
      : sp_instr(ip, ctx), m_count(count) {}

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *thd, String *str) override;

  /////////////////////////////////////////////////////////////////////////
  // sp_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool execute(THD *thd, uint *nextp) override;

 private:
  uint m_count;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////

/**
  sp_instr_copen represents OPEN statement (opens the cursor).
  However, the actual implementation is in sp_instr_cpush::exec_core().
*/
class sp_instr_copen : public sp_instr {
 public:
  sp_instr_copen(uint ip, sp_pcontext *ctx, int cursor_idx)
      : sp_instr(ip, ctx), m_cursor_idx(cursor_idx) {}

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *thd, String *str) override;

  /////////////////////////////////////////////////////////////////////////
  // sp_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool execute(THD *thd, uint *nextp) override;

 private:
  /// Used to identify the cursor in the sp_rcontext.
  int m_cursor_idx;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////

/**
  The instruction corresponds to the CLOSE statement.
  It just forwards the close-call to the appropriate sp_cursor object in the
  sp_rcontext.
*/
class sp_instr_cclose : public sp_instr {
 public:
  sp_instr_cclose(uint ip, sp_pcontext *ctx, int cursor_idx)
      : sp_instr(ip, ctx), m_cursor_idx(cursor_idx) {}

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *thd, String *str) override;

  /////////////////////////////////////////////////////////////////////////
  // sp_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool execute(THD *thd, uint *nextp) override;

 private:
  /// Used to identify the cursor in the sp_rcontext.
  int m_cursor_idx;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////

/**
  The instruction corresponds to the FETCH statement.
  It just forwards the close-call to the appropriate sp_cursor object in the
  sp_rcontext.
*/
class sp_instr_cfetch : public sp_instr {
 public:
  sp_instr_cfetch(uint ip, sp_pcontext *ctx, int cursor_idx)
      : sp_instr(ip, ctx), m_cursor_idx(cursor_idx) {}

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *thd, String *str) override;

  /////////////////////////////////////////////////////////////////////////
  // sp_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool execute(THD *thd, uint *nextp) override;

  void add_to_varlist(sp_variable *var) { m_varlist.push_back(var); }

 private:
  /// List of SP-variables to store fetched values.
  List<sp_variable> m_varlist;

  /// Used to identify the cursor in the sp_rcontext.
  int m_cursor_idx;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

/**
  sp_instr_error just throws an SQL-condition if the execution flow comes to it.
  It's used in the CASE implementation to perform runtime-check that the
  CASE-expression is handled by some WHEN/ELSE clause.
*/
class sp_instr_error : public sp_instr {
 public:
  sp_instr_error(uint ip, sp_pcontext *ctx, int errcode)
      : sp_instr(ip, ctx), m_errcode(errcode) {}

  /////////////////////////////////////////////////////////////////////////
  // sp_printable implementation.
  /////////////////////////////////////////////////////////////////////////

  void print(const THD *thd, String *str) override;

  /////////////////////////////////////////////////////////////////////////
  // sp_instr implementation.
  /////////////////////////////////////////////////////////////////////////

  bool execute(THD *, uint *nextp) override {
    my_error(m_errcode, MYF(0));
    *nextp = get_ip() + 1;
    return true;
  }

  uint opt_mark(sp_head *, List<sp_instr> *) override {
    m_marked = true;
    return UINT_MAX;
  }

 private:
  /// The error code, which should be raised by this instruction.
  int m_errcode;

#ifdef HAVE_PSI_INTERFACE
 public:
  PSI_statement_info *get_psi_info() override { return &psi_info; }

  static PSI_statement_info psi_info;
#endif
};

///////////////////////////////////////////////////////////////////////////

#endif  // _SP_INSTR_H_