[BUG] Hive SQLUtils.format duplicates and merges comments before SELECT items
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
SELECT
-- first column
a,
-- second column
b
FROM demo_tableMinimal Reproduction
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
SELECT -- first column
a -- first column-- second column
, b
FROM demo_tableThe output has the following problems:
-- first columnis emitted twice.-- first columnand-- second columnare concatenated.- The comments are no longer associated with the correct SELECT items.
- 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:
SELECT
-- first column
a,
-- second column
b
FROM demo_tableThe formatter should guarantee that:
- no comment is lost;
- no comment is duplicated;
- comments are not concatenated;
- comment order is preserved;
- 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:
SELECT a, b
FROM demo_tableIn 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
SQLSelectItemAST node during parsing; - the Hive SQL output visitor emits before/after comments incorrectly.
Related Issues
There are older open issues concerning lost comments:
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