PabloRMira / sql_formatter

A Python based SQL formatter
https://pablormira.github.io/sql_formatter/
Apache License 2.0
43 stars 10 forks source link

Table names that include the substring `select` in them break subsequent formatting #162

Closed sambrilleman closed 3 years ago

sambrilleman commented 3 years ago

Describe the bug

Any table name that includes the substring select in it disables the subsequent formatting (or at least line breaks and indenting) of the subsequent statements (e.g. JOIN, ORDER BY, etc).

To Reproduce

Example 1: using select in the name of the FROM table leads to formatting all the subsequent statements without line breaks:

sql_formatter.core.format_sql("""
    SELECT var 
    FROM table_selection as a 
    LEFT JOIN table2 as b ON a.id = b.id 
    LEFT JOIN table3 as c ON a.id = c.id 
    ORDER BY 1
""")
Out[55]: 'SELECT var\nFROM   table_selection as a LEFT JOIN table2 as b ON a.id = b.id LEFT JOIN table3 as c ON a.id = c.id ORDER BY 1'

Example 2: using select in the name of the JOIN table leads to formatting all the subsequent statements without line breaks:

sql_formatter.core.format_sql("""
    SELECT var 
    FROM table1 as a 
    LEFT JOIN table_selection as b ON a.id = b.id 
    LEFT JOIN table3 as c ON a.id = c.id 
    ORDER BY 1
""")
Out[53]: 'SELECT var\nFROM   table1 as a\n    LEFT JOIN table_selection as b ON a.id = b.id LEFT JOIN table3 as c ON a.id = c.id ORDER BY 1'

Expected behavior

Line breaks before keywords, as in:

sql_formatter.core.format_sql("""
    SELECT var 
    FROM table1 as a 
    LEFT JOIN table2 as b ON a.id = b.id 
    LEFT JOIN table3 as c ON a.id = c.id 
    ORDER BY 1
""")
Out[54]: 'SELECT var\nFROM   table1 as a\n    LEFT JOIN table2 as b\n        ON a.id = b.id\n    LEFT JOIN table3 as c\n        ON a.id = c.id\nORDER BY 1'

Screenshots

NA

PabloRMira commented 3 years ago

Thanks again for pointing out and for the very nice report!