Skip to content

Commit ecef4b2

Browse files
lthfacebook-github-bot
authored andcommitted
Fix optimizer_full_scan to not error for explain queries
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
1 parent 60a92a7 commit ecef4b2

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

mysql-test/r/optimizer_full_scan.result

+6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ ERROR HY000: Full table/index scan is disabled
7373
set optimizer_full_scan = on;
7474
select * from t a straight_join t b where a.i = 10;
7575
i j i j
76+
set optimizer_full_scan = off;
77+
explain select * from (select * from t) a group by i;
78+
id select_type table type possible_keys key key_len ref rows Extra
79+
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
80+
2 DERIVED t ALL NULL NULL NULL NULL 6 NULL
81+
set optimizer_full_scan = on;
7682
# Test integration with optimizer_force_index_for_range
7783
alter table t drop index i, add primary key (i, j);
7884
# Test range plans

mysql-test/t/optimizer_full_scan.test

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ select * from t a straight_join t b where a.i = 10;
4444
set optimizer_full_scan = on;
4545
select * from t a straight_join t b where a.i = 10;
4646

47+
--replace_column 9 #
48+
set optimizer_full_scan = off;
49+
explain select * from (select * from t) a group by i;
50+
set optimizer_full_scan = on;
51+
4752
--echo # Test integration with optimizer_force_index_for_range
4853
alter table t drop index i, add primary key (i, j);
4954

sql/sql_select.cc

+15-11
Original file line numberDiff line numberDiff line change
@@ -2809,7 +2809,7 @@ bool JOIN::setup_materialized_table(JOIN_TAB *tab, uint tableno,
28092809
bool
28102810
make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
28112811
{
2812-
const bool statistics= MY_TEST(!(join->select_options & SELECT_DESCRIBE));
2812+
const bool statistics= MY_TEST(!(join->thd->lex->select_lex.options & SELECT_DESCRIBE));
28132813

28142814
DBUG_ENTER("make_join_readinfo");
28152815

@@ -2900,12 +2900,14 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
29002900
{
29012901
join->thd->set_status_no_index_used();
29022902
if (statistics)
2903+
{
29032904
join->thd->inc_status_select_scan();
2904-
/* Block full table/index scans, if optimizer_full_scan is off. */
2905-
if (!join->thd->variables.optimizer_full_scan &&
2906-
!(join->select_options & SELECT_DESCRIBE)) {
2907-
my_error(ER_FULL_SCAN_DISABLED, MYF(0));
2908-
DBUG_RETURN(TRUE);
2905+
/* Block full table/index scans, if optimizer_full_scan is off. */
2906+
if (!join->thd->variables.optimizer_full_scan)
2907+
{
2908+
my_error(ER_FULL_SCAN_DISABLED, MYF(0));
2909+
DBUG_RETURN(TRUE);
2910+
}
29092911
}
29102912
}
29112913
}
@@ -2920,12 +2922,14 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
29202922
{
29212923
join->thd->set_status_no_index_used();
29222924
if (statistics)
2925+
{
29232926
join->thd->inc_status_select_full_join();
2924-
/* Block full table/index scans, if optimizer_full_scan is off. */
2925-
if (!join->thd->variables.optimizer_full_scan &&
2926-
!(join->select_options & SELECT_DESCRIBE)) {
2927-
my_error(ER_FULL_SCAN_DISABLED, MYF(0));
2928-
DBUG_RETURN(TRUE);
2927+
/* Block full table/index scans, if optimizer_full_scan is off. */
2928+
if (!join->thd->variables.optimizer_full_scan)
2929+
{
2930+
my_error(ER_FULL_SCAN_DISABLED, MYF(0));
2931+
DBUG_RETURN(TRUE);
2932+
}
29292933
}
29302934
}
29312935
}

0 commit comments

Comments
 (0)