本文整理汇总了Golang中github.com/pingcap/tidb/util/types.Compare函数的典型用法代码示例。如果您正苦于以下问题:Golang Compare函数的具体用法?Golang Compare怎么用?Golang Compare使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Compare函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleComparisonOp
func (e *Evaluator) handleComparisonOp(o *ast.BinaryOperationExpr) bool {
a, b := types.Coerce(o.L.GetValue(), o.R.GetValue())
if types.IsNil(a) || types.IsNil(b) {
// for <=>, if a and b are both nil, return true.
// if a or b is nil, return false.
if o.Op == opcode.NullEQ {
if types.IsNil(a) || types.IsNil(b) {
o.SetValue(oneI64)
} else {
o.SetValue(zeroI64)
}
} else {
o.SetValue(nil)
}
return true
}
n, err := types.Compare(a, b)
if err != nil {
e.err = errors.Trace(err)
return false
}
r, err := getCompResult(o.Op, n)
if err != nil {
e.err = errors.Trace(err)
return false
}
if r {
o.SetValue(oneI64)
} else {
o.SetValue(zeroI64)
}
return true
}
示例2: Less
// Less implements sort.Interface Less interface.
func (t *orderByTable) Less(i, j int) bool {
for index, asc := range t.Ascs {
v1 := t.Rows[i].Key[index]
v2 := t.Rows[j].Key[index]
ret, err := types.Compare(v1, v2)
if err != nil {
// we just have to log this error and skip it.
// TODO: record this error and handle it out later.
log.Errorf("compare %v %v err %v", v1, v2, err)
}
if !asc {
ret = -ret
}
if ret < 0 {
return true
} else if ret > 0 {
return false
}
}
return false
}
示例3: replaceRow
func replaceRow(ctx context.Context, t table.Table, handle int64, replaceRow []interface{}) error {
row, err := t.Row(ctx, handle)
if err != nil {
return errors.Trace(err)
}
result := 0
isReplace := false
touched := make(map[int]bool, len(row))
for i, val := range row {
result, err = types.Compare(val, replaceRow[i])
if err != nil {
return errors.Trace(err)
}
if result != 0 {
touched[i] = true
isReplace = true
}
}
if isReplace {
variable.GetSessionVars(ctx).AddAffectedRows(1)
if err = t.UpdateRecord(ctx, handle, row, replaceRow, touched); err != nil {
return errors.Trace(err)
}
}
return nil
}
示例4: caseExpr
func (e *Evaluator) caseExpr(v *ast.CaseExpr) bool {
var target interface{} = true
if v.Value != nil {
target = v.Value.GetValue()
}
if target != nil {
for _, val := range v.WhenClauses {
cmp, err := types.Compare(target, val.Expr.GetValue())
if err != nil {
e.err = errors.Trace(err)
return false
}
if cmp == 0 {
v.SetValue(val.Result.GetValue())
return true
}
}
}
if v.ElseClause != nil {
v.SetValue(v.ElseClause.GetValue())
} else {
v.SetValue(nil)
}
return true
}
示例5: checkAnyResult
func (e *Evaluator) checkAnyResult(cs *ast.CompareSubqueryExpr, lv interface{}, result []interface{}) (interface{}, error) {
hasNull := false
for _, v := range result {
if v == nil {
hasNull = true
continue
}
comRes, err := types.Compare(lv, v)
if err != nil {
return nil, errors.Trace(err)
}
res, err := getCompResult(cs.Op, comRes)
if err != nil {
return nil, errors.Trace(err)
}
if res {
return true, nil
}
}
if hasNull {
// If no matched but we get null, return null.
// Like `insert t (c) values (1),(2),(null)`, then
// `select 0 > any (select c from t)`, returns null.
return nil, nil
}
return false, nil
}
示例6: checkInList
func (e *Evaluator) checkInList(not bool, in interface{}, list []interface{}) (interface{}, error) {
hasNull := false
for _, v := range list {
if types.IsNil(v) {
hasNull = true
continue
}
r, err := types.Compare(in, v)
if err != nil {
return nil, errors.Trace(err)
}
if r == 0 {
return !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 not, nil
}
示例7: indexCompare
// comparison function that takes minNotNullVal and maxVal into account.
func indexCompare(a interface{}, b interface{}) int {
if a == nil && b == nil {
return 0
} else if b == nil {
return 1
} else if b == nil {
return -1
}
// a and b both not nil
if a == minNotNullVal && b == minNotNullVal {
return 0
} else if b == minNotNullVal {
return 1
} else if a == minNotNullVal {
return -1
}
// a and b both not min value
if a == maxVal && b == maxVal {
return 0
} else if a == maxVal {
return 1
} else if b == maxVal {
return -1
}
n, err := types.Compare(a, b)
if err != nil {
// Old compare panics if err, so here we do the same thing now.
// TODO: return err instead of panic.
panic(fmt.Sprintf("should never happend %v", err))
}
return n
}
示例8: evalComparisonOp
// operator: >=, >, <=, <, !=, <>, = <=>, etc.
// see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html
func (o *BinaryOperation) evalComparisonOp(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
//TODO: support <=> later
a, b, err := o.get2(ctx, args)
if err != nil {
return nil, err
}
if a == nil || b == nil {
// TODO: for <=>, if a and b are both nil, return true
return nil, nil
}
n, err := types.Compare(a, b)
if err != nil {
return nil, o.traceErr(err)
}
switch o.Op {
case opcode.LT:
return n < 0, nil
case opcode.LE:
return n <= 0, nil
case opcode.GE:
return n >= 0, nil
case opcode.GT:
return n > 0, nil
case opcode.EQ:
return n == 0, nil
case opcode.NE:
return n != 0, nil
default:
return nil, o.errorf("invalid op %v in comparision operation", o.Op)
}
}
示例9: checkInList
func (n *PatternIn) checkInList(in interface{}, list []interface{}) (interface{}, error) {
hasNull := false
for _, v := range list {
if types.IsNil(v) {
hasNull = true
continue
}
r, err := types.Compare(in, v)
if err != nil {
return nil, err
}
if r == 0 {
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
}
示例10: evalComparisonOp
// operator: >=, >, <=, <, !=, <>, = <=>, etc.
// see https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html
func (o *BinaryOperation) evalComparisonOp(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
//TODO: support <=> later
a, b, err := o.get2(ctx, args)
if err != nil {
return nil, err
}
if a == nil || b == nil {
// for <=>, if a and b are both nil, return true.
// if a or b is nil, return false.
if o.Op == opcode.NullEQ {
return a == b, nil
}
return nil, nil
}
n, err := types.Compare(a, b)
if err != nil {
return nil, o.traceErr(err)
}
r, err := getCompResult(o.Op, n)
if err != nil {
return nil, o.errorf(err.Error())
}
return r, nil
}
示例11: updateMaxMin
func (n *AggregateFuncExpr) updateMaxMin(max bool) error {
ctx := n.GetContext()
if len(n.Args) != 1 {
return errors.New("Wrong number of args for AggFuncFirstRow")
}
v := n.Args[0].GetValue()
if !ctx.evaluated {
ctx.Value = v
ctx.evaluated = true
return nil
}
c, err := types.Compare(ctx.Value, v)
if err != nil {
return errors.Trace(err)
}
if max {
if c == -1 {
ctx.Value = v
}
} else {
if c == 1 {
ctx.Value = v
}
}
return nil
}
示例12: patternIn
func (e *Evaluator) patternIn(n *ast.PatternInExpr) bool {
lhs := n.Expr.GetValue()
if types.IsNil(lhs) {
n.SetValue(nil)
return true
}
hasNull := false
for _, v := range n.List {
if types.IsNil(v.GetValue()) {
hasNull = true
continue
}
r, err := types.Compare(n.Expr.GetValue(), v.GetValue())
if err != nil {
e.err = errors.Trace(err)
return false
}
if r == 0 {
n.SetValue(boolToInt64(!n.Not))
return true
}
}
if hasNull {
// if no matched but we got null in In, return null
// e.g 1 in (null, 2, 3) returns null
n.SetValue(nil)
return true
}
n.SetValue(boolToInt64(n.Not))
return true
}
示例13: indexCompare
// comparison function that takes minNotNullVal and maxVal into account.
func indexCompare(a interface{}, b interface{}) int {
if a == nil && b == nil {
return 0
} else if b == nil {
return 1
} else if b == nil {
return -1
}
// a and b both not nil
if a == minNotNullVal && b == minNotNullVal {
return 0
} else if b == minNotNullVal {
return 1
} else if a == minNotNullVal {
return -1
}
// a and b both not min value
if a == maxVal && b == maxVal {
return 0
} else if a == maxVal {
return 1
} else if b == maxVal {
return -1
}
return types.Compare(a, b)
}
示例14: indexColumnCompare
// comparison function that takes minNotNullVal and maxVal into account.
func indexColumnCompare(a interface{}, b interface{}) (int, error) {
if a == nil && b == nil {
return 0, nil
} else if b == nil {
return 1, nil
} else if a == nil {
return -1, nil
}
// a and b both not nil
if a == plan.MinNotNullVal && b == plan.MinNotNullVal {
return 0, nil
} else if b == plan.MinNotNullVal {
return 1, nil
} else if a == plan.MinNotNullVal {
return -1, nil
}
// a and b both not min value
if a == plan.MaxVal && b == plan.MaxVal {
return 0, nil
} else if a == plan.MaxVal {
return 1, nil
} else if b == plan.MaxVal {
return -1, nil
}
n, err := types.Compare(a, b)
if err != nil {
return 0, errors.Trace(err)
}
return n, nil
}
示例15: Update
// Update implements AggregationFunction interface.
func (mmf *maxMinFunction) Update(row []types.Datum, groupKey []byte, ectx context.Context) error {
ctx := mmf.getContext(groupKey)
if len(mmf.Args) != 1 {
return errors.New("Wrong number of args for AggFuncMaxMin")
}
a := mmf.Args[0]
value, err := a.Eval(row, ectx)
if err != nil {
return errors.Trace(err)
}
if !ctx.Evaluated {
ctx.Value = value.GetValue()
}
if value.GetValue() == nil {
return nil
}
var c int
c, err = types.Compare(ctx.Value, value.GetValue())
if err != nil {
return errors.Trace(err)
}
if (mmf.isMax && c == -1) || (!mmf.isMax && c == 1) {
ctx.Value = value.GetValue()
}
ctx.Evaluated = true
return nil
}