sql-formatter-org / sql-formatter

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

[FORMATTING] Formatter adding space for sqlc macros #786

Closed B4Dmonkey closed 1 month ago

B4Dmonkey commented 1 month ago

Input data

Which SQL and options did you provide as input?

SELECT
  sqlc.embed(transactions),
  sqlc.embed(documents_meta)
FROM
  transactions
  JOIN documents_meta ON transactions.document_id=documents_meta.id
WHERE
  transactions.posting_date>=@start_date
  AND transactions.posting_date<=@end_date
  AND documents_meta.publishing_date=(
    SELECT
      MAX(publishing_date)
    FROM
      documents_meta
  )
ORDER BY
  transactions.posting_date DESC;

Expected Output

SELECT
  sqlc.embed(transactions),
  sqlc.embed(documents_meta)
FROM
  transactions
  JOIN documents_meta ON transactions.document_id=documents_meta.id
WHERE
  transactions.posting_date>=@start_date
  AND transactions.posting_date<=@end_date
  AND documents_meta.publishing_date=(
    SELECT
      MAX(publishing_date)
    FROM
      documents_meta
  )
ORDER BY
  transactions.posting_date DESC;

Actual Output

SELECT
  sqlc.embed (transactions),
  sqlc.embed (documents_meta)
FROM
  transactions
  JOIN documents_meta ON transactions.document_id=documents_meta.id
WHERE
  transactions.posting_date>=@start_date
  AND transactions.posting_date<=@end_date
  AND documents_meta.publishing_date=(
    SELECT
      MAX(publishing_date)
    FROM
      documents_meta
  )
ORDER BY
  transactions.posting_date DESC;

Usage

Additional information When I'm running the formatter it's inserting a space for sqlc.embed(transactions). This is an issue because it breaks sqlc's compilation. As best I can tell theres no configuration to prevent inserting this space.

nene commented 1 month ago

Thanks for reporting, but I should point out that you're using a pretty outdated version of SQL Formatter library (the latest version is 15.x). Perhaps also read the FAQ.

However this problem is also present in the latest version. There's an old issue open for this #444. So I'm closing this as duplicate.

I might suggest you instead give a try to prettier-plugin-sql-cst which does not have this issue and has in general pretty solid SQLite support.