Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
MariaDB
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
MariaDB
Commits
8283d7f7
Commit
8283d7f7
authored
Nov 13, 2016
by
Igor Babaev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added the test case from mdev-11259.
parent
92bcb906
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
559 additions
and
0 deletions
+559
-0
mysql-test/r/cte_recursive.result
mysql-test/r/cte_recursive.result
+465
-0
mysql-test/t/cte_recursive.test
mysql-test/t/cte_recursive.test
+94
-0
No files found.
mysql-test/r/cte_recursive.result
View file @
8283d7f7
...
...
@@ -1862,3 +1862,468 @@ a
2
drop view v1;
drop table t1,t2;
#
# MDEV-11259: recursive CTE with concatenation operation
#
DROP TABLE IF EXISTS edges;
Warnings:
Note 1051 Unknown table 'test.edges'
CREATE TABLE edges(
a int(10) unsigned NOT NULL,
b int(10) unsigned NOT NULL,
PRIMARY KEY (a,b),
KEY b(b)
);
INSERT INTO edges
VALUES (1,3),(2,1),(2,4),(3,4),(3,5),(3,6),(4,7),(5,1),(5,6),(6,1);
DROP TABLE IF EXISTS edges2;
Warnings:
Note 1051 Unknown table 'test.edges2'
CREATE VIEW edges2 (a, b) AS
SELECT a, b FROM edges UNION ALL SELECT b, a FROM edges;
WITH RECURSIVE transitive_closure(a, b, distance, path_string) AS
( SELECT a, b, 1 AS distance,
concat(a, '.', b, '.') AS path_string
FROM edges
UNION ALL
SELECT tc.a, e.b, tc.distance + 1,
concat(tc.path_string, e.b, '.') AS path_string
FROM edges AS e
JOIN transitive_closure AS tc
ON e.a = tc.b
WHERE tc.path_string NOT LIKE concat('%', e.b, '.%')
)
SELECT * FROM transitive_closure
ORDER BY a, b, distance;
a b distance path_string
1 3 1 1.3.
1 4 2 1.3.4.
1 5 2 1.3.5.
1 6 2 1.3.6.
1 6 3 1.3.5.6.
1 7 3 1.3.4.7.
2 1 1 2.1.
2 3 2 2.1.3.
2 4 1 2.4.
2 4 3 2.1.3.4.
2 5 3 2.1.3.5.
2 6 3 2.1.3.6.
2 6 4 2.1.3.5.6.
2 7 2 2.4.7.
2 7 4 2.1.3.4.7.
3 1 2 3.6.1.
3 1 2 3.5.1.
3 1 3 3.5.6.1.
3 4 1 3.4.
3 5 1 3.5.
3 6 1 3.6.
3 6 2 3.5.6.
3 7 2 3.4.7.
4 7 1 4.7.
5 1 1 5.1.
5 1 2 5.6.1.
5 3 2 5.1.3.
5 3 3 5.6.1.3.
5 4 3 5.1.3.4.
5 4 4 5.6.1.3.4.
5 6 1 5.6.
5 6 3 5.1.3.6.
5 7 4 5.1.3.4.7.
5 7 5 5.6.1.3.4.7.
6 1 1 6.1.
6 3 2 6.1.3.
6 4 3 6.1.3.4.
6 5 3 6.1.3.5.
6 7 4 6.1.3.4.7.
WITH RECURSIVE transitive_closure(a, b, distance, path_string) AS
( SELECT a, b, 1 AS distance,
concat(a, '.', b, '.') AS path_string
FROM edges
WHERE a = 1 -- source
UNION ALL
SELECT tc.a, e.b, tc.distance + 1,
concat(tc.path_string, e.b, '.') AS path_string
FROM edges AS e
JOIN transitive_closure AS tc ON e.a = tc.b
WHERE tc.path_string NOT LIKE concat('%', e.b, '.%')
)
SELECT * FROM transitive_closure
WHERE b = 6 -- destination
ORDER BY a, b, distance;
a b distance path_string
1 6 2 1.3.6.
1 6 3 1.3.5.6.
WITH RECURSIVE transitive_closure(a, b, distance, path_string) AS
( SELECT a, b, 1 AS distance,
concat(a, '.', b, '.') AS path_string
FROM edges2
UNION ALL
SELECT tc.a, e.b, tc.distance + 1,
concat(tc.path_string, e.b, '.') AS path_string
FROM edges2 AS e
JOIN transitive_closure AS tc ON e.a = tc.b
WHERE tc.path_string NOT LIKE concat('%', e.b, '.%')
)
SELECT * FROM transitive_closure
ORDER BY a, b, distance;
a b distance path_string
1 2 1 1.2.
1 2 3 1.3.4.2.
1 2 4 1.5.3.4.2.
1 2 4 1.6.3.4.2.
1 2 5 1.5.6.3.4.2.
1 2 5 1.6.5.3.4.2.
1 3 1 1.3.
1 3 2 1.5.3.
1 3 2 1.6.3.
1 3 3 1.2.4.3.
1 3 3 1.5.6.3.
1 3 3 1.6.5.3.
1 4 2 1.2.4.
1 4 2 1.3.4.
1 4 3 1.5.3.4.
1 4 3 1.6.3.4.
1 4 4 1.5.6.3.4.
1 4 4 1.6.5.3.4.
1 5 1 1.5.
1 5 2 1.3.5.
1 5 2 1.6.5.
1 5 3 1.3.6.5.
1 5 3 1.6.3.5.
1 5 4 1.2.4.3.5.
1 5 5 1.2.4.3.6.5.
1 6 1 1.6.
1 6 2 1.3.6.
1 6 2 1.5.6.
1 6 3 1.3.5.6.
1 6 3 1.5.3.6.
1 6 4 1.2.4.3.6.
1 6 5 1.2.4.3.5.6.
1 7 3 1.2.4.7.
1 7 3 1.3.4.7.
1 7 4 1.5.3.4.7.
1 7 4 1.6.3.4.7.
1 7 5 1.5.6.3.4.7.
1 7 5 1.6.5.3.4.7.
2 1 1 2.1.
2 1 3 2.4.3.1.
2 1 4 2.4.3.5.1.
2 1 4 2.4.3.6.1.
2 1 5 2.4.3.5.6.1.
2 1 5 2.4.3.6.5.1.
2 3 2 2.1.3.
2 3 2 2.4.3.
2 3 3 2.1.5.3.
2 3 3 2.1.6.3.
2 3 4 2.1.5.6.3.
2 3 4 2.1.6.5.3.
2 4 1 2.4.
2 4 3 2.1.3.4.
2 4 4 2.1.5.3.4.
2 4 4 2.1.6.3.4.
2 4 5 2.1.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edges2
UNION ALL
SELECT tc.a, e.b, tc.distance + 1,
concat(tc.path_string, e.b, '.') AS path_string
FROM edges2 AS e
JOIN transitive_closure AS tc ON e.a = tc.b
WHERE tc.path_string NOT LIKE concat('%', e.b, '.%')
)
SELECT a, b, min(distance) AS dist FROM transitive_closure
GROUP BY a, b
ORDER BY a, dist, b;
a b dist
1 2 1
1 3 1
1 4 2
1 5 1
1 6 1
1 7 3
2 1 1
2 3 2
2 4 1
2 5 2
2 6 2
2 7 2
3 1 1
3 2 2
3 4 1
3 5 1
3 6 1
3 7 2
4 1 2
4 2 1
4 3 1
4 5 2
4 6 2
4 7 1
5 1 1
5 2 2
5 3 1
5 4 2
5 6 1
5 7 3
6 1 1
6 2 2
6 3 1
6 4 2
6 5 1
6 7 3
7 1 3
7 2 2
7 3 2
7 4 1
7 5 3
7 6 3
DROP VIEW edges2;
DROP TABLE edges;
mysql-test/t/cte_recursive.test
View file @
8283d7f7
...
...
@@ -1389,3 +1389,97 @@ select * from t1;
drop
view
v1
;
drop
table
t1
,
t2
;
--
echo
#
--
echo
# MDEV-11259: recursive CTE with concatenation operation
--
echo
#
DROP
TABLE
IF
EXISTS
edges
;
CREATE
TABLE
edges
(
a
int
(
10
)
unsigned
NOT
NULL
,
b
int
(
10
)
unsigned
NOT
NULL
,
PRIMARY
KEY
(
a
,
b
),
KEY
b
(
b
)
);
INSERT
INTO
edges
VALUES
(
1
,
3
),(
2
,
1
),(
2
,
4
),(
3
,
4
),(
3
,
5
),(
3
,
6
),(
4
,
7
),(
5
,
1
),(
5
,
6
),(
6
,
1
);
DROP
TABLE
IF
EXISTS
edges2
;
CREATE
VIEW
edges2
(
a
,
b
)
AS
SELECT
a
,
b
FROM
edges
UNION
ALL
SELECT
b
,
a
FROM
edges
;
WITH
RECURSIVE
transitive_closure
(
a
,
b
,
distance
,
path_string
)
AS
(
SELECT
a
,
b
,
1
AS
distance
,
concat
(
a
,
'.'
,
b
,
'.'
)
AS
path_string
FROM
edges
UNION
ALL
SELECT
tc
.
a
,
e
.
b
,
tc
.
distance
+
1
,
concat
(
tc
.
path_string
,
e
.
b
,
'.'
)
AS
path_string
FROM
edges
AS
e
JOIN
transitive_closure
AS
tc
ON
e
.
a
=
tc
.
b
WHERE
tc
.
path_string
NOT
LIKE
concat
(
'%'
,
e
.
b
,
'.%'
)
)
SELECT
*
FROM
transitive_closure
ORDER
BY
a
,
b
,
distance
;
--
sorted_result
WITH
RECURSIVE
transitive_closure
(
a
,
b
,
distance
,
path_string
)
AS
(
SELECT
a
,
b
,
1
AS
distance
,
concat
(
a
,
'.'
,
b
,
'.'
)
AS
path_string
FROM
edges
WHERE
a
=
1
--
source
UNION
ALL
SELECT
tc
.
a
,
e
.
b
,
tc
.
distance
+
1
,
concat
(
tc
.
path_string
,
e
.
b
,
'.'
)
AS
path_string
FROM
edges
AS
e
JOIN
transitive_closure
AS
tc
ON
e
.
a
=
tc
.
b
WHERE
tc
.
path_string
NOT
LIKE
concat
(
'%'
,
e
.
b
,
'.%'
)
)
SELECT
*
FROM
transitive_closure
WHERE
b
=
6
--
destination
ORDER
BY
a
,
b
,
distance
;
--
sorted_result
WITH
RECURSIVE
transitive_closure
(
a
,
b
,
distance
,
path_string
)
AS
(
SELECT
a
,
b
,
1
AS
distance
,
concat
(
a
,
'.'
,
b
,
'.'
)
AS
path_string
FROM
edges2
UNION
ALL
SELECT
tc
.
a
,
e
.
b
,
tc
.
distance
+
1
,
concat
(
tc
.
path_string
,
e
.
b
,
'.'
)
AS
path_string
FROM
edges2
AS
e
JOIN
transitive_closure
AS
tc
ON
e
.
a
=
tc
.
b
WHERE
tc
.
path_string
NOT
LIKE
concat
(
'%'
,
e
.
b
,
'.%'
)
)
SELECT
*
FROM
transitive_closure
ORDER
BY
a
,
b
,
distance
;
--
sorted_result
WITH
RECURSIVE
transitive_closure
(
a
,
b
,
distance
,
path_string
)
AS
(
SELECT
a
,
b
,
1
AS
distance
,
concat
(
a
,
'.'
,
b
,
'.'
)
AS
path_string
FROM
edges2
UNION
ALL
SELECT
tc
.
a
,
e
.
b
,
tc
.
distance
+
1
,
concat
(
tc
.
path_string
,
e
.
b
,
'.'
)
AS
path_string
FROM
edges2
AS
e
JOIN
transitive_closure
AS
tc
ON
e
.
a
=
tc
.
b
WHERE
tc
.
path_string
NOT
LIKE
concat
(
'%'
,
e
.
b
,
'.%'
)
)
SELECT
a
,
b
,
min
(
distance
)
AS
dist
FROM
transitive_closure
GROUP
BY
a
,
b
ORDER
BY
a
,
dist
,
b
;
DROP
VIEW
edges2
;
DROP
TABLE
edges
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment