google / google-sql-syntax-ts

Apache License 2.0
5 stars 1 forks source link

Formatting of CTEs #2

Open Schnurres opened 3 months ago

Schnurres commented 3 months ago

Hello,

We use Dataform and BigQuery extensively in our projects. The built-in SQL formatter, which appears to be based on this repository, presents significant challenges in its current form due to how it handles Common Table Expressions (CTEs).

The primary issue is the lack of clear indentation, making it difficult to discern where CTEs begin and end in the formatted SQL. Below are examples that illustrate this problem:

Example 1: BigQuery/Dataform Formatter

The image shows a query formatted by the current formatter. Notice the minimal indentation, making it hard to distinguish the boundaries of the CTEs. BigQuery/Dataform Formatter

Example 2: SQLfluff Formatter

The same query formatted with SQLfluff. The CTEs are clearly indented, making it easy to identify where they start and end. SQLfluff Formatter

Example 3: JetBrains Default Formatter (PyCharm/DataGrip)

Another example using JetBrains' default formatter. It also clearly shows the structure of CTEs through proper indentation. JetBrains Formatter

Suggestion

To enhance readability and usability, we suggest the following improvements for CTE formatting:

Implementing these changes would significantly improve the readability of formatted SQL queries, making it easier for developers to work with complex queries involving multiple CTEs.

Thank you for considering this suggestion.

Best regards, Schnurres

Ekrekr commented 3 months ago

Thanks for reporting this!

It seems like a simple but effective solution to this would be to update our default indentation from two spaces to four spaces - I'll see if I can make this happen.

Line breaks is more difficult, but I'll look deeper.

Ekrekr commented 2 months ago

This whitespace change may be invalidated by https://github.com/google/google-sql-syntax-ts/issues/3, where Charis-Nicolas is suggesting updating the BigQuery SQL formatter to similar to other formatters.

While easy to update by itself, I would have to make this indentation user configurable, to prevent changing the functionality that users currently expect.

I'd recommend filing this as a feature request to try and get traction! https://cloud.google.com/bigquery/docs/getting-support#issuetracker