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

Golang types.CoerceDatum函数代码示例

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

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



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

示例1: arithmeticFuncFactory

func arithmeticFuncFactory(op opcode.Op) BuiltinFunc {
	return func(args []types.Datum, _ context.Context) (d types.Datum, err error) {
		a, err := types.CoerceArithmetic(args[0])
		if err != nil {
			return d, errors.Trace(err)
		}

		b, err := types.CoerceArithmetic(args[1])
		if err != nil {
			return d, errors.Trace(err)
		}

		a, b = types.CoerceDatum(a, b)
		if a.IsNull() || b.IsNull() {
			return
		}

		switch op {
		case opcode.Plus:
			return types.ComputePlus(a, b)
		case opcode.Minus:
			return types.ComputeMinus(a, b)
		case opcode.Mul:
			return types.ComputeMul(a, b)
		case opcode.Div:
			return types.ComputeDiv(a, b)
		case opcode.Mod:
			return types.ComputeMod(a, b)
		case opcode.IntDiv:
			return types.ComputeIntDiv(a, b)
		default:
			return d, ErrInvalidOperation.Gen("invalid op %v in arithmetic operation", op)
		}
	}
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:35,代码来源:builtin_other.go


示例2: ComputeBit

// ComputeBit computes the bitwise operation on two datums.
func ComputeBit(op tipb.ExprType, left, right types.Datum) (types.Datum, error) {
	var result types.Datum
	a, err := types.CoerceArithmetic(left)
	if err != nil {
		return result, errors.Trace(err)
	}

	b, err := types.CoerceArithmetic(right)
	if err != nil {
		return result, errors.Trace(err)
	}
	a, b, err = types.CoerceDatum(a, b)
	if err != nil {
		return result, errors.Trace(err)
	}
	if a.IsNull() || b.IsNull() {
		return result, nil
	}

	switch op {
	case tipb.ExprType_BitAnd:
		return types.ComputeBitAnd(a, b)
	case tipb.ExprType_BitOr:
		return types.ComputeBitOr(a, b)
	case tipb.ExprType_BitXor:
		return types.ComputeBitXor(a, b)
	case tipb.ExprType_LeftShift:
		return types.ComputeLeftShift(a, b)
	case tipb.ExprType_RighShift:
		return types.ComputeRightShift(a, b)
	default:
		return result, errors.Errorf("Unknown binop type: %v", op)
	}
}
开发者ID:jmptrader,项目名称:tidb,代码行数:35,代码来源:eval_bit_ops.go


示例3: builtinIn

// See http://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html
func builtinIn(args []types.Datum, _ context.Context) (d types.Datum, err error) {
	if args[0].IsNull() {
		return
	}

	var hasNull bool
	for _, v := range args[1:] {
		if v.IsNull() {
			hasNull = true
			continue
		}

		a, b := types.CoerceDatum(args[0], v)
		ret, err := a.CompareDatum(b)
		if err != nil {
			return d, errors.Trace(err)
		}
		if ret == 0 {
			d.SetInt64(1)
			return d, nil
		}
	}

	if hasNull {
		// If it's no matched but we get null in In, returns null.
		// e.g 1 in (null, 2, 3) returns null.
		return
	}
	d.SetInt64(0)
	return
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:32,代码来源:builtin_other.go


示例4: bitOpFactory

func bitOpFactory(op opcode.Op) BuiltinFunc {
	return func(args []types.Datum, _ context.Context) (d types.Datum, err error) {
		a, b := types.CoerceDatum(args[0], args[1])
		if a.IsNull() || b.IsNull() {
			return
		}

		x, err := a.ToInt64()
		if err != nil {
			return d, errors.Trace(err)
		}

		y, err := b.ToInt64()
		if err != nil {
			return d, errors.Trace(err)
		}

		// use a int64 for bit operator, return uint64
		switch op {
		case opcode.And:
			d.SetUint64(uint64(x & y))
		case opcode.Or:
			d.SetUint64(uint64(x | y))
		case opcode.Xor:
			d.SetUint64(uint64(x ^ y))
		case opcode.RightShift:
			d.SetUint64(uint64(x) >> uint64(y))
		case opcode.LeftShift:
			d.SetUint64(uint64(x) << uint64(y))
		default:
			return d, ErrInvalidOperation.Gen("invalid op %v in bit operation", op)
		}
		return
	}
}
开发者ID:yuyongwei,项目名称:tidb,代码行数:35,代码来源:builtin_other.go


示例5: ComputeArithmetic

// ComputeArithmetic computes the arithmetic operation on two datums.
func ComputeArithmetic(op tipb.ExprType, left types.Datum, right types.Datum) (types.Datum, error) {
	var result types.Datum
	a, err := types.CoerceArithmetic(left)
	if err != nil {
		return result, errors.Trace(err)
	}

	b, err := types.CoerceArithmetic(right)
	if err != nil {
		return result, errors.Trace(err)
	}
	a, b = types.CoerceDatum(a, b)
	if a.IsNull() || b.IsNull() {
		return result, nil
	}

	switch op {
	case tipb.ExprType_Plus:
		return types.ComputePlus(a, b)
	case tipb.ExprType_Div:
		return types.ComputeDiv(a, b)
	default:
		return result, errors.Errorf("Unknown binop type: %v", op)
	}
}
开发者ID:XuHuaiyu,项目名称:tidb,代码行数:26,代码来源:eval.go


示例6: evalArithmetic

func (e *Evaluator) evalArithmetic(expr *tipb.Expr) (types.Datum, error) {
	var result types.Datum
	left, right, err := e.evalTwoChildren(expr)
	if err != nil {
		return result, errors.Trace(err)
	}
	a, err := types.CoerceArithmetic(left)
	if err != nil {
		return result, errors.Trace(err)
	}

	b, err := types.CoerceArithmetic(right)
	if err != nil {
		return result, errors.Trace(err)
	}
	a, b = types.CoerceDatum(a, b)
	if a.IsNull() || b.IsNull() {
		return result, nil
	}

	switch expr.GetTp() {
	case tipb.ExprType_Plus:
		return types.ComputePlus(a, b)
	case tipb.ExprType_Div:
		return types.ComputeDiv(a, b)
	default:
		return result, errors.Errorf("Unknown binop type: %v", expr.GetTp())
	}
}
开发者ID:anywhy,项目名称:tidb,代码行数:29,代码来源:eval.go


示例7: compareFuncFactory

func compareFuncFactory(op opcode.Op) BuiltinFunc {
	return func(args []types.Datum, _ context.Context) (d types.Datum, err error) {
		var a, b = args[0], args[1]
		if op != opcode.NullEQ {
			a, b, err = types.CoerceDatum(a, b)
			if err != nil {
				return d, errors.Trace(err)
			}
		}
		if a.IsNull() || b.IsNull() {
			// for <=>, if a and b are both nil, return true.
			// if a or b is nil, return false.
			if op == opcode.NullEQ {
				if a.IsNull() && b.IsNull() {
					d.SetInt64(oneI64)
				} else {
					d.SetInt64(zeroI64)
				}
			}
			return
		}

		n, err := a.CompareDatum(b)
		if err != nil {
			return d, errors.Trace(err)
		}
		var result bool
		switch op {
		case opcode.LT:
			result = n < 0
		case opcode.LE:
			result = n <= 0
		case opcode.EQ, opcode.NullEQ:
			result = n == 0
		case opcode.GT:
			result = n > 0
		case opcode.GE:
			result = n >= 0
		case opcode.NE:
			result = n != 0
		default:
			return d, ErrInvalidOperation.Gen("invalid op %v in comparison operation", op)
		}
		if result {
			d.SetInt64(oneI64)
		} else {
			d.SetInt64(zeroI64)
		}
		return
	}
}
开发者ID:jmptrader,项目名称:tidb,代码行数:51,代码来源:builtin_other.go


示例8: handleComparisonOp

func (e *Evaluator) handleComparisonOp(o *ast.BinaryOperationExpr) bool {
	var a, b = *o.L.GetDatum(), *o.R.GetDatum()
	var err error
	if o.Op != opcode.NullEQ {
		a, b, err = types.CoerceDatum(e.sc, *o.L.GetDatum(), *o.R.GetDatum())
		if err != nil {
			e.err = errors.Trace(err)
			return false
		}
	}
	if a.IsNull() || b.IsNull() {
		// for <=>, if a and b are both nil, return true.
		// if a or b is nil, return false.
		if o.Op == opcode.NullEQ {
			if a.IsNull() && b.IsNull() {
				o.SetInt64(oneI64)
			} else {
				o.SetInt64(zeroI64)
			}
		} else {
			o.SetNull()
		}
		return true
	}

	n, err := a.CompareDatum(e.sc, 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.SetInt64(oneI64)
	} else {
		o.SetInt64(zeroI64)
	}
	return true
}
开发者ID:pingcap,项目名称:tidb,代码行数:44,代码来源:evaluator_binop.go


示例9: handleArithmeticOp

func (e *Evaluator) handleArithmeticOp(o *ast.BinaryOperationExpr) bool {
	a, err := types.CoerceArithmetic(e.sc, *o.L.GetDatum())
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}
	b, err := types.CoerceArithmetic(e.sc, *o.R.GetDatum())
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}

	a, b, err = types.CoerceDatum(e.sc, a, b)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}
	if a.IsNull() || b.IsNull() {
		o.SetNull()
		return true
	}

	var result types.Datum
	switch o.Op {
	case opcode.Plus:
		result, e.err = types.ComputePlus(a, b)
	case opcode.Minus:
		result, e.err = types.ComputeMinus(a, b)
	case opcode.Mul:
		result, e.err = types.ComputeMul(a, b)
	case opcode.Div:
		result, e.err = types.ComputeDiv(e.sc, a, b)
	case opcode.Mod:
		result, e.err = types.ComputeMod(e.sc, a, b)
	case opcode.IntDiv:
		result, e.err = types.ComputeIntDiv(e.sc, a, b)
	default:
		e.err = ErrInvalidOperation.Gen("invalid op %v in arithmetic operation", o.Op)
		return false
	}
	o.SetDatum(result)
	return e.err == nil
}
开发者ID:pingcap,项目名称:tidb,代码行数:43,代码来源:evaluator_binop.go


示例10: handleBitOp

func (e *Evaluator) handleBitOp(o *ast.BinaryOperationExpr) bool {
	a, b, err := types.CoerceDatum(e.sc, *o.L.GetDatum(), *o.R.GetDatum())
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}
	if a.IsNull() || b.IsNull() {
		o.SetNull()
		return true
	}

	x, err := a.ToInt64(e.sc)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}

	y, err := b.ToInt64(e.sc)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}

	// use a int64 for bit operator, return uint64
	switch o.Op {
	case opcode.And:
		o.SetUint64(uint64(x & y))
	case opcode.Or:
		o.SetUint64(uint64(x | y))
	case opcode.Xor:
		o.SetUint64(uint64(x ^ y))
	case opcode.RightShift:
		o.SetUint64(uint64(x) >> uint64(y))
	case opcode.LeftShift:
		o.SetUint64(uint64(x) << uint64(y))
	default:
		e.err = ErrInvalidOperation.Gen("invalid op %v in bit operation", o.Op)
		return false
	}
	return true
}
开发者ID:pingcap,项目名称:tidb,代码行数:41,代码来源:evaluator_binop.go


示例11: checkInList

func (e *Evaluator) checkInList(not bool, in types.Datum, list []types.Datum) (d types.Datum) {
	hasNull := false
	for _, v := range list {
		if v.IsNull() {
			hasNull = true
			continue
		}

		a, b, err := types.CoerceDatum(in, v)
		if err != nil {
			e.err = errors.Trace(err)
			return d
		}
		r, err := a.CompareDatum(b)
		if err != nil {
			e.err = errors.Trace(err)
			return d
		}
		if r == 0 {
			if !not {
				d.SetInt64(1)
				return d
			}
			d.SetInt64(0)
			return d
		}
	}

	if hasNull {
		// If no matched but we got null in In, return null.
		// e.g 1 in (null, 2, 3) returns null.
		return d
	}
	if not {
		d.SetInt64(1)
		return d
	}
	d.SetInt64(0)
	return d
}
开发者ID:yangxuanjia,项目名称:tidb,代码行数:40,代码来源:evaluator.go


示例12: ComputeArithmetic

// ComputeArithmetic computes the arithmetic operation on two datums.
func ComputeArithmetic(sc *variable.StatementContext, op tipb.ExprType, left types.Datum, right types.Datum) (types.Datum, error) {
	var result types.Datum
	a, err := types.CoerceArithmetic(sc, left)
	if err != nil {
		return result, errors.Trace(err)
	}

	b, err := types.CoerceArithmetic(sc, right)
	if err != nil {
		return result, errors.Trace(err)
	}
	a, b, err = types.CoerceDatum(sc, a, b)
	if err != nil {
		return result, errors.Trace(err)
	}
	if a.IsNull() || b.IsNull() {
		return result, nil
	}

	switch op {
	case tipb.ExprType_Plus:
		return types.ComputePlus(a, b)
	case tipb.ExprType_Div:
		return types.ComputeDiv(sc, a, b)
	case tipb.ExprType_Minus:
		return types.ComputeMinus(a, b)
	case tipb.ExprType_Mul:
		return types.ComputeMul(a, b)
	case tipb.ExprType_IntDiv:
		return types.ComputeIntDiv(sc, a, b)
	case tipb.ExprType_Mod:
		return types.ComputeMod(sc, a, b)
	default:
		return result, errors.Errorf("Unknown binop type: %v", op)
	}
}
开发者ID:pingcap,项目名称:tidb,代码行数:37,代码来源:eval_arithmetic_ops.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang types.Compare函数代码示例发布时间:2022-05-28
下一篇:
Golang types.Coerce函数代码示例发布时间: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