Skip to content

Commit

Permalink
Merge branch 'master' into disksing/update-codec
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing committed Dec 14, 2015
2 parents a66bc59 + d15bd86 commit c8d7ed1
Show file tree
Hide file tree
Showing 32 changed files with 713 additions and 244 deletions.
20 changes: 18 additions & 2 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ type Node interface {
SetText(text string)
}

// Flags indicates whether an expression contains certain types of expression.
const (
FlagConstant uint64 = 0
FlagHasParamMarker uint64 = 1 << iota
FlagHasFunc
FlagHasReference
FlagHasAggregateFunc
FlagHasSubquery
FlagHasVariable
FlagHasDefault
)

// ExprNode is a node that can be evaluated.
// Name of implementations should have 'Expr' suffix.
type ExprNode interface {
// Node is embeded in ExprNode.
Node
// IsStatic means it can be evaluated independently.
IsStatic() bool
// SetType sets evaluation type to the expression.
SetType(tp *types.FieldType)
// GetType gets the evaluation type of the expression.
Expand All @@ -53,6 +63,12 @@ type ExprNode interface {
SetValue(val interface{})
// GetValue gets value of the expression.
GetValue() interface{}
// SetFlag sets flag to the expression.
// Flag indicates whether the expression contains
// parameter marker, reference, aggregate function...
SetFlag(flag uint64)
// GetFlag returns the flag of the expression.
GetFlag() uint64
}

// FuncNode represents function call expression node.
Expand Down
16 changes: 11 additions & 5 deletions ast/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ func (dn *dmlNode) dmlStatement() {}
type exprNode struct {
node
types.DataItem
}

// IsStatic implements Expression interface.
func (en *exprNode) IsStatic() bool {
return false
flag uint64
}

// SetType implements Expression interface.
Expand All @@ -90,6 +86,16 @@ func (en *exprNode) GetValue() interface{} {
return en.Data
}

// SetFlag implements Expression interface.
func (en *exprNode) SetFlag(flag uint64) {
en.flag = flag
}

// GetFlag implements Expression interface.
func (en *exprNode) GetFlag() uint64 {
return en.flag
}

type funcNode struct {
exprNode
}
Expand Down
76 changes: 0 additions & 76 deletions ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ func NewValueExpr(value interface{}) *ValueExpr {
return ve
}

// IsStatic implements ExprNode interface.
func (n *ValueExpr) IsStatic() bool {
return true
}

// Accept implements Node interface.
func (n *ValueExpr) Accept(v Visitor) (Node, bool) {
newNod, skipChildren := v.Enter(n)
Expand Down Expand Up @@ -159,11 +154,6 @@ func (n *BetweenExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *BetweenExpr) IsStatic() bool {
return n.Expr.IsStatic() && n.Left.IsStatic() && n.Right.IsStatic()
}

// BinaryOperationExpr is for binary operation like 1 + 1, 1 - 1, etc.
type BinaryOperationExpr struct {
exprNode
Expand Down Expand Up @@ -198,11 +188,6 @@ func (n *BinaryOperationExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *BinaryOperationExpr) IsStatic() bool {
return n.L.IsStatic() && n.R.IsStatic()
}

// WhenClause is the when clause in Case expression for "when condition then result".
type WhenClause struct {
node
Expand Down Expand Up @@ -275,22 +260,6 @@ func (n *CaseExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *CaseExpr) IsStatic() bool {
if n.Value != nil && !n.Value.IsStatic() {
return false
}
for _, w := range n.WhenClauses {
if !w.Expr.IsStatic() || !w.Result.IsStatic() {
return false
}
}
if n.ElseClause != nil && !n.ElseClause.IsStatic() {
return false
}
return true
}

// SubqueryExpr represents a subquery.
type SubqueryExpr struct {
exprNode
Expand Down Expand Up @@ -517,11 +486,6 @@ func (n *IsNullExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *IsNullExpr) IsStatic() bool {
return n.Expr.IsStatic()
}

// IsTruthExpr is the expression for true/false check.
type IsTruthExpr struct {
exprNode
Expand All @@ -548,11 +512,6 @@ func (n *IsTruthExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *IsTruthExpr) IsStatic() bool {
return n.Expr.IsStatic()
}

// PatternLikeExpr is the expression for like operator, e.g, expr like "%123%"
type PatternLikeExpr struct {
exprNode
Expand Down Expand Up @@ -593,11 +552,6 @@ func (n *PatternLikeExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *PatternLikeExpr) IsStatic() bool {
return n.Expr.IsStatic() && n.Pattern.IsStatic()
}

// ParamMarkerExpr expresion holds a place for another expression.
// Used in parsing prepare statement.
type ParamMarkerExpr struct {
Expand Down Expand Up @@ -639,11 +593,6 @@ func (n *ParenthesesExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *ParenthesesExpr) IsStatic() bool {
return n.Expr.IsStatic()
}

// PositionExpr is the expression for order by and group by position.
// MySQL use position expression started from 1, it looks a little confused inner.
// maybe later we will use 0 at first.
Expand All @@ -655,11 +604,6 @@ type PositionExpr struct {
Refer *ResultField
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *PositionExpr) IsStatic() bool {
return false
}

// Accept implements Node Accept interface.
func (n *PositionExpr) Accept(v Visitor) (Node, bool) {
newNod, skipChildren := v.Enter(n)
Expand Down Expand Up @@ -706,11 +650,6 @@ func (n *PatternRegexpExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *PatternRegexpExpr) IsStatic() bool {
return n.Expr.IsStatic() && n.Pattern.IsStatic()
}

// RowExpr is the expression for row constructor.
// See https://dev.mysql.com/doc/refman/5.7/en/row-subqueries.html
type RowExpr struct {
Expand All @@ -736,16 +675,6 @@ func (n *RowExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *RowExpr) IsStatic() bool {
for _, v := range n.Values {
if !v.IsStatic() {
return false
}
}
return true
}

// UnaryOperationExpr is the expression for unary operator.
type UnaryOperationExpr struct {
exprNode
Expand All @@ -770,11 +699,6 @@ func (n *UnaryOperationExpr) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// IsStatic implements the ExprNode IsStatic interface.
func (n *UnaryOperationExpr) IsStatic() bool {
return n.V.IsStatic()
}

// ValuesExpr is the expression used in INSERT VALUES
type ValuesExpr struct {
exprNode
Expand Down
Loading

0 comments on commit c8d7ed1

Please sign in to comment.