summaryrefslogtreecommitdiff
path: root/mysql-test/t
diff options
context:
space:
mode:
authorIgor Babaev <igor@askmonty.org>2018-12-01 15:06:04 -0800
committerIgor Babaev <igor@askmonty.org>2018-12-01 15:06:04 -0800
commit46960365b102b1b446c300ed4da606e63ddfab5d (patch)
treece2eefc5d12d7b26affd96f576dcd6908922d170 /mysql-test/t
parent3e5162d814c522da6b0f19c3f6baae1ab5035db8 (diff)
downloadmariadb-git-46960365b102b1b446c300ed4da606e63ddfab5d.tar.gz
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.
Diffstat (limited to 'mysql-test/t')
-rw-r--r--mysql-test/t/cte_recursive.test39
1 files changed, 39 insertions, 0 deletions
diff --git a/mysql-test/t/cte_recursive.test b/mysql-test/t/cte_recursive.test
index 3c5bc62ae1a..0923ca3fdd1 100644
--- a/mysql-test/t/cte_recursive.test
+++ b/mysql-test/t/cte_recursive.test
@@ -2495,3 +2495,42 @@ SELECT GROUP_CONCAT(
FROM Zt
GROUP BY Iy
ORDER BY Iy;
+
+--echo #
+--echo # MDEV-17871: EXPLAIN for query with not used recursive cte
+--echo #
+
+create table t1 (a int);
+insert into t1 values (2), (1), (4), (3);
+
+let $rec_cte =
+with recursive cte as
+ (select * from t1 where a=1 union select a+1 from cte where a<3);
+
+eval
+explain extended
+$rec_cte
+select * from cte as t;
+
+eval
+$rec_cte
+select * from cte as t;
+
+eval
+explain extended
+$rec_cte
+select * from t1 as t;
+
+eval
+$rec_cte
+select * from t1 as t;
+
+create table t2 ( i1 int, i2 int);
+insert into t2 values (1,1),(2,2);
+
+explain
+with recursive cte as
+ ( select * from t1 union select s1.* from t1 as s1, cte where s1.i1 = cte.i2 )
+select * from t1 as t;
+
+drop table t1,t2;