Skip to content

Commit 6aaddc0

Browse files
authored
Remove an extra leading space when formatting the WINDOW FUNCTION (#174)
1 parent 91718f8 commit 6aaddc0

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

parser/ast.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5790,20 +5790,20 @@ func (w *WindowExpr) End() Pos {
57905790
}
57915791

57925792
func (w *WindowExpr) String() string {
5793-
var builder strings.Builder
5794-
builder.WriteByte('(')
5793+
parts := make([]string, 0)
57955794
if w.PartitionBy != nil {
5796-
builder.WriteString(" ")
5797-
builder.WriteString(w.PartitionBy.String())
5795+
parts = append(parts, w.PartitionBy.String())
57985796
}
57995797
if w.OrderBy != nil {
5800-
builder.WriteString(" ")
5801-
builder.WriteString(w.OrderBy.String())
5798+
parts = append(parts, w.OrderBy.String())
58025799
}
58035800
if w.Frame != nil {
5804-
builder.WriteString(" ")
5805-
builder.WriteString(w.Frame.String())
5801+
parts = append(parts, w.Frame.String())
58065802
}
5803+
5804+
var builder strings.Builder
5805+
builder.WriteByte('(')
5806+
builder.WriteString(strings.Join(parts, " "))
58075807
builder.WriteByte(')')
58085808
return builder.String()
58095809
}

parser/testdata/ddl/format/create_materialized_view_with_empty_table_schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ from
1616
where rn = 1;
1717

1818
-- Format SQL:
19-
CREATE MATERIALIZED VIEW test.t0 ON CLUSTER default_cluster ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/{layer}-{shard}/test/t0', '{replica}') PARTITION BY toYYYYMM(f0) ORDER BY (f0) POPULATE AS SELECT f0, f1, f2, coalesce(f0, f1) AS f333 FROM (SELECT f0, f1, f2, ROW_NUMBER() OVER ( PARTITION BY f0 ORDER BY coalesce(f1, f2)) AS rn FROM test.t WHERE f3 IN ('foo', 'bar', 'test') AND env = 'test') AS tmp WHERE rn = 1;
19+
CREATE MATERIALIZED VIEW test.t0 ON CLUSTER default_cluster ENGINE = ReplicatedAggregatingMergeTree('/clickhouse/{layer}-{shard}/test/t0', '{replica}') PARTITION BY toYYYYMM(f0) ORDER BY (f0) POPULATE AS SELECT f0, f1, f2, coalesce(f0, f1) AS f333 FROM (SELECT f0, f1, f2, ROW_NUMBER() OVER (PARTITION BY f0 ORDER BY coalesce(f1, f2)) AS rn FROM test.t WHERE f3 IN ('foo', 'bar', 'test') AND env = 'test') AS tmp WHERE rn = 1;

parser/testdata/query/format/select_simple.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ GROUP BY f0, f1
1212
Limit 100, 10 By f0;
1313

1414
-- Format SQL:
15-
SELECT f0, coalesce(f1, f2) AS f3, row_number() OVER ( PARTITION BY f0 ORDER BY f1 ASC) AS rn FROM test.events_local WHERE (f0 IN ('foo', 'bar', 'test')) AND (f1 = 'testing') AND (f2 NOT LIKE 'testing2') AND f3 NOT IN ('a', 'b', 'c') GROUP BY f0, f1 LIMIT 10 OFFSET 100 BY f0;
15+
SELECT f0, coalesce(f1, f2) AS f3, row_number() OVER (PARTITION BY f0 ORDER BY f1 ASC) AS rn FROM test.events_local WHERE (f0 IN ('foo', 'bar', 'test')) AND (f1 = 'testing') AND (f2 NOT LIKE 'testing2') AND f3 NOT IN ('a', 'b', 'c') GROUP BY f0, f1 LIMIT 10 OFFSET 100 BY f0;

parser/testdata/query/format/select_with_window_function.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ WINDOW w AS (PARTITION BY aggregation_target
1414
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING);
1515

1616
-- Format SQL:
17-
SELECT aggregation_target AS aggregation_target, timestamp AS timestamp, step_0 AS step_0, latest_0 AS latest_0, step_1 AS step_1, latest_1 AS latest_1, step_2 AS step_2, min(latest_2) OVER ( PARTITION BY aggregation_target ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) AS latest_2, min(latest_1) OVER w AS latest_1 FROM t0 WINDOW w AS ( PARTITION BY aggregation_target ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING);
17+
SELECT aggregation_target AS aggregation_target, timestamp AS timestamp, step_0 AS step_0, latest_0 AS latest_0, step_1 AS step_1, latest_1 AS latest_1, step_2 AS step_2, min(latest_2) OVER (PARTITION BY aggregation_target ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) AS latest_2, min(latest_1) OVER w AS latest_1 FROM t0 WINDOW w AS (PARTITION BY aggregation_target ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING);

0 commit comments

Comments
 (0)