Skip to content

Bad indentation of the then statement in case expressions #302

@f-tremblay

Description

@f-tremblay

Describe the bug
In case expressions, when there are long when statements, sqlfmt put the then statements at the same indentation as the when statements, which makes them harder to read.
If we look at the dbt style guide (e.g. here and here), the then is more indented.

To Reproduce

with
  source as (select * from {{ source("my_application", "users") }}),
  renamed as (
      select
          sum(
              case when order_status = 'complete' then order_total end
          ) as monthly_total,
          nullif(
              trim(
                  case
                      when regexp_count(nullif(full_name, ''), ' ') = 0
                      then nullif(full_name, '')
                      when regexp_count(nullif(full_name, ''), ' ') = 1
                      then split_part(nullif(full_name, ''), ' ', 1)
                      -- this user has more than two names!
                      else regexp_substr(nullif(full_name, ''), '.* .* ')
                  end
              )

              from source
          )
      select *
      from renamed

Expected behavior

with
  source as (select * from {{ source("my_application", "users") }}),
  renamed as (
      select
          sum(
              case when order_status = 'complete' then order_total end
          ) as monthly_total,
          nullif(
              trim(
                  case
                      when regexp_count(nullif(full_name, ''), ' ') = 0
                          then nullif(full_name, '')
                      when regexp_count(nullif(full_name, ''), ' ') = 1
                          then split_part(nullif(full_name, ''), ' ', 1)
                      -- this user has more than two names!
                      else regexp_substr(nullif(full_name, ''), '.* .* ')
                  end
              )

              from source
          )
      select *
      from renamed

Actual behavior
thenare not indented properly (see To Reproduce )

Additional context
What is the output of sqlfmt --version?
sqlfmt, version 0.13.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions