summaryrefslogtreecommitdiff
path: root/mysql-test/main/cte_recursive.test
diff options
context:
space:
mode:
authorSergei Golubchik <serg@mariadb.org>2018-12-07 00:33:51 +0100
committerSergei Golubchik <serg@mariadb.org>2018-12-12 10:39:17 +0100
commit56d3a0e73be6213765274d6efcc31d9c9a9cbcd1 (patch)
tree0c30211802cac683f1164193d9d374613cae08b6 /mysql-test/main/cte_recursive.test
parentac31ff6275cfb5de74c0069a53e5575dac317225 (diff)
downloadmariadb-git-56d3a0e73be6213765274d6efcc31d9c9a9cbcd1.tar.gz
MDEV-17967 Add a solution of the 8 queens problem to the regression test for CTE
Diffstat (limited to 'mysql-test/main/cte_recursive.test')
-rw-r--r--mysql-test/main/cte_recursive.test34
1 files changed, 34 insertions, 0 deletions
diff --git a/mysql-test/main/cte_recursive.test b/mysql-test/main/cte_recursive.test
index 0ed9c2d56e3..483e1ea8c7a 100644
--- a/mysql-test/main/cte_recursive.test
+++ b/mysql-test/main/cte_recursive.test
@@ -1200,6 +1200,40 @@ select * from my_ancestors;
drop table my_ancestors;
+#
+# MDEV-17967 Add a solution of the 8 queens problem to the regression test for CTE
+#
+# adapted to MariaDB from https://rosettacode.org/wiki/N-queens_problem#SQL
+#
+let $N=4; # 8 takes too long for a test
+eval WITH RECURSIVE
+ positions(i) AS (
+ VALUES(0)
+ UNION SELECT ALL
+ i+1 FROM positions WHERE i < $N*$N-1
+ ),
+ solutions(board, n_queens) AS (
+ SELECT REPEAT('-', $N*$N), 0
+ FROM positions
+ UNION
+ SELECT
+ concat(substr(board, 1, i),'*',substr(board, i+2)),n_queens + 1 AS n_queens
+ FROM positions AS ps, solutions
+ WHERE n_queens < $N
+ AND substr(board,1,i) != '*'
+ AND NOT EXISTS (
+ SELECT 1 FROM positions WHERE
+ substr(board,i+1,1) = '*' AND
+ (
+ i % $N = ps.i % $N OR
+ i div $N = ps.i div $N OR
+ i div $N + (i % $N) = ps.i div $N + (ps.i % $N) OR
+ i div $N - (i % $N) = ps.i div $N - (ps.i % $N)
+ )
+ )
+ )
+SELECT regexp_replace(board,concat('(',REPEAT('.', $N),')'),'\\\\1\\n') n_queens FROM solutions WHERE n_queens = $N;
+
--echo #
--echo # MDEV-10883: execution of prepared statement from SELECT
--echo # with recursive CTE that renames columns