Skip to content

Commit

Permalink
*: remove evaluate.Eval from ddl (pingcap#2264)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored Dec 19, 2016
1 parent 507201b commit cbe49fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/terror"
Expand Down Expand Up @@ -541,7 +542,7 @@ func columnDefToCol(ctx context.Context, offset int, colDef *ast.ColumnDef) (*ta
col.Flag |= mysql.OnUpdateNowFlag
setOnUpdateNow = true
case ast.ColumnOptionComment:
value, err := evaluator.Eval(ctx, v.Expr)
value, err := plan.EvalAstExpr(v.Expr, ctx)
if err != nil {
return nil, nil, errors.Trace(err)
}
Expand Down Expand Up @@ -591,7 +592,7 @@ func getDefaultValue(ctx context.Context, c *ast.ColumnOption, tp byte, fsp int)

return value, nil
}
v, err := evaluator.Eval(ctx, c.Expr)
v, err := plan.EvalAstExpr(c.Expr, ctx)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
18 changes: 18 additions & 0 deletions plan/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ import (
// EvalSubquery evaluates incorrelated subqueries once.
var EvalSubquery func(p PhysicalPlan, is infoschema.InfoSchema, ctx context.Context) ([]types.Datum, error)

// EvalAstExpr evaluates ast expression directly.
func EvalAstExpr(expr ast.ExprNode, ctx context.Context) (types.Datum, error) {
if val, ok := expr.(*ast.ValueExpr); ok {
return val.Datum, nil
}
b := &planBuilder{
ctx: ctx,
allocator: new(idAllocator),
colMapper: make(map[*ast.ColumnNameExpr]int),
is: ctx.GetSessionVars().TxnCtx.InfoSchema.(infoschema.InfoSchema),
}
newExpr, _, err := b.rewrite(expr, nil, nil, true)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
return newExpr.Eval(nil, ctx)
}

// rewrite function rewrites ast expr to expression.Expression.
// aggMapper maps ast.AggregateFuncExpr to the columns offset in p's output schema.
// asScalar means whether this expression must be treated as a scalar expression.
Expand Down

0 comments on commit cbe49fa

Please sign in to comment.