Skip to content

Allow Non-Linear CTE #7259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Morikko opened this issue Oct 29, 2021 · 4 comments
Closed

Allow Non-Linear CTE #7259

Morikko opened this issue Oct 29, 2021 · 4 comments
Labels
sql use case not really a feature or a bug; can be support for new DB features or user use cases not anticipated
Milestone

Comments

@Morikko
Copy link

Morikko commented Oct 29, 2021

Describe the use case

Non Linear CTE:

  • Multiple references to the recursive CTE
  • Often with multiple UNIONs

Useful for example to do graph traversal with directional edges. One UNION to one side and the other UNION to the other side.

Databases / Backends / Drivers targeted

SQLite & MySQL support its and maybe more. (Also: https://modern-sql.com/caniuse/with_recursive_(non-linear))

Example Use

From https://www.sqlite.org/lang_with.html at 3.3. Queries Against A Graph.

WITH RECURSIVE nodes(x) AS (
   SELECT 59
   UNION
   SELECT aa FROM edge JOIN nodes ON bb=x
   UNION
   SELECT bb FROM edge JOIN nodes ON aa=x
)
SELECT x FROM nodes;

Additional context

No response

@Morikko Morikko added requires triage New issue that requires categorization use case not really a feature or a bug; can be support for new DB features or user use cases not anticipated labels Oct 29, 2021
@sqla-tester
Copy link
Collaborator

Mike Bayer has proposed a fix for this issue in the master branch:

Add Non linear CTE support https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3220

@zzzeek zzzeek added sql and removed requires triage New issue that requires categorization labels Oct 29, 2021
@zzzeek zzzeek added this to the 1.4.x milestone Oct 29, 2021
@sqla-tester
Copy link
Collaborator

Eric Masseran has proposed a fix for this issue in the master branch:

Add Non linear CTE support https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3256

@sqla-tester
Copy link
Collaborator

Eric Masseran has proposed a fix for this issue in the main branch:

Add Non linear CTE support https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3220

@sqla-tester
Copy link
Collaborator

Eric Masseran has proposed a fix for this issue in the rel_1_4 branch:

Add Non linear CTE support https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3312

sqlalchemy-bot pushed a commit that referenced this issue Nov 17, 2021
"Compound select" methods like :meth:`_sql.Select.union`,
:meth:`_sql.Select.intersect_all` etc. now accept ``*other`` as an argument
rather than ``other`` to allow for multiple additional SELECTs to be
compounded with the parent statement at once. In particular, the change as
applied to :meth:`_sql.CTE.union` and :meth:`_sql.CTE.union_all` now allow
for a so-called "non-linear CTE" to be created with the :class:`_sql.CTE`
construct, whereas previously there was no way to have more than two CTE
sub-elements in a UNION together while still correctly calling upon the CTE
in recursive fashion. Pull request courtesy Eric Masseran.

Allow:

```sql
WITH RECURSIVE nodes(x) AS (
   SELECT 59
   UNION
   SELECT aa FROM edge JOIN nodes ON bb=x
   UNION
   SELECT bb FROM edge JOIN nodes ON aa=x
)
SELECT x FROM nodes;
```

Based on @zzzeek suggestion: #7133 (comment)

Fixes: #7259
Closes: #7260
Pull-request: #7260
Pull-request-sha: 2565a5f

Change-Id: I685c8379762b5fb6ab4107ff8f4d8a4de70c0ca6
(cherry picked from commit 958f902)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql use case not really a feature or a bug; can be support for new DB features or user use cases not anticipated
Projects
None yet
Development

No branches or pull requests

3 participants