nene / prettier-plugin-sql-cst

Prettier SQL plugin that uses sql-parser-cst

Home Page:https://nene.github.io/prettier-sql-playground/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Format Option Request

dang1507 opened this issue · comments

I have a few option requests that would mean a lot to me if they could be be realized.

  1. Add an option to not add a line break immediately after WHERE or FROM.
    Or simply add 1=1 after WHERE and prepend the next line with AND (no spaces please)
  2. Add an option to move commas before the field name, makes modifying queries much faster.
  3. Personally, I hate floating statements. This is a bigger ask, but it would be awesome if those could be eliminated.

FROM / WHERE Example

SELECT
    TEST1,
    TEST2,
    TEST3,
    CAST(FREQUENCY AS FLOAT64) AS BLUECHILD,
    SUM(FIELD4) OVER (
        PARTITION BY
            FIELD5,
            FIELD6
    ) AS WINDOW_FIELD
FROM
    `project.DATASET.MYTABLE` T
    LEFT JOIN `project.DATASET.MYTABLE2` T2 ON T2.PK = T.PK
WHERE
    1 = 1
    AND FILED1 >= 22342342
    OR FIELD2 = "pi"
;

Desired Output

SELECT
    TEST1
    ,TEST2
    ,TEST3
    ,CAST(FREQUENCY AS FLOAT64) AS BLUECHILD
    ,SUM(FIELD4) OVER(PARTITION BY FIELD5, FIELD6) AS WINDOW_FIELD
FROM `project.DATASET.MYTABLE` T
LEFT JOIN `project.DATASET.MYTABLE2` T2 
    ON T2.PK = T.PK
WHERE 1=1
    AND FILED1 >= 22342342
    OR FIELD2 = "pi"
;

Floating statement examples

WHERE
     1=1 -- I look weird 
     AND 2=2
;

WHERE 1=1 -- I look way cooler
     AND 2=2 
;

UPDATE TABLE
SET
     FIELD1 = FIELD2 -- floating field when only 1 line
WHERE
     1=1
;

UPDATE TABLE
SET FIELD1 = FIELD2 -- more compact, looks cleaner (to me)
WHERE 1=1
;

UPDATE TABLE
SET
     FIELD1 = FIELD2, -- multi line is fine
     FIELD3=FIELD4
WHERE 1=1
;

I guess from the other issue you opened that this one also isn't about prettier-plugin-sql-cst. But I'll respond anyway as I'm maintaining both this library and sql-formatter.

In general the philosophy behind prettier-plugin-sql-cst is to support one SQL formatting style and do it well. Just like the original Prettier library does for JavaScript.

With sql-formatter, this is less of a case in theory. In the past it had several more options to configure the formatting style. But the maintenance overhead just wasn't worth it as the implementations of these other styles were mostly terrible hacks. That's why I've been gradually deprecating and removing such options. For example there used to be an option for comma-first style, but it has been removed by now.

Regarding this comma-first style, did you know that BigQuery allows for trailing commas in SELECT columns list. That's IMHO a much better solution for the same problem. Currently prettier-plugin-sql-cst removes such trailing commas, but I'm planning to introduce an option for configuring it (as it's a syntax that can't be used in all SQL dialects).

Regarding these "floating statements":

UPDATE my_table
SET
     field1 = 'hello'
WHERE
     1=1;

prettier-plugin-sql-cst already does what you're asking for:

UPDATE my_table
SET field1 = 'hello'
WHERE 1 = 1;

It decides dynamically whether it should split WHERE-clause to multiple lines or keep it on a single line. It's actually even more dynamic than that. It will format SELECT foo, bar on a single line if it fits within line-length limit and is on a single line in original source code. But if the original code has it broken to multiple lines, it will format it to multiple lines. This sort of thing is still work-in-progress. It works for some clauses, but the plan is to support it in most places.

It does not however support the implicit-indents style (using SQLFluff terminology):

WHERE 1=1
     AND 2=2;

Currently there's no plan to support such a style in neither formatting library. Even if on one day I would consider supporting it, I would prefer doing it consistently. That is, I would also apply it to the columns in SELECT-clause and everywhere else.