[MDEV-20632] Recursive CTE cycle detection using CYCLE clause (nonstandard) Created: 2019-09-19 Updated: 2020-08-24 Resolved: 2020-03-10 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer - CTE |
| Fix Version/s: | 10.5.2 |
| Type: | Task | Priority: | Critical |
| Reporter: | Sergei Golubchik | Assignee: | Oleksandr Byelkin |
| Resolution: | Fixed | Votes: | 1 |
| Labels: | None | ||
| Issue Links: |
|
||||||||||||
| Description |
|
One can use UNION DISTINCT as an easy way of avoiding cycles when traversing a graph with a CTE:
But often one needs to know more than edges, for example
and here DISTINCT no longer works. SQL Standard specifies that a CTE can have a CYCLE clause as
where
While in the standard all clauses in the CYCLE are mandatory, we'll relax this grammar to allow only CYCLE <cycle column list>. This task is about implementing optional CYCLE <cycle column list> clause after the recursive CTE definition. There is a simple way to implement it by changing CTE's UNION ALL or UNION DISTINCT operator to enforce distinct-ness only over <cycle column list> columns, not over all columns that CTE returns. The example from above would look like
Note that it doesn't matter whether the CTE uses UNION ALL or UNION DISTINCT anymore. UNION ALL means "all rows, but without cycles", which is exactly what we'll do. And UNION DISTINCT means all rows should be different, which, again, is what will happen — as we'll enforce uniqueness over a subset of columns, complete rows will automatically be all different. |
| Comments |
| Comment by Oleksandr Byelkin [ 2020-01-13 ] |
|
in standart there is CYCLE ( field list) |
| Comment by Oleksandr Byelkin [ 2020-02-28 ] |
|
Added RESTRICT to mark non-standard usage and resolve parsing conflicts |
| Comment by Oleksandr Byelkin [ 2020-02-28 ] |
|
commit be683b85ccada4bfdcd6b08fe4014442b33ce711 (HEAD Added CYCLE clause to recursive CTE. |