Skip to content
Permalink
Browse files
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.
  • Loading branch information
shagalla committed Feb 19, 2018
1 parent 84e8e07 commit a128fe4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
@@ -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;
@@ -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;
@@ -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);

0 comments on commit a128fe4

Please sign in to comment.