Skip to content

Commit

Permalink
Adding support for comment in table/view (#104)
Browse files Browse the repository at this point in the history
Signed-off-by: Sharad Gaur <[email protected]>
Co-authored-by: hulk <[email protected]>
  • Loading branch information
sharadgaur and git-hulk authored Oct 9, 2024
1 parent edc40cc commit 004bbd6
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 18 deletions.
11 changes: 11 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,7 @@ type CreateTable struct {
Engine *EngineExpr
SubQuery *SubQuery
HasTemporary bool
Comment *StringLiteral
}

func (c *CreateTable) Pos() Pos {
Expand Down Expand Up @@ -1534,6 +1535,10 @@ func (c *CreateTable) String() string {
builder.WriteString(" AS ")
builder.WriteString(c.SubQuery.String())
}
if c.Comment != nil {
builder.WriteString(" COMMENT ")
builder.WriteString(c.Comment.String())
}
return builder.String()
}

Expand Down Expand Up @@ -1578,6 +1583,7 @@ type CreateMaterializedView struct {
Destination *DestinationClause
SubQuery *SubQuery
Populate bool
Comment *StringLiteral
}

func (c *CreateMaterializedView) Pos() Pos {
Expand Down Expand Up @@ -1621,6 +1627,11 @@ func (c *CreateMaterializedView) String() string {
builder.WriteString(" AS ")
builder.WriteString(c.SubQuery.String())
}

if c.Comment != nil {
builder.WriteString(" COMMENT ")
builder.WriteString(c.Comment.String())
}
return builder.String()
}

Expand Down
7 changes: 7 additions & 0 deletions parser/parser_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ func (p *Parser) tryParseUUID() (*UUID, error) {
return p.parseUUID()
}

func (p *Parser) tryParseComment() (*StringLiteral, error) {
if p.tryConsumeKeyword(KeywordComment) == nil {
return nil, nil
}
return p.parseString(p.Pos())
}

func (p *Parser) tryParseIfExists() (bool, error) {
if p.tryConsumeKeyword(KeywordIf) == nil {
return false, nil
Expand Down
6 changes: 6 additions & 0 deletions parser/parser_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ func (p *Parser) parseCreateTable(pos Pos) (*CreateTable, error) {
createTable.SubQuery = subQuery
createTable.StatementEnd = subQuery.End()
}

comment, err := p.tryParseComment()
if err != nil {
return nil, err
}
createTable.Comment = comment
return createTable, nil
}

Expand Down
6 changes: 6 additions & 0 deletions parser/parser_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func (p *Parser) parseCreateMaterializedView(pos Pos) (*CreateMaterializedView,
createMaterializedView.SubQuery = subQuery
createMaterializedView.StatementEnd = subQuery.End()
}

comment, err := p.tryParseComment()
if err != nil {
return nil, err
}
createMaterializedView.Comment = comment
return createMaterializedView, nil
}

Expand Down
3 changes: 2 additions & 1 deletion parser/testdata/ddl/create_materialized_view_basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ SELECT f1,
FROM
infra_bm.table_name1
WHERE
infra_bm.table_name1.event = 'test-event';
infra_bm.table_name1.event = 'test-event'
COMMENT 'Comment for table';
3 changes: 2 additions & 1 deletion parser/testdata/ddl/create_table_basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ CREATE TABLE IF NOT EXISTS test.events_local (
PRIMARY KEY (f0, f1, f2)
PARTITION BY toYYYYMMDD(f3)
TTL f3 + INTERVAL 6 MONTH
ORDER BY (f1,f2,f3)
ORDER BY (f1,f2,f3)
COMMENT 'Comment for table';
5 changes: 3 additions & 2 deletions parser/testdata/ddl/format/create_materialized_view_basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ SELECT f1,
FROM
infra_bm.table_name1
WHERE
infra_bm.table_name1.event = 'test-event';
infra_bm.table_name1.event = 'test-event'
COMMENT 'Comment for table';

-- Format SQL:
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';
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';
3 changes: 2 additions & 1 deletion parser/testdata/ddl/format/create_table_basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ PRIMARY KEY (f0, f1, f2)
PARTITION BY toYYYYMMDD(f3)
TTL f3 + INTERVAL 6 MONTH
ORDER BY (f1,f2,f3)
COMMENT 'Comment for table';

-- Format SQL:
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);
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';
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
3 changes: 2 additions & 1 deletion parser/testdata/ddl/output/bug_001.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@
"Except": null
}
},
"Populate": false
"Populate": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"OrderBy": null
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@
"Except": null
}
},
"Populate": false
"Populate": false,
"Comment": {
"LiteralPos": 548,
"LiteralEnd": 565,
"Literal": "Comment for table"
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@
"Except": null
}
},
"Populate": true
"Populate": true,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,11 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": {
"LiteralPos": 687,
"LiteralEnd": 704,
"Literal": "Comment for table"
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@
}
},
"SubQuery": null,
"HasTemporary": false
"HasTemporary": false,
"Comment": null
}
]

0 comments on commit 004bbd6

Please sign in to comment.