Skip to content

Formatting of CTEs #163

@sambrilleman

Description

@sambrilleman

Is your feature request related to a problem? Please describe

Thanks for this package! Is it possible to have the start of a CTE definition on a new line?

The current behaviour is that a CTE is a continuation of the same line, as in the following example:

print(
    sql_formatter.core.format_sql("""
    WITH
    
    first_cte AS (
        SELECT var 
        FROM table1
    ),
    
    second_cte AS (
        SELECT var 
        FROM table2
    )
    
    SELECT a.var
    FROM first_cte
    JOIN second_cte USING (var)
    """)
)

which leads to the following output:

with first_cte as (SELECT var
                   FROM   table1), second_cte as (SELECT var
                               FROM   table2)
SELECT a.var
FROM   first_cte join second_cte using (var)

Describe the solution you'd like

The desired output would be:

WITH

first_cte as (
    SELECT var
    FROM   table1
), 

second_cte as (
    SELECT var
    FROM   table2
)

SELECT a.var
FROM   first_cte join second_cte using (var)

That is:

  • the WITH would be capitalised on its own line
  • the name of the CTE would be on a new line (preferably with an empty line above)
  • the SELECT statement inside the definition of the CTE would start on a new line, idented to represent a block nested within the parentheses ()

Describe alternatives you've considered

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions