Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ func (a *AlterTableModifyQuery) Accept(visitor ASTVisitor) error {
type AlterTableModifyTTL struct {
ModifyPos Pos
StatementEnd Pos
TTL *TTLExpr
TTL *TTLClause
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AlterTableModifyTTL.TTL changed from *TTLExpr to *TTLClause, which is a breaking change for any external consumers of this package’s AST. If this repo aims to keep API stability, consider preserving the old field (e.g., add a new TTLClause field while keeping TTLExpr for the single-item case), or provide a helper accessor/migration path and document the breaking change (potentially requiring a major version bump).

Suggested change
TTL *TTLClause
TTL *TTLClause
// TTLExpr is kept for backwards compatibility with older versions where
// TTL was of type *TTLExpr. New code should use TTL (*TTLClause).
TTLExpr *TTLExpr

Copilot uses AI. Check for mistakes.
}

func (a *AlterTableModifyTTL) Pos() Pos {
Expand All @@ -1272,7 +1272,6 @@ func (a *AlterTableModifyTTL) AlterType() string {
func (a *AlterTableModifyTTL) String() string {
var builder strings.Builder
builder.WriteString("MODIFY ")
builder.WriteString("TTL ")
builder.WriteString(a.TTL.String())
return builder.String()
}
Expand Down
16 changes: 13 additions & 3 deletions parser/parser_alter.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,15 +645,25 @@ func (p *Parser) parseAlterTableModify(pos Pos) (AlterTableClause, error) {
case p.matchKeyword(KeywordColumn):
return p.parseAlterTableModifyColumn(pos)
case p.matchKeyword(KeywordTtl):
ttlPos := p.Pos()
_ = p.lexer.consumeToken()
ttlExpr, err := p.parseTTLExpr(p.Pos())
items, err := p.parseTTLClause(ttlPos, true)
if err != nil {
Comment on lines 647 to 651

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Stop MODIFY TTL parsing before next alter clause

With allowMultiValues set to true, parseTTLClause greedily consumes any comma as another TTL expression separator. In ALTER TABLE, commas also separate alter clauses, so a statement like ALTER TABLE t MODIFY TTL x, ADD COLUMN y Int now fails because parseTTLClause will attempt to parse ADD as part of a TTL expression and error. This makes valid multi-clause ALTER statements containing MODIFY TTL unparsable; consider stopping TTL parsing when the next token starts another alter clause (ADD/DROP/...) or only enabling multi-values when MODIFY TTL is the final clause.

Useful? React with 👍 / 👎.

return nil, err
}
listEnd := ttlPos
if len(items) > 0 {
listEnd = items[len(items)-1].End()
}
Comment on lines +654 to +657
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseTTLClause always appends the first parsed TTL expression before returning, so len(items) can’t be 0 on the success path. The fallback listEnd := ttlPos and the if len(items) > 0 branch are therefore redundant and can be simplified.

Suggested change
listEnd := ttlPos
if len(items) > 0 {
listEnd = items[len(items)-1].End()
}
listEnd := items[len(items)-1].End()

Copilot uses AI. Check for mistakes.
ttlClause := &TTLClause{
TTLPos: ttlPos,
ListEnd: listEnd,
Items: items,
}
return &AlterTableModifyTTL{
ModifyPos: pos,
StatementEnd: ttlExpr.End(),
TTL: ttlExpr,
StatementEnd: ttlClause.End(),
TTL: ttlClause,
Comment on lines +654 to +666
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listEnd is derived from items[len(items)-1].End(), but TTLExpr.End() currently ignores any attached TTL policy (e.g., ... TO DISK 'x', ... DELETE, etc.). If the last TTL item has a policy, AlterTableModifyTTL.StatementEnd/TTLClause.ListEnd will point before the policy tokens, making node ranges inconsistent. Consider updating TTLExpr.End() (in parser/ast.go) to return Policy.End() when Policy != nil, and then use that for ListEnd here.

Copilot uses AI. Check for mistakes.
}, nil
case p.matchKeyword(KeywordQuery):
_ = p.lexer.consumeToken()
Expand Down
4 changes: 4 additions & 0 deletions parser/testdata/dml/alter_table_modify_ttl_multiple.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE db.t0 ON CLUSTER default_cluster
MODIFY TTL
toDateTime(timestamp / 1000000000) + INTERVAL 30 DAY TO DISK 'gcs',
toDateTime(timestamp / 1000000000) + INTERVAL 60 DAY;
Comment on lines +2 to +4
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new test covers multiple TTL expressions, but it doesn’t cover the case where the last TTL expression has a TTL policy (e.g., ... + INTERVAL 60 DAY TO DISK 'gcs'). Adding such a case would help ensure the multi-item parsing/AST boundaries behave correctly for the common TO DISK/TO VOLUME variants.

Copilot uses AI. Check for mistakes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Origin SQL:
ALTER TABLE db.t0 ON CLUSTER default_cluster
MODIFY TTL
toDateTime(timestamp / 1000000000) + INTERVAL 30 DAY TO DISK 'gcs',
toDateTime(timestamp / 1000000000) + INTERVAL 60 DAY;


-- Format SQL:
ALTER TABLE db.t0 ON CLUSTER default_cluster MODIFY TTL toDateTime(timestamp / 1000000000) + INTERVAL 30 DAY TO DISK 'gcs', toDateTime(timestamp / 1000000000) + INTERVAL 60 DAY;
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
[
{
"AlterPos": 0,
"StatementEnd": 184,
"TableIdentifier": {
"Database": {
"Name": "db",
"QuoteType": 1,
"NamePos": 12,
"NameEnd": 14
},
"Table": {
"Name": "t0",
"QuoteType": 1,
"NamePos": 15,
"NameEnd": 17
}
},
"OnCluster": {
"OnPos": 18,
"Expr": {
"Name": "default_cluster",
"QuoteType": 1,
"NamePos": 29,
"NameEnd": 44
}
},
"AlterExprs": [
{
"ModifyPos": 45,
"StatementEnd": 184,
"TTL": {
"TTLPos": 52,
"ListEnd": 184,
"Items": [
{
"TTLPos": 52,
"Expr": {
"LeftExpr": {
"Name": {
"Name": "toDateTime",
"QuoteType": 1,
"NamePos": 60,
"NameEnd": 70
},
"Params": {
"LeftParenPos": 70,
"RightParenPos": 93,
"Items": {
"ListPos": 71,
"ListEnd": 93,
"HasDistinct": false,
"Items": [
{
"Expr": {
"LeftExpr": {
"Name": "timestamp",
"QuoteType": 1,
"NamePos": 71,
"NameEnd": 80
},
"Operation": "/",
"RightExpr": {
"NumPos": 83,
"NumEnd": 93,
"Literal": "1000000000",
"Base": 10
},
"HasGlobal": false,
"HasNot": false
},
"Alias": null
}
]
},
"ColumnArgList": null
}
},
"Operation": "+",
"RightExpr": {
"IntervalPos": 97,
"Expr": {
"NumPos": 106,
"NumEnd": 108,
"Literal": "30",
"Base": 10
},
"Unit": {
"Name": "DAY",
"QuoteType": 1,
"NamePos": 109,
"NameEnd": 112
}
},
"HasGlobal": false,
"HasNot": false
},
"Policy": {
"Item": {
"RulePos": 113,
"ToVolume": null,
"ToDisk": {
"LiteralPos": 122,
"LiteralEnd": 125,
"Literal": "gcs"
},
"Action": null
},
"Where": null,
"GroupBy": null
}
},
{
"TTLPos": 52,
"Expr": {
"LeftExpr": {
"Name": {
"Name": "toDateTime",
"QuoteType": 1,
"NamePos": 132,
"NameEnd": 142
},
"Params": {
"LeftParenPos": 142,
"RightParenPos": 165,
"Items": {
"ListPos": 143,
"ListEnd": 165,
"HasDistinct": false,
"Items": [
{
"Expr": {
"LeftExpr": {
"Name": "timestamp",
"QuoteType": 1,
"NamePos": 143,
"NameEnd": 152
},
"Operation": "/",
"RightExpr": {
"NumPos": 155,
"NumEnd": 165,
"Literal": "1000000000",
"Base": 10
},
"HasGlobal": false,
"HasNot": false
},
"Alias": null
}
]
},
"ColumnArgList": null
}
},
"Operation": "+",
"RightExpr": {
"IntervalPos": 169,
"Expr": {
"NumPos": 178,
"NumEnd": 180,
"Literal": "60",
"Base": 10
},
"Unit": {
"Name": "DAY",
"QuoteType": 1,
"NamePos": 181,
"NameEnd": 184
}
},
"HasGlobal": false,
"HasNot": false
},
"Policy": null
}
]
}
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,40 @@
"ModifyPos": 73,
"StatementEnd": 112,
"TTL": {
"TTLPos": 84,
"Expr": {
"LeftExpr": {
"Name": "created_at",
"QuoteType": 1,
"NamePos": 84,
"NameEnd": 94
},
"Operation": "+",
"RightExpr": {
"IntervalPos": 97,
"TTLPos": 80,
"ListEnd": 112,
"Items": [
{
"TTLPos": 80,
"Expr": {
"NumPos": 106,
"NumEnd": 107,
"Literal": "3",
"Base": 10
"LeftExpr": {
"Name": "created_at",
"QuoteType": 1,
"NamePos": 84,
"NameEnd": 94
},
"Operation": "+",
"RightExpr": {
"IntervalPos": 97,
"Expr": {
"NumPos": 106,
"NumEnd": 107,
"Literal": "3",
"Base": 10
},
"Unit": {
"Name": "YEAR",
"QuoteType": 1,
"NamePos": 108,
"NameEnd": 112
}
},
"HasGlobal": false,
"HasNot": false
},
"Unit": {
"Name": "YEAR",
"QuoteType": 1,
"NamePos": 108,
"NameEnd": 112
}
},
"HasGlobal": false,
"HasNot": false
},
"Policy": null
"Policy": null
}
]
}
}
]
Expand Down
Loading