Skip to content

Commit

Permalink
MDEV-16473 WITH statement throws 'no database selected' error
Browse files Browse the repository at this point in the history
Different fix, just use NULL, not no_db,
  • Loading branch information
vuvova committed Jun 28, 2018
1 parent 090febb commit 52a25d7
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 45 deletions.
11 changes: 0 additions & 11 deletions mysql-test/r/cte_nonrecursive.result
Original file line number Diff line number Diff line change
Expand Up @@ -1511,15 +1511,4 @@ a a
3 3
1 1
drop database db_mdev_16473;
create database `*` ;
create table `*`.t1 (a int);
insert into `*`.t1 values (2), (7), (3), (1);
use `*`;
select * from t1;
a
2
7
3
1
drop database `*`;
use test;
7 changes: 0 additions & 7 deletions mysql-test/t/cte_nonrecursive.test
Original file line number Diff line number Diff line change
Expand Up @@ -1056,11 +1056,4 @@ select * from cte, db_mdev_16473.t1 as t where cte.a=t.a;

drop database db_mdev_16473;

create database `*` ;
create table `*`.t1 (a int);
insert into `*`.t1 values (2), (7), (3), (1);
use `*`;
select * from t1;
drop database `*`;

use test;
2 changes: 1 addition & 1 deletion sql/sql_acl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7581,7 +7581,7 @@ bool check_grant(THD *thd, ulong want_access, TABLE_LIST *tables,
tl->correspondent_table ? tl->correspondent_table : tl;
sctx= t_ref->security_ctx ? t_ref->security_ctx : thd->security_ctx;

if (tl->with ||
if (tl->with || !tl->db ||
(tl->select_lex &&
(tl->with= tl->select_lex->find_table_def_in_with_clauses(tl))))
continue;
Expand Down
14 changes: 6 additions & 8 deletions sql/sql_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,12 @@ bool open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx)
DBUG_RETURN(true);
}

if (!table_list->db)
{
my_error(ER_NO_DB_ERROR, MYF(0));
DBUG_RETURN(true);
}

key_length= get_table_def_key(table_list, &key);

/*
Expand Down Expand Up @@ -3330,14 +3336,6 @@ open_and_process_table(THD *thd, LEX *lex, TABLE_LIST *tables,
/*
Not a placeholder: must be a base/temporary table or a view. Let us open it.
*/

if (tables->no_default_db && !tables->is_fqtn)
{
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
error= TRUE;
goto end;
}

if (tables->table)
{
/*
Expand Down
9 changes: 3 additions & 6 deletions sql/sql_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -3959,12 +3959,9 @@ class THD :public Statement,
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
return TRUE;
}
/*
It does not matter what database name is set in this case
because it will never be used after parser stage
*/
*p_db_length= strlen(no_db);
*p_db= strmake(no_db, *p_db_length);
/* This will allow to throw an error later for non-CTE references */
*p_db= NULL;
*p_db_length= 0;
}
else
{
Expand Down
2 changes: 0 additions & 2 deletions sql/sql_lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ struct LEX_TYPE
extern const LEX_STRING null_lex_str;
extern const LEX_STRING empty_lex_str;

extern const char *no_db;

enum enum_sp_suid_behaviour
{
SP_IS_DEFAULT_SUID= 0,
Expand Down
10 changes: 2 additions & 8 deletions sql/sql_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ static bool execute_show_status(THD *, TABLE_LIST *);
static bool check_rename_table(THD *, TABLE_LIST *, TABLE_LIST *);

const char *any_db="*any*"; // Special symbol for check_access
const char *no_db="*"; // Used when no default db is set

const LEX_STRING command_name[257]={
{ C_STRING_WITH_LEN("Sleep") }, //0
Expand Down Expand Up @@ -6685,11 +6684,7 @@ check_access(THD *thd, ulong want_access, const char *db, ulong *save_priv,
THD_STAGE_INFO(thd, stage_checking_permissions);
if ((!db || !db[0]) && !thd->db && !dont_check_global_grants)
{
DBUG_PRINT("error",("No database"));
if (!no_errors)
my_message(ER_NO_DB_ERROR, ER_THD(thd, ER_NO_DB_ERROR),
MYF(0)); /* purecov: tested */
DBUG_RETURN(TRUE); /* purecov: tested */
DBUG_RETURN(FALSE); // CTE reference or an error later
}

if ((db != NULL) && (db != any_db))
Expand Down Expand Up @@ -8188,7 +8183,6 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
DBUG_RETURN(0);
else
ptr->is_fqtn= FALSE;
ptr->no_default_db= !thd->db && !(lex->sphead && lex->sphead->m_name.str);

ptr->alias= alias_str;
ptr->is_alias= alias ? TRUE : FALSE;
Expand Down Expand Up @@ -8301,7 +8295,7 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
lex->add_to_query_tables(ptr);

// Pure table aliases do not need to be locked:
if (!MY_TEST(table_options & TL_OPTION_ALIAS))
if (ptr->db && !(table_options & TL_OPTION_ALIAS))
{
ptr->mdl_request.init(MDL_key::TABLE, ptr->db, ptr->table_name, mdl_type,
MDL_TRANSACTION);
Expand Down
3 changes: 1 addition & 2 deletions sql/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -2095,8 +2095,7 @@ struct TABLE_LIST
qualified name (<db_name>.<table_name>).
*/
bool is_fqtn;
/** TRUE if no default database is defined for the table name */
bool no_default_db;

/* TRUE <=> derived table should be filled right after optimization. */
bool fill_me;
/* TRUE <=> view/DT is merged. */
Expand Down

0 comments on commit 52a25d7

Please sign in to comment.