Skip to content

Commit ff27c77

Browse files
sharadgaurgit-hulk
andcommitted
Adding support for comment in table/view (AfterShip#104)
Signed-off-by: Sharad Gaur <[email protected]> Co-authored-by: hulk <[email protected]>
1 parent 82ef53e commit ff27c77

21 files changed

+73
-18
lines changed

Diff for: parser/ast.go

+11
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,7 @@ type CreateTable struct {
14901490
Engine *EngineExpr
14911491
SubQuery *SubQuery
14921492
HasTemporary bool
1493+
Comment *StringLiteral
14931494
}
14941495

14951496
func (c *CreateTable) Pos() Pos {
@@ -1534,6 +1535,10 @@ func (c *CreateTable) String() string {
15341535
builder.WriteString(" AS ")
15351536
builder.WriteString(c.SubQuery.String())
15361537
}
1538+
if c.Comment != nil {
1539+
builder.WriteString(" COMMENT ")
1540+
builder.WriteString(c.Comment.String())
1541+
}
15371542
return builder.String()
15381543
}
15391544

@@ -1578,6 +1583,7 @@ type CreateMaterializedView struct {
15781583
Destination *DestinationClause
15791584
SubQuery *SubQuery
15801585
Populate bool
1586+
Comment *StringLiteral
15811587
}
15821588

15831589
func (c *CreateMaterializedView) Pos() Pos {
@@ -1621,6 +1627,11 @@ func (c *CreateMaterializedView) String() string {
16211627
builder.WriteString(" AS ")
16221628
builder.WriteString(c.SubQuery.String())
16231629
}
1630+
1631+
if c.Comment != nil {
1632+
builder.WriteString(" COMMENT ")
1633+
builder.WriteString(c.Comment.String())
1634+
}
16241635
return builder.String()
16251636
}
16261637

Diff for: parser/parser_common.go

+7
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ func (p *Parser) tryParseUUID() (*UUID, error) {
137137
return p.parseUUID()
138138
}
139139

140+
func (p *Parser) tryParseComment() (*StringLiteral, error) {
141+
if p.tryConsumeKeyword(KeywordComment) == nil {
142+
return nil, nil
143+
}
144+
return p.parseString(p.Pos())
145+
}
146+
140147
func (p *Parser) tryParseIfExists() (bool, error) {
141148
if p.tryConsumeKeyword(KeywordIf) == nil {
142149
return false, nil

Diff for: parser/parser_table.go

+6
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ func (p *Parser) parseCreateTable(pos Pos) (*CreateTable, error) {
166166
createTable.SubQuery = subQuery
167167
createTable.StatementEnd = subQuery.End()
168168
}
169+
170+
comment, err := p.tryParseComment()
171+
if err != nil {
172+
return nil, err
173+
}
174+
createTable.Comment = comment
169175
return createTable, nil
170176
}
171177

Diff for: parser/parser_view.go

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func (p *Parser) parseCreateMaterializedView(pos Pos) (*CreateMaterializedView,
6969
createMaterializedView.SubQuery = subQuery
7070
createMaterializedView.StatementEnd = subQuery.End()
7171
}
72+
73+
comment, err := p.tryParseComment()
74+
if err != nil {
75+
return nil, err
76+
}
77+
createMaterializedView.Comment = comment
7278
return createMaterializedView, nil
7379
}
7480

Diff for: parser/testdata/ddl/create_materialized_view_basic.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ SELECT f1,
1818
FROM
1919
infra_bm.table_name1
2020
WHERE
21-
infra_bm.table_name1.event = 'test-event';
21+
infra_bm.table_name1.event = 'test-event'
22+
COMMENT 'Comment for table';

Diff for: parser/testdata/ddl/create_table_basic.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ CREATE TABLE IF NOT EXISTS test.events_local (
2626
PRIMARY KEY (f0, f1, f2)
2727
PARTITION BY toYYYYMMDD(f3)
2828
TTL f3 + INTERVAL 6 MONTH
29-
ORDER BY (f1,f2,f3)
29+
ORDER BY (f1,f2,f3)
30+
COMMENT 'Comment for table';

Diff for: parser/testdata/ddl/format/create_materialized_view_basic.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ SELECT f1,
1919
FROM
2020
infra_bm.table_name1
2121
WHERE
22-
infra_bm.table_name1.event = 'test-event';
22+
infra_bm.table_name1.event = 'test-event'
23+
COMMENT 'Comment for table';
2324

2425
-- Format SQL:
25-
CREATE MATERIALIZED VIEW infra_bm.view_name ON CLUSTER 'default_cluster' TO infra_bm.table_name (`f1` DateTime64(3), `f2` String, `f3` String, `f4` String, `f5` String, `f6` Int64) AS SELECT f1, f2, visitParamExtractString(properties, 'f3') AS f3, visitParamExtractString(properties, 'f4') AS f4, visitParamExtractString(properties, 'f5') AS f5, visitParamExtractInt(properties, 'f6') AS f6 FROM infra_bm.table_name1 WHERE infra_bm.table_name1.event = 'test-event';
26+
CREATE MATERIALIZED VIEW infra_bm.view_name ON CLUSTER 'default_cluster' TO infra_bm.table_name (`f1` DateTime64(3), `f2` String, `f3` String, `f4` String, `f5` String, `f6` Int64) AS SELECT f1, f2, visitParamExtractString(properties, 'f3') AS f3, visitParamExtractString(properties, 'f4') AS f4, visitParamExtractString(properties, 'f5') AS f5, visitParamExtractInt(properties, 'f6') AS f6 FROM infra_bm.table_name1 WHERE infra_bm.table_name1.event = 'test-event' COMMENT 'Comment for table';

Diff for: parser/testdata/ddl/format/create_table_basic.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ PRIMARY KEY (f0, f1, f2)
2828
PARTITION BY toYYYYMMDD(f3)
2929
TTL f3 + INTERVAL 6 MONTH
3030
ORDER BY (f1,f2,f3)
31+
COMMENT 'Comment for table';
3132

3233
-- Format SQL:
33-
CREATE TABLE IF NOT EXISTS test.events_local (f0 String, f1 String CODEC(ZSTD(1)), f2 VARCHAR(255), f3 Datetime, f4 Datetime, f5 Map(String, String), f6 String, f7 Nested(f70 UInt32, f71 UInt32, f72 DateTime, f73 Int64, f74 Int64, f75 String), f8 Datetime DEFAULT now(), f9 String MATERIALIZED toString(f7['f70']), f10 String ALIAS f11) ENGINE = MergeTree PRIMARY KEY (f0, f1, f2) PARTITION BY toYYYYMMDD(f3) TTL f3 + INTERVAL 6 MONTH ORDER BY (f1, f2, f3);
34+
CREATE TABLE IF NOT EXISTS test.events_local (f0 String, f1 String CODEC(ZSTD(1)), f2 VARCHAR(255), f3 Datetime, f4 Datetime, f5 Map(String, String), f6 String, f7 Nested(f70 UInt32, f71 UInt32, f72 DateTime, f73 Int64, f74 Int64, f75 String), f8 Datetime DEFAULT now(), f9 String MATERIALIZED toString(f7['f70']), f10 String ALIAS f11) ENGINE = MergeTree PRIMARY KEY (f0, f1, f2) PARTITION BY toYYYYMMDD(f3) TTL f3 + INTERVAL 6 MONTH ORDER BY (f1, f2, f3) COMMENT 'Comment for table';

Diff for: parser/testdata/ddl/output/attach_table_basic.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@
476476
}
477477
},
478478
"SubQuery": null,
479-
"HasTemporary": false
479+
"HasTemporary": false,
480+
"Comment": null
480481
}
481482
]

Diff for: parser/testdata/ddl/output/bug_001.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@
564564
"Except": null
565565
}
566566
},
567-
"Populate": false
567+
"Populate": false,
568+
"Comment": null
568569
}
569570
]

Diff for: parser/testdata/ddl/output/create_distributed_table.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
"OrderBy": null
140140
},
141141
"SubQuery": null,
142-
"HasTemporary": false
142+
"HasTemporary": false,
143+
"Comment": null
143144
}
144145
]

Diff for: parser/testdata/ddl/output/create_materialized_view_basic.sql.golden.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,11 @@
531531
"Except": null
532532
}
533533
},
534-
"Populate": false
534+
"Populate": false,
535+
"Comment": {
536+
"LiteralPos": 548,
537+
"LiteralEnd": 565,
538+
"Literal": "Comment for table"
539+
}
535540
}
536541
]

Diff for: parser/testdata/ddl/output/create_materialized_view_with_empty_table_schema.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@
541541
"Except": null
542542
}
543543
},
544-
"Populate": true
544+
"Populate": true,
545+
"Comment": null
545546
}
546547
]

Diff for: parser/testdata/ddl/output/create_table_basic.sql.golden.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,11 @@
838838
}
839839
},
840840
"SubQuery": null,
841-
"HasTemporary": false
841+
"HasTemporary": false,
842+
"Comment": {
843+
"LiteralPos": 687,
844+
"LiteralEnd": 704,
845+
"Literal": "Comment for table"
846+
}
842847
}
843848
]

Diff for: parser/testdata/ddl/output/create_table_with_codec_delta.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@
763763
}
764764
},
765765
"SubQuery": null,
766-
"HasTemporary": false
766+
"HasTemporary": false,
767+
"Comment": null
767768
}
768769
]

Diff for: parser/testdata/ddl/output/create_table_with_index.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@
817817
}
818818
},
819819
"SubQuery": null,
820-
"HasTemporary": false
820+
"HasTemporary": false,
821+
"Comment": null
821822
}
822823
]

Diff for: parser/testdata/ddl/output/create_table_with_keyword_partition_by.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
}
245245
},
246246
"SubQuery": null,
247-
"HasTemporary": false
247+
"HasTemporary": false,
248+
"Comment": null
248249
}
249250
]

Diff for: parser/testdata/ddl/output/create_table_with_nullable.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@
422422
}
423423
},
424424
"SubQuery": null,
425-
"HasTemporary": false
425+
"HasTemporary": false,
426+
"Comment": null
426427
}
427428
]

Diff for: parser/testdata/ddl/output/create_table_with_on_clsuter.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@
476476
}
477477
},
478478
"SubQuery": null,
479-
"HasTemporary": false
479+
"HasTemporary": false,
480+
"Comment": null
480481
}
481482
]

Diff for: parser/testdata/ddl/output/create_table_with_sample_by.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@
304304
}
305305
},
306306
"SubQuery": null,
307-
"HasTemporary": false
307+
"HasTemporary": false,
308+
"Comment": null
308309
}
309310
]

Diff for: parser/testdata/ddl/output/create_table_with_uuid.sql.golden.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@
482482
}
483483
},
484484
"SubQuery": null,
485-
"HasTemporary": false
485+
"HasTemporary": false,
486+
"Comment": null
486487
}
487488
]

0 commit comments

Comments
 (0)