Skip to content

Commit

Permalink
fix: misplaced commas when formatting CTAS statements (#3058)
Browse files Browse the repository at this point in the history
- commas now placed after each projection field on same line
  - fixed unit tests to reflect new format
  • Loading branch information
vinothchandar authored Jul 10, 2019
1 parent 0346933 commit c05615d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public static String formatSql(final Node root, final boolean unmangleNames) {
return StringUtils.stripEnd(builder.toString(), "\n");
}

private static final class Formatter
extends AstVisitor<Void, Integer> {
private static final class Formatter extends AstVisitor<Void, Integer> {

private final StringBuilder builder;
private final boolean unmangledNames;
Expand Down Expand Up @@ -147,9 +146,9 @@ protected Void visitSelect(final Select node, final Integer indent) {
if (selectItems.size() > 1) {
boolean first = true;
for (final SelectItem item : selectItems) {
builder.append("\n")
.append(indentString(indent))
.append(first ? " " : ", ");
builder.append(first ? "" : ",")
.append("\n ")
.append(indentString(indent));

process(item, indent);
first = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ public void shouldFormatSelectStarCorrectlyWithOtherFields() {
.getStatement();
assertThat(SqlFormatter.formatSql(statement),
equalTo("CREATE STREAM S AS SELECT\n"
+ " *\n"
+ ", ADDRESS.ADDRESS \"CITY\"\n"
+ " *,\n"
+ " ADDRESS.ADDRESS \"CITY\"\n"
+ "FROM ADDRESS ADDRESS"));
}

Expand All @@ -337,8 +337,8 @@ public void shouldFormatSelectStarCorrectlyWithJoin() {
.getStatement();
assertThat(SqlFormatter.formatSql(statement),
equalTo("CREATE STREAM S AS SELECT\n"
+ " ADDRESS.*\n"
+ ", ITEMID.*\n"
+ " ADDRESS.*,\n"
+ " ITEMID.*\n"
+ "FROM ADDRESS ADDRESS\n"
+ "INNER JOIN ITEMID ITEMID ON ((ADDRESS.ADDRESS = ITEMID.ADDRESS->ADDRESS))"));
}
Expand All @@ -351,8 +351,8 @@ public void shouldFormatSelectStarCorrectlyWithJoinOneSidedStar() {
.getStatement();
assertThat(SqlFormatter.formatSql(statement),
equalTo("CREATE STREAM S AS SELECT\n"
+ " ADDRESS.*\n"
+ ", ITEMID.ORDERTIME \"ORDERTIME\"\n"
+ " ADDRESS.*,\n"
+ " ITEMID.ORDERTIME \"ORDERTIME\"\n"
+ "FROM ADDRESS ADDRESS\n"
+ "INNER JOIN ITEMID ITEMID ON ((ADDRESS.ADDRESS = ITEMID.ADDRESS->ADDRESS))"));
}
Expand All @@ -364,8 +364,8 @@ public void shouldFormatSelectCorrectlyWithDuplicateFields() {
.getStatement();
assertThat(SqlFormatter.formatSql(statement),
equalTo("CREATE STREAM S AS SELECT\n"
+ " ADDRESS.ADDRESS \"ONE\"\n"
+ ", ADDRESS.ADDRESS \"TWO\"\n"
+ " ADDRESS.ADDRESS \"ONE\",\n"
+ " ADDRESS.ADDRESS \"TWO\"\n"
+ "FROM ADDRESS ADDRESS"));
}

Expand Down Expand Up @@ -532,8 +532,8 @@ public void shouldFormatTumblingWindow() {
final String result = SqlFormatter.formatSql(statement);

assertThat(result, is("CREATE STREAM S AS SELECT\n"
+ " ORDERS.ITEMID \"ITEMID\"\n"
+ ", COUNT(*) \"KSQL_COL_1\"\n"
+ " ORDERS.ITEMID \"ITEMID\",\n"
+ " COUNT(*) \"KSQL_COL_1\"\n"
+ "FROM ORDERS ORDERS\n"
+ "WINDOW TUMBLING ( SIZE 7 DAYS ) \n"
+ "GROUP BY ORDERS.ITEMID"));
Expand All @@ -549,8 +549,8 @@ public void shouldFormatHoppingWindow() {
final String result = SqlFormatter.formatSql(statement);

assertThat(result, is("CREATE STREAM S AS SELECT\n"
+ " ORDERS.ITEMID \"ITEMID\"\n"
+ ", COUNT(*) \"KSQL_COL_1\"\n"
+ " ORDERS.ITEMID \"ITEMID\",\n"
+ " COUNT(*) \"KSQL_COL_1\"\n"
+ "FROM ORDERS ORDERS\n"
+ "WINDOW HOPPING ( SIZE 20 SECONDS , ADVANCE BY 5 SECONDS ) \n"
+ "GROUP BY ORDERS.ITEMID"));
Expand All @@ -566,8 +566,8 @@ public void shouldFormatSessionWindow() {
final String result = SqlFormatter.formatSql(statement);

assertThat(result, is("CREATE STREAM S AS SELECT\n"
+ " ORDERS.ITEMID \"ITEMID\"\n"
+ ", COUNT(*) \"KSQL_COL_1\"\n"
+ " ORDERS.ITEMID \"ITEMID\",\n"
+ " COUNT(*) \"KSQL_COL_1\"\n"
+ "FROM ORDERS ORDERS\n"
+ "WINDOW SESSION ( 15 MINUTES ) \n"
+ "GROUP BY ORDERS.ITEMID"));
Expand Down

0 comments on commit c05615d

Please sign in to comment.