Skip to content

Commit eaff68f

Browse files
laojianzigit-hulk
authored andcommitted
Fix missing DROP keyword in alter table drop clause (AfterShip#81)
1 parent bfc8e6d commit eaff68f

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

parser/ast.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,20 @@ func (a *AlterTableDetachPartition) Accept(visitor ASTVisitor) error {
324324
}
325325

326326
type AlterTableDropPartition struct {
327-
DropPos Pos
328-
Partition *PartitionClause
327+
DropPos Pos
328+
HasDetached bool
329+
Partition *PartitionClause
330+
Settings *SettingsClause
329331
}
330332

331333
func (a *AlterTableDropPartition) Pos() Pos {
332334
return a.DropPos
333335
}
334336

335337
func (a *AlterTableDropPartition) End() Pos {
338+
if a.Settings != nil {
339+
a.Settings.End()
340+
}
336341
return a.Partition.End()
337342
}
338343

@@ -343,7 +348,14 @@ func (a *AlterTableDropPartition) AlterType() string {
343348
func (a *AlterTableDropPartition) String(level int) string {
344349
var builder strings.Builder
345350
builder.WriteString("DROP ")
351+
if a.HasDetached {
352+
builder.WriteString("DETACHED ")
353+
}
346354
builder.WriteString(a.Partition.String(level))
355+
if a.Settings != nil {
356+
builder.WriteByte(' ')
357+
builder.WriteString(a.Settings.String(level))
358+
}
347359
return builder.String()
348360
}
349361

parser/parser_alter.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,7 @@ func (p *Parser) parseAlterTableDrop(pos Pos) (AlterTableClause, error) {
295295
switch {
296296
case p.matchKeyword(KeywordColumn), p.matchKeyword(KeywordIndex), p.matchKeyword(KeywordProjection):
297297
return p.parseAlterTableDropClause(pos)
298-
case p.matchKeyword(KeywordDetached):
299-
_ = p.lexer.consumeToken()
300-
return p.parseAlterTableDetachPartition(pos)
301-
case p.matchKeyword(KeywordPartition):
298+
case p.matchKeyword(KeywordDetached), p.matchKeyword(KeywordPartition):
302299
return p.parseAlterTableDropPartition(pos)
303300
default:
304301
return nil, errors.New("expected keyword: COLUMN|INDEX|PROJECTION|DETACHED|PARTITION")
@@ -443,6 +440,11 @@ func (p *Parser) tryParseAfterClause() (*NestedIdentifier, error) {
443440

444441
// Syntax: ALTER TABLE DROP partitionClause
445442
func (p *Parser) parseAlterTableDropPartition(pos Pos) (AlterTableClause, error) {
443+
var hasDetached bool
444+
if p.matchKeyword(KeywordDetached) {
445+
_ = p.lexer.consumeToken()
446+
hasDetached = true
447+
}
446448
partitionPos := p.Pos()
447449
if err := p.consumeKeyword(KeywordPartition); err != nil {
448450
return nil, err
@@ -456,9 +458,16 @@ func (p *Parser) parseAlterTableDropPartition(pos Pos) (AlterTableClause, error)
456458
}
457459
partition.Expr = expr
458460

461+
settings, err := p.tryParseSettingsClause(p.Pos())
462+
if err != nil {
463+
return nil, err
464+
}
465+
459466
return &AlterTableDropPartition{
460-
DropPos: pos,
461-
Partition: partition,
467+
DropPos: pos,
468+
Partition: partition,
469+
HasDetached: hasDetached,
470+
Settings: settings,
462471
}, nil
463472
}
464473

parser/testdata/ddl/format/alter_table_drop_detach_partition.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ ALTER TABLE app_utc_00.app_message_as_notification_organization_sent_stats_i_d_l
33

44
-- Format SQL:
55
ALTER TABLE app_utc_00.app_message_as_notification_organization_sent_stats_i_d_local
6-
DETACH PARTITION '2022-05-24' SETTINGS allow_drop_detached=1;
6+
DROP DETACHED PARTITION '2022-05-24' SETTINGS allow_drop_detached=1;

parser/testdata/ddl/output/alter_table_drop_detach_partition.sql.golden.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"OnCluster": null,
2020
"AlterExprs": [
2121
{
22-
"DetachPos": 85,
22+
"DropPos": 85,
23+
"HasDetached": true,
2324
"Partition": {
2425
"PartitionPos": 99,
2526
"Expr": {

parser/testdata/ddl/output/alter_table_drop_partition.sql.golden.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"AlterExprs": [
2828
{
2929
"DropPos": 53,
30+
"HasDetached": false,
3031
"Partition": {
3132
"PartitionPos": 58,
3233
"Expr": {
@@ -36,7 +37,8 @@
3637
},
3738
"ID": null,
3839
"All": false
39-
}
40+
},
41+
"Settings": null
4042
}
4143
]
4244
}

0 commit comments

Comments
 (0)