Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#6722·druid

[BUG] Hive SQLUtils.format duplicates and merges comments before SELECT items

Author: WCY-dtCreated Sep 18, 2026Updated Sep 18, 2026

Description

When formatting Hive SQL, SQLUtils.format does not preserve line comments before SELECT items correctly.

In Druid 1.2.8, these comments are removed completely. In the latest release, Druid 1.2.28, the comments are retained partially but are duplicated, concatenated, and associated with the wrong SELECT items.

Environment

  • Database Type: Hive
  • Database Version: N/A — the problem occurs in the SQL formatter without connecting to a database
  • Druid Version: 1.2.28
  • JDK Version: OpenJDK 17.0.19

Minimal SQL

sql
SELECT
    -- first column
    a,
    -- second column
    b
FROM demo_table

Minimal Reproduction

java
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;

public class DruidHiveCommentFormatTest {

    public static void main(String[] args) {
        String sql =
                "SELECT\n"
              + "    -- first column\n"
              + "    a,\n"
              + "    -- second column\n"
              + "    b\n"
              + "FROM demo_table";

        String formatted = SQLUtils.format(sql, DbType.hive);
        System.out.println(formatted);
    }
}

Actual Output with Druid 1.2.28

sql
SELECT -- first column
    a -- first column-- second column
    
    , b
FROM demo_table

The output has the following problems:

  1. -- first column is emitted twice.
  2. -- first column and -- second column are concatenated.
  3. The comments are no longer associated with the correct SELECT items.
  4. The formatter changes the comment structure and meaning of the original SQL.

Expected Output

The exact indentation is not important, but each comment should be emitted exactly once, in the original order, and remain associated with the corresponding SELECT item.

For example:

sql
SELECT
    -- first column
    a,
    -- second column
    b
FROM demo_table

The formatter should guarantee that:

  1. no comment is lost;
  2. no comment is duplicated;
  3. comments are not concatenated;
  4. comment order is preserved;
  5. each comment remains associated with the correct SQL element.

Behavior in Druid 1.2.8

Running the same reproduction with Druid 1.2.8 produces:

sql
SELECT a, b
FROM demo_table

In 1.2.8, both comments are removed completely.

Upgrading from 1.2.8 to 1.2.28 therefore does not resolve the problem. The behavior changes from dropping comments to duplicating and merging them.

Additional Analysis

SQLUtils.format already uses SQLParserFeature.KeepComments in its default formatting features. Therefore, this does not appear to be caused by a missing caller-side option.

The problem may be related to one or both of the following:

  • leading comments are not attached to the correct SQLSelectItem AST node during parsing;
  • the Hive SQL output visitor emits before/after comments incorrectly.

Related Issues

There are older open issues concerning lost comments:

  • https://github.com/alibaba/druid/issues/4889
  • https://github.com/alibaba/druid/issues/4240

This report provides a minimal reproduction against the current latest release, Druid 1.2.28. It also demonstrates a different current behavior: comments are not only lost but may be duplicated, merged, and attached to the wrong SELECT item.

Please confirm whether this has the same root cause as the existing issues or should be tracked separately.

Source: alibaba/druid

View original on GitHubView discussion on GitHub