Skip to content

Commit bf5be12

Browse files
Copilotgit-hulk
andauthored
Add beautify format support for CREATE DICTIONARY statements (#253)
--------- 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>
1 parent 17e829d commit bf5be12

4 files changed

Lines changed: 63 additions & 25 deletions

File tree

parser/format.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -771,12 +771,13 @@ func (c *CreateDictionary) FormatSQL(formatter *Formatter) {
771771
}
772772

773773
if c.Engine != nil {
774-
formatter.WriteByte(whitespace)
774+
formatter.Break()
775775
formatter.WriteExpr(c.Engine)
776776
}
777777

778778
if c.Comment != nil {
779-
formatter.WriteString(" COMMENT ")
779+
formatter.Break()
780+
formatter.WriteString("COMMENT ")
780781
formatter.WriteExpr(c.Comment)
781782
}
782783

@@ -1182,42 +1183,36 @@ func (d *DictionaryAttribute) FormatSQL(formatter *Formatter) {
11821183
}
11831184

11841185
func (d *DictionaryEngineClause) FormatSQL(formatter *Formatter) {
1185-
paddingSpace := false
11861186
if d.PrimaryKey != nil {
11871187
formatter.WriteExpr(d.PrimaryKey)
1188-
paddingSpace = true
11891188
}
11901189
if d.Source != nil {
1191-
if paddingSpace {
1192-
formatter.WriteByte(whitespace)
1190+
if d.PrimaryKey != nil {
1191+
formatter.Break()
11931192
}
11941193
formatter.WriteExpr(d.Source)
1195-
paddingSpace = true
11961194
}
11971195
if d.Lifetime != nil {
1198-
if paddingSpace {
1199-
formatter.WriteByte(whitespace)
1196+
if d.PrimaryKey != nil || d.Source != nil {
1197+
formatter.Break()
12001198
}
12011199
formatter.WriteExpr(d.Lifetime)
1202-
paddingSpace = true
12031200
}
12041201
if d.Layout != nil {
1205-
if paddingSpace {
1206-
formatter.WriteByte(whitespace)
1202+
if d.PrimaryKey != nil || d.Source != nil || d.Lifetime != nil {
1203+
formatter.Break()
12071204
}
12081205
formatter.WriteExpr(d.Layout)
1209-
paddingSpace = true
12101206
}
12111207
if d.Range != nil {
1212-
if paddingSpace {
1213-
formatter.WriteByte(whitespace)
1208+
if d.PrimaryKey != nil || d.Source != nil || d.Lifetime != nil || d.Layout != nil {
1209+
formatter.Break()
12141210
}
12151211
formatter.WriteExpr(d.Range)
1216-
paddingSpace = true
12171212
}
12181213
if d.Settings != nil {
1219-
if paddingSpace {
1220-
formatter.WriteByte(whitespace)
1214+
if d.PrimaryKey != nil || d.Source != nil || d.Lifetime != nil || d.Layout != nil || d.Range != nil {
1215+
formatter.Break()
12211216
}
12221217
formatter.WriteString("SETTINGS(")
12231218
for i, item := range d.Settings.Items {
@@ -1275,14 +1270,20 @@ func (d *DictionaryRangeClause) FormatSQL(formatter *Formatter) {
12751270
}
12761271

12771272
func (d *DictionarySchemaClause) FormatSQL(formatter *Formatter) {
1278-
formatter.WriteString("(")
1273+
formatter.WriteByte('(')
1274+
formatter.Indent()
12791275
for i, attr := range d.Attributes {
1280-
if i > 0 {
1281-
formatter.WriteString(", ")
1276+
if i == 0 {
1277+
formatter.NewLine()
1278+
} else {
1279+
formatter.WriteByte(',')
1280+
formatter.Break()
12821281
}
12831282
formatter.WriteExpr(attr)
12841283
}
1285-
formatter.WriteString(")")
1284+
formatter.Dedent()
1285+
formatter.NewLine()
1286+
formatter.WriteByte(')')
12861287
}
12871288

12881289
func (d *DictionarySourceClause) FormatSQL(formatter *Formatter) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ LAYOUT(HASHED())
2121
SETTINGS(max_block_size = 8192);
2222

2323
-- Beautify SQL:
24-
CREATE DICTIONARY test.my_dict (id UInt64, name String DEFAULT '', value Float64 EXPRESSION toFloat64OrZero(name), parent_id UInt64 HIERARCHICAL, is_active UInt8 INJECTIVE, object_id UInt64 IS_OBJECT_ID) PRIMARY KEY id SOURCE(MYSQL(host 'localhost' port 3306 user 'default' password '' db 'test' table 'dict_table')) LIFETIME(MIN 1000 MAX 2000) LAYOUT(HASHED()) SETTINGS(max_block_size=8192);
24+
CREATE DICTIONARY test.my_dict (
25+
id UInt64,
26+
name String DEFAULT '',
27+
value Float64 EXPRESSION toFloat64OrZero(name),
28+
parent_id UInt64 HIERARCHICAL,
29+
is_active UInt8 INJECTIVE,
30+
object_id UInt64 IS_OBJECT_ID
31+
)
32+
PRIMARY KEY id
33+
SOURCE(MYSQL(host 'localhost' port 3306 user 'default' password '' db 'test' table 'dict_table'))
34+
LIFETIME(MIN 1000 MAX 2000)
35+
LAYOUT(HASHED())
36+
SETTINGS(max_block_size=8192);

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ LAYOUT(HASHED())
2424
SETTINGS(max_block_size = 8192, max_insert_block_size = 1048576);
2525

2626
-- Beautify SQL:
27-
CREATE OR REPLACE DICTIONARY test.comprehensive_dict UUID '12345678-1234-1234-1234-123456789012' ON CLUSTER production_cluster (id UInt64, name String DEFAULT '', value Float64 EXPRESSION toFloat64OrZero(name), parent_id UInt64 HIERARCHICAL, is_active UInt8 INJECTIVE, object_id UInt64 IS_OBJECT_ID) PRIMARY KEY id SOURCE(MYSQL(host 'localhost' port 3306 user 'root' password 'secret' db 'test_db' table 'dictionary_table')) LIFETIME(MIN 1000 MAX 2000) LAYOUT(HASHED()) SETTINGS(max_block_size=8192, max_insert_block_size=1048576);
27+
CREATE OR REPLACE DICTIONARY test.comprehensive_dict UUID '12345678-1234-1234-1234-123456789012' ON CLUSTER production_cluster (
28+
id UInt64,
29+
name String DEFAULT '',
30+
value Float64 EXPRESSION toFloat64OrZero(name),
31+
parent_id UInt64 HIERARCHICAL,
32+
is_active UInt8 INJECTIVE,
33+
object_id UInt64 IS_OBJECT_ID
34+
)
35+
PRIMARY KEY id
36+
SOURCE(MYSQL(host 'localhost' port 3306 user 'root' password 'secret' db 'test_db' table 'dictionary_table'))
37+
LIFETIME(MIN 1000 MAX 2000)
38+
LAYOUT(HASHED())
39+
SETTINGS(max_block_size=8192, max_insert_block_size=1048576);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,17 @@ COMMENT 'This is a test dictionary with comment';
2323

2424

2525
-- Beautify SQL:
26-
CREATE DICTIONARY test.my_dict (id UInt64, name String DEFAULT '', value Float64 EXPRESSION toFloat64OrZero(name), parent_id UInt64 HIERARCHICAL, is_active UInt8 INJECTIVE, object_id UInt64 IS_OBJECT_ID) PRIMARY KEY id SOURCE(MYSQL(host 'localhost' port 3306 user 'default' password '' db 'test' table 'dict_table')) LIFETIME(MIN 1000 MAX 2000) LAYOUT(HASHED()) SETTINGS(max_block_size=8192) COMMENT 'This is a test dictionary with comment';
26+
CREATE DICTIONARY test.my_dict (
27+
id UInt64,
28+
name String DEFAULT '',
29+
value Float64 EXPRESSION toFloat64OrZero(name),
30+
parent_id UInt64 HIERARCHICAL,
31+
is_active UInt8 INJECTIVE,
32+
object_id UInt64 IS_OBJECT_ID
33+
)
34+
PRIMARY KEY id
35+
SOURCE(MYSQL(host 'localhost' port 3306 user 'default' password '' db 'test' table 'dict_table'))
36+
LIFETIME(MIN 1000 MAX 2000)
37+
LAYOUT(HASHED())
38+
SETTINGS(max_block_size=8192)
39+
COMMENT 'This is a test dictionary with comment';

0 commit comments

Comments
 (0)