Skip to content

Commit 2588297

Browse files
committed
Add ident format for GROUP BY
1 parent 95a195f commit 2588297

6 files changed

Lines changed: 25 additions & 40 deletions

File tree

parser/format.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,43 +1504,30 @@ func (g *GranteesClause) FormatSQL(formatter *Formatter) {
15041504

15051505
func (g *GroupByClause) FormatSQL(formatter *Formatter) {
15061506
formatter.WriteString("GROUP BY")
1507+
1508+
formatter.Indent()
1509+
defer formatter.Dedent()
15071510
if g.AggregateType != "" {
1508-
formatter.WriteByte(whitespace)
1511+
formatter.Break()
15091512
formatter.WriteString(g.AggregateType)
15101513
}
15111514
if g.Expr != nil {
1512-
if g.AggregateType == "" && formatter.mode == FormatModeBeautify {
1513-
if columnList, ok := g.Expr.(*ColumnExprList); ok && len(columnList.Items) > 0 {
1514-
formatter.Indent()
1515-
for i, item := range columnList.Items {
1516-
if i == 0 {
1517-
formatter.Break()
1518-
} else {
1519-
formatter.WriteByte(',')
1520-
formatter.Break()
1521-
}
1522-
formatter.WriteExpr(item)
1523-
}
1524-
formatter.Dedent()
1525-
} else {
1526-
formatter.WriteByte(whitespace)
1527-
formatter.WriteExpr(g.Expr)
1528-
}
1529-
} else if g.AggregateType == "" {
1530-
formatter.WriteByte(whitespace)
1531-
formatter.WriteExpr(g.Expr)
1532-
} else {
1533-
formatter.WriteExpr(g.Expr)
1515+
if g.AggregateType == "" {
1516+
formatter.Break()
15341517
}
1518+
formatter.WriteExpr(g.Expr)
15351519
}
15361520
if g.WithCube {
1537-
formatter.WriteString(" WITH CUBE")
1521+
formatter.Break()
1522+
formatter.WriteString("WITH CUBE")
15381523
}
15391524
if g.WithRollup {
1540-
formatter.WriteString(" WITH ROLLUP")
1525+
formatter.Break()
1526+
formatter.WriteString("WITH ROLLUP")
15411527
}
15421528
if g.WithTotals {
1543-
formatter.WriteString(" WITH TOTALS")
1529+
formatter.Break()
1530+
formatter.WriteString("WITH TOTALS")
15441531
}
15451532
}
15461533

parser/testdata/query/format/beautify/select_extract_with_regex.sql

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@ GROUP BY
4141
CASE
4242
WHEN length(EXTRACT(instance, '((\\d+\\.){3}\\d+)')) > 0 THEN instance
4343
ELSE ''
44-
END,
45-
CASE
44+
END, CASE
4645
WHEN length(EXTRACT(client_ip, '((\\d+\\.){3}\\d+)')) > 0 THEN client_ip
4746
ELSE ''
48-
END,
49-
src_type,
50-
node_class,
51-
port,
52-
client_port
47+
END, src_type, node_class, port, client_port
5348
LIMIT 10000;

parser/testdata/query/format/beautify/select_simple.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ AND
2727
AND
2828
f3 NOT IN ('a', 'b', 'c')
2929
GROUP BY
30-
f0,
31-
f1
30+
f0, f1
3231
LIMIT 10 OFFSET 100 BY f0;

parser/testdata/query/format/beautify/select_simple_with_group_by_with_cube_totals.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ SELECT
66
a,
77
COUNT(b)
88
FROM group_by_all
9-
GROUP BY CUBE(a) WITH CUBE WITH TOTALS
9+
GROUP BY
10+
CUBE(a)
11+
WITH CUBE
12+
WITH TOTALS
1013
ORDER BY
1114
a;

parser/testdata/query/format/beautify/select_window_cte.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ WITH
3838
WHERE
3939
year = 2023
4040
GROUP BY
41-
month,
42-
department),
41+
month, department),
4342
ranked AS (SELECT
4443
month,
4544
department,

parser/testdata/query/format/beautify/select_with_group_by.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ SELECT
2727
distro,
2828
SUM(quantity) AS qty
2929
FROM servers
30-
GROUP BY GROUPING SETS((datacenter, distro), (datacenter), (distro), ());
30+
GROUP BY
31+
GROUPING SETS((datacenter, distro), (datacenter), (distro), ());
3132
SELECT
3233
datacenter,
3334
distro,
3435
SUM(quantity) AS qty
3536
FROM servers
36-
GROUP BY ALL;
37+
GROUP BY
38+
ALL;

0 commit comments

Comments
 (0)