summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGalina Shalygina <galina.shalygina@mariadb.com>2018-02-20 01:16:50 +0200
committerGalina Shalygina <galina.shalygina@mariadb.com>2018-02-20 01:16:50 +0200
commita128fe4346a466aa8e059baa620a3b689fa21241 (patch)
treef31e3486a56a466d8770b99bdc232b7a4d544f40
parent84e8e07e8e185fe98ad2932bd32e94424fb9383a (diff)
downloadmariadb-git-a128fe4346a466aa8e059baa620a3b689fa21241.tar.gz
MDEV-14297: Lost name of a explicitly named CTE column used in
the non-recursive CTE via prepared statement The problem appears as the column names of the CTE were allocated on the wrong MEMROOT and after the preparation of the statement they disappear. To fix it in the procedure With_element::rename_columns_of_derived_unit the CTE column names are now allocated in the permanent MEMROOT for the prepared statements and stored procedures.
-rw-r--r--mysql-test/r/cte_nonrecursive.result27
-rw-r--r--mysql-test/t/cte_nonrecursive.test25
-rw-r--r--sql/sql_cte.cc7
3 files changed, 59 insertions, 0 deletions
diff --git a/mysql-test/r/cte_nonrecursive.result b/mysql-test/r/cte_nonrecursive.result
index 33e14af900a..f2bfce9f6ba 100644
--- a/mysql-test/r/cte_nonrecursive.result
+++ b/mysql-test/r/cte_nonrecursive.result
@@ -1398,3 +1398,30 @@ i
2
3
DROP TABLE t1;
+#
+# MDEV-14297: Lost name of a explicitly named CTE column used in
+# the non-recursive CTE via prepared statement
+#
+CREATE TABLE t1 (i int);
+INSERT INTO t1 VALUES (1),(2),(3);
+PREPARE stmt FROM "WITH cte(a) AS (SELECT 1) SELECT * FROM cte";
+EXECUTE stmt;
+a
+1
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "CREATE VIEW v1 AS WITH cte(a) AS (SELECT 1) SELECT * FROM cte";
+EXECUTE stmt;
+SELECT * FROM v1;
+a
+1
+DEALLOCATE PREPARE stmt;
+PREPARE stmt FROM "CREATE VIEW v2 AS WITH cte(a) AS (SELECT * FROM t1) SELECT * FROM cte";
+EXECUTE stmt;
+SELECT * FROM v2;
+a
+1
+2
+3
+DEALLOCATE PREPARE stmt;
+DROP TABLE t1;
+DROP VIEW v1,v2;
diff --git a/mysql-test/t/cte_nonrecursive.test b/mysql-test/t/cte_nonrecursive.test
index 05de03fc7af..3d073183877 100644
--- a/mysql-test/t/cte_nonrecursive.test
+++ b/mysql-test/t/cte_nonrecursive.test
@@ -964,3 +964,28 @@ WITH RECURSIVE c1 AS (WITH c3 AS (SELECT * FROM c2) SELECT * FROM c3),
SELECT * FROM c1;
DROP TABLE t1;
+
+--echo #
+--echo # MDEV-14297: Lost name of a explicitly named CTE column used in
+--echo # the non-recursive CTE via prepared statement
+--echo #
+
+CREATE TABLE t1 (i int);
+INSERT INTO t1 VALUES (1),(2),(3);
+
+PREPARE stmt FROM "WITH cte(a) AS (SELECT 1) SELECT * FROM cte";
+EXECUTE stmt;
+DEALLOCATE PREPARE stmt;
+
+PREPARE stmt FROM "CREATE VIEW v1 AS WITH cte(a) AS (SELECT 1) SELECT * FROM cte";
+EXECUTE stmt;
+SELECT * FROM v1;
+DEALLOCATE PREPARE stmt;
+
+PREPARE stmt FROM "CREATE VIEW v2 AS WITH cte(a) AS (SELECT * FROM t1) SELECT * FROM cte";
+EXECUTE stmt;
+SELECT * FROM v2;
+DEALLOCATE PREPARE stmt;
+
+DROP TABLE t1;
+DROP VIEW v1,v2;
diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc
index ac27f1713f9..dd46295d799 100644
--- a/sql/sql_cte.cc
+++ b/sql/sql_cte.cc
@@ -910,12 +910,19 @@ With_element::rename_columns_of_derived_unit(THD *thd,
my_error(ER_WITH_COL_WRONG_LIST, MYF(0));
return true;
}
+
+ Query_arena *arena, backup;
+ arena= thd->activate_stmt_arena_if_needed(&backup);
+
/* Rename the columns of the first select in the unit */
while ((item= it++, name= nm++))
{
item->set_name(thd, name->str, (uint) name->length, system_charset_info);
item->is_autogenerated_name= false;
}
+
+ if (arena)
+ thd->restore_active_arena(arena, &backup);
}
else
make_valid_column_names(thd, select->item_list);