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

Parenthesis on FROM clause in a new line

vascosmota opened this issue · comments

Hi @nene , thank you for the great work.

As a Data Scientist working with very long queries often, a good formatter is essential to understand what is going on.

I came to you with a style problem: how the FROM clause gets handled (in the bigquery dialect).

The first query is the query as formated by GCP BigQuery online editor

WITH
  sentiment AS (
  SELECT
    updatedDate,
    score,
    scoreCat,
    customerId,
    CONCAT(user_id, updatedDate) AS ID
  FROM (
    SELECT
      clientId,
      comment,
      customerId,
      score,
      CASE
        WHEN score = 'VeryDissatisfied' THEN 1
        WHEN score = 'Dissatisfied' THEN 2
        WHEN score = 'Neutral' THEN 3
        WHEN score = 'Satisfied' THEN 4
        WHEN score = 'VerySatisfied' THEN 5
    END
      AS scoreCat,
      TIMESTAMP(date) AS updatedDate,
      user.email AS user_email,
      userId AS user_id
    FROM
      `sentiments-main`) )
SELECT
  *
FROM
  sentiment

it gets formated as

WITH
  sentiment AS (
    SELECT
      updatedDate,
      score,
      scoreCat,
      customerId,
      CONCAT(user_id, updatedDate) AS ID
    FROM
      (
        SELECT
          clientId,
          comment,
          customerId,
          score,
          CASE
            WHEN score = 'VeryDissatisfied' THEN 1
            WHEN score = 'Dissatisfied' THEN 2
            WHEN score = 'Neutral' THEN 3
            WHEN score = 'Satisfied' THEN 4
            WHEN score = 'VerySatisfied' THEN 5
          END AS scoreCat,
          TIMESTAMP(date) AS updatedDate,
          user.email AS user_email,
          userId AS user_id
        FROM `sentiments-main`
      )
  )
SELECT
  *
FROM sentiment;

I love that the parenthesis are being closed on a new line, at the same level that they are opened (something that the online editor of GCP BigQuery fails to do).

My question is about the first FROM clause. On the first FROM clause, the following parenthesis is put on a new line and indented. I think this is unnecessary, since the parenthesis could open right after the FROM clause. It also adds a an extra indentation level that is unnecessary.

Is it possible for the FROM clause to open the parenthesis on the same line?

I don't know which algorithm makes these parsing decisions, I am also curious about it.

Thanks for the suggestion.

Currently there's simply no special handling of parenthesized expressions v/s any other. So for example in a simpler FROM clause one would just have a list of table names, in which case placing each one of them on a separate line is all fine:

SELECT *
FROM
  my_table,
  other_table

Also in the case of short subqueries it for me makes more sense to format them on a separate line:

SELECT *
FROM
  (SELECT * FROM tbl) AS t
  LEFT JOIN tbl2 ON (t.id = tbl2.id)

So you really would like a special case for a long subquery in case there's just a single one:

SELECT *
FROM (
  SELECT *
  FROM some_long_table
)

Because if there's more than one, this style wouldn't quite work. It would be odd to have:

SELECT *
FROM (
  SELECT *
  FROM some_long_table
)
  LEFT JOIN some_tbl

One would really want to indent the first subquery by an additional step:

SELECT *
FROM (
    SELECT *
    FROM some_long_table
  )
  LEFT JOIN some_tbl

but that sort of indentation increase when another table is added would IMHO be a pretty weird behavior. Of course we could add this extra indentation also to the single-subquery case... but then that would look wrong IMHO.

I guess this style would be more feasible when I would indent each JOIN on the save level as FROM, which is a pretty widespread style. But that would be a pretty fundamental change to the formatting style used by this tool. I'd also like to avoid adding any options for tweaking the formatting style.

So, in conclusion. While your suggestion looks all fine on the surface, it doesn't seem to play well with the general formatting style of this tool.

Feel free to comment and elaborate though, as I wrote lots of it-feels-to-me arguments, and things might not feel the same from your perspective.