Skip to content

Commit b4a7bde

Browse files
committed
MDEV-19112 WITH clause does not work with information_schema as default database
With INFORMATION_SCHEMA set as the default database the check that a table referred in the processed query is defined in INORMATION_SCHEMA must be postponed until all CTE names can be identified.
1 parent b30fb70 commit b4a7bde

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

mysql-test/r/cte_nonrecursive.result

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,3 +1659,17 @@ a
16591659
2
16601660
drop view v1;
16611661
drop table t1,t2;
1662+
#
1663+
# MDEV-19112: CTE usage when information_schema is set as default db
1664+
#
1665+
with t as (select 1 as t ) select * from t;
1666+
t
1667+
1
1668+
use information_schema;
1669+
with t as (select 1 as t) select * from t;
1670+
t
1671+
1
1672+
with columns as (select 1 as t) select * from columns;
1673+
t
1674+
1
1675+
use test;

mysql-test/t/cte_nonrecursive.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,3 +1168,15 @@ select * from v1;
11681168

11691169
drop view v1;
11701170
drop table t1,t2;
1171+
1172+
--echo #
1173+
--echo # MDEV-19112: CTE usage when information_schema is set as default db
1174+
--echo #
1175+
1176+
with t as (select 1 as t ) select * from t;
1177+
1178+
use information_schema;
1179+
with t as (select 1 as t) select * from t;
1180+
with columns as (select 1 as t) select * from columns;
1181+
1182+
use test;

sql/sql_base.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,6 +3344,30 @@ open_and_process_table(THD *thd, LEX *lex, TABLE_LIST *tables,
33443344
goto end;
33453345
}
33463346
}
3347+
3348+
if (!tables->derived &&
3349+
is_infoschema_db(tables->db, tables->db_length))
3350+
{
3351+
/*
3352+
Check whether the information schema contains a table
3353+
whose name is tables->schema_table_name
3354+
*/
3355+
ST_SCHEMA_TABLE *schema_table;
3356+
schema_table= find_schema_table(thd, tables->schema_table_name);
3357+
if (!schema_table ||
3358+
(schema_table->hidden &&
3359+
((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 ||
3360+
/*
3361+
this check is used for show columns|keys from I_S hidden table
3362+
*/
3363+
lex->sql_command == SQLCOM_SHOW_FIELDS ||
3364+
lex->sql_command == SQLCOM_SHOW_KEYS)))
3365+
{
3366+
my_error(ER_UNKNOWN_TABLE, MYF(0),
3367+
tables->schema_table_name, INFORMATION_SCHEMA_NAME.str);
3368+
DBUG_RETURN(1);
3369+
}
3370+
}
33473371
/*
33483372
If this TABLE_LIST object is a placeholder for an information_schema
33493373
table, create a temporary table to represent the information_schema

sql/sql_cte.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ bool TABLE_LIST::set_as_with_table(THD *thd, With_element *with_elem)
10941094
table= 0;
10951095
}
10961096
with= with_elem;
1097+
schema_table= NULL;
10971098
if (!with_elem->is_referenced() || with_elem->is_recursive)
10981099
{
10991100
derived= with_elem->spec;

sql/sql_parse.cc

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8222,7 +8222,6 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
82228222
ptr->derived= table->sel;
82238223
if (!ptr->derived && is_infoschema_db(ptr->db, ptr->db_length))
82248224
{
8225-
ST_SCHEMA_TABLE *schema_table;
82268225
if (ptr->updating &&
82278226
/* Special cases which are processed by commands itself */
82288227
lex->sql_command != SQLCOM_CHECK &&
@@ -8234,20 +8233,8 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
82348233
INFORMATION_SCHEMA_NAME.str);
82358234
DBUG_RETURN(0);
82368235
}
8236+
ST_SCHEMA_TABLE *schema_table;
82378237
schema_table= find_schema_table(thd, ptr->table_name);
8238-
if (!schema_table ||
8239-
(schema_table->hidden &&
8240-
((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 ||
8241-
/*
8242-
this check is used for show columns|keys from I_S hidden table
8243-
*/
8244-
lex->sql_command == SQLCOM_SHOW_FIELDS ||
8245-
lex->sql_command == SQLCOM_SHOW_KEYS)))
8246-
{
8247-
my_error(ER_UNKNOWN_TABLE, MYF(0),
8248-
ptr->table_name, INFORMATION_SCHEMA_NAME.str);
8249-
DBUG_RETURN(0);
8250-
}
82518238
ptr->schema_table_name= ptr->table_name;
82528239
ptr->schema_table= schema_table;
82538240
}

0 commit comments

Comments
 (0)