Skip to content

Commit

Permalink
Add support of MATERIALIZED|ALIAS expr for the column def (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk authored Jul 22, 2024
1 parent 6a6d450 commit f6b95a0
Show file tree
Hide file tree
Showing 22 changed files with 509 additions and 288 deletions.
61 changes: 26 additions & 35 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2461,35 +2461,6 @@ func (o *ClusterClause) Accept(visitor ASTVisitor) error {
return visitor.VisitOnClusterExpr(o)
}

type DefaultExpr struct {
DefaultPos Pos
Expr Expr
}

func (d *DefaultExpr) Pos() Pos {
return d.DefaultPos
}

func (d *DefaultExpr) End() Pos {
return d.Expr.End()
}

func (d *DefaultExpr) String(level int) string {
var builder strings.Builder
builder.WriteString("DEFAULT ")
builder.WriteString(d.Expr.String(level + 1))
return builder.String()
}

func (d *DefaultExpr) Accept(visitor ASTVisitor) error {
visitor.enter(d)
defer visitor.leave(d)
if err := d.Expr.Accept(visitor); err != nil {
return err
}
return visitor.VisitDefaultExpr(d)
}

type PartitionClause struct {
PartitionPos Pos
Expr Expr
Expand Down Expand Up @@ -3015,7 +2986,9 @@ type ColumnExpr struct {
NotNull *NotNullLiteral
Nullable *NullLiteral

Property Expr
DefaultExpr Expr
MaterializedExpr Expr
AliasExpr Expr

Codec *CompressionCodec
TTL Expr
Expand Down Expand Up @@ -3044,9 +3017,17 @@ func (c *ColumnExpr) String(level int) string {
} else if c.Nullable != nil {
builder.WriteString(" NULL")
}
if c.Property != nil {
builder.WriteByte(' ')
builder.WriteString(c.Property.String(level))
if c.DefaultExpr != nil {
builder.WriteString(" DEFAULT ")
builder.WriteString(c.DefaultExpr.String(level))
}
if c.MaterializedExpr != nil {
builder.WriteString(" MATERIALIZED ")
builder.WriteString(c.MaterializedExpr.String(level))
}
if c.AliasExpr != nil {
builder.WriteString(" ALIAS ")
builder.WriteString(c.AliasExpr.String(level))
}
if c.Codec != nil {
builder.WriteByte(' ')
Expand Down Expand Up @@ -3084,8 +3065,18 @@ func (c *ColumnExpr) Accept(visitor ASTVisitor) error {
return err
}
}
if c.Property != nil {
if err := c.Property.Accept(visitor); err != nil {
if c.DefaultExpr != nil {
if err := c.DefaultExpr.Accept(visitor); err != nil {
return err
}
}
if c.MaterializedExpr != nil {
if err := c.MaterializedExpr.Accept(visitor); err != nil {
return err
}
}
if c.AliasExpr != nil {
if err := c.AliasExpr.Accept(visitor); err != nil {
return err
}
}
Expand Down
8 changes: 0 additions & 8 deletions parser/ast_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type ASTVisitor interface {
VisitTableArgListExpr(expr *TableArgListExpr) error
VisitTableFunctionExpr(expr *TableFunctionExpr) error
VisitOnClusterExpr(expr *ClusterClause) error
VisitDefaultExpr(expr *DefaultExpr) error
VisitPartitionExpr(expr *PartitionClause) error
VisitPartitionByExpr(expr *PartitionByClause) error
VisitPrimaryKeyExpr(expr *PrimaryKeyClause) error
Expand Down Expand Up @@ -541,13 +540,6 @@ func (v *DefaultASTVisitor) VisitOnClusterExpr(expr *ClusterClause) error {
return nil
}

func (v *DefaultASTVisitor) VisitDefaultExpr(expr *DefaultExpr) error {
if v.Visit != nil {
return v.Visit(expr)
}
return nil
}

func (v *DefaultASTVisitor) VisitPartitionExpr(expr *PartitionClause) error {
if v.Visit != nil {
return v.Visit(expr)
Expand Down
14 changes: 3 additions & 11 deletions parser/parser_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,6 @@ func (p *Parser) parseColumnExpr(pos Pos) (Expr, error) { //nolint:funlen
}
}

func (p *Parser) tryParseTableColumnPropertyExpr(pos Pos) (Expr, error) {
switch {
case p.matchKeyword(KeywordDefault):
return p.parseDefaultExpr(pos)
case p.matchKeyword(KeywordMaterialized):
case p.matchKeyword(KeywordAlias):
}
return nil, nil // nolint
}

func (p *Parser) parseColumnCastExpr(pos Pos) (Expr, error) {
if err := p.consumeKeyword(KeywordCast); err != nil {
return nil, err
Expand Down Expand Up @@ -558,7 +548,9 @@ func (p *Parser) parseFunctionParams(pos Pos) (*ParamExprList, error) {
Items: params,
}

// try to parse column arg list
// For some aggregate functions might support parametric arguments:
// e.g. QUANTILE(0.5)(x) or QUANTILE(0.5, 0.9)(x).
// So we need to have a check if there is another argument list with detecting the left bracket.
if p.matchTokenKind("(") {
columnArgList, err := p.parseColumnArgList(p.Pos())
if err != nil {
Expand Down
30 changes: 11 additions & 19 deletions parser/parser_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,20 @@ func (p *Parser) parseTableColumnExpr(pos Pos) (*ColumnExpr, error) {
columnEnd = notNull.End()
}

property, err := p.tryParseTableColumnPropertyExpr(p.Pos())
switch {
case p.tryConsumeKeyword(KeywordDefault) != nil:
column.DefaultExpr, err = p.parseExpr(p.Pos())
columnEnd = column.DefaultExpr.End()
case p.tryConsumeKeyword(KeywordMaterialized) != nil:
column.MaterializedExpr, err = p.parseExpr(p.Pos())
columnEnd = column.MaterializedExpr.End()
case p.tryConsumeKeyword(KeywordAlias) != nil:
column.AliasExpr, err = p.parseExpr(p.Pos())
columnEnd = column.AliasExpr.End()
}
if err != nil {
return nil, err
}
if property != nil {
columnEnd = property.End()
}

comment, err := p.tryParseColumnComment(p.Pos())
if err != nil {
Expand All @@ -464,7 +471,6 @@ func (p *Parser) parseTableColumnExpr(pos Pos) (*ColumnExpr, error) {
column.Codec = codec
column.Nullable = nullable
column.NotNull = notNull
column.Property = property
return column, nil
}

Expand Down Expand Up @@ -802,20 +808,6 @@ func (p *Parser) parseSettingsExprList(pos Pos) (*SettingExprList, error) {
}, nil
}

func (p *Parser) parseDefaultExpr(pos Pos) (Expr, error) {
if err := p.consumeKeyword(KeywordDefault); err != nil {
return nil, err
}
expr, err := p.parseExpr(pos)
if err != nil {
return nil, err
}
return &DefaultExpr{
DefaultPos: pos,
Expr: expr,
}, nil
}

func (p *Parser) parseDestinationClause(pos Pos) (*DestinationClause, error) {
if err := p.consumeKeyword(KeywordTo); err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion parser/testdata/ddl/create_table_basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ CREATE TABLE IF NOT EXISTS test.events_local (
f74 Int64,
f75 String
),
f8 Datetime DEFAULT now()
f8 Datetime DEFAULT now(),
f9 String MATERIALIZED toString(f7['f70']),
f10 String ALIAS f11,
) ENGINE = MergeTree
PRIMARY KEY (f0, f1, f2)
PARTITION BY toYYYYMMDD(f3)
Expand Down
8 changes: 6 additions & 2 deletions parser/testdata/ddl/format/create_table_basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ CREATE TABLE IF NOT EXISTS test.events_local (
f74 Int64,
f75 String
),
f8 Datetime DEFAULT now()
f8 Datetime DEFAULT now(),
f9 String MATERIALIZED toString(f7['f70']),
f10 String ALIAS f11,
) ENGINE = MergeTree
PRIMARY KEY (f0, f1, f2)
PARTITION BY toYYYYMMDD(f3)
Expand All @@ -44,7 +46,9 @@ CREATE TABLE IF NOT EXISTS test.events_local
f73 Int64,
f74 Int64,
f75 String),
f8 Datetime DEFAULT now()
f8 Datetime DEFAULT now(),
f9 String MATERIALIZED toString(f7['f70']),
f10 String ALIAS f11
)
ENGINE = MergeTree
PRIMARY KEY (f0, f1, f2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"Type": null,
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down
65 changes: 39 additions & 26 deletions parser/testdata/ddl/output/attach_table_basic.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down Expand Up @@ -80,7 +82,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down Expand Up @@ -108,7 +112,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down Expand Up @@ -136,7 +142,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down Expand Up @@ -164,7 +172,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down Expand Up @@ -212,7 +222,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down Expand Up @@ -240,7 +252,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down Expand Up @@ -268,28 +282,27 @@
},
"NotNull": null,
"Nullable": null,
"Property": {
"DefaultPos": 213,
"Expr": {
"Name": {
"Name": "now",
"QuoteType": 1,
"NamePos": 221,
"NameEnd": 224
"DefaultExpr": {
"Name": {
"Name": "now",
"QuoteType": 1,
"NamePos": 221,
"NameEnd": 224
},
"Params": {
"LeftParenPos": 224,
"RightParenPos": 225,
"Items": {
"ListPos": 225,
"ListEnd": 225,
"HasDistinct": false,
"Items": []
},
"Params": {
"LeftParenPos": 224,
"RightParenPos": 225,
"Items": {
"ListPos": 225,
"ListEnd": 225,
"HasDistinct": false,
"Items": []
},
"ColumnArgList": null
}
"ColumnArgList": null
}
},
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
},
"NotNull": null,
"Nullable": null,
"Property": null,
"DefaultExpr": null,
"MaterializedExpr": null,
"AliasExpr": null,
"Codec": null,
"TTL": null,
"Comment": null,
Expand Down
Loading

0 comments on commit f6b95a0

Please sign in to comment.