Example #1
0
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
}