Skip to content

Commit 4fa6011

Browse files
authored
Add support of the MATRIALIZE PROJECTION|INDEX statement (#64)
1 parent 11d6061 commit 4fa6011

9 files changed

+272
-1
lines changed

parser/ast.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,106 @@ func (a *AlterTableDropPartition) Accept(visitor ASTVisitor) error {
356356
return visitor.VisitAlterTableDropPartition(a)
357357
}
358358

359+
type AlterTableMaterializeProjection struct {
360+
MaterializedPos Pos
361+
StatementEnd Pos
362+
IfExists bool
363+
ProjectionName *NestedIdentifier
364+
Partition *PartitionExpr
365+
}
366+
367+
func (a *AlterTableMaterializeProjection) Pos() Pos {
368+
return a.MaterializedPos
369+
}
370+
371+
func (a *AlterTableMaterializeProjection) End() Pos {
372+
return a.StatementEnd
373+
}
374+
375+
func (a *AlterTableMaterializeProjection) AlterType() string {
376+
return "MATERIALIZE_PROJECTION"
377+
}
378+
379+
func (a *AlterTableMaterializeProjection) String(level int) string {
380+
var builder strings.Builder
381+
builder.WriteString("MATERIALIZE PROJECTION")
382+
383+
if a.IfExists {
384+
builder.WriteString(" IF EXISTS")
385+
}
386+
builder.WriteString(" ")
387+
builder.WriteString(a.ProjectionName.String(level))
388+
if a.Partition != nil {
389+
builder.WriteString(" IN ")
390+
builder.WriteString(a.Partition.String(level))
391+
}
392+
return builder.String()
393+
}
394+
395+
func (a *AlterTableMaterializeProjection) Accept(visitor ASTVisitor) error {
396+
visitor.enter(a)
397+
defer visitor.leave(a)
398+
if err := a.ProjectionName.Accept(visitor); err != nil {
399+
return err
400+
}
401+
if a.Partition != nil {
402+
if err := a.Partition.Accept(visitor); err != nil {
403+
return err
404+
}
405+
}
406+
return visitor.VisitAlterTableMaterializeProjection(a)
407+
}
408+
409+
type AlterTableMaterializeIndex struct {
410+
MaterializedPos Pos
411+
StatementEnd Pos
412+
IfExists bool
413+
IndexName *NestedIdentifier
414+
Partition *PartitionExpr
415+
}
416+
417+
func (a *AlterTableMaterializeIndex) Pos() Pos {
418+
return a.MaterializedPos
419+
}
420+
421+
func (a *AlterTableMaterializeIndex) End() Pos {
422+
return a.StatementEnd
423+
}
424+
425+
func (a *AlterTableMaterializeIndex) AlterType() string {
426+
return "MATERIALIZE_INDEX"
427+
}
428+
429+
func (a *AlterTableMaterializeIndex) String(level int) string {
430+
var builder strings.Builder
431+
builder.WriteString("MATERIALIZE INDEX")
432+
433+
if a.IfExists {
434+
builder.WriteString(" IF EXISTS")
435+
}
436+
builder.WriteString(" ")
437+
builder.WriteString(a.IndexName.String(level))
438+
if a.Partition != nil {
439+
builder.WriteString(" IN ")
440+
builder.WriteString(a.Partition.String(level))
441+
}
442+
return builder.String()
443+
}
444+
445+
func (a *AlterTableMaterializeIndex) Accept(visitor ASTVisitor) error {
446+
visitor.enter(a)
447+
defer visitor.leave(a)
448+
if err := a.IndexName.Accept(visitor); err != nil {
449+
return err
450+
}
451+
if a.Partition != nil {
452+
if err := a.Partition.Accept(visitor); err != nil {
453+
return err
454+
}
455+
}
456+
return visitor.VisitAlterTableMaterializeIndex(a)
457+
}
458+
359459
type AlterTableFreezePartition struct {
360460
FreezePos Pos
361461
StatementEnd Pos

parser/ast_visitor.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type ASTVisitor interface {
2222
VisitAlterTableClearColumn(expr *AlterTableClearColumn) error
2323
VisitAlterTableClearIndex(expr *AlterTableClearIndex) error
2424
VisitAlterTableClearProjection(expr *AlterTableClearProjection) error
25+
VisitAlterTableMaterializeIndex(expr *AlterTableMaterializeIndex) error
26+
VisitAlterTableMaterializeProjection(expr *AlterTableMaterializeProjection) error
2527
VisitAlterTableRenameColumn(expr *AlterTableRenameColumn) error
2628
VisitAlterTableModifyTTL(expr *AlterTableModifyTTL) error
2729
VisitAlterTableModifyColumn(expr *AlterTableModifyColumn) error
@@ -315,6 +317,20 @@ func (v *DefaultASTVisitor) VisitAlterTableClearProjection(expr *AlterTableClear
315317
return nil
316318
}
317319

320+
func (v *DefaultASTVisitor) VisitAlterTableMaterializeProjection(expr *AlterTableMaterializeProjection) error {
321+
if v.Visit != nil {
322+
return v.Visit(expr)
323+
}
324+
return nil
325+
}
326+
327+
func (v *DefaultASTVisitor) VisitAlterTableMaterializeIndex(expr *AlterTableMaterializeIndex) error {
328+
if v.Visit != nil {
329+
return v.Visit(expr)
330+
}
331+
return nil
332+
}
333+
318334
func (v *DefaultASTVisitor) VisitAlterTableRenameColumn(expr *AlterTableRenameColumn) error {
319335
if v.Visit != nil {
320336
return v.Visit(expr)

parser/parser_alter.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func (p *Parser) parseAlterTable(pos Pos) (*AlterTable, error) {
4949
alterExpr, err = p.parseAlterTableModify(p.Pos())
5050
case p.matchKeyword(KeywordReplace):
5151
alterExpr, err = p.parseAlterTableReplacePartition(p.Pos())
52-
52+
case p.matchKeyword(KeywordMaterialize):
53+
alterExpr, err = p.parseAlterTableMaterialize(p.Pos())
5354
default:
5455
return nil, errors.New("expected token: ADD|DROP|ATTACH|DETACH|FREEZE|REMOVE|CLEAR")
5556
}
@@ -709,3 +710,53 @@ func (p *Parser) parseAlterTableReplacePartition(pos Pos) (AlterTableExpr, error
709710
Table: table,
710711
}, nil
711712
}
713+
714+
func (p *Parser) parseAlterTableMaterialize(pos Pos) (AlterTableExpr, error) {
715+
if err := p.consumeKeyword(KeywordMaterialize); err != nil {
716+
return nil, err
717+
}
718+
var kind string
719+
switch {
720+
case p.matchKeyword(KeywordIndex):
721+
kind = KeywordIndex
722+
case p.matchKeyword(KeywordProjection):
723+
kind = KeywordProjection
724+
default:
725+
return nil, fmt.Errorf("expected keyword: INDEX|PROJECTION, but got %q", p.lastTokenKind())
726+
}
727+
_ = p.lexer.consumeToken()
728+
729+
ifExists, err := p.tryParseIfExists()
730+
if err != nil {
731+
return nil, err
732+
}
733+
name, err := p.ParseNestedIdentifier(p.Pos())
734+
if err != nil {
735+
return nil, err
736+
}
737+
statementEnd := name.End()
738+
var partitionExpr *PartitionExpr
739+
if p.tryConsumeKeyword(KeywordIn) != nil {
740+
partitionExpr, err = p.tryParsePartitionExpr(p.Pos())
741+
if err != nil {
742+
return nil, err
743+
}
744+
statementEnd = partitionExpr.End()
745+
}
746+
if kind == KeywordIndex {
747+
return &AlterTableMaterializeIndex{
748+
MaterializedPos: pos,
749+
StatementEnd: statementEnd,
750+
IfExists: ifExists,
751+
IndexName: name,
752+
Partition: partitionExpr,
753+
}, nil
754+
}
755+
return &AlterTableMaterializeProjection{
756+
MaterializedPos: pos,
757+
StatementEnd: statementEnd,
758+
IfExists: ifExists,
759+
ProjectionName: name,
760+
Partition: partitionExpr,
761+
}, nil
762+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE visits_order MATERIALIZE INDEX IF EXISTS user_name_index IN PARTITION '20240403';
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE visits_order MATERIALIZE PROJECTION IF EXISTS user_name_projection IN PARTITION '20240403';
2+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Origin SQL:
2+
ALTER TABLE visits_order MATERIALIZE INDEX IF EXISTS user_name_index IN PARTITION '20240403';
3+
4+
5+
6+
-- Format SQL:
7+
ALTER TABLE visits_order
8+
MATERIALIZE INDEX IF EXISTS user_name_index IN PARTITION '20240403';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Origin SQL:
2+
ALTER TABLE visits_order MATERIALIZE PROJECTION IF EXISTS user_name_projection IN PARTITION '20240403';
3+
4+
5+
6+
-- Format SQL:
7+
ALTER TABLE visits_order
8+
MATERIALIZE PROJECTION IF EXISTS user_name_projection IN PARTITION '20240403';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
{
3+
"AlterPos": 0,
4+
"StatementEnd": 91,
5+
"TableIdentifier": {
6+
"Database": null,
7+
"Table": {
8+
"Name": "visits_order",
9+
"QuoteType": 1,
10+
"NamePos": 12,
11+
"NameEnd": 24
12+
}
13+
},
14+
"OnCluster": null,
15+
"AlterExprs": [
16+
{
17+
"MaterializedPos": 25,
18+
"StatementEnd": 91,
19+
"IfExists": true,
20+
"IndexName": {
21+
"Ident": {
22+
"Name": "user_name_index",
23+
"QuoteType": 1,
24+
"NamePos": 53,
25+
"NameEnd": 68
26+
},
27+
"DotIdent": null
28+
},
29+
"Partition": {
30+
"PartitionPos": 72,
31+
"Expr": {
32+
"LiteralPos": 83,
33+
"LiteralEnd": 91,
34+
"Literal": "20240403"
35+
},
36+
"ID": null,
37+
"All": false
38+
}
39+
}
40+
]
41+
}
42+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
{
3+
"AlterPos": 0,
4+
"StatementEnd": 101,
5+
"TableIdentifier": {
6+
"Database": null,
7+
"Table": {
8+
"Name": "visits_order",
9+
"QuoteType": 1,
10+
"NamePos": 12,
11+
"NameEnd": 24
12+
}
13+
},
14+
"OnCluster": null,
15+
"AlterExprs": [
16+
{
17+
"MaterializedPos": 25,
18+
"StatementEnd": 101,
19+
"IfExists": true,
20+
"ProjectionName": {
21+
"Ident": {
22+
"Name": "user_name_projection",
23+
"QuoteType": 1,
24+
"NamePos": 58,
25+
"NameEnd": 78
26+
},
27+
"DotIdent": null
28+
},
29+
"Partition": {
30+
"PartitionPos": 82,
31+
"Expr": {
32+
"LiteralPos": 93,
33+
"LiteralEnd": 101,
34+
"Literal": "20240403"
35+
},
36+
"ID": null,
37+
"All": false
38+
}
39+
}
40+
]
41+
}
42+
]

0 commit comments

Comments
 (0)