sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.32k stars 398 forks source link

Feature Request: Comma Position #697

Open ChenZhouUC opened 9 months ago

ChenZhouUC commented 9 months ago

When using the previous version of sql formatter, I found comma position configuration very useful in debugging. I wondered if this feature could be brought back sometime.

nene commented 9 months ago

As a workaround. If you really want to simply get this feature back as it was, you can take the old implementation (formatCommaPositions.ts) and run the formatted code through it. Something like:

import { format } from "sql-formatter";
import { formatCommaPositions } from "./formatCommaPositions";

const sql = format("SELECT foo, bar, baz FROM tbl", { language: "sqlite" });
console.log(formatCommaPositions(sql, "before", "  "));

Issues

You should be aware that this whole implementation is one big hack and will easily break. e.g. it will break for BigQuery which allows a trailing comma in SELECT (though this style won't really make much sense in case of BigQuery anyway). And it will break when it encounters comments in unexpected places. And likely many more scenarios...

The implementation also conflicts with how the same feature is implemented in SQLFluff. Namely, SQLFluff expects leading commas to be indented like so (keeping the actual indentation of each line):

SELECT
    foo
    , bar
    , baz
FROM
    tbl

But this implementation unindents each line with a comma by two spaces:

SELECT
    foo
  , bar
  , baz
FROM
    tbl

Which is IMHO pretty awkward and also prevents it from working when TABs are used for indentation.

Future

I currently don't have any plans to re-implement this feature. I'm open to pull requests if anybody is interested in creating this feature properly.

ChenZhouUC commented 9 months ago

As a workaround. If you really want to simply get this feature back as it was, you can take the old implementation (formatCommaPositions.ts) and run the formatted code through it. Something like:

import { format } from "sql-formatter";
import { formatCommaPositions } from "./formatCommaPositions";

const sql = format("SELECT foo, bar, baz FROM tbl", { language: "sqlite" });
console.log(formatCommaPositions(sql, "before", "  "));

Issues

You should be aware that this whole implementation is one big hack and will easily break. e.g. it will break for BigQuery which allows a trailing comma in SELECT (though this style won't really make much sense in case of BigQuery anyway). And it will break when it encounters comments in unexpected places. And likely many more scenarios...

The implementation also conflicts with how the same feature is implemented in SQLFluff. Namely, SQLFluff expects leading commas to be indented like so (keeping the actual indentation of each line):

SELECT
    foo
    , bar
    , baz
FROM
    tbl

But this implementation unindents each line with a comma by two spaces:

SELECT
    foo
  , bar
  , baz
FROM
    tbl

Which is IMHO pretty awkward and also prevents it from working when TABs are used for indentation.

Future

I currently don't have any plans to re-implement this feature. I'm open to pull requests if anybody is interested in creating this feature properly.

Thank you for the workaround and clear explanation. Looking forward to the future plans.