Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: round function for int should use round half up rule #27128

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion expression/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (b *builtinRoundWithFracIntSig) evalInt(row chunk.Row) (int64, bool, error)
if isNull || err != nil {
return 0, isNull, err
}
return int64(types.Round(float64(val), int(frac))), false, nil
return int64(types.Round(float64(val), int(frac), types.RoundHalfUp)), false, nil
}

type builtinRoundWithFracDecSig struct {
Expand Down
3 changes: 3 additions & 0 deletions expression/builtin_math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ func (s *testEvaluatorSuite) TestRound(c *C) {
{[]interface{}{-1.5, 0}, -2},
{[]interface{}{1.5, 0}, 2},
{[]interface{}{23.298, -1}, 20},
{[]interface{}{49.99999, -2}, 0},
{[]interface{}{50, -2}, 100},
{[]interface{}{50.00001, -2}, 100},
{[]interface{}{newDec("-1.23")}, newDec("-1")},
{[]interface{}{newDec("-1.23"), 1}, newDec("-1.2")},
{[]interface{}{newDec("-1.58")}, newDec("-2")},
Expand Down
2 changes: 2 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,8 @@ func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {
result.Check(testkit.Rows("100000000000000 1000000000000000 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
result = tk.MustQuery("SELECT ROUND(1e-14, 1), ROUND(1e-15, 1), ROUND(1e-308, 1)")
result.Check(testkit.Rows("0 0 0"))
result = tk.MustQuery("SELECT round(49.99999, -2), round(50, -2), round(50.00001, -2)")
result.Check(testkit.Rows("0 100 100"))

// for truncate
result = tk.MustQuery("SELECT truncate(123, -2), truncate(123, 2), truncate(123, 1), truncate(123, -1);")
Expand Down
41 changes: 36 additions & 5 deletions types/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,56 @@ import (
"github.com/pingcap/errors"
)

// RoundRule indicate the 2 round rule for The ROUND() function
type RoundRule int

const (
// RoundHalfUp For exact-value numbers, ROUND() uses the "round half up" rule
RoundHalfUp RoundRule = iota + 1

// RoundNearestEven For approximate-value numbers, the result depends on the C library. On many systems,
// this means that ROUND() uses the "round to nearest even" rule
RoundNearestEven
)

// RoundFloat rounds float val to the nearest even integer value with float64 format, like MySQL Round function.
// RoundFloat uses default rounding mode, see https://dev.mysql.com/doc/refman/5.7/en/precision-math-rounding.html
// so rounding use "round to nearest even".
// see https://dev.mysql.com/doc/refman/5.7/en/precision-math-rounding.html
// e.g, 1.5 -> 2, -1.5 -> -2.
func RoundFloat(f float64) float64 {
return math.RoundToEven(f)
}

// Round rounds the argument f to dec decimal places.
// RoundInt uses the "round half up" rule: A value with a fractional part of .5 or greater is rounded up to the next integer
// if positive or down to the next integer if negative. (In other words, it is rounded away from zero.)
// A value with a fractional part less than .5 is rounded down to the next integer if positive or up to the next integer
// if negative. (In other words, it is rounded toward zero.)
func RoundInt(i int64) int64 {
return i
}

// Round rounds the argument f to dec decimal places use "round to nearest even" rule as default.
// dec defaults to 0 if not specified. dec can be negative
// to cause dec digits left of the decimal point of the
// value f to become zero.
func Round(f float64, dec int) float64 {
func Round(f float64, dec int, r ...RoundRule) float64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks too hacky to me, perhaps we can just add a rule parameter to the signature of this function and change all the places where it is used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a function func RoundInt64(f int64, dec int) int64?

Copy link
Contributor Author

@feitian124 feitian124 Aug 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i came up with 2 change way:

  • current one
    • prons: less change, adhere to golang which is only float64 version of round also; and since it is float64, it make sense use RoundNearestEven rule as default
    • crons: a little performance affact
  • addtional methons like RoundInt64
    • prons: better performance, straightforward, maybe adhere to mysql implements better
    • crons: more change

Copy link
Contributor Author

@feitian124 feitian124 Aug 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each have its own advantage, if you agree later one is better, i can update

Copy link
Contributor

@riteme riteme Aug 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better that integer round does not reuse floating-point round, because Float64 can't represent all integer values and will introduce inaccuracy in computation.

I can offer an example that shows why floating-point round isn't suitable:

In TiDB:

mysql> select round(123456789, -5);
+----------------------+
| round(123456789, -5) |
+----------------------+
|            123499999 |
+----------------------+
1 row in set (0.001 sec)

In MySQL 8.0:

mysql> select round(123456789, -5);
+----------------------+
| round(123456789, -5) |
+----------------------+
|            123500000 |
+----------------------+
1 row in set (0.006 sec)

TiDB's output is expected for floating-point round even if you use "round to even" rule: https://coliru.stacked-crooked.com/a/a3dbb35ff61b3944.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, MySQL 8 will report an error if round result overflows:

mysql> select round(18446744073709551615, -19);
ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in 'round(18446744073709551615,-(19))'

Copy link
Contributor Author

@feitian124 feitian124 Aug 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for suggestion, i will add a round function for integer, and maybe adjust some old names to make them consistent

Copy link
Contributor Author

@feitian124 feitian124 Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TiDB's output is expected for floating-point round even if you use "round to even" rule: https://coliru.stacked-crooked.com/a/a3dbb35ff61b3944.

	f := 123499999.99999999
	i := int64(f)
	i2 := math.Round(f)
	fmt.Printf("i: %+v, i2: %+v", i, i2)
	// OutPut: i: 123499999, i2: 1.235e+08

for tidb round(123456789, -5) get 123499999 is caused by convert int64(f), can be fixed by use convert math.Round(f)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be fixed by use convert math.Round(f)

I can offer another example:

TiDB:

mysql> select round(2146213728964879326, -15);
+---------------------------------+
| round(2146213728964879326, -15) |
+---------------------------------+
|             2145999999999999744 |
+---------------------------------+
1 row in set (0.000 sec)

MySQL 8.0:

mysql> select round(2146213728964879326, -15);
+---------------------------------+
| round(2146213728964879326, -15) |
+---------------------------------+
|             2146000000000000000 |
+---------------------------------+
1 row in set (0.001 sec)

I guess this one cannot be simply fixed by math.Round.

Copy link
Contributor Author

@feitian124 feitian124 Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you are right, round is not as easy as thought, see the link in my comments.
I will add your example as test case and hopefully get a perfect solution.

shift := math.Pow10(dec)
tmp := f * shift
if math.IsInf(tmp, 0) {
return f
}
result := RoundFloat(tmp) / shift

var rr = RoundNearestEven
if len(r) > 0 {
rr = r[0]
}

var result float64
if rr == RoundNearestEven {
result = math.RoundToEven(tmp) / shift
} else {
result = math.Round(tmp) / shift
}

if math.IsNaN(result) {
return 0
}
Expand Down