Skip to content

Commit

Permalink
*: remove useless ExprType (pingcap#6313)
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason authored Apr 18, 2018
1 parent ffd47ec commit 5725b43
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 374 deletions.
81 changes: 4 additions & 77 deletions expression/distsql_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"time"

"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/sessionctx"
Expand All @@ -29,66 +28,6 @@ import (
tipb "github.com/pingcap/tipb/go-tipb"
)

var distFuncs = map[tipb.ExprType]string{
// compare op
tipb.ExprType_LT: ast.LT,
tipb.ExprType_LE: ast.LE,
tipb.ExprType_GT: ast.GT,
tipb.ExprType_GE: ast.GE,
tipb.ExprType_EQ: ast.EQ,
tipb.ExprType_NE: ast.NE,
tipb.ExprType_NullEQ: ast.NullEQ,

// bit op
tipb.ExprType_BitAnd: ast.And,
tipb.ExprType_BitOr: ast.Or,
tipb.ExprType_BitXor: ast.Xor,
tipb.ExprType_RighShift: ast.RightShift,
tipb.ExprType_LeftShift: ast.LeftShift,
tipb.ExprType_BitNeg: ast.BitNeg,

// logical op
tipb.ExprType_And: ast.LogicAnd,
tipb.ExprType_Or: ast.LogicOr,
tipb.ExprType_Xor: ast.LogicXor,
tipb.ExprType_Not: ast.UnaryNot,

// arithmetic operator
tipb.ExprType_Plus: ast.Plus,
tipb.ExprType_Minus: ast.Minus,
tipb.ExprType_Mul: ast.Mul,
tipb.ExprType_Div: ast.Div,
tipb.ExprType_IntDiv: ast.IntDiv,
tipb.ExprType_Mod: ast.Mod,

// control operator
tipb.ExprType_Case: ast.Case,
tipb.ExprType_If: ast.If,
tipb.ExprType_IfNull: ast.Ifnull,
tipb.ExprType_NullIf: ast.Nullif,

// other operator
tipb.ExprType_Like: ast.Like,
tipb.ExprType_In: ast.In,
tipb.ExprType_IsNull: ast.IsNull,
tipb.ExprType_Coalesce: ast.Coalesce,

// for json functions.
tipb.ExprType_JsonType: ast.JSONType,
tipb.ExprType_JsonExtract: ast.JSONExtract,
tipb.ExprType_JsonUnquote: ast.JSONUnquote,
tipb.ExprType_JsonMerge: ast.JSONMerge,
tipb.ExprType_JsonSet: ast.JSONSet,
tipb.ExprType_JsonInsert: ast.JSONInsert,
tipb.ExprType_JsonReplace: ast.JSONReplace,
tipb.ExprType_JsonRemove: ast.JSONRemove,
tipb.ExprType_JsonArray: ast.JSONArray,
tipb.ExprType_JsonObject: ast.JSONObject,

// date functions.
tipb.ExprType_DateFormat: ast.DateFormat,
}

func pbTypeToFieldType(tp *tipb.FieldType) *types.FieldType {
return &types.FieldType{
Tp: byte(tp.Tp),
Expand Down Expand Up @@ -527,18 +466,6 @@ func newDistSQLFunctionBySig(sc *stmtctx.StatementContext, sigCode tipb.ScalarFu
}, nil
}

// newDistSQLFunction only creates function for mocktikv.
func newDistSQLFunction(sc *stmtctx.StatementContext, exprType tipb.ExprType, args []Expression) (Expression, error) {
name, ok := distFuncs[exprType]
if !ok {
return nil, errFunctionNotExists.GenByArgs("FUNCTION", exprType)
}
// TODO: Too ugly...
ctx := mock.NewContext()
ctx.GetSessionVars().StmtCtx = sc
return NewFunction(ctx, name, types.NewFieldType(mysql.TypeUnspecified), args...)
}

// PBToExpr converts pb structure to expression.
func PBToExpr(expr *tipb.Expr, tps []*types.FieldType, sc *stmtctx.StatementContext) (Expression, error) {
switch expr.Tp {
Expand Down Expand Up @@ -569,6 +496,9 @@ func PBToExpr(expr *tipb.Expr, tps []*types.FieldType, sc *stmtctx.StatementCont
case tipb.ExprType_MysqlTime:
return convertTime(expr.Val, expr.FieldType, sc.TimeZone)
}
if expr.Tp != tipb.ExprType_ScalarFunc {
panic("should be a tipb.ExprType_ScalarFunc")
}
// Then it must be a scalar function.
args := make([]Expression, 0, len(expr.Children))
for _, child := range expr.Children {
Expand All @@ -589,10 +519,7 @@ func PBToExpr(expr *tipb.Expr, tps []*types.FieldType, sc *stmtctx.StatementCont
}
args = append(args, arg)
}
if expr.Tp == tipb.ExprType_ScalarFunc {
return newDistSQLFunctionBySig(sc, expr.Sig, expr.FieldType, args)
}
return newDistSQLFunction(sc, expr.Tp, args)
return newDistSQLFunctionBySig(sc, expr.Sig, expr.FieldType, args)
}

func fieldTypeFromPB(ft *tipb.FieldType) *types.FieldType {
Expand Down
237 changes: 0 additions & 237 deletions expression/distsql_builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,199 +79,6 @@ func (s *testEvalSuite) TestEval(c *C) {
columnExpr(0),
types.NewIntDatum(100),
},
// Comparison operations.
{
buildExpr(tipb.ExprType_LT, types.NewIntDatum(100), types.NewIntDatum(1)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_LT, types.NewIntDatum(1), types.NewIntDatum(100)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_LT, types.NewIntDatum(100), types.Datum{}),
types.Datum{},
},
{
buildExpr(tipb.ExprType_LE, types.NewIntDatum(100), types.NewIntDatum(1)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_LE, types.NewIntDatum(1), types.NewIntDatum(1)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_LE, types.NewIntDatum(100), types.Datum{}),
types.Datum{},
},
{
buildExpr(tipb.ExprType_EQ, types.NewIntDatum(100), types.NewIntDatum(1)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_EQ, types.NewIntDatum(100), types.NewIntDatum(100)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_EQ, types.NewIntDatum(100), types.Datum{}),
types.Datum{},
},
{
buildExpr(tipb.ExprType_NE, types.NewIntDatum(100), types.NewIntDatum(100)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_NE, types.NewIntDatum(100), types.NewIntDatum(1)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_NE, types.NewIntDatum(100), types.Datum{}),
types.Datum{},
},
{
buildExpr(tipb.ExprType_GE, types.NewIntDatum(1), types.NewIntDatum(100)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_GE, types.NewIntDatum(100), types.NewIntDatum(100)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_GE, types.NewIntDatum(100), types.Datum{}),
types.Datum{},
},
{
buildExpr(tipb.ExprType_GT, types.NewIntDatum(100), types.NewIntDatum(100)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_GT, types.NewIntDatum(100), types.NewIntDatum(1)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_GT, types.NewIntDatum(100), types.Datum{}),
types.Datum{},
},
{
buildExpr(tipb.ExprType_NullEQ, types.NewIntDatum(1), types.Datum{}),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_NullEQ, types.Datum{}, types.Datum{}),
types.NewIntDatum(1),
},
// Logic operation.
{
buildExpr(tipb.ExprType_And, types.NewIntDatum(0), types.NewIntDatum(1)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_And, buildExpr(tipb.ExprType_And, types.NewIntDatum(1), types.NewIntDatum(1)), buildExpr(tipb.ExprType_And, types.NewIntDatum(0), types.NewIntDatum(1))),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_And, types.NewIntDatum(1), types.NewIntDatum(1)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_And, types.NewIntDatum(0), types.Datum{}),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_And, types.Datum{}, types.NewIntDatum(0)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_And, types.NewIntDatum(1), types.Datum{}),
types.Datum{},
},
{
buildExpr(tipb.ExprType_Or, types.NewIntDatum(0), types.NewIntDatum(0)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_Or, types.NewIntDatum(0), types.NewIntDatum(1)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_Or, types.NewIntDatum(0), types.Datum{}),
types.Datum{},
},
{
buildExpr(tipb.ExprType_Or, types.NewIntDatum(1), types.Datum{}),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_Or, types.Datum{}, types.NewIntDatum(1)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_And,
buildExpr(tipb.ExprType_EQ, types.NewIntDatum(1), types.NewIntDatum(1)),
buildExpr(tipb.ExprType_EQ, types.NewIntDatum(1), types.NewIntDatum(1))),
types.NewIntDatum(1),
},
{
notExpr(datumExpr(types.NewIntDatum(1))),
types.NewIntDatum(0),
},
{
notExpr(datumExpr(types.NewIntDatum(0))),
types.NewIntDatum(1),
},
{
notExpr(datumExpr(types.Datum{})),
types.Datum{},
},
// Arithmetic operation.
{
buildExpr(tipb.ExprType_Plus, types.NewIntDatum(-1), types.NewIntDatum(1)),
types.NewIntDatum(0),
},
{
buildExpr(tipb.ExprType_Plus, types.NewIntDatum(-1), types.NewFloat64Datum(1.5)),
types.NewFloat64Datum(0.5),
},
{
buildExpr(tipb.ExprType_Minus, types.NewIntDatum(-1), types.NewIntDatum(1)),
types.NewIntDatum(-2),
},
{
buildExpr(tipb.ExprType_Minus, types.NewIntDatum(-1), types.NewFloat64Datum(1.5)),
types.NewFloat64Datum(-2.5),
},
{
buildExpr(tipb.ExprType_Mul, types.NewFloat64Datum(-1), types.NewFloat64Datum(1)),
types.NewFloat64Datum(-1),
},
{
buildExpr(tipb.ExprType_Mul, types.NewFloat64Datum(-1.5), types.NewFloat64Datum(2)),
types.NewFloat64Datum(-3),
},
{
buildExpr(tipb.ExprType_Div, types.NewFloat64Datum(-3), types.NewFloat64Datum(2)),
types.NewFloat64Datum(-1.5),
},
{
buildExpr(tipb.ExprType_Div, types.NewFloat64Datum(-3), types.NewFloat64Datum(0)),
types.NewDatum(nil),
},
{
buildExpr(tipb.ExprType_IntDiv, types.NewIntDatum(3), types.NewIntDatum(2)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_IntDiv, types.NewFloat64Datum(3.0), types.NewFloat64Datum(1.9)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_Mod, types.NewIntDatum(3), types.NewIntDatum(2)),
types.NewIntDatum(1),
},
{
buildExpr(tipb.ExprType_Mod, types.NewFloat64Datum(3.0), types.NewFloat64Datum(1.9)),
types.NewFloat64Datum(1.1),
},
}
sc := new(stmtctx.StatementContext)
for _, tt := range tests {
Expand Down Expand Up @@ -340,47 +147,3 @@ func columnExpr(columnID int64) *tipb.Expr {
expr.Val = codec.EncodeInt(nil, columnID)
return expr
}

func notExpr(value interface{}) *tipb.Expr {
expr := new(tipb.Expr)
expr.Tp = tipb.ExprType_Not
switch x := value.(type) {
case types.Datum:
expr.Children = []*tipb.Expr{datumExpr(x)}
case *tipb.Expr:
expr.Children = []*tipb.Expr{x}
}
return expr
}

func (s *testEvalSuite) TestEvalIsNull(c *C) {
null, trueAns, falseAns := types.Datum{}, types.NewIntDatum(1), types.NewIntDatum(0)
tests := []struct {
expr *tipb.Expr
result types.Datum
}{
{
expr: buildExpr(tipb.ExprType_IsNull, types.NewStringDatum("abc")),
result: falseAns,
},
{
expr: buildExpr(tipb.ExprType_IsNull, null),
result: trueAns,
},
{
expr: buildExpr(tipb.ExprType_IsNull, types.NewIntDatum(0)),
result: falseAns,
},
}
sc := new(stmtctx.StatementContext)
for _, tt := range tests {
expr, err := PBToExpr(tt.expr, nil, sc)
c.Assert(err, IsNil, Commentf("%v", tt))
result, err := expr.Eval(nil)
c.Assert(err, IsNil, Commentf("%v", tt))
c.Assert(result.Kind(), Equals, tt.result.Kind(), Commentf("%v", tt))
cmp, err := result.CompareDatum(sc, &tt.result)
c.Assert(err, IsNil, Commentf("%v", tt))
c.Assert(cmp, Equals, 0, Commentf("%v", tt))
}
}
Loading

0 comments on commit 5725b43

Please sign in to comment.