diff options
author | Igor Babaev <igor@askmonty.org> | 2018-02-05 18:07:31 -0800 |
---|---|---|
committer | Igor Babaev <igor@askmonty.org> | 2018-02-05 18:07:31 -0800 |
commit | f271100836d8a91a775894ec36b869a66a3145e5 (patch) | |
tree | 179679955378d944a5414dd7022d31f1186f03b7 | |
parent | 465979eabfe3acacc0566fa93402e449e1a9f476 (diff) | |
download | mariadb-git-f271100836d8a91a775894ec36b869a66a3145e5.tar.gz |
Fixed mdev-15162 Query with CTE hangs if assignment operator (:=) is used
If setting user variable was used in the specification of a recursive CTE
then Item_func_set_user_var::fix_fields() went into an infinite loop.
-rw-r--r-- | mysql-test/r/cte_recursive.result | 13 | ||||
-rw-r--r-- | mysql-test/t/cte_recursive.test | 12 | ||||
-rw-r--r-- | sql/item_func.cc | 6 |
3 files changed, 30 insertions, 1 deletions
diff --git a/mysql-test/r/cte_recursive.result b/mysql-test/r/cte_recursive.result index d87802f57f3..37eef20bfc5 100644 --- a/mysql-test/r/cte_recursive.result +++ b/mysql-test/r/cte_recursive.result @@ -3079,3 +3079,16 @@ ERROR 21000: The used SELECT statements have a different number of columns DROP TABLE a_tbl; WITH RECURSIVE x AS (SELECT 1,2 UNION ALL SELECT 1 FROM x) SELECT * FROM x; ERROR 21000: The used SELECT statements have a different number of columns +# +# MDEV-15162: Setting user variable in recursive CTE +# +SET @c=1; +WITH RECURSIVE cte AS +(SELECT 5 +UNION +SELECT @c:=@c+1 FROM cte WHERE @c<3) +SELECT * FROM cte; +5 +5 +2 +3 diff --git a/mysql-test/t/cte_recursive.test b/mysql-test/t/cte_recursive.test index d2e33b165f7..5f1a7d4e1bf 100644 --- a/mysql-test/t/cte_recursive.test +++ b/mysql-test/t/cte_recursive.test @@ -2113,3 +2113,15 @@ DROP TABLE a_tbl; --error ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT WITH RECURSIVE x AS (SELECT 1,2 UNION ALL SELECT 1 FROM x) SELECT * FROM x; + +--echo # +--echo # MDEV-15162: Setting user variable in recursive CTE +--echo # + +SET @c=1; + +WITH RECURSIVE cte AS + (SELECT 5 + UNION + SELECT @c:=@c+1 FROM cte WHERE @c<3) +SELECT * FROM cte; diff --git a/sql/item_func.cc b/sql/item_func.cc index e130831cb6a..3583b6aed62 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -54,6 +54,7 @@ #include "set_var.h" #include "debug_sync.h" #include "sql_base.h" +#include "sql_cte.h" #ifdef NO_EMBEDDED_ACCESS_CHECKS #define sp_restore_security_context(A,B) while (0) {} @@ -4514,10 +4515,13 @@ bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref) TABLE_LIST *derived; for (derived= unit->derived; derived; - derived= derived->select_lex->master_unit()->derived) + derived= unit->derived) { derived->set_materialized_derived(); derived->prohibit_cond_pushdown= true; + if (unit->with_element && unit->with_element->is_recursive) + break; + unit= derived->select_lex->master_unit(); } } |