From 4d991abd4fc7f60e758ec46301b0dd2bee71245c Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Fri, 7 Sep 2018 20:10:04 -0700 Subject: MDEV-17024 Crash on large query This problem manifested itself when a join query used two or more materialized CTE such that each of them employed the same recursive CTE. The bug caused a crash. The crash happened because the cleanup() function was performed premature for recursive CTE. This clean up was induced by the cleanup of the first CTE referenced the recusrsive CTE. This cleanup destroyed the structures that would allow to read from the temporary table containing the rows of the recursive CTE and an attempt to read these rows for the second CTE referencing the recursive CTE triggered a crash. The clean up for a recursive CTE R should be performed after the cleanup of the last materialized CTE that uses R. --- sql/sql_base.cc | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'sql/sql_base.cc') diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 36bf39ea8f3..cae5b4a3f7d 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -3295,6 +3295,15 @@ open_and_process_table(THD *thd, LEX *lex, TABLE_LIST *tables, */ if (tables->with) { + if (tables->is_recursive_with_table() && + !tables->is_with_table_recursive_reference()) + { + tables->with->rec_outer_references++; + With_element *with_elem= tables->with; + while ((with_elem= with_elem->get_next_mutually_recursive()) != + tables->with) + with_elem->rec_outer_references++; + } if (tables->set_as_with_table(thd, tables->with)) DBUG_RETURN(1); else -- cgit v1.2.1