vertical-blank/sql-formatter

Extra whitespace inserted in string concatenation operator

rhinotake opened this issue · 3 comments

In Dialect.PostgreSql, when using the string concatenation operator (||), extra whitespace is inserted.

    val res = SqlFormatter
      .of(Dialect.PostgreSql)
      .format("select aa || bb from zzz")

    println(res)
    // select
    //   aa | | bb
    //       ^
    // from
    //   zzz

If you run dialectConfig.plusOperators("||"), no extra whitespace will be inserted.

    val res = SqlFormatter
      .of(Dialect.PostgreSql)
      .extend { dialectConfig -> dialectConfig.plusOperators("||") }
      .format("select aa || bb from zzz")

    println(res)
    // select
    //   aa || bb
    // from
    //   zzz

Similar whitespace will be inserted in Dialect.N1ql and Dialect.TSql.

I can't judge whether the response is appropriate, but after applying the following patch, plusOperators("||") is no longer needed.

diff --git a/src/main/java/com/github/vertical_blank/sqlformatter/languages/PostgreSqlFormatter.java b/src/main/java/com/github/vertical_blank/sqlformatter/languages/PostgreSqlFormatter.java
index e8c4f0a..15e76ae 100644
--- a/src/main/java/com/github/vertical_blank/sqlformatter/languages/PostgreSqlFormatter.java
+++ b/src/main/java/com/github/vertical_blank/sqlformatter/languages/PostgreSqlFormatter.java
@@ -531,7 +531,7 @@ public class PostgreSqlFormatter extends AbstractFormatter {
         .operators(
             Arrays.asList(
                 "!=", "<<", ">>", "||/", "|/", "::", "->>", "->", "~~*", "~~", "!~~*", "!~~", "~*",
-                "!~*", "!~", "!!", "@@", "@@@"))
+                "!~*", "!~", "!!", "@@", "@@@", "||"))
         .build();
   }

@rhinotake
As you said, || should be treated as a string concatenation operator on PostgreSQL and CouchBase N1QL.
But, T-SQL seems to use + as a string concatenation operator.
https://docs.microsoft.com/ja-jp/sql/t-sql/language-elements/string-concatenation-transact-sql?view=sql-server-ver15

I'm going to fix this.
Thank you for reporting.

I had this problem as well, the formatting inserted extra spaces, and so did the qualifiers

Raw SQL:

select `t1`.`teacher_id` as id, `t1`.`first_name` as firstName, upper(md5(`t1`.`first_name`)) as firstName, (select `t1`.`gender` as gender from `teacher` as `t1` limit 1) as nested1 from `teacher` as `t1` inner join `subject` as `subject` on `t1`.`teacher_id` = `subject`.`teacher_id` or `t1`.`teacher_id` < 1 where  `t1`.`teacher_id` != -1 and exists (select `t1`.`gender` as gender from `teacher` as `t1` limit 1) group by `t1`.`teacher_id` limit 0, 10

Formatted SQL:

select
  ` t1 `.` teacher_id ` as id,
  ` t1 `.` first_name ` as firstName,
  upper(md5(` t1 `.` first_name `)) as firstName,
  (
    select
      ` t1 `.` gender ` as gender
    from
      ` teacher ` as ` t1 `
    limit
      1
  ) as nested1
from
  ` teacher ` as ` t1 `
  inner join ` subject ` as ` subject ` on ` t1 `.` teacher_id ` = ` subject `.` teacher_id `
  or ` t1 `.` teacher_id ` < 1
where
  ` t1 `.` teacher_id ` ! = -1
  and exists (
    select
      ` t1 `.` gender ` as gender
    from
      ` teacher ` as ` t1 `
    limit
      1
  )
group by
  ` t1 `.` teacher_id `
limit
  0, 10