Skip to content

Commit

Permalink
Add the support of MODIFY TTL syntax (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk authored Jan 9, 2024
1 parent e45d112 commit 5351740
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 0 deletions.
26 changes: 26 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,32 @@ func (a *AlterTableRenameColumn) String(level int) string {
return builder.String()
}

type AlterTableModifyTTL struct {
ModifyPos Pos
StatementEnd Pos
TTL *TTLExpr
}

func (a *AlterTableModifyTTL) Pos() Pos {
return a.ModifyPos
}

func (a *AlterTableModifyTTL) End() Pos {
return a.StatementEnd
}

func (a *AlterTableModifyTTL) AlterType() string {
return "MODIFY_TTL"
}

func (a *AlterTableModifyTTL) String(level int) string {
var builder strings.Builder
builder.WriteString("MODIFY ")
builder.WriteString("TTL ")
builder.WriteString(a.TTL.String(level))
return builder.String()
}

type AlterTableModifyColumn struct {
ModifyPos Pos
StatementEnd Pos
Expand Down
11 changes: 11 additions & 0 deletions parser/parser_alter.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,17 @@ func (p *Parser) parseAlterTableModify(pos Pos) (AlterTableExpr, error) {
switch {
case p.matchKeyword(KeywordColumn):
return p.parseAlterTableModifyColumn(pos)
case p.matchKeyword(KeywordTtl):
_ = p.lexer.consumeToken()
ttlExpr, err := p.parseTTLExpr(p.Pos())
if err != nil {
return nil, err
}
return &AlterTableModifyTTL{
ModifyPos: pos,
StatementEnd: ttlExpr.End(),
TTL: ttlExpr,
}, nil
default:
return nil, fmt.Errorf("expected keyword: COLUMN, but got %q",
p.last().String)
Expand Down
1 change: 1 addition & 0 deletions parser/testdata/dml/alter_table_with_modify_remove_ttl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE infra.flow_processed_emails_local ON CLUSTER default_cluster REMOVE TTL;
1 change: 1 addition & 0 deletions parser/testdata/dml/alter_table_with_modify_ttl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE infra.flow_processed_emails_local ON CLUSTER default_cluster MODIFY TTL created_at + INTERVAL 3 YEAR;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Origin SQL:
ALTER TABLE infra.flow_processed_emails_local ON CLUSTER default_cluster REMOVE TTL;

-- Format SQL:
ALTER TABLE infra.flow_processed_emails_local
ON CLUSTER default_cluster
REMOVE TTL;
7 changes: 7 additions & 0 deletions parser/testdata/dml/format/alter_table_with_modify_ttl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Origin SQL:
ALTER TABLE infra.flow_processed_emails_local ON CLUSTER default_cluster MODIFY TTL created_at + INTERVAL 3 YEAR;

-- Format SQL:
ALTER TABLE infra.flow_processed_emails_local
ON CLUSTER default_cluster
MODIFY TTL created_at + INTERVAL 3 YEAR;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"AlterPos": 0,
"StatementEnd": 83,
"TableIdentifier": {
"Database": {
"Name": "infra",
"Unquoted": false,
"NamePos": 12,
"NameEnd": 17
},
"Table": {
"Name": "flow_processed_emails_local",
"Unquoted": false,
"NamePos": 18,
"NameEnd": 45
}
},
"OnCluster": {
"OnPos": 46,
"Expr": {
"Name": "default_cluster",
"Unquoted": false,
"NamePos": 57,
"NameEnd": 72
}
},
"AlterExprs": [
{
"RemovePos": 73,
"StatementEnd": 83
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[
{
"AlterPos": 0,
"StatementEnd": 112,
"TableIdentifier": {
"Database": {
"Name": "infra",
"Unquoted": false,
"NamePos": 12,
"NameEnd": 17
},
"Table": {
"Name": "flow_processed_emails_local",
"Unquoted": false,
"NamePos": 18,
"NameEnd": 45
}
},
"OnCluster": {
"OnPos": 46,
"Expr": {
"Name": "default_cluster",
"Unquoted": false,
"NamePos": 57,
"NameEnd": 72
}
},
"AlterExprs": [
{
"ModifyPos": 73,
"StatementEnd": 112,
"TTL": {
"TTLPos": 84,
"Expr": {
"LeftExpr": {
"Name": "created_at",
"Unquoted": false,
"NamePos": 84,
"NameEnd": 94
},
"Operation": "+",
"RightExpr": {
"IntervalPos": 97,
"Expr": {
"NumPos": 106,
"NumEnd": 107,
"Literal": "3",
"Base": 10
},
"Unit": {
"Name": "YEAR",
"Unquoted": false,
"NamePos": 108,
"NameEnd": 112
}
},
"HasGlobal": false,
"HasNot": false
}
}
}
]
}
]

0 comments on commit 5351740

Please sign in to comment.