本文整理汇总了Golang中github.com/pingcap/tidb/util/types.ToBool函数的典型用法代码示例。如果您正苦于以下问题:Golang ToBool函数的具体用法?Golang ToBool怎么用?Golang ToBool使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToBool函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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)
})
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}