diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index b36697d093f..b09c00a883c 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -4277,3 +4277,21 @@ Warnings: Note 1003 select `v2`.`b` AS `b` from `test`.`v2` where 0 DROP VIEW v1,v2; DROP TABLE t1; +# +# LP bug #793386: unexpected 'Duplicate column name ''' error +# at the second execution of a PS using a view +# +CREATE TABLE t1 (f1 int, f2 int, f3 int, f4 int); +CREATE VIEW v1 AS +SELECT t.f1, t.f2, s.f3, s.f4 FROM t1 t, t1 s +WHERE t.f4 >= s.f2 AND s.f3 < 0; +PREPARE stmt1 FROM +"SELECT s.f1 AS f1, s.f2 AS f2, s.f3 AS f3, t.f4 AS f4 + FROM v1 AS t LEFT JOIN v1 AS s ON t.f4=s.f4 WHERE t.f2 <> 1225"; +EXECUTE stmt1; +f1 f2 f3 f4 +EXECUTE stmt1; +f1 f2 f3 f4 +DEALLOCATE PREPARE stmt1; +DROP VIEW v1; +DROP TABLE t1; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index bef2e2a2402..9eafae1729f 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -4213,3 +4213,25 @@ SELECT * FROM (SELECT b FROM v2 WHERE b = 0) t WHERE b; DROP VIEW v1,v2; DROP TABLE t1; + +--echo # +--echo # LP bug #793386: unexpected 'Duplicate column name ''' error +--echo # at the second execution of a PS using a view +--echo # + +CREATE TABLE t1 (f1 int, f2 int, f3 int, f4 int); + +CREATE VIEW v1 AS + SELECT t.f1, t.f2, s.f3, s.f4 FROM t1 t, t1 s + WHERE t.f4 >= s.f2 AND s.f3 < 0; + +PREPARE stmt1 FROM + "SELECT s.f1 AS f1, s.f2 AS f2, s.f3 AS f3, t.f4 AS f4 + FROM v1 AS t LEFT JOIN v1 AS s ON t.f4=s.f4 WHERE t.f2 <> 1225"; +EXECUTE stmt1; +EXECUTE stmt1; + +DEALLOCATE PREPARE stmt1; + +DROP VIEW v1; +DROP TABLE t1; diff --git a/sql/sql_base.cc b/sql/sql_base.cc index fee407f9e0e..5f5e1334148 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -5861,10 +5861,15 @@ find_field_in_view(THD *thd, TABLE_LIST *table_list, */ if (*ref && !(*ref)->is_autogenerated_name) { + if (register_tree_change && + thd->stmt_arena->is_stmt_prepare_or_first_stmt_execute()) + arena= thd->activate_stmt_arena_if_needed(&backup); item->set_name((*ref)->name, (*ref)->name_length, system_charset_info); item->real_item()->set_name((*ref)->name, (*ref)->name_length, system_charset_info); + if (arena) + thd->restore_active_arena(arena, &backup); } if (register_tree_change) thd->change_item_tree(ref, item);