Skip to content
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

joins conflict with WITH RECURSIVE forms #27

Open
Rscho314 opened this issue Dec 18, 2021 · 0 comments
Open

joins conflict with WITH RECURSIVE forms #27

Rscho314 opened this issue Dec 18, 2021 · 0 comments

Comments

@Rscho314
Copy link

Rscho314 commented Dec 18, 2021

Hi,

This code fails at runtime, when run against the db:

(sql                                                                                                                                   
 (with #:recursive                                                                                                                     
     ([(cte id name parent)                                                                                                          
       (inner-join                                                                                                                   
        (union                                                                                                                       
         (select h.id h.name h.parent #:from (as heap h) #:where (and (= name ?) (is-null parent)))                                  
         (select h.id h.name h.parent #:from (as heap h))                                                                            
         #:all)                                                                                                                      
        cte                                                                                                                          
        #:on (= cte.id h.parent))])                                                                                                  
     (select name #:from (select id name #:from cte #:order-by id #:desc))))

This results in the query:

(sql-statement                                                                                                                           
 "WITH RECURSIVE cte(id, name, parent) AS ((SELECT h.id, h.name, h.parent FROM heap AS h WHERE ((name = ?) AND (parent IS NULL)) UNION ALL SELECT h.id, h.name, h.parent FROM heap AS h) INNER JOIN cte ON (cte.id = h.parent)) SELECT name FROM (SELECT id, name FROM cte ORDER BY id DESC)") 

The problem is that according to SQLite docs, the 1st AS( must be directly followed by a SELECT statement, whereas here the statement is enclosed in an additional pair of parenthesis produced by the inner-join, and this is unfortunately not accepted by the SQLite parser.

I'm attaching a self-contained test file that can be run as-is in the SQLite3 CLI, where you'll find that the 2nd WITH RECURSIVE statement fails, while the 1st one that has the parentheses commented out runs fine.
test.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant