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

[FORMATTING] when set Indentation style to "Tabular, Right", it doesn't format "case when" well. #401

Closed
AndyGeng123 opened this issue Aug 19, 2022 · 5 comments · Fixed by #458
Labels

Comments

@AndyGeng123
Copy link

AndyGeng123 commented Aug 19, 2022

Standard style for "case when" is correct, but "Tabular Right" get incorrect format.
I tested it on demo site.

select
  x,
  case  1 then 'a' else 'none'  end a,
  case  2 then 'b' else 'none'  end b
from andy.t_test
where id < 5;

Actual Output

   select x,
          case
                    1 then 'a'
                         else 'none'
          end a,
          case
                    2 then 'b'
                         else 'none'
          end b
     from andy.t_test
    where id < 5;
@nene
Copy link
Collaborator

nene commented Aug 26, 2022

Sorry for not responding earlier. It seems like you're using incorrect CASE expression syntax. Although you mention "when" inside the title, you don't use that keyword in the example code. At least according to SQL standard, CASE expression syntax should be one of the following:

CASE expr
  [WHEN expr THEN expr]...
  [ELSE expr]
END

CASE
  [WHEN condition THEN expr]...
  [ELSE expr]
END

I'm wondering whether you're perhaps using some SQL dialect that supports CASE-expressions without WHEN-keywords.

Anyway, the problem definitely is there, as even with WHEN-keywords the indentation is pretty bad:

   select x,
          case
                    x
                         when 1 then 'a'
                         else 'none'
          end a
     from tbl;

A good result would probably be something like:

   select x,
               case x
               when 1 then 'a'
               else 'none'
                end a
     from tbl;

or

   select x,
          case x
               when 1 then 'a'
               else 'none'
          end a
     from tbl;

Not sure. I don't really have a well-formed opinion of how these tabular formatting styles should really ideally look.

@AndyGeng123
Copy link
Author

Hi, Oracle database have two case syntax:
In online test website, If set "Indentation Style" to "Standard", format is well,
But if set to "Tabular Right" or "Tabular Left", case have extra spaces.

Here is a demo,
https://sql-formatter-org.github.io/sql-formatter/
select
case
when object_id > 500 then 'a'
else 'none_a'
end as col1,
case object_id
when 1 then 'a'
when 2 then 'b'
else 'none_b'
end as col2
from dba_objects
where rownum < 5;

@nene
Copy link
Collaborator

nene commented Aug 28, 2022

Yep. I do understand the problem. (Thanks for also mentioning the SQL flavor you're using.)

It's just that I'm not really sure what the ideal solution would be.

@AndyGeng123
Copy link
Author

Hi,
There is another online format website, after set "Stacked align" to "Align right", it can format "case" well.
Just FYI: https://www.dpriver.com/pp/sqlformat.htm
Many thanks for your great work!

@nene
Copy link
Collaborator

nene commented Sep 27, 2022

The formatting has now been improved in 10.8.0-beta.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants