Пример #1
0
// See https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html#operator_div
func (o *BinaryOperation) evalIntDiv(a interface{}, b interface{}) (interface{}, error) {
	switch x := a.(type) {
	case int64:
		switch y := b.(type) {
		case int64:
			if y == 0 {
				return nil, nil
			}
			return x / y, nil
		case uint64:
			if y == 0 {
				return nil, nil
			}
			// For MySQL, if any is unsigned, return unsigned
			// TODO: check overflow
			return uint64(x) / y, nil
		}
	case uint64:
		switch y := b.(type) {
		case int64:
			if y == 0 {
				return nil, nil
			}
			// For MySQL, if any is unsigned, return unsigned
			// TODO: check overflow
			return x / uint64(y), nil
		case uint64:
			if y == 0 {
				return nil, nil
			}
			return x / y, nil
		}
	}

	// if any is none integer, use decimal to calculate
	x, err := types.ToDecimal(a)
	if err != nil {
		return nil, o.traceErr(err)
	}

	y, err := types.ToDecimal(b)
	if err != nil {
		return nil, o.traceErr(err)
	}

	if f, _ := y.Float64(); f == 0 {
		return nil, nil
	}

	return x.Div(y).IntPart(), nil
}
Пример #2
0
// See https://dev.mysql.com/doc/refman/5.7/en/arithmetic-functions.html#operator_div
func (o *BinaryOperation) evalIntDiv(a interface{}, b interface{}) (interface{}, error) {
	switch x := a.(type) {
	case int64:
		switch y := b.(type) {
		case int64:
			if y == 0 {
				return nil, nil
			}
			return types.DivInt64(x, y)
		case uint64:
			if y == 0 {
				return nil, nil
			}
			return types.DivIntWithUint(x, y)
		}
	case uint64:
		switch y := b.(type) {
		case int64:
			if y == 0 {
				return nil, nil
			}
			return types.DivUintWithInt(x, y)
		case uint64:
			if y == 0 {
				return nil, nil
			}
			return x / y, nil
		}
	}

	// if any is none integer, use decimal to calculate
	x, err := types.ToDecimal(a)
	if err != nil {
		return nil, o.traceErr(err)
	}

	y, err := types.ToDecimal(b)
	if err != nil {
		return nil, o.traceErr(err)
	}

	if f, _ := y.Float64(); f == 0 {
		return nil, nil
	}

	return x.Div(y).IntPart(), nil
}
Пример #3
0
func computeIntDiv(a, b interface{}) (interface{}, error) {
	switch x := a.(type) {
	case int64:
		switch y := b.(type) {
		case int64:
			if y == 0 {
				return nil, nil
			}
			return types.DivInt64(x, y)
		case uint64:
			if y == 0 {
				return nil, nil
			}
			return types.DivIntWithUint(x, y)
		}
	case uint64:
		switch y := b.(type) {
		case int64:
			if y == 0 {
				return nil, nil
			}
			return types.DivUintWithInt(x, y)
		case uint64:
			if y == 0 {
				return nil, nil
			}
			return x / y, nil
		}
	}

	// if any is none integer, use decimal to calculate
	x, err := types.ToDecimal(a)
	if err != nil {
		return nil, err
	}

	y, err := types.ToDecimal(b)
	if err != nil {
		return nil, err
	}

	if f, _ := y.Float64(); f == 0 {
		return nil, nil
	}

	return x.Div(y).IntPart(), nil
}
Пример #4
0
func (o *BinaryOperation) evalDiv(a interface{}, b interface{}) (interface{}, error) {
	// MySQL support integer divison Div and division operator /
	// we use opcode.Div for division operator and will use another for integer division later.
	// for division operator, we will use float64 for calculation.
	switch x := a.(type) {
	case float64:
		y, err := types.ToFloat64(b)
		if err != nil {
			return nil, err
		}

		if y == 0 {
			return nil, nil
		}

		return x / y, nil
	default:
		// the scale of the result is the scale of the first operand plus
		// the value of the div_precision_increment system variable (which is 4 by default)
		// we will use 4 here

		xa, err := types.ToDecimal(a)
		if err != nil {
			return nil, o.traceErr(err)
		}

		xb, err := types.ToDecimal(b)
		if err != nil {
			return nil, o.traceErr(err)
		}
		if f, _ := xb.Float64(); f == 0 {
			// division by zero return null
			return nil, nil
		}

		return xa.Div(xb), nil
	}
}