Skip to content

Commit 033e873

Browse files
Copilotgit-hulk
andauthored
Add line break and indentation for SETTINGS clause in beautify mode (#245)
--------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: git-hulk <4987594+git-hulk@users.noreply.github.com> Co-authored-by: hulk <hulk.website@gmail.com>
1 parent 1cf1e3d commit 033e873

15 files changed

Lines changed: 69 additions & 25 deletions

parser/format.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (a *AlterTableAddColumn) FormatSQL(formatter *Formatter) {
224224
formatter.WriteExpr(a.After)
225225
}
226226
if a.Settings != nil {
227-
formatter.WriteByte(whitespace)
227+
formatter.Break()
228228
formatter.WriteExpr(a.Settings)
229229
}
230230
}
@@ -310,7 +310,7 @@ func (a *AlterTableDetachPartition) FormatSQL(formatter *Formatter) {
310310
formatter.WriteString("DETACH ")
311311
formatter.WriteExpr(a.Partition)
312312
if a.Settings != nil {
313-
formatter.WriteByte(whitespace)
313+
formatter.Break()
314314
formatter.WriteExpr(a.Settings)
315315
}
316316
}
@@ -338,7 +338,7 @@ func (a *AlterTableDropPartition) FormatSQL(formatter *Formatter) {
338338
}
339339
formatter.WriteExpr(a.Partition)
340340
if a.Settings != nil {
341-
formatter.WriteByte(whitespace)
341+
formatter.Break()
342342
formatter.WriteExpr(a.Settings)
343343
}
344344
}
@@ -858,7 +858,12 @@ func (c *CreateMaterializedView) FormatSQL(formatter *Formatter) {
858858
formatter.WriteExpr(c.Settings)
859859
}
860860
if c.HasAppend {
861-
formatter.WriteString(" APPEND")
861+
if c.Settings != nil {
862+
formatter.Break()
863+
} else {
864+
formatter.WriteByte(whitespace)
865+
}
866+
formatter.WriteString("APPEND")
862867
}
863868
if c.Engine != nil {
864869
formatter.WriteExpr(c.Engine)
@@ -2307,13 +2312,18 @@ func (s *SettingPair) FormatSQL(formatter *Formatter) {
23072312
}
23082313

23092314
func (s *SettingsClause) FormatSQL(formatter *Formatter) {
2310-
formatter.WriteString("SETTINGS ")
2315+
formatter.WriteString("SETTINGS")
2316+
formatter.Indent()
23112317
for i, item := range s.Items {
2312-
if i > 0 {
2313-
formatter.WriteString(", ")
2318+
if i == 0 {
2319+
formatter.Break()
2320+
} else {
2321+
formatter.WriteByte(',')
2322+
formatter.Break()
23142323
}
23152324
formatter.WriteExpr(item)
23162325
}
2326+
formatter.Dedent()
23172327
}
23182328

23192329
func (s *ShowStmt) FormatSQL(formatter *Formatter) {

parser/testdata/ddl/format/beautify/alter_table_add_column.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ ALTER TABLE test.events_local ON CLUSTER 'default_cluster' ADD COLUMN f1 String
33

44

55
-- Beautify SQL:
6-
ALTER TABLE test.events_local ON CLUSTER 'default_cluster' ADD COLUMN f1 String AFTER f0 SETTINGS alter_sync=2;
6+
ALTER TABLE test.events_local ON CLUSTER 'default_cluster' ADD COLUMN f1 String AFTER f0
7+
SETTINGS
8+
alter_sync=2;

parser/testdata/ddl/format/beautify/alter_table_drop_detach_partition.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
ALTER TABLE app_utc_00.app_message_as_notification_organization_sent_stats_i_d_local DROP DETACHED PARTITION '2022-05-24' SETTINGS allow_drop_detached = 1;
33

44
-- Beautify SQL:
5-
ALTER TABLE app_utc_00.app_message_as_notification_organization_sent_stats_i_d_local DROP DETACHED PARTITION '2022-05-24' SETTINGS allow_drop_detached=1;
5+
ALTER TABLE app_utc_00.app_message_as_notification_organization_sent_stats_i_d_local DROP DETACHED PARTITION '2022-05-24'
6+
SETTINGS
7+
allow_drop_detached=1;

parser/testdata/ddl/format/beautify/create_distributed_table.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ ENGINE = Distributed(
1414
CREATE TABLE test.event_all ON CLUSTER 'default_cluster'
1515
AS test.evnets_local
1616
ENGINE = Distributed(default_cluster, test, events_local, rand())
17-
SETTINGS fsync_after_insert=0;
17+
SETTINGS
18+
fsync_after_insert=0;

parser/testdata/ddl/format/beautify/create_materialized_view_with_refresh.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ AS SELECT
1616
FROM table_v5
1717

1818
-- Beautify SQL:
19-
CREATE MATERIALIZED VIEW fresh_mv REFRESH EVERY 1 HOUR OFFSET 10 MINUTE RANDOMIZE FOR 1 SECOND DEPENDS ON table_v5 SETTINGS randomize_for=1, randomize_offset=10, randomize_period=1 APPEND TO target_table_name EMPTY AS SELECT
19+
CREATE MATERIALIZED VIEW fresh_mv REFRESH EVERY 1 HOUR OFFSET 10 MINUTE RANDOMIZE FOR 1 SECOND DEPENDS ON table_v5 SETTINGS
20+
randomize_for=1,
21+
randomize_offset=10,
22+
randomize_period=1
23+
APPEND TO target_table_name EMPTY AS SELECT
2024
`field_1`,
2125
`field_2`,
2226
`field_3`,

parser/testdata/ddl/format/beautify/create_table_with_codec_delta.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ ORDER BY
3434
(toUnixTimestamp64Nano(`timestamp`), `api_id`)
3535
PARTITION BY toStartOfHour(`timestamp`)
3636
TTL toStartOfHour(`timestamp`) + INTERVAL 7 DAY, toStartOfHour(`timestamp`) + INTERVAL 2 DAY
37-
SETTINGS execute_merges_on_single_replica_time_threshold=1200, index_granularity=16384, max_bytes_to_merge_at_max_space_in_pool=64424509440, storage_policy='main', ttl_only_drop_parts=1;
37+
SETTINGS
38+
execute_merges_on_single_replica_time_threshold=1200,
39+
index_granularity=16384,
40+
max_bytes_to_merge_at_max_space_in_pool=64424509440,
41+
storage_policy='main',
42+
ttl_only_drop_parts=1;

parser/testdata/ddl/format/beautify/create_table_with_enum_fields.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ ORDER BY
2121
(method, timestamp)
2222
PARTITION BY toDate(timestamp)
2323
TTL toDate(timestamp) + toIntervalDay(3)
24-
SETTINGS index_granularity=8192;
24+
SETTINGS
25+
index_granularity=8192;

parser/testdata/ddl/format/beautify/create_table_with_index.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ ORDER BY
4646
(toUnixTimestamp64Nano(`timestamp`), `api_id`)
4747
PARTITION BY toStartOfHour(`timestamp`)
4848
TTL toStartOfHour(`timestamp`) + INTERVAL 7 DAY, toStartOfHour(`timestamp`) + INTERVAL 2 DAY
49-
SETTINGS execute_merges_on_single_replica_time_threshold=1200, index_granularity=16384, max_bytes_to_merge_at_max_space_in_pool=64424509440, storage_policy='main', ttl_only_drop_parts=1;
49+
SETTINGS
50+
execute_merges_on_single_replica_time_threshold=1200,
51+
index_granularity=16384,
52+
max_bytes_to_merge_at_max_space_in_pool=64424509440,
53+
storage_policy='main',
54+
ttl_only_drop_parts=1;

parser/testdata/ddl/format/beautify/create_table_with_keyword_partition_by.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ ENGINE = ReplacingMergeTree
2121
ORDER BY
2222
(f1, f2)
2323
PARTITION BY date
24-
SETTINGS index_granularity=8192;
24+
SETTINGS
25+
index_granularity=8192;

parser/testdata/ddl/format/beautify/create_table_with_nullable.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ ENGINE = MergeTree
2929
ORDER BY
3030
label_id
3131
PARTITION BY xxHash32(tag_id) % 20
32-
SETTINGS index_granularity=8192;
32+
SETTINGS
33+
index_granularity=8192;

0 commit comments

Comments
 (0)