Skip to content

Commit 53e4097

Browse files
kyleconroyclaude
andcommitted
Add ALTER TABLE SPLIT/MERGE RANGE partition support
- Add AlterTableAlterPartitionStatement AST type for partition operations - Add parsing support for ALTER TABLE table SPLIT/MERGE RANGE (value) - Add JSON marshaling for the new statement type Note: AlterTableStatementTests130 has additional differences related to system versioning features (PERIOD FOR SYSTEM_TIME, SystemTimePeriodDefinition) that need separate implementation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a869586 commit 53e4097

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ast
2+
3+
// AlterTableAlterPartitionStatement represents ALTER TABLE table SPLIT/MERGE RANGE (value)
4+
type AlterTableAlterPartitionStatement struct {
5+
SchemaObjectName *SchemaObjectName
6+
BoundaryValue ScalarExpression
7+
IsSplit bool
8+
}
9+
10+
func (*AlterTableAlterPartitionStatement) node() {}
11+
func (*AlterTableAlterPartitionStatement) statement() {}

parser/marshal.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ func statementToJSON(stmt ast.Statement) jsonNode {
591591
return alterTableSetStatementToJSON(s)
592592
case *ast.AlterTableRebuildStatement:
593593
return alterTableRebuildStatementToJSON(s)
594+
case *ast.AlterTableAlterPartitionStatement:
595+
return alterTableAlterPartitionStatementToJSON(s)
594596
case *ast.AlterTableChangeTrackingModificationStatement:
595597
return alterTableChangeTrackingStatementToJSON(s)
596598
case *ast.InsertBulkStatement:
@@ -16934,6 +16936,20 @@ func alterTableRebuildStatementToJSON(s *ast.AlterTableRebuildStatement) jsonNod
1693416936
return node
1693516937
}
1693616938

16939+
func alterTableAlterPartitionStatementToJSON(s *ast.AlterTableAlterPartitionStatement) jsonNode {
16940+
node := jsonNode{
16941+
"$type": "AlterTableAlterPartitionStatement",
16942+
"IsSplit": s.IsSplit,
16943+
}
16944+
if s.BoundaryValue != nil {
16945+
node["BoundaryValue"] = scalarExpressionToJSON(s.BoundaryValue)
16946+
}
16947+
if s.SchemaObjectName != nil {
16948+
node["SchemaObjectName"] = schemaObjectNameToJSON(s.SchemaObjectName)
16949+
}
16950+
return node
16951+
}
16952+
1693716953
func alterTableChangeTrackingStatementToJSON(s *ast.AlterTableChangeTrackingModificationStatement) jsonNode {
1693816954
node := jsonNode{
1693916955
"$type": "AlterTableChangeTrackingModificationStatement",

parser/parse_ddl.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5049,9 +5049,50 @@ func (p *Parser) parseAlterTableStatement() (ast.Statement, error) {
50495049
return p.parseAlterTableRebuildStatement(tableName)
50505050
}
50515051

5052+
// Check for SPLIT RANGE / MERGE RANGE (partition operations)
5053+
upperLit := strings.ToUpper(p.curTok.Literal)
5054+
if upperLit == "SPLIT" || upperLit == "MERGE" {
5055+
return p.parseAlterTableAlterPartitionStatement(tableName, upperLit == "SPLIT")
5056+
}
5057+
50525058
return nil, fmt.Errorf("unexpected token in ALTER TABLE: %s", p.curTok.Literal)
50535059
}
50545060

5061+
func (p *Parser) parseAlterTableAlterPartitionStatement(tableName *ast.SchemaObjectName, isSplit bool) (*ast.AlterTableAlterPartitionStatement, error) {
5062+
// Consume SPLIT or MERGE
5063+
p.nextToken()
5064+
5065+
// Expect RANGE
5066+
if strings.ToUpper(p.curTok.Literal) != "RANGE" {
5067+
return nil, fmt.Errorf("expected RANGE after SPLIT/MERGE, got %s", p.curTok.Literal)
5068+
}
5069+
p.nextToken()
5070+
5071+
// Expect (
5072+
if p.curTok.Type != TokenLParen {
5073+
return nil, fmt.Errorf("expected ( after RANGE, got %s", p.curTok.Literal)
5074+
}
5075+
p.nextToken()
5076+
5077+
// Parse boundary value
5078+
value, err := p.parseScalarExpression()
5079+
if err != nil {
5080+
return nil, err
5081+
}
5082+
5083+
// Expect )
5084+
if p.curTok.Type != TokenRParen {
5085+
return nil, fmt.Errorf("expected ) after boundary value, got %s", p.curTok.Literal)
5086+
}
5087+
p.nextToken()
5088+
5089+
return &ast.AlterTableAlterPartitionStatement{
5090+
SchemaObjectName: tableName,
5091+
BoundaryValue: value,
5092+
IsSplit: isSplit,
5093+
}, nil
5094+
}
5095+
50555096
func (p *Parser) parseAlterTableDropStatement(tableName *ast.SchemaObjectName) (*ast.AlterTableDropTableElementStatement, error) {
50565097
// Consume DROP
50575098
p.nextToken()

0 commit comments

Comments
 (0)