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