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

Parsing nested CTEs #165

Closed
Gelio opened this issue Jun 28, 2023 · 2 comments · Fixed by #169
Closed

Parsing nested CTEs #165

Gelio opened this issue Jun 28, 2023 · 2 comments · Fixed by #169

Comments

@Gelio
Copy link

Gelio commented Jun 28, 2023

I found that the following valid PostgreSQL query involving nested CTEs is not parsed correctly by this grammar:

WITH top_cte AS (
  WITH nested_cte AS (
    SELECT 1 as one, 2 as two
  )
  SELECT one, two
  FROM nested_cte
)
SELECT *
FROM top_cte;

Received AST:

(program
  (ERROR
    (statement
      (keyword_with)
      (cte
        (identifier)
        (keyword_as)
        (ERROR
          (keyword_as))
        (statement
          (select
            (keyword_select)
            (select_expression
              (term
                (literal)
                (keyword_as)
                (identifier))
              (term
                (literal)
                (keyword_as)
                (identifier))))))
      (select
        (keyword_select)
        (select_expression
          (term
            (field
              (identifier)))
          (term
            (field
              (identifier)))))
      (from
        (keyword_from)
        (relation
          (object_reference
            (identifier))))))
  (statement
    (select
      (keyword_select)
      (select_expression
        (term
          (all_fields))))
    (from
      (keyword_from)
      (relation
        (object_reference
          (identifier))))))

This SO answer says nested CTEs are not valid, at least for T-SQL. I found that PostgreSQL has no problem executing that query, though:

image

@matthias-Q
Copy link
Collaborator

@dmfay I was thinking that this should be fairly simple. I tested to add $._cte to the choices in

$._update_statement,
. In my mind this should work, but I still get an error. Any ideas?

@Gelio
Copy link
Author

Gelio commented Jun 29, 2023

Thank you both @matthias-Q and @dmfay for fixing it so fast!

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

Successfully merging a pull request may close this issue.

2 participants