[Commits] 231c344: MDEV-21603 Crashing SHOW TABLES with derived table in WHERE condition

IgorBabaev igor at mariadb.com
Tue May 4 07:44:52 EEST 2021


revision-id: 231c344969e9dfc263066ee0335c1fe39a2c6764 (mariadb-10.2.31-918-g231c344)
parent(s): b1ac251bf1668d5d2472c2c520d6db71fb835065
author: Igor Babaev
committer: Igor Babaev
timestamp: 2021-05-03 21:44:52 -0700
message:

MDEV-21603 Crashing SHOW TABLES with derived table in WHERE condition

---
 mysql-test/r/derived.result | 28 ++++++++++++++++++++++++++++
 mysql-test/t/derived.test   | 25 +++++++++++++++++++++++++
 sql/sql_base.cc             |  1 -
 sql/sql_derived.cc          |  6 ------
 sql/sql_prepare.cc          |  8 ++++----
 sql/sql_show.cc             |  4 ++--
 6 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/mysql-test/r/derived.result b/mysql-test/r/derived.result
index 2106ba5..c374669 100644
--- a/mysql-test/r/derived.result
+++ b/mysql-test/r/derived.result
@@ -1204,5 +1204,33 @@ REPLACE INTO v2 ( SELECT * FROM v4 ) UNION ( SELECT f FROM v2 );
 drop view v1,v2,v3,v4;
 drop table t1,t2,t3;
 #
+# MDEV-21603: materialized derived used in SHOW TABLES
+#
+create table t1 (nm varchar(32), a int);
+insert into t1 values ('1',1),('2',2),('3',3);
+use mysql;
+show tables
+where tables_in_mysql in (select *
+from (select nm from test.t1 group by nm) dt);
+Tables_in_mysql
+show fields from test.t1
+where Field in (select * from (select nm from test.t1 group by nm) dt);
+Field	Type	Null	Key	Default	Extra
+insert into test.t1 values ('nm',0);
+show fields from test.t1
+where Field in (select * from (select nm from test.t1 group by nm) dt);
+Field	Type	Null	Key	Default	Extra
+nm	varchar(32)	YES		NULL	
+show fields from test.t1
+where Field in
+(select * from (select column_name from information_schema.columns
+where table_name='t1'
+                            group by column_name) dt);
+Field	Type	Null	Key	Default	Extra
+nm	varchar(32)	YES		NULL	
+a	int(11)	YES		NULL	
+use test;
+drop table t1;
+#
 # End of 10.2 tests
 #
diff --git a/mysql-test/t/derived.test b/mysql-test/t/derived.test
index 6d9d5e2..fc03acc 100644
--- a/mysql-test/t/derived.test
+++ b/mysql-test/t/derived.test
@@ -1037,5 +1037,30 @@ drop view v1,v2,v3,v4;
 drop table t1,t2,t3;
 
 --echo #
+--echo # MDEV-21603: materialized derived used in SHOW TABLES
+--echo #
+
+create table t1 (nm varchar(32), a int);
+insert into t1 values ('1',1),('2',2),('3',3);
+use mysql;
+show tables
+  where tables_in_mysql in (select *
+                              from (select nm from test.t1 group by nm) dt);
+show fields from test.t1
+   where Field in (select * from (select nm from test.t1 group by nm) dt);
+insert into test.t1 values ('nm',0);
+show fields from test.t1
+   where Field in (select * from (select nm from test.t1 group by nm) dt);
+
+show fields from test.t1
+   where Field in
+        (select * from (select column_name from information_schema.columns
+                          where table_name='t1'
+                            group by column_name) dt);
+
+use test;
+drop table t1;
+
+--echo #
 --echo # End of 10.2 tests
 --echo #
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index 3403f9e..16689f8 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -4939,7 +4939,6 @@ bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags,
   uint counter;
   MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
   DBUG_ENTER("open_normal_and_derived_tables");
-  DBUG_ASSERT(!thd->fill_derived_tables());
   if (open_tables(thd, &tables, &counter, flags, &prelocking_strategy) ||
       mysql_handle_derived(thd->lex, dt_phases))
     goto end;
diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc
index 5f90f2f..fc7dffe 100644
--- a/sql/sql_derived.cc
+++ b/sql/sql_derived.cc
@@ -70,7 +70,6 @@ bool
 mysql_handle_derived(LEX *lex, uint phases)
 {
   bool res= FALSE;
-  THD *thd= lex->thd;
   DBUG_ENTER("mysql_handle_derived");
   DBUG_PRINT("enter", ("phases: 0x%x", phases));
   if (!lex->derived_tables)
@@ -85,8 +84,6 @@ mysql_handle_derived(LEX *lex, uint phases)
       break;
     if (!(phases & phase_flag))
       continue;
-    if (phase_flag >= DT_CREATE && !thd->fill_derived_tables())
-      break;
 
     for (SELECT_LEX *sl= lex->all_selects_list;
 	 sl && !res;
@@ -169,7 +166,6 @@ bool
 mysql_handle_single_derived(LEX *lex, TABLE_LIST *derived, uint phases)
 {
   bool res= FALSE;
-  THD *thd= lex->thd;
   uint8 allowed_phases= (derived->is_merged_derived() ? DT_PHASES_MERGE :
                          DT_PHASES_MATERIALIZE);
   DBUG_ENTER("mysql_handle_single_derived");
@@ -192,8 +188,6 @@ mysql_handle_single_derived(LEX *lex, TABLE_LIST *derived, uint phases)
     if (phase_flag != DT_PREPARE &&
         !(allowed_phases & phase_flag))
       continue;
-    if (phase_flag >= DT_CREATE && !thd->fill_derived_tables())
-      break;
 
     if ((res= (*processors[phase])(lex->thd, lex, derived)))
       break;
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index 56a3875..a8a6710 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -1659,7 +1659,7 @@ static int mysql_test_select(Prepared_statement *stmt,
   }
 
   if (open_normal_and_derived_tables(thd, tables,  MYSQL_OPEN_FORCE_SHARED_MDL,
-                                     DT_INIT | DT_PREPARE | DT_CREATE))
+                                     DT_INIT | DT_PREPARE))
     goto error;
 
   thd->lex->used_tables= 0;                        // Updated by setup_fields
@@ -1721,7 +1721,7 @@ static bool mysql_test_do_fields(Prepared_statement *stmt,
     DBUG_RETURN(TRUE);
 
   if (open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL,
-                                     DT_INIT | DT_PREPARE | DT_CREATE))
+                                     DT_INIT | DT_PREPARE))
     DBUG_RETURN(TRUE);
   DBUG_RETURN(setup_fields(thd, Ref_ptr_array(),
                            *values, MARK_COLUMNS_NONE, 0, NULL, 0));
@@ -1753,7 +1753,7 @@ static bool mysql_test_set_fields(Prepared_statement *stmt,
   if ((tables &&
        check_table_access(thd, SELECT_ACL, tables, FALSE, UINT_MAX, FALSE)) ||
       open_normal_and_derived_tables(thd, tables, MYSQL_OPEN_FORCE_SHARED_MDL,
-                                     DT_INIT | DT_PREPARE | DT_CREATE))
+                                     DT_INIT | DT_PREPARE))
     goto error;
 
   while ((var= it++))
@@ -1918,7 +1918,7 @@ static bool mysql_test_create_table(Prepared_statement *stmt)
 
     if (open_normal_and_derived_tables(stmt->thd, lex->query_tables,
                                        MYSQL_OPEN_FORCE_SHARED_MDL,
-                                       DT_INIT | DT_PREPARE | DT_CREATE))
+                                       DT_INIT | DT_PREPARE))
       DBUG_RETURN(TRUE);
 
     select_lex->context.resolve_in_select_list= TRUE;
diff --git a/sql/sql_show.cc b/sql/sql_show.cc
index 7023e5f..b562249 100644
--- a/sql/sql_show.cc
+++ b/sql/sql_show.cc
@@ -1528,7 +1528,7 @@ mysqld_list_fields(THD *thd, TABLE_LIST *table_list, const char *wild)
 
   if (open_normal_and_derived_tables(thd, table_list,
                                      MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL,
-                                     DT_INIT | DT_PREPARE | DT_CREATE))
+                                     DT_INIT | DT_PREPARE))
     DBUG_VOID_RETURN;
   table= table_list->table;
 
@@ -4414,7 +4414,7 @@ fill_schema_table_by_open(THD *thd, bool is_show_fields_or_keys,
                                            MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL |
                                            (can_deadlock ?
                                             MYSQL_OPEN_FAIL_ON_MDL_CONFLICT : 0)),
-					  DT_INIT | DT_PREPARE | DT_CREATE));
+					  DT_INIT | DT_PREPARE));
 
   /*
     Restore old value of sql_command back as it is being looked at in


More information about the commits mailing list