Skip to content

Commit 8bb4771

Browse files
committed
Support TTL DELETE WHERE clause in ALTER TABLE MODIFY TTL
- Update ALTER MODIFY TTL parsing to use parseTTLElement - Capture WHERE condition in TTLElement for conditional deletion - Update explain code to output TTLElement with WHERE as child
1 parent 700f2d9 commit 8bb4771

3 files changed

Lines changed: 34 additions & 19 deletions

File tree

internal/explain/statements.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,9 +1773,23 @@ func explainAlterCommand(sb *strings.Builder, cmd *ast.AlterCommand, indent stri
17731773
fmt.Fprintf(sb, "%s Identifier %s\n", indent, cmd.ConstraintName)
17741774
}
17751775
case ast.AlterModifyTTL:
1776-
if cmd.TTL != nil && cmd.TTL.Expression != nil {
1776+
if cmd.TTL != nil && len(cmd.TTL.Elements) > 0 {
17771777
// TTL is wrapped in ExpressionList and TTLElement
1778-
// Count total TTL elements (1 for Expression + len(Expressions))
1778+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(cmd.TTL.Elements))
1779+
for _, elem := range cmd.TTL.Elements {
1780+
// Count children: 1 for Expr, +1 for Where if present
1781+
ttlChildren := 1
1782+
if elem.Where != nil {
1783+
ttlChildren++
1784+
}
1785+
fmt.Fprintf(sb, "%s TTLElement (children %d)\n", indent, ttlChildren)
1786+
Node(sb, elem.Expr, depth+3)
1787+
if elem.Where != nil {
1788+
Node(sb, elem.Where, depth+3)
1789+
}
1790+
}
1791+
} else if cmd.TTL != nil && cmd.TTL.Expression != nil {
1792+
// Fallback for backward compatibility (Expression/Expressions fields)
17791793
ttlCount := 1 + len(cmd.TTL.Expressions)
17801794
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, ttlCount)
17811795
fmt.Fprintf(sb, "%s TTLElement (children 1)\n", indent)

parser/parser.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5682,18 +5682,24 @@ func (p *Parser) parseAlterCommand() *ast.AlterCommand {
56825682
cmd.Type = ast.AlterModifyTTL
56835683
p.nextToken()
56845684
cmd.TTL = &ast.TTLClause{
5685-
Position: p.current.Pos,
5686-
Expression: p.parseExpression(LOWEST),
5685+
Position: p.current.Pos,
56875686
}
5688-
// Skip RECOMPRESS CODEC(...) and other TTL modifiers
5689-
p.skipTTLModifiers()
5690-
// Parse additional TTL elements (comma-separated)
5691-
for p.currentIs(token.COMMA) {
5692-
p.nextToken() // skip comma
5693-
expr := p.parseExpression(LOWEST)
5694-
cmd.TTL.Expressions = append(cmd.TTL.Expressions, expr)
5695-
// Skip RECOMPRESS CODEC(...) if present
5696-
p.skipTTLModifiers()
5687+
// Parse TTL elements using parseTTLElement (captures WHERE clause)
5688+
for {
5689+
elem := p.parseTTLElement()
5690+
cmd.TTL.Elements = append(cmd.TTL.Elements, elem)
5691+
if p.currentIs(token.COMMA) {
5692+
p.nextToken()
5693+
} else {
5694+
break
5695+
}
5696+
}
5697+
// Keep backward compatibility with Expression/Expressions fields
5698+
if len(cmd.TTL.Elements) > 0 {
5699+
cmd.TTL.Expression = cmd.TTL.Elements[0].Expr
5700+
for i := 1; i < len(cmd.TTL.Elements); i++ {
5701+
cmd.TTL.Expressions = append(cmd.TTL.Expressions, cmd.TTL.Elements[i].Expr)
5702+
}
56975703
}
56985704
} else if p.currentIs(token.SETTINGS) || (p.currentIs(token.IDENT) && strings.ToUpper(p.current.Value) == "SETTING") {
56995705
// Both SETTINGS and SETTING (singular) are accepted
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt17": true,
4-
"stmt7": true
5-
}
6-
}
1+
{}

0 commit comments

Comments
 (0)