Parse context frames (sp_pcontext) can have holes in variable run-time offsets,
the missing offsets reside on the children contexts in such cases.
Example:
CREATE PROCEDURE p1() AS
x0 INT:=100; -- context 0, position 0, run-time 0
CURSOR cur(
p0 INT, -- context 1, position 0, run-time 1
p1 INT -- context 1, position 1, run-time 2
) IS SELECT p0, p1;
x1 INT:=101; -- context 0, position 1, run-time 3
BEGIN
...
END;
Fixing a few methods to take this into account:
- sp_pcontext::find_variable()
- sp_pcontext::retrieve_field_definitions()
- LEX::sp_variable_declarations_init()
- LEX::sp_variable_declarations_finalize()
- LEX::sp_variable_declarations_rowtype_finalize()
- LEX::sp_variable_declarations_with_ref_finalize()
Adding a convenience method:
sp_pcontext::get_last_context_variable(uint offset_from_the_end);
to access variables from the end, rather than from the beginning.
This helps to loop through the context variable array (m_vars)
on the fragment that does not have any holes.
Additionally, renaming sp_pcontext::find_context_variable() to
sp_pcontext::get_context_variable(). This method simply returns
the variable by its index. So let's rename to avoid assumptions
that some heavy lookup is going on inside.
Fixed that the Column_definition::pack_flag member corresponding to
ROW-type SP variables and their fields was not properly initialized.
This lead to sporadic test failures. Valgrind complained about jumps
depending on uninitialized value in VALGRIND builds.
This patch makes sure that sp_head::fill_spvar_definition() is always
called for ROW variables and their fields.
Additionally, fixed that a function with a scalar parameter
erroneously acceptes ROWs with one fields. Now an error is returned.
Implementing cursor%ROWTYPE variables, according to the task description.
This patch includes a refactoring in how sp_instr_cpush and sp_instr_copen
work. This is needed to implement MDEV-10598 later easier, to allow variable
declarations go after cursor declarations (which is currently not allowed).
Before this patch, sp_instr_cpush worked as a Query_arena associated with
the cursor. sp_instr_copen::execute() switched to the sp_instr_cpush's
Query_arena when executing the cursor SELECT statement.
Now the Query_arena associated with the cursor is stored inside an instance
of a new class sp_lex_cursor (a LEX descendand) that contains the cursor SELECT
statement.
This simplifies the implementation, because:
- It's easier to follow the code when everything related to execution
of the cursor SELECT statement is stored inside the same sp_lex_cursor
object (rather than distributed between LEX and sp_instr_cpush).
- It's easier to link an sp_instr_cursor_copy_struct to
sp_lex_cursor rather than to sp_instr_cpush.
- Also, it allows to perform sp_instr_cursor_copy_struct::exec_core()
without having a pointer to sp_instr_cpush, using a pointer to sp_lex_cursor
instead. This will be important for MDEV-10598, because sp_instr_cpush will
happen *after* sp_instr_cursor_copy_struct.
After MDEV-10598 is done, this declaration:
DECLARE
CURSOR cur IS SELECT * FROM t1;
rec cur%ROWTYPE;
BEGIN
OPEN cur;
FETCH cur INTO rec;
CLOSE cur;
END;
will generate about this code:
+-----+--------------------------+
| Pos | Instruction |
+-----+--------------------------+
| 0 | cursor_copy_struct rec@0 | Points to sp_cursor_lex through m_lex_keeper
| 1 | set rec@0 NULL |
| 2 | cpush cur@0 | Points to sp_cursor_lex through m_lex_keeper
| 3 | copen cur@0 | Points to sp_cursor_lex through m_cursor
| 4 | cfetch cur@0 rec@0 |
| 5 | cclose cur@0 |
| 6 | cpop 1 |
+-----+--------------------------+
Notice, "cursor_copy_struct" and "set" will go before "cpush".
Instructions at positions 0, 2, 3 point to the same sp_cursor_lex instance.
The crash happened because of a wrong reset_lex() .. restore_lex() sequence.
The Item in WHERE clause and the corresponding sp_instr_jump_if_not() were
erroneously created using different LEX.
This is a fix for "MDEV-10580 sql_mode=ORACLE: FOR loop statement"
The tokenizer now treats digits followed by two dots (e.g. '1..')
as an integer number '1' followed by DOT_DOT_SYM.
Previously this sequence was treated as a double number '1.' followed by '.'.
Fixed that the ITERATE statement inside a FOR LOOP statement did not
increment the index variable before jumping to the beginning
of the loop, which caused the loop to repeat endlessly.
Adding methods:
- LEX::sp_while_loop_expression()
- LEX::sp_while_loop_finalize()
to reuse code between sql_yacc.yy and sql_yacc_ora.yy.
FOR loop will also reuse these methods.
Part 5: EXIT statement
Adding unconditional EXIT statement:
EXIT [ label ]
Conditional EXIT statements with WHERE clause
will be added in a separate patch.
Moving similar code from sql_yacc.yy and sql_yacc_ora.yy to methods:
LEX::maybe_start_compound_statement()
LEX::sp_push_loop_label()
LEX::sp_push_loop_empty_label()
LEX::sp_pop_loop_label()
LEX::sp_pop_loop_empty_label()
The EXIT statement will also reuse this code.
Moving the code from *.yy to methods:
LEX::sp_change_context()
LEX::sp_leave_statement()
LEX::sp_iterate_statement()
to reuse the same code between LEAVE and ITERATE statements.
EXIT statement will also reuse the same code.
When processing an SP body:
CREATE PROCEDURE p1 (parameters)
AS [ declarations ]
BEGIN statements
[ EXCEPTION exceptions ]
END;
the parser generates two "jump" instructions:
- from the end of "declarations" to the beginning of EXCEPTION
- from the end of EXCEPTION to "statements"
These jumps are useless if EXCEPTION does not exist.
This patch makes sure that these two "jump" instructions are
generated only if EXCEPTION really exists.
- Part 9: EXCEPTION handlers
The top-most stored routine blocks now support EXCEPTION clause
in its correct place:
AS [ declarations ]
BEGIN statements
[ EXCEPTION exceptions ]
END
Inner block will be done in a separate commit.
- Part 14: IN OUT instead of INOUT (in SP parameter declarations)
1. Adding const qualifiers into a few method parameters.
2. Adding methods:
- sp_label::block_label_declare()
- LEX::sp_block_init()
- LEX::sp_block_finalize()
to share more code between the files sql_yacc.yy and sql_yacc_ora.yy,
as well as between the rules sp_labeled_block, sp_unlabeled_block,
sp_unlabeled_block_not_atomic.
3. sql_yacc.yy, sql_yacc_ora.yy changes:
- Removing sp_block_content
- Reorganizing the grammar so the rules sp_labeled_block,
sp_unlabeled_block, sp_unlabeled_block_not_atomic now
contain both BEGIN_SYM and END keywords. Previously,
BEGIN_SYM and END resided in different rules.
This change makes the grammar easier to read,
as well as simplifies adding Oracle-style DECLARE section (coming soon):
DECLARE
..
BEGIN
..
END;
Good side effects:
- SP block related grammar does not use Lex->name any more.
- The "splabel" member was removed from %union
- Adding a new grammar file sql_yacc_ora.yy, which is currently
almost a full copy of sql_yacc.yy.
Note, it's now assumed that sql_yacc.yy and sql_yacc_ora.yy
use the same set of %token directives and exactly the same
%union directive.
These declarations should eventually be moved into a shared
included file, to make sure that sql_yacc.h and sql_yacc_ora.h
are compatible.
- Removing the "-p MYSQL" flag from cmake/bison.cmake, using
the %name-prefix directive inside sql_yacc.yy and sql_yacc_ora.yy instead
- Adding other CMake related changes to build sql_yacc_ora.o
form sql_yacc_ora.yy
- Adding NUMBER(M,N) as a synonym to DECIMAL(M,N) as the first
Oracle compatibility syntax understood in sql_mode=ORACLE.
- Adding prototypes to functions add_virtual_expression()
and handle_sql2003_note184_exception(), so they can be used
in both sql_yacc.yy and sql_yacc_ora.yy.
- Adding a new test suite compat/oracle, with the first test "type_number".
Use this:
./mtr compat/oracle.type_number # to run a single test
./mtr --suite=compat/oracle # to run the entire new suite
- Adding compat/oracle into the list of default suites,
so BuildBot can run it automatically on pushes.