[Commits] 2a2b17c: MDEV-20411 Procedure containing CTE incorrectly stored in mysql.proc

IgorBabaev igor at mariadb.com
Sat Jun 26 04:03:29 EEST 2021


revision-id: 2a2b17ce6dd1a61cd6d26af8211dc93c339956f1 (mariadb-10.2.31-1024-g2a2b17c)
parent(s): 9258cfa4b469ab0d841e32ced7e501e04043637f
author: Igor Babaev
committer: Igor Babaev
timestamp: 2021-06-25 18:03:29 -0700
message:

MDEV-20411 Procedure containing CTE incorrectly stored in mysql.proc

If the first token of the body of a stored procedure was 'WITH' then
the beginning of the body was determined incorrectly and that token was
missing in the string representing the body of the SP in mysql.proc. As a
resultnany call of such procedure failed as the string representing the
body could not be parsed.

The patch corrects the code of the functions get_tok_start() and
get_cpp_tok_start() of the class Lex_input_stream to make them take into
account look ahead tokens. The patch is needed only for 10.2 as this
problem has neen resolved in 10.3+.

---
 mysql-test/r/cte_nonrecursive.result | 55 ++++++++++++++++++++++++++++++++++++
 mysql-test/r/fulltext.result         |  2 +-
 mysql-test/t/cte_nonrecursive.test   | 29 +++++++++++++++++++
 sql/sql_lex.h                        |  4 +--
 4 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/mysql-test/r/cte_nonrecursive.result b/mysql-test/r/cte_nonrecursive.result
index 7c6c6e8..c1d7fd0 100644
--- a/mysql-test/r/cte_nonrecursive.result
+++ b/mysql-test/r/cte_nonrecursive.result
@@ -1964,4 +1964,59 @@ call p1();
 ERROR 42S22: Unknown column 'a' in 'field list'
 drop procedure p1;
 drop table t1,t2;
+#
+# MDEV-20411: SP containing only one SELECT with WITH clause
+#
+create procedure sp1 ()
+with cte as (select 1 as a) select * from cte;
+call sp1();
+a
+1
+call sp1();
+a
+1
+create table t1 (a int);
+insert into t1 values (3), (7), (1), (7), (1), (1), (3), (1), (5);
+create procedure sp2 ()
+with cte as (select * from t1) select * from cte;
+call sp2();
+a
+3
+7
+1
+7
+1
+1
+3
+1
+5
+call sp2();
+a
+3
+7
+1
+7
+1
+1
+3
+1
+5
+create procedure sp3 ()
+with cte as (select * from t1 group by a) select * from cte;
+call sp3();
+a
+1
+3
+5
+7
+call sp3();
+a
+1
+3
+5
+7
+drop procedure sp1;
+drop procedure sp2;
+drop procedure sp3;
+drop table t1;
 # End of 10.2 tests
diff --git a/mysql-test/r/fulltext.result b/mysql-test/r/fulltext.result
index 5118cc3..709e91c 100644
--- a/mysql-test/r/fulltext.result
+++ b/mysql-test/r/fulltext.result
@@ -49,7 +49,7 @@ a	b
 Full-text indexes	are called collections
 Only MyISAM tables	support collections
 select * from t1 where MATCH(a,b) AGAINST ("indexes" IN BOOLEAN MODE WITH QUERY EXPANSION);
-ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'QUERY EXPANSION)' at line 1
+ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WITH QUERY EXPANSION)' at line 1
 explain select * from t1 where MATCH(a,b) AGAINST ("collections");
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	fulltext	a	a	0		1	Using where
diff --git a/mysql-test/t/cte_nonrecursive.test b/mysql-test/t/cte_nonrecursive.test
index f994781..cbe4f8b 100644
--- a/mysql-test/t/cte_nonrecursive.test
+++ b/mysql-test/t/cte_nonrecursive.test
@@ -1463,4 +1463,33 @@ drop procedure p1;
 
 drop table t1,t2;
 
+
+--echo #
+--echo # MDEV-20411: SP containing only one SELECT with WITH clause
+--echo #
+
+create procedure sp1 ()
+with cte as (select 1 as a) select * from cte;
+call sp1();
+call sp1();
+
+create table t1 (a int);
+insert into t1 values (3), (7), (1), (7), (1), (1), (3), (1), (5);
+
+create procedure sp2 ()
+with cte as (select * from t1) select * from cte;
+call sp2();
+call sp2();
+
+create procedure sp3 ()
+with cte as (select * from t1 group by a) select * from cte;
+call sp3();
+call sp3();
+
+drop procedure sp1;
+drop procedure sp2;
+drop procedure sp3;
+
+drop table t1;
+
 --echo # End of 10.2 tests
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index 03c06b9..bdf52e8 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -2176,7 +2176,7 @@ class Lex_input_stream
   /** Get the token start position, in the raw buffer. */
   const char *get_tok_start()
   {
-    return m_tok_start;
+    return lookahead_token >= 0 ? m_tok_start_prev : m_tok_start;
   }
 
   void set_cpp_tok_start(const char *pos)
@@ -2222,7 +2222,7 @@ class Lex_input_stream
   /** Get the token start position, in the pre-processed buffer. */
   const char *get_cpp_tok_start()
   {
-    return m_cpp_tok_start;
+    return lookahead_token >= 0 ? m_cpp_tok_start_prev : m_cpp_tok_start;
   }
 
   /** Get the token end position, in the pre-processed buffer. */


More information about the commits mailing list