commit 06d1ef2e3b5c9f30f059af0d0172f3cb19a2dd16 Author: kaiwang.ckw Date: Wed Feb 19 01:34:25 2020 +0800 Bug #98635 Explain neglects db part of identifier after create view Problem ------- Some commands expect identifiers to be printed correctly, including SHOW CREATE, SHOW TABLES and CREATE VIEW, as described by LEX::need_correct_ident(). However, when these command are issued, they change the cached TABLE object, and that change is not reset properly thus might unexpectedly applies to subsequent commands. As a result, the expanded query in EXPLAIN output is kind of unpredictable. Fix --- Ensure resetting alias_name_used in TABLE::init(). diff --git a/mysql-test/r/open_table.result b/mysql-test/r/open_table.result new file mode 100644 index 00000000000..ba454dc955c --- /dev/null +++ b/mysql-test/r/open_table.result @@ -0,0 +1,27 @@ +# +# BUG#98635 Explain neglects db part of identifier after create view +# +CREATE TABLE t1 ( +pk INT, +col_int_key INT, +col_int_nokey INT, +col_varchar_key VARCHAR(10), +col_varchar_nokey VARCHAR(10), +KEY col_int_key (col_int_key), +KEY col_varchar_key (col_varchar_key) +); +INSERT INTO t1 VALUES (), (); +CREATE VIEW v1 AS +SELECT alias1.col_int_nokey AS field1, +(SELECT alias2.col_int_key +FROM t1 AS alias2 +WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey +) AS field2 +FROM t1 AS alias1; +DROP VIEW v1; +EXPLAIN SELECT pk AS foo, col_int_key AS bar FROM t1; +id select_type table partitions type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 2 100.00 NULL +Warnings: +Note 1003 /* select#1 */ select `test`.`t1`.`pk` AS `foo`,`test`.`t1`.`col_int_key` AS `bar` from `test`.`t1` +DROP TABLE t1; diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 09432d86b07..c3764d2409c 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -5581,7 +5581,7 @@ id select_type table partitions type possible_keys key key_len ref rows filtered 2 DEPENDENT SUBQUERY t2 NULL ALL NULL NULL NULL NULL # # Using where Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,(/* select#2 */ select count(0) from `test`.`t1` `t2` where (`t2`.`a` = `test`.`t1`.`a`)) AS `s` from `test`.`t1` +Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,(/* select#2 */ select count(0) from `test`.`t1` `t2` where (`test`.`t2`.`a` = `test`.`t1`.`a`)) AS `s` from `test`.`t1` DROP VIEW v1; CREATE VIEW v1 AS SELECT a, b, EXISTS (SELECT COUNT(*) FROM t1 AS t2 WHERE a=t1.a) AS s FROM t1; @@ -5642,7 +5642,7 @@ id select_type table partitions type possible_keys key key_len ref rows filtered 2 DEPENDENT SUBQUERY t2 NULL ALL NULL NULL NULL NULL # # Using where Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,exists(/* select#2 */ select count(0) from `test`.`t1` `t2` where (`t2`.`a` = `test`.`t1`.`a`)) AS `s` from `test`.`t1` +Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,exists(/* select#2 */ select count(0) from `test`.`t1` `t2` where (`test`.`t2`.`a` = `test`.`t1`.`a`)) AS `s` from `test`.`t1` DROP VIEW v1; CREATE VIEW v1 AS SELECT (SELECT a FROM t2 WHERE a=FLOOR(COUNT(t1.a)/2)) AS s FROM t1; diff --git a/mysql-test/t/open_table.test b/mysql-test/t/open_table.test new file mode 100644 index 00000000000..1fc2a0b32d3 --- /dev/null +++ b/mysql-test/t/open_table.test @@ -0,0 +1,30 @@ + +--echo # +--echo # BUG#98635 Explain neglects db part of identifier after create view +--echo # + +CREATE TABLE t1 ( + pk INT, + col_int_key INT, + col_int_nokey INT, + col_varchar_key VARCHAR(10), + col_varchar_nokey VARCHAR(10), + KEY col_int_key (col_int_key), + KEY col_varchar_key (col_varchar_key) +); +INSERT INTO t1 VALUES (), (); + +CREATE VIEW v1 AS +SELECT alias1.col_int_nokey AS field1, + (SELECT alias2.col_int_key + FROM t1 AS alias2 + WHERE alias1.col_varchar_key <= alias1.col_varchar_nokey + ) AS field2 +FROM t1 AS alias1; + +DROP VIEW v1; + +EXPLAIN SELECT pk AS foo, col_int_key AS bar FROM t1; + +DROP TABLE t1; + diff --git a/sql/table.cc b/sql/table.cc index a2057a01075..0ebb1f8c8b3 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -4073,6 +4073,9 @@ void TABLE::init(THD *thd, TABLE_LIST *tl) { if (thd->lex->need_correct_ident()) alias_name_used = my_strcasecmp(table_alias_charset, s->table_name.str, tl->alias); + else + alias_name_used = false; + /* Fix alias if table name changes. */ if (strcmp(alias, tl->alias)) { size_t length = strlen(tl->alias) + 1;