From 46960365b102b1b446c300ed4da606e63ddfab5d Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Sat, 1 Dec 2018 15:06:04 -0800 Subject: MDEV-17871 Crash when running explain with CTE When the with clause of a query contains a recursive CTE that is not used then processing of EXPLAIN for this query does not require optimization of the unit specifying this CTE. In this case if 'derived' is the TABLE_LIST object created for this CTE then derived->derived_result is NULL and any assignment to derived->derived_result->table causes a crash. After fixing this problem in the code of st_select_lex_unit::prepare() EXPLAIN for such a query worked without crashes. Yet an execution plan for the recursive CTE appeared there. The cause of this problem was an incorrect condition used in JOIN::save_explain_data_intern() that determined whether CTE was to be optimized or not. A similar condition was used in select_describe() and this patch has corrected it as well. --- sql/sql_union.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'sql/sql_union.cc') diff --git a/sql/sql_union.cc b/sql/sql_union.cc index 44879f66edf..eb66dce25e3 100644 --- a/sql/sql_union.cc +++ b/sql/sql_union.cc @@ -694,8 +694,11 @@ bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, instantiate_tmp_table, false)) goto err; if (!derived->table) - derived->table= derived->derived_result->table= - with_element->rec_result->rec_tables.head(); + { + derived->table= with_element->rec_result->rec_tables.head(); + if (derived->derived_result) + derived->derived_result->table= derived->table; + } with_element->mark_as_with_prepared_anchor(); is_rec_result_table_created= true; } -- cgit v1.2.1