Skip to content

Blank lines between common table expressions (CTEs) misreported as syntax error #372

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

Open
aleclarson opened this issue Apr 24, 2025 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@aleclarson
Copy link

aleclarson commented Apr 24, 2025

I assume this has to do with how statement splitting is implemented.

The SQL formatter I use likes to put blank lines between CTEs.

WITH users_cte AS (
  SELECT
    id,
    email
  FROM account
  WHERE tier = 'Pro'
),

active_sessions_cte AS (
  SELECT
    accountid,
    count(*) AS session_count
  FROM session
  WHERE expiresat > current_timestamp
  GROUP BY accountid
)

SELECT
  u.id,
  u.email,
  coalesce(s.session_count, 0) AS active_sessions
FROM users_cte AS u
LEFT JOIN active_sessions_cte AS s ON u.id = s.accountid
ORDER BY active_sessions DESC;

Output

path/to/example.sql:7:3 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  × Expected Ident
  
     5 │   FROM account
     6 │   WHERE tier = 'Pro'
   > 7 │ ),
       │   
   > 8 │ 
   > 9 │ active_sessions_cte AS (
       │ 
    10 │   SELECT
    11 │     accountid,
  

path/to/example.sql:7:3 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  × Expected As
  
     5 │   FROM account
     6 │   WHERE tier = 'Pro'
   > 7 │ ),
       │   
   > 8 │ 
   > 9 │ active_sessions_cte AS (
       │ 
    10 │   SELECT
    11 │     accountid,
  

path/to/example.sql:7:3 syntax ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  × Expected Ascii40
  
     5 │   FROM account
     6 │   WHERE tier = 'Pro'
   > 7 │ ),
       │   
   > 8 │ 
   > 9 │ active_sessions_cte AS (
       │ 
    10 │   SELECT
    11 │     accountid,
  

Checked 1 file in 37ms. No fixes applied.
Found 3 errors.
check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  × Some errors were emitted while running checks.
  

@aleclarson aleclarson added the bug Something isn't working label Apr 24, 2025
@aleclarson
Copy link
Author

As a workaround, it'd be great to have an option that tells the LSP to skip statement splitting and assume each file is one statement.

@psteinroe
Copy link
Collaborator

We should be able to implement an exception to the double newlines rule for this case. But I also like your idea - makes a few use cases much simpler.

@psteinroe psteinroe self-assigned this Apr 24, 2025
@juleswritescode
Copy link
Collaborator

juleswritescode commented Apr 25, 2025

That's a cool idea.
Maybe we could also work with annotations:

-- pg-stmt-start
...
-- pg-stmt-end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants