Commit graph

2096 commits

Author SHA1 Message Date
Alexander Barkov
e1cff0ac5d MDEV-12441 Variables declared after cursors with parameters lose values
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.
2017-04-05 15:03:02 +04:00
Alexander Barkov
d433277f53 A cleanup for MDEV-10914 ROW data type for stored routine variables
Addressing Monty's review suggestions
2017-04-05 15:03:02 +04:00
Alexander Barkov
cae6bf2b9c Cleanup for MDEV-10581 sql_mode=ORACLE: Explicit cursor FOR LOOP
Addressing Monty's review suggestions.
2017-04-05 15:03:02 +04:00
Alexander Barkov
ce4b291b51 A cleanup patch for MDEV-12011 sql_mode=ORACLE: cursor%ROWTYPE in variable declarations
Addressing Monty's review suggestions
2017-04-05 15:03:02 +04:00
Alexander Barkov
48a7ea6da3 Uninitialized Column_definition::pack_flag for ROW-type SP variables and their fields
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.
2017-04-05 15:03:01 +04:00
Alexander Barkov
ec19e48021 MDEV-12314 Implicit cursor FOR LOOP for cursors with parameters 2017-04-05 15:03:00 +04:00
Alexander Barkov
9dfe7bf86d MDEV-10598 Variable declarations can go after cursor declarations
Based on a contributed patch from Jerome Brauge.
2017-04-05 15:03:00 +04:00
Alexander Barkov
84c55a5668 MDEV-10581 sql_mode=ORACLE: Explicit cursor FOR LOOP
MDEV-12098 sql_mode=ORACLE: Implicit cursor FOR loop
2017-04-05 15:02:59 +04:00
Alexander Barkov
f429b5a834 MDEV-12011 sql_mode=ORACLE: cursor%ROWTYPE in variable declarations
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.
2017-04-05 15:02:59 +04:00
Alexander Barkov
1b8a0c879d MDEV-12133 sql_mode=ORACLE: table%ROWTYPE in variable declarations 2017-04-05 15:02:59 +04:00
Alexander Barkov
29e7cf01c3 MDEV-12107 sql_mode=ORACLE: Inside routines the CALL keywoard is optional 2017-04-05 15:02:58 +04:00
halfspawn
af7f287b3b MDEV-10697 GOTO statement 2017-04-05 15:02:57 +04:00
Alexander Barkov
72f43df623 MDEV-10914 ROW data type for stored routine variables 2017-04-05 15:02:56 +04:00
Alexander Barkov
46255b0c0d Fixing that LEX::sp_add_for_loop_variable() did not initialize pack_flag
This fixes compat/oracle.sp test failure in release builds.
2017-04-05 15:02:55 +04:00
Alexander Barkov
46d076d67a MDEV-10577 sql_mode=ORACLE: %TYPE in variable declarations 2017-04-05 15:02:54 +04:00
Alexander Barkov
f8a714c848 MDEV-10597 Cursors with parameters 2017-04-05 15:02:53 +04:00
Alexander Barkov
ffca1e4830 MDEV-10578 sql_mode=ORACLE: SP control functions SQLCODE, SQLERRM 2017-04-05 15:02:52 +04:00
Alexander Barkov
054d00a9a3 A fix for MDEV-10411 Providing compatibility for basic PL/SQL constructs (Part 6: Assignment operator)
Fixed that a crash in this script:

SET sql_mode=ORACLE;
max_sort_length:= 1024;
2017-04-05 15:02:52 +04:00
Alexander Barkov
7fa1ad14dc MDEV-10840 sql_mode=ORACLE: RAISE statement for predefined exceptions 2017-04-05 15:02:51 +04:00
Alexander Barkov
ec527face3 MDEV-10801 sql_mode: dynamic SQL placeholders 2017-04-05 15:02:49 +04:00
Alexander Barkov
f564ceb473 Fixed a crash in a EXIT/CONTINUE with an unknown identifier in the WHEN clause
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.
2017-04-05 15:02:48 +04:00
Alexander Barkov
cfb6345982 Fixed that 'FOR i IN 1..10' with no spaces around '..' returned a syntax error.
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 '.'.
2017-04-05 15:02:48 +04:00
Alexander Barkov
5721ea6ab7 MDEV-10579 sql_mode=ORACLE: Triggers: Understand :NEW.c1 and :OLD.c1 instead of NEW.c1 and OLD.c1 2017-04-05 15:02:47 +04:00
Alexander Barkov
ca242117ce MDEV-10411 Providing compatibility for basic PL/SQL constructs
Part 19: CONTINUE statement
2017-04-05 15:02:47 +04:00
Alexander Barkov
442ea81ed3 MDEV-10411 Providing compatibility for basic PL/SQL constructs
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.
2017-04-05 15:02:47 +04:00
Alexander Barkov
c570636ba2 MDEV-10580 sql_mode=ORACLE: FOR loop statement
Adding non-labeled FOR LOOP statement.
2017-04-05 15:02:46 +04:00
Alexander Barkov
71a0a12e61 Changing a LEX::sp_variable_declarations_finalize() parameter
from "const Lex_field_type_st &" to "const Column_definition &".
2017-04-05 15:02:46 +04:00
Alexander Barkov
8ec4cf1f01 Refactoring for MDEV-10580 sql_mode=ORACLE: FOR loop statement
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.
2017-04-05 15:02:45 +04:00
Alexander Barkov
a83d0aee96 MDEV-10411 Providing compatibility for basic PL/SQL constructs
Part 5: EXIT statement

Adding optional WHEN clause:

EXIT [label] [WHEN expr]
2017-04-05 15:02:44 +04:00
Alexander Barkov
8feb984211 MDEV-10411 Providing compatibility for basic PL/SQL constructs
Part 5: EXIT statement

Adding unconditional EXIT statement:

  EXIT [ label ]

Conditional EXIT statements with WHERE clause
will be added in a separate patch.
2017-04-05 15:02:44 +04:00
Alexander Barkov
765d9d6429 MDEV-10411 Providing compatibility for basic PL/SQL constructs
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.
2017-04-05 15:02:44 +04:00
Alexander Barkov
f37a943f49 MDEV-10411 Providing compatibility for basic PL/SQL constructs
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.
2017-04-05 15:02:43 +04:00
Alexander Barkov
d2b007d6bc Optimization for MDEV-10411 Providing compatibility for basic PL/SQL constructs
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.
2017-04-05 15:02:43 +04:00
Alexander Barkov
81ba971d03 MDEV-10411 Providing compatibility for basic PL/SQL constructs
- 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)
2017-04-05 15:02:42 +04:00
Alexander Barkov
f3a0df72f2 Reusing code: Adding LEX::make_sp_head() and LEX::make_sp_head_no_recursive() 2017-04-05 15:02:41 +04:00
Alexander Barkov
365e0b3178 sql_lex.yy / sql_yacc_ora.yy refactoring for MDEV-10411.
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
2017-04-05 15:02:40 +04:00
Alexander Barkov
36b80caed1 Moving the code from *.yy to new methods to LEX and sp_context
Adding:
  LEX::sp_variable_declarations_init()
  LEX::sp_variable_declarations_finalize()
  LEX::sp_handler_declaration_init()
  LEX::sp_handler_declaration_finalize()
  LEX::sp_declare_cursor()
  sp_context::declare_condition()
2017-04-05 15:02:39 +04:00
Alexander Barkov
892af78085 MDEV-10411 Providing compatibility for basic PL/SQL constructs
Part6: assignment operator

  var:= 10;
2017-04-05 15:02:39 +04:00
Alexander Barkov
7e10e38825 MDEV-10411 Providing compatibility for basic PL/SQL constructs
Part2: Different order of IN, OUT, INOUT keywords in CREATE PROCEDURE params
2017-04-05 15:02:38 +04:00
Alexander Barkov
c21fc0085b Moving the code from my_parse_error() to THD::parse_error().
Reusing THD::parse_error() in sql_yacc.yy and sql_yacc_ora.yy
2017-04-05 15:02:37 +04:00
Alexander Barkov
9f6aca198c Adding an alternative grammar file sql_yacc_ora.yy for sql_mode=ORACLE
- 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.
2017-04-05 15:01:59 +04:00
Alexander Barkov
f1b0b04651 MDEV-12411 Remove Lex::text_string_is_7bit 2017-04-04 16:54:02 +04:00
Alexander Barkov
1694c0e8d8 MDEV-12394 Add function is_native_function_with_warn() 2017-03-29 18:15:28 +04:00
Alexander Barkov
fb43180c4f Merge remote-tracking branch 'origin/10.2' into bb-10.2-ext 2017-03-29 07:24:05 +04:00
Alexander Barkov
c5520a37d6 MDEV-12390 Wrong error line numbers reported with sql_mode=IGNORE_SPACE 2017-03-29 07:21:34 +04:00
Alexander Barkov
7d0c354f5c Merge remote-tracking branch 'origin/10.2' into bb-10.2-ext 2017-03-18 14:20:06 +04:00
Oleksandr Byelkin
05d3c3d3f7 MDEV-10141: Add support for INTERSECT (and common parts for EXCEPT)
MDEV-10140: Add support for EXCEPT
2017-03-14 11:52:00 +01:00
Vladislav Vaintroub
f2fe5cb282 Fix several compile warnings on Windows 2017-03-10 19:07:07 +00:00
Sergei Golubchik
f3914d10b6 Merge branch 'bb-10.2-serg-merge' into 10.2 2017-02-11 09:45:34 +01:00
Sergei Golubchik
2195bb4e41 Merge branch '10.1' into 10.2 2017-02-10 17:01:45 +01:00