Skip to content

Commit 089230e

Browse files
committed
Improve the ALTER TABLE format
1 parent ece3a11 commit 089230e

10 files changed

Lines changed: 67 additions & 28 deletions

parser/format.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ func (a *AlterTableClearProjection) FormatSQL(formatter *Formatter) {
312312
}
313313

314314
func (a *AlterTableDelete) FormatSQL(formatter *Formatter) {
315-
formatter.WriteString("DELETE WHERE ")
315+
formatter.WriteString("DELETE")
316+
formatter.Break()
317+
formatter.WriteString("WHERE ")
316318
formatter.WriteExpr(a.WhereClause)
317319
}
318320

@@ -409,18 +411,26 @@ func (a *AlterTableModifyColumn) FormatSQL(formatter *Formatter) {
409411
}
410412

411413
func (a *AlterTableModifyQuery) FormatSQL(formatter *Formatter) {
412-
formatter.WriteString("MODIFY QUERY ")
414+
formatter.WriteString("MODIFY QUERY")
415+
formatter.Indent()
416+
formatter.Break()
413417
formatter.WriteExpr(a.SelectExpr)
418+
formatter.Dedent()
414419
}
415420

416421
func (a *AlterTableModifySetting) FormatSQL(formatter *Formatter) {
417-
formatter.WriteString("MODIFY SETTING ")
422+
formatter.WriteString("MODIFY SETTING")
423+
formatter.Indent()
418424
for i, setting := range a.Settings {
419-
if i > 0 {
420-
formatter.WriteString(", ")
425+
if i == 0 {
426+
formatter.Break()
427+
} else {
428+
formatter.WriteByte(',')
429+
formatter.Break()
421430
}
422431
formatter.WriteExpr(setting)
423432
}
433+
formatter.Dedent()
424434
}
425435

426436
func (a *AlterTableModifyTTL) FormatSQL(formatter *Formatter) {
@@ -450,28 +460,40 @@ func (a *AlterTableReplacePartition) FormatSQL(formatter *Formatter) {
450460
}
451461

452462
func (a *AlterTableResetSetting) FormatSQL(formatter *Formatter) {
453-
formatter.WriteString("RESET SETTING ")
463+
formatter.WriteString("RESET SETTING")
464+
formatter.Indent()
454465
for i, setting := range a.Settings {
455-
if i > 0 {
456-
formatter.WriteString(", ")
466+
if i == 0 {
467+
formatter.Break()
468+
} else {
469+
formatter.WriteByte(',')
470+
formatter.Break()
457471
}
458472
formatter.WriteExpr(setting)
459473
}
474+
formatter.Dedent()
460475
}
461476

462477
func (a *AlterTableUpdate) FormatSQL(formatter *Formatter) {
463-
formatter.WriteString("UPDATE ")
478+
formatter.WriteString("UPDATE")
479+
formatter.Indent()
464480
for i, assignment := range a.Assignments {
465-
if i > 0 {
466-
formatter.WriteString(", ")
481+
if i == 0 {
482+
formatter.Break()
483+
} else {
484+
formatter.WriteByte(',')
485+
formatter.Break()
467486
}
468487
formatter.WriteExpr(assignment)
469488
}
489+
formatter.Dedent()
470490
if a.InPartition != nil {
471-
formatter.WriteString(" IN ")
491+
formatter.Break()
492+
formatter.WriteString("IN ")
472493
formatter.WriteExpr(a.InPartition)
473494
}
474-
formatter.WriteString(" WHERE ")
495+
formatter.Break()
496+
formatter.WriteString("WHERE ")
475497
formatter.WriteExpr(a.WhereClause)
476498
}
477499

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ ALTER TABLE test.events DELETE WHERE created_at < '2023-01-01';
44

55
-- Beautify SQL:
66
ALTER TABLE test.events
7-
DELETE WHERE created_at < '2023-01-01';
7+
DELETE
8+
WHERE created_at < '2023-01-01';

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ALTER TABLE test.events ON CLUSTER 'default_cluster' DELETE WHERE id = 123 AND s
55
-- Beautify SQL:
66
ALTER TABLE test.events
77
ON CLUSTER 'default_cluster'
8-
DELETE WHERE id = 123
8+
DELETE
9+
WHERE id = 123
910
AND
1011
status = 'deleted';

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ ALTER TABLE example_table MODIFY SETTING max_part_loading_threads=8, max_parts_i
33

44
-- Beautify SQL:
55
ALTER TABLE example_table
6-
MODIFY SETTING max_part_loading_threads=8, max_parts_in_total=50000;
6+
MODIFY SETTING
7+
max_part_loading_threads=8,
8+
max_parts_in_total=50000;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ ALTER TABLE example_table RESET SETTING max_part_loading_threads, max_parts_in_t
33

44
-- Beautify SQL:
55
ALTER TABLE example_table
6-
RESET SETTING max_part_loading_threads, max_parts_in_total, another_setting;
6+
RESET SETTING
7+
max_part_loading_threads,
8+
max_parts_in_total,
9+
another_setting;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ ALTER TABLE example_table RESET SETTING max_part_loading_threads;
33

44
-- Beautify SQL:
55
ALTER TABLE example_table
6-
RESET SETTING max_part_loading_threads;
6+
RESET SETTING
7+
max_part_loading_threads;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ ALTER TABLE test.users UPDATE status = 'active', updated_at = now() WHERE status
44

55
-- Beautify SQL:
66
ALTER TABLE test.users
7-
UPDATE status = 'active', updated_at = now() WHERE status = 'pending';
7+
UPDATE
8+
status = 'active',
9+
updated_at = now()
10+
WHERE status = 'pending';

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ ALTER TABLE test.users UPDATE status = 'inactive' IN PARTITION '2024-01-01' WHER
44

55
-- Beautify SQL:
66
ALTER TABLE test.users
7-
UPDATE status = 'inactive' IN PARTITION '2024-01-01' WHERE status = 'active';
7+
UPDATE
8+
status = 'inactive'
9+
IN PARTITION '2024-01-01'
10+
WHERE status = 'active';

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ ALTER TABLE db.table ON CLUSTER cluster1 UPDATE column1 = column1 + 1 WHERE id >
55
-- Beautify SQL:
66
ALTER TABLE db.table
77
ON CLUSTER cluster1
8-
UPDATE column1 = column1 + 1 WHERE id > 100;
8+
UPDATE
9+
column1 = column1 + 1
10+
WHERE id > 100;

parser/testdata/dml/format/beautify/alter_table_modify_query.sql

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ ALTER TABLE test.some_mv ON CLUSTER cluster MODIFY QUERY SELECT field1, field2 F
44
-- Beautify SQL:
55
ALTER TABLE test.some_mv
66
ON CLUSTER cluster
7-
MODIFY QUERY SELECT
8-
field1,
9-
field2
10-
FROM
11-
test.some_table
12-
WHERE
13-
count >= 3;
7+
MODIFY QUERY
8+
SELECT
9+
field1,
10+
field2
11+
FROM
12+
test.some_table
13+
WHERE
14+
count >= 3;

0 commit comments

Comments
 (0)