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 }
func (e *Evaluator) handleLogicOperation(o *ast.BinaryOperationExpr) bool { leftVal, err := types.Convert(o.L.GetValue(), o.GetType()) if err != nil { e.err = err return false } rightVal, err := types.Convert(o.R.GetValue(), o.GetType()) if err != nil { e.err = err return false } if leftVal == nil || rightVal == nil { o.SetValue(nil) return true } var boolVal bool switch o.Op { case opcode.AndAnd: boolVal = leftVal != zeroI64 && rightVal != zeroI64 case opcode.OrOr: boolVal = leftVal != zeroI64 || rightVal != zeroI64 case opcode.LogicXor: boolVal = (leftVal == zeroI64 && rightVal != zeroI64) || (leftVal != zeroI64 && rightVal == zeroI64) default: panic("should never happen") } if boolVal { o.SetValue(oneI64) } else { o.SetValue(zeroI64) } return true }
func (e *Evaluator) handleOrOr(o *ast.BinaryOperationExpr) bool { leftDatum := o.L.GetDatum() if !leftDatum.IsNull() { x, err := leftDatum.ToBool(e.sc) if err != nil { e.err = errors.Trace(err) return false } else if x == 1 { // true || any other types is true. o.SetInt64(x) return true } } righDatum := o.R.GetDatum() if !righDatum.IsNull() { y, err := righDatum.ToBool(e.sc) if err != nil { e.err = errors.Trace(err) return false } else if y == 1 { o.SetInt64(y) return true } } if leftDatum.IsNull() || righDatum.IsNull() { o.SetNull() return true } o.SetInt64(int64(0)) return true }
func (e *Evaluator) handleAndAnd(o *ast.BinaryOperationExpr) bool { leftDatum := o.L.GetDatum() rightDatum := o.R.GetDatum() if leftDatum.Kind() != types.KindNull { x, err := leftDatum.ToBool() if err != nil { e.err = errors.Trace(err) return false } else if x == 0 { // false && any other types is false o.SetInt64(x) return true } } if rightDatum.Kind() != types.KindNull { y, err := rightDatum.ToBool() if err != nil { e.err = errors.Trace(err) return false } else if y == 0 { o.SetInt64(y) return true } } if leftDatum.Kind() == types.KindNull || rightDatum.Kind() == types.KindNull { o.SetNull() return true } o.SetInt64(int64(1)) return true }
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 }
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 }
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 }
func (e *Evaluator) handleArithmeticOp(o *ast.BinaryOperationExpr) bool { a, err := coerceArithmetic(o.L.GetValue()) if err != nil { e.err = err return false } b, err := coerceArithmetic(o.R.GetValue()) if err != nil { e.err = err return false } a, b = types.Coerce(a, b) if a == nil || b == nil { // TODO: for <=>, if a and b are both nil, return true o.SetValue(nil) return true } // TODO: support logic division DIV var result interface{} switch o.Op { case opcode.Plus: result, e.err = computePlus(a, b) case opcode.Minus: result, e.err = computeMinus(a, b) case opcode.Mul: result, e.err = computeMul(a, b) case opcode.Div: result, e.err = computeDiv(a, b) case opcode.Mod: result, e.err = computeMod(a, b) case opcode.IntDiv: result, e.err = computeIntDiv(a, b) default: e.err = errors.Errorf("invalid op %v in arithmetic operation", o.Op) return false } o.SetValue(result) return e.err == nil }
func (e *Evaluator) handleXor(o *ast.BinaryOperationExpr) bool { leftDatum := o.L.GetDatum() righDatum := o.R.GetDatum() if leftDatum.IsNull() || righDatum.IsNull() { o.SetNull() return true } x, err := leftDatum.ToBool(e.sc) if err != nil { e.err = errors.Trace(err) return false } y, err := righDatum.ToBool(e.sc) if err != nil { e.err = errors.Trace(err) return false } if x == y { o.SetInt64(int64(0)) } else { o.SetInt64(int64(1)) } return true }
func (v *typeInferrer) binaryOperation(x *ast.BinaryOperationExpr) { switch x.Op { case opcode.AndAnd, opcode.OrOr, opcode.LogicXor: x.Type = types.NewFieldType(mysql.TypeLonglong) case opcode.LT, opcode.LE, opcode.GE, opcode.GT, opcode.EQ, opcode.NE, opcode.NullEQ: x.Type = types.NewFieldType(mysql.TypeLonglong) case opcode.RightShift, opcode.LeftShift, opcode.And, opcode.Or, opcode.Xor: x.Type = types.NewFieldType(mysql.TypeLonglong) x.Type.Flag |= mysql.UnsignedFlag case opcode.IntDiv: x.Type = types.NewFieldType(mysql.TypeLonglong) case opcode.Plus, opcode.Minus, opcode.Mul, opcode.Mod: if x.L.GetType() != nil && x.R.GetType() != nil { xTp := mergeArithType(x.L.GetType().Tp, x.R.GetType().Tp) x.Type = types.NewFieldType(xTp) leftUnsigned := x.L.GetType().Flag & mysql.UnsignedFlag rightUnsigned := x.R.GetType().Flag & mysql.UnsignedFlag // If both operands are unsigned, result is unsigned. x.Type.Flag |= (leftUnsigned & rightUnsigned) } case opcode.Div: if x.L.GetType() != nil && x.R.GetType() != nil { xTp := mergeArithType(x.L.GetType().Tp, x.R.GetType().Tp) if xTp == mysql.TypeLonglong { xTp = mysql.TypeDecimal } x.Type = types.NewFieldType(xTp) } } x.Type.Charset = charset.CharsetBin x.Type.Collate = charset.CollationBin }
func (e *Evaluator) handleArithmeticOp(o *ast.BinaryOperationExpr) bool { a, err := coerceArithmetic(types.RawData(o.L.GetValue())) if err != nil { e.err = errors.Trace(err) return false } b, err := coerceArithmetic(types.RawData(o.R.GetValue())) if err != nil { e.err = errors.Trace(err) return false } a, b = types.Coerce(a, b) if a == nil || b == nil { o.SetValue(nil) return true } var result interface{} switch o.Op { case opcode.Plus: result, e.err = computePlus(a, b) case opcode.Minus: result, e.err = computeMinus(a, b) case opcode.Mul: result, e.err = computeMul(a, b) case opcode.Div: result, e.err = computeDiv(a, b) case opcode.Mod: result, e.err = computeMod(a, b) case opcode.IntDiv: result, e.err = computeIntDiv(a, b) default: e.err = ErrInvalidOperation.Gen("invalid op %v in arithmetic operation", o.Op) return false } o.SetValue(result) return e.err == nil }
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 }
func (e *Evaluator) binaryOperation(o *ast.BinaryOperationExpr) bool { // all operands must have same column. if e.err = hasSameColumnCount(o.L, o.R); e.err != nil { return false } // row constructor only supports comparison operation. switch o.Op { case opcode.LT, opcode.LE, opcode.GE, opcode.GT, opcode.EQ, opcode.NE, opcode.NullEQ: default: if !checkAllOneColumn(o.L) { e.err = errors.Errorf("Operand should contain 1 column(s)") return false } } leftVal, err := types.Convert(o.L.GetValue(), o.GetType()) if err != nil { e.err = err return false } rightVal, err := types.Convert(o.R.GetValue(), o.GetType()) if err != nil { e.err = err return false } if leftVal == nil || rightVal == nil { o.SetValue(nil) return true } switch o.Op { case opcode.AndAnd, opcode.OrOr, opcode.LogicXor: return e.handleLogicOperation(o) case opcode.LT, opcode.LE, opcode.GE, opcode.GT, opcode.EQ, opcode.NE, opcode.NullEQ: return e.handleComparisonOp(o) case opcode.RightShift, opcode.LeftShift, opcode.And, opcode.Or, opcode.Xor: // TODO: MySQL doesn't support and not, we should remove it later. return e.handleBitOp(o) case opcode.Plus, opcode.Minus, opcode.Mod, opcode.Div, opcode.Mul, opcode.IntDiv: return e.handleArithmeticOp(o) default: panic("should never happen") } }
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 }
func (e *Evaluator) handleBitOp(o *ast.BinaryOperationExpr) bool { a, b := types.Coerce(o.L.GetValue(), o.R.GetValue()) if types.IsNil(a) || types.IsNil(b) { o.SetValue(nil) return true } x, err := types.ToInt64(a) if err != nil { e.err = errors.Trace(err) return false } y, err := types.ToInt64(b) 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.SetValue(uint64(x & y)) case opcode.Or: o.SetValue(uint64(x | y)) case opcode.Xor: o.SetValue(uint64(x ^ y)) case opcode.RightShift: o.SetValue(uint64(x) >> uint64(y)) case opcode.LeftShift: o.SetValue(uint64(x) << uint64(y)) default: e.err = ErrInvalidOperation.Gen("invalid op %v in bit operation", o.Op) return false } return true }