inferrinizzard / prettier-sql

[ARCHIVED] Please use https://github.com/sql-formatter-org/sql-formatter
https://github.com/sql-formatter-org/sql-formatter
MIT License
21 stars 5 forks source link

[FORMATTING] CASE statements in SUM() #69

Closed tanukihee closed 2 years ago

tanukihee commented 2 years ago

Usage

Describe the bug Bugs occur when there are CASE statements in SUM().

  1. When there is only one SUM(CASE ...), like
    select sum(case a when foo then bar end) from quaz

    Expected Output

    SELECT
    SUM(CASE a
    WHEN foo
    THEN bar END
    )
    FROM
    quaz

Actual Output

SELECT
  SUM(CASE a
  WHEN foo
  THEN barEND
)
FROM
  quaz

Note there should be a space before the END .

  1. When there is something else after SUM(CASE ...), like
    select sum(case a when foo then bar end), b from quaz

    Expected Output

    SELECT
    SUM(CASE a
    WHEN foo
    THEN bar END
    )
    , b
    FROM
    quaz

Actual Output

An Error Occurred, please report this at:
https://github.com/inferrinizzard/prettier-sql/issues

Stack Trace:
TypeError: Cannot read properties of null (reading '1')
    at https://unpkg.com/prettier-sql@latest/dist/prettier-sql.min.js:1:9589
    at Array.map (<anonymous>)
    at https://unpkg.com/prettier-sql@latest/dist/prettier-sql.min.js:1:9539
    at I.value (https://unpkg.com/prettier-sql@latest/dist/prettier-sql.min.js:1:9715)
    at I.value (https://unpkg.com/prettier-sql@latest/dist/prettier-sql.min.js:1:8987)
    at I.value (https://unpkg.com/prettier-sql@latest/dist/prettier-sql.min.js:1:8800)
    at Module.VT (https://unpkg.com/prettier-sql@latest/dist/prettier-sql.min.js:1:184856)
    at HTMLTextAreaElement.format (https://inferrinizzard.github.io/prettier-sql/index.js:42:31)
tanukihee commented 2 years ago

Maybe the bug label is more suitable😂

CreationEdge commented 2 years ago

This occurs for BigQuery as well.

But I was able to circumvent it by setting my Line Width to 40 (the issue occurs at 60 and 80).

Additionally, it doesn't occur consistently. If I have 8 such SUM(CASE ... END) statements in a row, it may only be the last 3 that get the incorrect formatting. In may case, turning NULL END into NULLEND.

inferrinizzard commented 2 years ago

I can't seem to reproduce the final error you reported but there is a fix out for the END spacing issue in #79

tanukihee commented 2 years ago

I can't seem to reproduce the final error you reported but there is a fix out for the END spacing issue in #79

My fault. Please change the CommaPosition option to before.

Screenshot_2022-04-05-12-37-07-300_com.microsoft.emmx.jpg