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

Feature request: Add line breaks in long expressions #777

Open
joncurrier opened this issue Jun 28, 2024 · 0 comments
Open

Feature request: Add line breaks in long expressions #777

joncurrier opened this issue Jun 28, 2024 · 0 comments

Comments

@joncurrier
Copy link

Suppose I have a query like this:

 SELECT CASE 
          WHEN condition_1 = 1 
            AND condition_2 = 2 
            AND condition_3 = 3 
            AND condition_4 = 4 
            AND condition_5 = 5
            THEN '5 conditions satisfied'
          WHEN condition_6_with_a_long_name = 6 
            THEN 'Condition 6 satisfied'
          ELSE 'Uh-oh'
        END AS long_case_field,
        field_with_long_name_1 + field_with_long_name_2 + field_with_long_name_3 + field_with_long_name_4 + field_with_long_name_5 + field_with_long_name_6 AS sum_of_fields
   FROM my_table

When I run sqlparse.format(sql, reindent_aligned=True), I get this:

SELECT CASE WHEN condition_1 = 1 AND condition_2 = 2 AND condition_3 = 3 AND condition_4 = 4 AND condition_5 = 5 THEN '5 conditions satisfied'
            WHEN condition_6_with_a_long_name = 6                                                                THEN 'Condition 6 satisfied'
            ELSE 'Uh-oh'
             END AS long_case_field,
       field_with_long_name_1 + field_with_long_name_2 + field_with_long_name_3 + field_with_long_name_4 + field_with_long_name_5 + field_with_long_name_6 AS sum_of_fields
  FROM my_table

I think CASE statements are generally more readable when there's a line break before every AND/OR, so I'd prefer the formatter not to collapse these all to one line. For the long function expression, having a max_line_length after which it would add a line break before every + or other operator would be very nice to have also, so the output would be like

SELECT CASE WHEN condition_1 = 1 
             AND condition_2 = 2 
             AND condition_3 = 3 
             AND condition_4 = 4 
             AND condition_5 = 5                  THEN '5 conditions satisfied'
            WHEN condition_6_with_a_long_name = 6 THEN 'Condition 6 satisfied'
            ELSE 'Uh-oh'
             END AS long_case_field,
       field_with_long_name_1 
       + field_with_long_name_2 
       + field_with_long_name_3 
       + field_with_long_name_4 
       + field_with_long_name_5 
       + field_with_long_name_6 AS sum_of_fields
  FROM my_table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants