• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang types.ToBool函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/pingcap/tidb/util/types.ToBool函数的典型用法代码示例。如果您正苦于以下问题:Golang ToBool函数的具体用法?Golang ToBool怎么用?Golang ToBool使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了ToBool函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: handleOrOr

func (e *Evaluator) handleOrOr(o *ast.BinaryOperationExpr) bool {
	leftVal := o.L.GetValue()
	righVal := o.R.GetValue()
	if !types.IsNil(leftVal) {
		x, err := types.ToBool(leftVal)
		if err != nil {
			e.err = errors.Trace(err)
			return false
		} else if x == 1 {
			// true || any other types is true.
			o.SetValue(x)
			return true
		}
	}
	if !types.IsNil(righVal) {
		y, err := types.ToBool(righVal)
		if err != nil {
			e.err = errors.Trace(err)
			return false
		} else if y == 1 {
			o.SetValue(y)
			return true
		}
	}
	if types.IsNil(leftVal) || types.IsNil(righVal) {
		o.SetValue(nil)
		return true
	}
	o.SetValue(int64(0))
	return true
}
开发者ID:lovedboy,项目名称:tidb,代码行数:31,代码来源:evaluator_binop.go


示例2: evalLogicXor

func (o *BinaryOperation) evalLogicXor(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
	a, err := o.L.Eval(ctx, args)
	if err != nil || a == nil {
		return nil, o.traceErr(err)
	}

	x, err := types.ToBool(a)
	if err != nil {
		return nil, o.traceErr(err)
	}

	b, err := o.R.Eval(ctx, args)
	if err != nil || b == nil {
		return nil, o.traceErr(err)
	}

	y, err := types.ToBool(b)
	if err != nil {
		return nil, o.traceErr(err)
	}

	if x == y {
		return int64(0), nil
	}

	return int64(1), nil
}
开发者ID:nengwang,项目名称:tidb,代码行数:27,代码来源:binop.go


示例3: handleXor

func (e *Evaluator) handleXor(o *ast.BinaryOperationExpr) bool {
	leftVal := o.L.GetValue()
	righVal := o.R.GetValue()
	if types.IsNil(leftVal) || types.IsNil(righVal) {
		o.SetValue(nil)
		return true
	}
	x, err := types.ToBool(leftVal)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}

	y, err := types.ToBool(righVal)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}
	if x == y {
		o.SetValue(int64(0))
	} else {
		o.SetValue(int64(1))
	}
	return true
}
开发者ID:lovedboy,项目名称:tidb,代码行数:25,代码来源:evaluator_binop.go


示例4: Eval

// Eval implements the Expression Eval interface.
func (is *IsTruth) Eval(ctx context.Context, args map[interface{}]interface{}) (v interface{}, err error) {
	if err := CheckOneColumn(is.Expr); err != nil {
		return nil, errors.Trace(err)
	}

	val, err := is.Expr.Eval(ctx, args)
	if err != nil {
		return
	}

	if val == nil {
		// null is true/false -> false
		// null is not true/false -> true
		return is.Not, nil
	}

	b, err := types.ToBool(val)
	if err != nil {
		return
	}

	if !is.Not {
		// true/false is true/false
		return b == is.True, nil
	}

	// true/false is not true/false
	return b != is.True, nil
}
开发者ID:no2key,项目名称:tidb,代码行数:30,代码来源:istruth.go


示例5: evalInList

func (n *PatternIn) evalInList(ctx context.Context, args map[interface{}]interface{},
	in interface{}, list []expression.Expression) (interface{}, error) {
	hasNull := false
	for _, v := range list {
		b := NewBinaryOperation(opcode.EQ, Value{in}, v)

		eVal, err := b.Eval(ctx, args)
		if err != nil {
			return nil, err
		}

		if eVal == nil {
			hasNull = true
			continue
		}

		r, err := types.ToBool(eVal)
		if err != nil {
			return nil, err
		}

		if r == 1 {
			return !n.Not, nil
		}
	}

	if hasNull {
		// if no matched but we got null in In, return null
		// e.g 1 in (null, 2, 3) returns null
		return nil, nil
	}

	return n.Not, nil
}
开发者ID:szctop,项目名称:tidb,代码行数:34,代码来源:in.go


示例6: Do

// Do implements plan.Plan Do interface.
func (r *FilterDefaultPlan) Do(ctx context.Context, f plan.RowIterFunc) (err error) {
	m := map[interface{}]interface{}{}
	fields := r.GetFields()
	return r.Plan.Do(ctx, func(rid interface{}, data []interface{}) (bool, error) {
		m[expressions.ExprEvalIdentFunc] = func(name string) (interface{}, error) {
			return getIdentValue(name, fields, data, field.DefaultFieldFlag)
		}
		val, err := r.Expr.Eval(ctx, m)
		if err != nil {
			return false, err
		}

		if val == nil {
			return true, nil
		}

		// Evaluate the expression, if the result is true, go on, otherwise
		// skip this row.
		x, err := types.ToBool(val)
		if err != nil {
			return false, err
		}

		if x == 0 {
			return true, nil
		}
		return f(rid, data)
	})
}
开发者ID:ninefive,项目名称:tidb,代码行数:30,代码来源:where.go


示例7: evalOrOr

func (o *BinaryOperation) evalOrOr(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
	a, err := o.L.Eval(ctx, args)
	if err != nil {
		return nil, o.traceErr(err)
	}

	var (
		x int64
		y int64
	)

	if a != nil {
		x, err = types.ToBool(a)
		if err != nil {
			return nil, o.traceErr(err)
		} else if x == 1 {
			// true || any other types is true
			return x, nil
		}
	}

	b, err := o.R.Eval(ctx, args)
	if err != nil {
		return nil, o.traceErr(err)
	}

	if b != nil {
		y, err = types.ToBool(b)
		if err != nil {
			return nil, o.traceErr(err)
		} else if y == 1 {
			return y, nil
		}
	}

	// here x and y are all not true
	// if a or b is nil
	if a == nil || b == nil {
		return nil, nil
	}

	return int64(0), nil
}
开发者ID:nengwang,项目名称:tidb,代码行数:43,代码来源:binop.go


示例8: meetCondition

func (r *FilterDefaultPlan) meetCondition(ctx context.Context) (bool, error) {
	val, err := r.Expr.Eval(ctx, r.evalArgs)
	if val == nil || err != nil {
		return false, errors.Trace(err)
	}
	x, err := types.ToBool(val)
	if err != nil {
		return false, errors.Trace(err)
	}
	return x == 1, nil
}
开发者ID:hxiaodon,项目名称:tidb,代码行数:11,代码来源:where.go


示例9: evalAndAnd

func (o *BinaryOperation) evalAndAnd(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
	a, err := o.L.Eval(ctx, args)
	if err != nil {
		return nil, o.traceErr(err)
	}

	if a != nil {
		var x int64
		x, err = types.ToBool(a)
		if err != nil {
			return nil, o.traceErr(err)
		} else if x == 0 {
			// false && any other types is false
			return x, nil
		}
	}

	b, err := o.R.Eval(ctx, args)
	if err != nil {
		return nil, o.traceErr(err)
	}

	if b != nil {
		var y int64
		y, err = types.ToBool(b)
		if err != nil {
			return nil, o.traceErr(err)
		} else if y == 0 {
			return y, nil
		}
	}

	// here x and y are all not false
	// if a or b is nil
	if a == nil || b == nil {
		return nil, nil
	}

	return int64(1), nil
}
开发者ID:nengwang,项目名称:tidb,代码行数:40,代码来源:binop.go


示例10: EvalBool

// EvalBool evalueates an expression to a boolean value.
func EvalBool(ctx context.Context, expr ast.ExprNode) (bool, error) {
	val, err := Eval(ctx, expr)
	if err != nil {
		return false, errors.Trace(err)
	}
	if val == nil {
		return false, nil
	}

	i, err := types.ToBool(val)
	if err != nil {
		return false, errors.Trace(err)
	}
	return i != 0, nil
}
开发者ID:mrtoms,项目名称:tidb,代码行数:16,代码来源:evaluator.go


示例11: EvalBoolExpr

// EvalBoolExpr evaluates an expression and convert its return value to bool.
func EvalBoolExpr(ctx context.Context, expr expression.Expression, m map[interface{}]interface{}) (bool, error) {
	val, err := expr.Eval(ctx, m)
	if err != nil {
		return false, err
	}
	if val == nil {
		return false, nil
	}

	x, err := types.ToBool(val)
	if err != nil {
		return false, err
	}

	return x != 0, nil
}
开发者ID:romanticode,项目名称:tidb,代码行数:17,代码来源:helper.go


示例12: isTruth

func (e *Evaluator) isTruth(v *ast.IsTruthExpr) bool {
	var boolVal bool
	val := v.Expr.GetValue()
	if !types.IsNil(val) {
		ival, err := types.ToBool(val)
		if err != nil {
			e.err = errors.Trace(err)
			return false
		}
		if ival == v.True {
			boolVal = true
		}
	}
	if v.Not {
		boolVal = !boolVal
	}
	v.SetValue(boolToInt64(boolVal))
	return true
}
开发者ID:mrtoms,项目名称:tidb,代码行数:19,代码来源:evaluator.go


示例13: evalAndCheck

// Check if satisified the target condition.
// If satisified, the second returned value is true.
func (w *WhenClause) evalAndCheck(ctx context.Context, args map[interface{}]interface{}, target interface{}) (interface{}, bool, error) {
	o := NewBinaryOperation(opcode.EQ, &Value{target}, w.Expr)

	// types.Compare wil return true/false for NULL
	// We must use BinaryOperation with opcode.Eq
	eq, err := o.Eval(ctx, args)
	if err != nil {
		return nil, false, err
	}
	if eq == nil {
		return nil, false, err
	}
	beq, err := types.ToBool(eq)
	if beq == 0 || err != nil {
		return nil, false, err
	}
	rv, err := w.Result.Eval(ctx, args)
	return rv, true, err
}
开发者ID:kingland,项目名称:tidb,代码行数:21,代码来源:case.go


示例14: checkArgs

func checkArgs(args ...interface{}) error {
	for i, v := range args {
		switch v.(type) {
		case bool:
			// We do not handle bool as int8 in tidb.
			vv, err := types.ToBool(v)
			if err != nil {
				return errors.Trace(err)
			}
			args[i] = vv
		case nil, float32, float64, string,
			int8, int16, int32, int64, int,
			uint8, uint16, uint32, uint64, uint,
			[]byte, time.Duration, time.Time:
		default:
			return errors.Errorf("cannot use arg[%d] (type %T):unsupported type", i, v)
		}
	}
	return nil
}
开发者ID:ninefive,项目名称:tidb,代码行数:20,代码来源:session.go


示例15: planStatic

func (r *WhereRset) planStatic(ctx context.Context, e expression.Expression) (plan.Plan, error) {
	val, err := e.Eval(nil, nil)
	if err != nil {
		return nil, err
	}
	if val == nil {
		// like `select * from t where null`.
		return &plans.NullPlan{Fields: r.Src.GetFields()}, nil
	}

	n, err := types.ToBool(val)
	if err != nil {
		return nil, err
	}
	if n == 0 {
		// like `select * from t where 0`.
		return &plans.NullPlan{Fields: r.Src.GetFields()}, nil
	}

	return &plans.FilterDefaultPlan{Plan: r.Src, Expr: e}, nil
}
开发者ID:netroby,项目名称:tidb,代码行数:21,代码来源:where.go


示例16: builtinIf

// See https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#function_if
func builtinIf(args []interface{}, m map[interface{}]interface{}) (interface{}, error) {
	// if(expr1, expr2, expr3)
	// if expr1 is true, return expr2, otherwise, return expr3
	v1 := args[0]
	v2 := args[1]
	v3 := args[2]

	if types.IsNil(v1) {
		return v3, nil
	}

	b, err := types.ToBool(v1)
	if err != nil {
		return nil, err
	}

	// TODO: check return type, must be numeric or string
	if b == 1 {
		return v2, nil
	}

	return v3, nil
}
开发者ID:lovedboy,项目名称:tidb,代码行数:24,代码来源:control.go


示例17: TestComparisonOp

func (s *testBinOpSuite) TestComparisonOp(c *C) {
	tbl := []struct {
		lhs    interface{}
		op     opcode.Op
		rhs    interface{}
		result int8 // 0 for false, 1 for true
	}{
		// test EQ
		{1, opcode.EQ, 2, 0},
		{false, opcode.EQ, false, 1},
		{false, opcode.EQ, true, 0},
		{true, opcode.EQ, true, 1},
		{true, opcode.EQ, false, 0},
		{"1", opcode.EQ, true, 1},
		{"1", opcode.EQ, false, 0},

		// test NEQ
		{1, opcode.NE, 2, 1},
		{false, opcode.NE, false, 0},
		{false, opcode.NE, true, 1},
		{true, opcode.NE, true, 0},
		{"1", opcode.NE, true, 0},
		{"1", opcode.NE, false, 1},

		// test GT, GE
		{1, opcode.GT, 0, 1},
		{1, opcode.GT, 1, 0},
		{1, opcode.GE, 1, 1},
		{3.14, opcode.GT, 3, 1},
		{3.14, opcode.GE, 3.14, 1},

		// test LT, LE
		{1, opcode.LT, 2, 1},
		{1, opcode.LT, 1, 0},
		{1, opcode.LE, 1, 1},
	}

	for _, t := range tbl {
		expr := NewBinaryOperation(t.op, Value{t.lhs}, Value{t.rhs})
		v, err := expr.Eval(nil, nil)
		c.Assert(err, IsNil)
		val, err := types.ToBool(v)
		c.Assert(err, IsNil)
		c.Assert(val, Equals, t.result)
	}

	// test nil
	nilTbl := []struct {
		lhs interface{}
		op  opcode.Op
		rhs interface{}
	}{
		{nil, opcode.EQ, nil},
		{nil, opcode.EQ, 1},
		{nil, opcode.NE, nil},
		{nil, opcode.NE, 1},
		{nil, opcode.LT, nil},
		{nil, opcode.LT, 1},
		{nil, opcode.LE, nil},
		{nil, opcode.LE, 1},
		{nil, opcode.GT, nil},
		{nil, opcode.GT, 1},
		{nil, opcode.GE, nil},
		{nil, opcode.GE, 1},
	}

	for _, t := range nilTbl {
		expr := NewBinaryOperation(t.op, Value{t.lhs}, Value{t.rhs})
		v, err := expr.Eval(nil, nil)
		c.Assert(err, IsNil)
		c.Assert(v, IsNil)
	}

	// test evalCompare function
	cmpTbl := []struct {
		lhs interface{}
		rhs interface{}
		ret int // 0, 1, -1
	}{
		{float64(1), float64(1), 0},
		{float64(1), "1", 0},
		{int64(1), int64(1), 0},
		{int64(-1), uint64(1), -1},
		{int64(-1), "-1", 0},
		{uint64(1), uint64(1), 0},
		{uint64(1), int64(-1), 1},
		{uint64(1), "1", 0},
		{mysql.NewDecimalFromInt(1, 0), mysql.NewDecimalFromInt(1, 0), 0},
		{mysql.NewDecimalFromInt(1, 0), "1", 0},
		{"1", "1", 0},
		{"1", int64(-1), 1},
		{"1", float64(2), -1},
		{"1", uint64(1), 0},
		{"1", mysql.NewDecimalFromInt(1, 0), 0},
		{"2011-01-01 11:11:11", mysql.Time{time.Now(), mysql.TypeDatetime, 0}, -1},
		{"12:00:00", mysql.ZeroDuration, 1},
		{mysql.ZeroDuration, mysql.ZeroDuration, 0},
		{mysql.Time{time.Now().Add(time.Second * 10), mysql.TypeDatetime, 0},
			mysql.Time{time.Now(), mysql.TypeDatetime, 0}, 1},
	}
//.........这里部分代码省略.........
开发者ID:npk,项目名称:tidb,代码行数:101,代码来源:binop_test.go


示例18: TestCompSubQuery


//.........这里部分代码省略.........
		{nil, opcode.LT, []interface{}{1, 2}, false, nil},
		{0, opcode.LT, []interface{}{1, 2}, false, 1},
		{0, opcode.LT, []interface{}{1, 2, nil}, false, 1},
		{1, opcode.LT, []interface{}{1, 2}, false, 1},
		{2, opcode.LT, []interface{}{1, 2}, false, 0},
		{2, opcode.LT, []interface{}{1, 2, nil}, false, nil},
		{3, opcode.LT, []interface{}{1, 2}, false, 0},
		{nil, opcode.LE, []interface{}{1, 2}, false, nil},
		{0, opcode.LE, []interface{}{1, 2}, false, 1},
		{0, opcode.LE, []interface{}{1, 2, nil}, false, 1},
		{1, opcode.LE, []interface{}{1, 2}, false, 1},
		{2, opcode.LE, []interface{}{1, 2}, false, 1},
		{3, opcode.LE, []interface{}{1, 2}, false, 0},
		{3, opcode.LE, []interface{}{1, 2, nil}, false, nil},

		// Test all subquery.
		{nil, opcode.EQ, []interface{}{1, 2}, true, nil},
		{0, opcode.EQ, []interface{}{1, 2}, true, 0},
		{0, opcode.EQ, []interface{}{1, 2, nil}, true, 0},
		{1, opcode.EQ, []interface{}{1, 2}, true, 0},
		{1, opcode.EQ, []interface{}{1, 2, nil}, true, 0},
		{1, opcode.EQ, []interface{}{1, 1}, true, 1},
		{1, opcode.EQ, []interface{}{1, 1, nil}, true, nil},
		{nil, opcode.NE, []interface{}{1, 2}, true, nil},
		{0, opcode.NE, []interface{}{1, 2}, true, 1},
		{1, opcode.NE, []interface{}{1, 2, nil}, true, 0},
		{1, opcode.NE, []interface{}{1, 1}, true, 0},
		{1, opcode.NE, []interface{}{1, 1, nil}, true, 0},
		{nil, opcode.GT, []interface{}{1, 2}, true, nil},
		{1, opcode.GT, []interface{}{1, 2}, true, 0},
		{1, opcode.GT, []interface{}{1, 2, nil}, true, 0},
		{2, opcode.GT, []interface{}{1, 2}, true, 0},
		{2, opcode.GT, []interface{}{1, 2, nil}, true, 0},
		{3, opcode.GT, []interface{}{1, 2}, true, 1},
		{3, opcode.GT, []interface{}{1, 2, nil}, true, nil},
		{nil, opcode.GE, []interface{}{1, 2}, true, nil},
		{0, opcode.GE, []interface{}{1, 2}, true, 0},
		{0, opcode.GE, []interface{}{1, 2, nil}, true, 0},
		{1, opcode.GE, []interface{}{1, 2}, true, 0},
		{1, opcode.GE, []interface{}{1, 2, nil}, true, 0},
		{2, opcode.GE, []interface{}{1, 2}, true, 1},
		{3, opcode.GE, []interface{}{1, 2}, true, 1},
		{3, opcode.GE, []interface{}{1, 2, nil}, true, nil},
		{nil, opcode.LT, []interface{}{1, 2}, true, nil},
		{0, opcode.LT, []interface{}{1, 2}, true, 1},
		{0, opcode.LT, []interface{}{1, 2, nil}, true, nil},
		{1, opcode.LT, []interface{}{1, 2}, true, 0},
		{2, opcode.LT, []interface{}{1, 2}, true, 0},
		{2, opcode.LT, []interface{}{1, 2, nil}, true, 0},
		{3, opcode.LT, []interface{}{1, 2}, true, 0},
		{nil, opcode.LE, []interface{}{1, 2}, true, nil},
		{0, opcode.LE, []interface{}{1, 2}, true, 1},
		{0, opcode.LE, []interface{}{1, 2, nil}, true, nil},
		{1, opcode.LE, []interface{}{1, 2}, true, 1},
		{2, opcode.LE, []interface{}{1, 2}, true, 0},
		{3, opcode.LE, []interface{}{1, 2}, true, 0},
		{3, opcode.LE, []interface{}{1, 2, nil}, true, 0},
	}

	for _, t := range tbl {
		lhs := convert(t.lhs)

		rhs := make([][]interface{}, 0, len(t.rhs))
		for _, v := range t.rhs {
			rhs = append(rhs, []interface{}{convert(v)})
		}

		sq := newMockSubQuery(rhs, []string{"c"})
		expr := NewCompareSubQuery(t.op, Value{lhs}, sq, t.all)

		c.Assert(expr.IsStatic(), IsFalse)

		str := expr.String()
		c.Assert(len(str), Greater, 0)

		v, err := expr.Eval(nil, nil)
		c.Assert(err, IsNil)

		switch x := t.result.(type) {
		case nil:
			c.Assert(v, IsNil)
		case int:
			val, err := types.ToBool(v)
			c.Assert(err, IsNil)
			c.Assert(val, Equals, int64(x))
		}
	}

	// Test error.
	sq := newMockSubQuery([][]interface{}{{1, 2}}, []string{"c1", "c2"})
	expr := NewCompareSubQuery(opcode.EQ, Value{1}, sq, true)

	_, err := expr.Eval(nil, nil)
	c.Assert(err, NotNil)

	expr = NewCompareSubQuery(opcode.EQ, Value{1}, sq, false)

	_, err = expr.Eval(nil, nil)
	c.Assert(err, NotNil)
}
开发者ID:hulunbier,项目名称:tidb,代码行数:101,代码来源:cmp_subquery_test.go


示例19: unaryOperation

func (e *Evaluator) unaryOperation(u *ast.UnaryOperationExpr) bool {
	defer func() {
		if er := recover(); er != nil {
			e.err = errors.Errorf("%v", er)
		}
	}()
	a := u.V.GetValue()
	a = types.RawData(a)
	if a == nil {
		u.SetValue(nil)
		return true
	}
	switch op := u.Op; op {
	case opcode.Not:
		n, err := types.ToBool(a)
		if err != nil {
			e.err = errors.Trace(err)
		} else if n == 0 {
			u.SetValue(int64(1))
		} else {
			u.SetValue(int64(0))
		}
	case opcode.BitNeg:
		// for bit operation, we will use int64 first, then return uint64
		n, err := types.ToInt64(a)
		if err != nil {
			e.err = errors.Trace(err)
			return false
		}
		u.SetValue(uint64(^n))
	case opcode.Plus:
		switch x := a.(type) {
		case bool:
			u.SetValue(boolToInt64(x))
		case float32:
			u.SetValue(+x)
		case float64:
			u.SetValue(+x)
		case int:
			u.SetValue(+x)
		case int8:
			u.SetValue(+x)
		case int16:
			u.SetValue(+x)
		case int32:
			u.SetValue(+x)
		case int64:
			u.SetValue(+x)
		case uint:
			u.SetValue(+x)
		case uint8:
			u.SetValue(+x)
		case uint16:
			u.SetValue(+x)
		case uint32:
			u.SetValue(+x)
		case uint64:
			u.SetValue(+x)
		case mysql.Duration:
			u.SetValue(x)
		case mysql.Time:
			u.SetValue(x)
		case string:
			u.SetValue(x)
		case mysql.Decimal:
			u.SetValue(x)
		case []byte:
			u.SetValue(x)
		case mysql.Hex:
			u.SetValue(x)
		case mysql.Bit:
			u.SetValue(x)
		case mysql.Enum:
			u.SetValue(x)
		case mysql.Set:
			u.SetValue(x)
		default:
			e.err = ErrInvalidOperation
			return false
		}
	case opcode.Minus:
		switch x := a.(type) {
		case bool:
			if x {
				u.SetValue(int64(-1))
			} else {
				u.SetValue(int64(0))
			}
		case float32:
			u.SetValue(-x)
		case float64:
			u.SetValue(-x)
		case int:
			u.SetValue(-x)
		case int8:
			u.SetValue(-x)
		case int16:
			u.SetValue(-x)
		case int32:
			u.SetValue(-x)
//.........这里部分代码省略.........
开发者ID:mrtoms,项目名称:tidb,代码行数:101,代码来源:evaluator.go


示例20: TestBinopComparison

func (s *testEvaluatorSuite) TestBinopComparison(c *C) {
	ctx := mock.NewContext()
	tbl := []struct {
		lhs    interface{}
		op     opcode.Op
		rhs    interface{}
		result int64 // 0 for false, 1 for true
	}{
		// test EQ
		{1, opcode.EQ, 2, 0},
		{false, opcode.EQ, false, 1},
		{false, opcode.EQ, true, 0},
		{true, opcode.EQ, true, 1},
		{true, opcode.EQ, false, 0},
		{"1", opcode.EQ, true, 1},
		{"1", opcode.EQ, false, 0},

		// test NEQ
		{1, opcode.NE, 2, 1},
		{false, opcode.NE, false, 0},
		{false, opcode.NE, true, 1},
		{true, opcode.NE, true, 0},
		{"1", opcode.NE, true, 0},
		{"1", opcode.NE, false, 1},

		// test GT, GE
		{1, opcode.GT, 0, 1},
		{1, opcode.GT, 1, 0},
		{1, opcode.GE, 1, 1},
		{3.14, opcode.GT, 3, 1},
		{3.14, opcode.GE, 3.14, 1},

		// test LT, LE
		{1, opcode.LT, 2, 1},
		{1, opcode.LT, 1, 0},
		{1, opcode.LE, 1, 1},
	}
	for _, t := range tbl {
		expr := &ast.BinaryOperationExpr{Op: t.op, L: ast.NewValueExpr(t.lhs), R: ast.NewValueExpr(t.rhs)}
		v, err := Eval(ctx, expr)
		c.Assert(err, IsNil)
		val, err := types.ToBool(v)
		c.Assert(err, IsNil)
		c.Assert(val, Equals, t.result)
	}

	// test nil
	nilTbl := []struct {
		lhs interface{}
		op  opcode.Op
		rhs interface{}
	}{
		{nil, opcode.EQ, nil},
		{nil, opcode.EQ, 1},
		{nil, opcode.NE, nil},
		{nil, opcode.NE, 1},
		{nil, opcode.LT, nil},
		{nil, opcode.LT, 1},
		{nil, opcode.LE, nil},
		{nil, opcode.LE, 1},
		{nil, opcode.GT, nil},
		{nil, opcode.GT, 1},
		{nil, opcode.GE, nil},
		{nil, opcode.GE, 1},
	}

	for _, t := range nilTbl {
		expr := &ast.BinaryOperationExpr{Op: t.op, L: ast.NewValueExpr(t.lhs), R: ast.NewValueExpr(t.rhs)}
		v, err := Eval(ctx, expr)
		c.Assert(err, IsNil)
		c.Assert(v, IsNil)
	}
}
开发者ID:mumubusu,项目名称:tidb,代码行数:73,代码来源:evaluator_test.go



注:本文中的github.com/pingcap/tidb/util/types.ToBool函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang types.ToDecimal函数代码示例发布时间:2022-05-28
下一篇:
Golang types.StrToFloat函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap