Skip to content

Commit

Permalink
Fix optimizer_full_scan to not error for explain queries
Browse files Browse the repository at this point in the history
Summary:
EXPLAIN queries with derived tables do not populate select_options in the JOIN structure properly. This means that we could error if we tried to run an explain on a query with derived tables.

Instead of reading from the JOIN struct, read from the LEX struct on the THD, as this is where the flag is original set on during query parsing.

This bug also means that in upstream, we increment some status variables tracking full table scans despite the fact that only an explain statement was done. This seems to have been fixed in 8.0 though.

Squash with: D7528820

Differential Revision: D7691148

fbshipit-source-id: b62bcf1
  • Loading branch information
lth authored and facebook-github-bot committed Apr 23, 2018
1 parent 60a92a7 commit ecef4b2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
6 changes: 6 additions & 0 deletions mysql-test/r/optimizer_full_scan.result
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ ERROR HY000: Full table/index scan is disabled
set optimizer_full_scan = on;
select * from t a straight_join t b where a.i = 10;
i j i j
set optimizer_full_scan = off;
explain select * from (select * from t) a group by i;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
2 DERIVED t ALL NULL NULL NULL NULL 6 NULL
set optimizer_full_scan = on;
# Test integration with optimizer_force_index_for_range
alter table t drop index i, add primary key (i, j);
# Test range plans
Expand Down
5 changes: 5 additions & 0 deletions mysql-test/t/optimizer_full_scan.test
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ select * from t a straight_join t b where a.i = 10;
set optimizer_full_scan = on;
select * from t a straight_join t b where a.i = 10;

--replace_column 9 #
set optimizer_full_scan = off;
explain select * from (select * from t) a group by i;
set optimizer_full_scan = on;

--echo # Test integration with optimizer_force_index_for_range
alter table t drop index i, add primary key (i, j);

Expand Down
26 changes: 15 additions & 11 deletions sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2809,7 +2809,7 @@ bool JOIN::setup_materialized_table(JOIN_TAB *tab, uint tableno,
bool
make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
{
const bool statistics= MY_TEST(!(join->select_options & SELECT_DESCRIBE));
const bool statistics= MY_TEST(!(join->thd->lex->select_lex.options & SELECT_DESCRIBE));

DBUG_ENTER("make_join_readinfo");

Expand Down Expand Up @@ -2900,12 +2900,14 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
{
join->thd->set_status_no_index_used();
if (statistics)
{
join->thd->inc_status_select_scan();
/* Block full table/index scans, if optimizer_full_scan is off. */
if (!join->thd->variables.optimizer_full_scan &&
!(join->select_options & SELECT_DESCRIBE)) {
my_error(ER_FULL_SCAN_DISABLED, MYF(0));
DBUG_RETURN(TRUE);
/* Block full table/index scans, if optimizer_full_scan is off. */
if (!join->thd->variables.optimizer_full_scan)
{
my_error(ER_FULL_SCAN_DISABLED, MYF(0));
DBUG_RETURN(TRUE);
}
}
}
}
Expand All @@ -2920,12 +2922,14 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
{
join->thd->set_status_no_index_used();
if (statistics)
{
join->thd->inc_status_select_full_join();
/* Block full table/index scans, if optimizer_full_scan is off. */
if (!join->thd->variables.optimizer_full_scan &&
!(join->select_options & SELECT_DESCRIBE)) {
my_error(ER_FULL_SCAN_DISABLED, MYF(0));
DBUG_RETURN(TRUE);
/* Block full table/index scans, if optimizer_full_scan is off. */
if (!join->thd->variables.optimizer_full_scan)
{
my_error(ER_FULL_SCAN_DISABLED, MYF(0));
DBUG_RETURN(TRUE);
}
}
}
}
Expand Down

0 comments on commit ecef4b2

Please sign in to comment.