Skip to content

Commit 11d6061

Browse files
authored
Add support of the DROP PROJECTION statement (#63)
1 parent 6193ec0 commit 11d6061

File tree

6 files changed

+129
-34
lines changed

6 files changed

+129
-34
lines changed

parser/ast.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,43 @@ func (a *AlterTableDropIndex) Accept(visitor ASTVisitor) error {
739739
return visitor.VisitAlterTableDropIndex(a)
740740
}
741741

742+
type AlterTableDropProjection struct {
743+
DropPos Pos
744+
ProjectionName *NestedIdentifier
745+
IfExists bool
746+
}
747+
748+
func (a *AlterTableDropProjection) Pos() Pos {
749+
return a.DropPos
750+
}
751+
752+
func (a *AlterTableDropProjection) End() Pos {
753+
return a.ProjectionName.End()
754+
}
755+
756+
func (a *AlterTableDropProjection) AlterType() string {
757+
return "DROP_PROJECTION"
758+
}
759+
760+
func (a *AlterTableDropProjection) String(level int) string {
761+
var builder strings.Builder
762+
builder.WriteString("DROP PROJECTION ")
763+
builder.WriteString(a.ProjectionName.String(level))
764+
if a.IfExists {
765+
builder.WriteString(" IF EXISTS")
766+
}
767+
return builder.String()
768+
}
769+
770+
func (a *AlterTableDropProjection) Accept(visitor ASTVisitor) error {
771+
visitor.enter(a)
772+
defer visitor.leave(a)
773+
if err := a.ProjectionName.Accept(visitor); err != nil {
774+
return err
775+
}
776+
return visitor.VisitAlterTableDropProjection(a)
777+
}
778+
742779
type AlterTableRemoveTTL struct {
743780
RemovePos Pos
744781
StatementEnd Pos

parser/ast_visitor.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type ASTVisitor interface {
1717
VisitProjectionSelect(expr *ProjectionSelect) error
1818
VisitAlterTableDropColumn(expr *AlterTableDropColumn) error
1919
VisitAlterTableDropIndex(expr *AlterTableDropIndex) error
20+
VisitAlterTableDropProjection(expr *AlterTableDropProjection) error
2021
VisitAlterTableRemoveTTL(expr *AlterTableRemoveTTL) error
2122
VisitAlterTableClearColumn(expr *AlterTableClearColumn) error
2223
VisitAlterTableClearIndex(expr *AlterTableClearIndex) error
@@ -279,6 +280,13 @@ func (v *DefaultASTVisitor) VisitAlterTableDropIndex(expr *AlterTableDropIndex)
279280
return nil
280281
}
281282

283+
func (v *DefaultASTVisitor) VisitAlterTableDropProjection(expr *AlterTableDropProjection) error {
284+
if v.Visit != nil {
285+
return v.Visit(expr)
286+
}
287+
return nil
288+
}
289+
282290
func (v *DefaultASTVisitor) VisitAlterTableRemoveTTL(expr *AlterTableRemoveTTL) error {
283291
if v.Visit != nil {
284292
return v.Visit(expr)

parser/parser_alter.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,15 @@ func (p *Parser) parseAlterTableDrop(pos Pos) (AlterTableExpr, error) {
292292
}
293293

294294
switch {
295-
case p.matchKeyword(KeywordColumn):
296-
return p.parseAlterTableDropColumn(pos)
297-
case p.matchKeyword(KeywordIndex):
298-
return p.parseAlterTableDropIndex(pos)
295+
case p.matchKeyword(KeywordColumn), p.matchKeyword(KeywordIndex), p.matchKeyword(KeywordProjection):
296+
return p.parseAlterTableDropStatement(pos)
299297
case p.matchKeyword(KeywordDetached):
300298
_ = p.lexer.consumeToken()
301299
return p.parseAlterTableDetachPartition(pos)
302300
case p.matchKeyword(KeywordPartition):
303301
return p.parseAlterTableDropPartition(pos)
304302
default:
305-
return nil, errors.New("expected keyword: COLUMN|INDEX|DETACH")
303+
return nil, errors.New("expected keyword: COLUMN|INDEX|PROJECTION|DETACHED|PARTITION")
306304
}
307305
}
308306

@@ -389,10 +387,19 @@ func (p *Parser) parseAlterTableAttachPartition(pos Pos) (AlterTableExpr, error)
389387
return alterTable, nil
390388
}
391389

392-
func (p *Parser) parseAlterTableDropColumn(pos Pos) (AlterTableExpr, error) {
393-
if err := p.consumeKeyword(KeywordColumn); err != nil {
394-
return nil, err
390+
func (p *Parser) parseAlterTableDropStatement(pos Pos) (AlterTableExpr, error) {
391+
var kind string
392+
switch {
393+
case p.matchKeyword(KeywordColumn):
394+
kind = KeywordColumn
395+
case p.matchKeyword(KeywordIndex):
396+
kind = KeywordIndex
397+
case p.matchKeyword(KeywordProjection):
398+
kind = KeywordProjection
399+
default:
400+
return nil, fmt.Errorf("expected token: COLUMN|INDEX|PROJECTION, but got %s", p.lastTokenKind())
395401
}
402+
_ = p.lexer.consumeToken()
396403

397404
ifExists, err := p.tryParseIfExists()
398405
if err != nil {
@@ -404,33 +411,25 @@ func (p *Parser) parseAlterTableDropColumn(pos Pos) (AlterTableExpr, error) {
404411
return nil, err
405412
}
406413

407-
return &AlterTableDropColumn{
408-
DropPos: pos,
409-
ColumnName: name,
410-
IfExists: ifExists,
411-
}, nil
412-
}
413-
414-
func (p *Parser) parseAlterTableDropIndex(pos Pos) (AlterTableExpr, error) {
415-
if err := p.consumeKeyword(KeywordIndex); err != nil {
416-
return nil, err
417-
}
418-
419-
ifExists, err := p.tryParseIfExists()
420-
if err != nil {
421-
return nil, err
422-
}
423-
424-
name, err := p.ParseNestedIdentifier(p.Pos())
425-
if err != nil {
426-
return nil, err
414+
if kind == KeywordProjection {
415+
return &AlterTableDropProjection{
416+
DropPos: pos,
417+
ProjectionName: name,
418+
IfExists: ifExists,
419+
}, nil
420+
} else if kind == KeywordColumn {
421+
return &AlterTableDropColumn{
422+
DropPos: pos,
423+
ColumnName: name,
424+
IfExists: ifExists,
425+
}, nil
426+
} else {
427+
return &AlterTableDropIndex{
428+
DropPos: pos,
429+
IndexName: name,
430+
IfExists: ifExists,
431+
}, nil
427432
}
428-
429-
return &AlterTableDropIndex{
430-
DropPos: pos,
431-
IndexName: name,
432-
IfExists: ifExists,
433-
}, nil
434433
}
435434

436435
func (p *Parser) tryParseAfterClause() (*NestedIdentifier, error) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE test.event_local ON CLUSTER 'default_cluster' DROP PROJECTION f1;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Origin SQL:
2+
ALTER TABLE test.event_local ON CLUSTER 'default_cluster' DROP PROJECTION f1;
3+
4+
-- Format SQL:
5+
ALTER TABLE test.event_local
6+
ON CLUSTER 'default_cluster'
7+
DROP PROJECTION f1;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[
2+
{
3+
"AlterPos": 0,
4+
"StatementEnd": 76,
5+
"TableIdentifier": {
6+
"Database": {
7+
"Name": "test",
8+
"QuoteType": 1,
9+
"NamePos": 12,
10+
"NameEnd": 16
11+
},
12+
"Table": {
13+
"Name": "event_local",
14+
"QuoteType": 1,
15+
"NamePos": 17,
16+
"NameEnd": 28
17+
}
18+
},
19+
"OnCluster": {
20+
"OnPos": 29,
21+
"Expr": {
22+
"LiteralPos": 41,
23+
"LiteralEnd": 56,
24+
"Literal": "default_cluster"
25+
}
26+
},
27+
"AlterExprs": [
28+
{
29+
"DropPos": 58,
30+
"ProjectionName": {
31+
"Ident": {
32+
"Name": "f1",
33+
"QuoteType": 1,
34+
"NamePos": 74,
35+
"NameEnd": 76
36+
},
37+
"DotIdent": null
38+
},
39+
"IfExists": false
40+
}
41+
]
42+
}
43+
]

0 commit comments

Comments
 (0)