From b0d9934fb05a0b61878eb72a0bd437388160c1b0 Mon Sep 17 00:00:00 2001 From: Zhuomin Liu Date: Tue, 15 Dec 2020 12:12:55 +0800 Subject: [PATCH] types: fix compare float64 with float64 in json (#21709) Signed-off-by: lzmhhh123 --- expression/integration_test.go | 7 +++++++ types/json/binary_functions.go | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index e6b4e0680fe14..0d5753ba176a1 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4404,6 +4404,13 @@ func (s *testIntegrationSuite) TestFuncJSON(c *C) { // #16267 tk.MustQuery(`select json_array(922337203685477580) = json_array(922337203685477581);`).Check(testkit.Rows("0")) + + // #10461 + tk.MustExec("drop table if exists tx1") + tk.MustExec("create table tx1(id int key, a double, b double, c double, d double)") + tk.MustExec("insert into tx1 values (1, 0.1, 0.2, 0.3, 0.0)") + tk.MustQuery("select a+b, c from tx1").Check(testkit.Rows("0.30000000000000004 0.3")) + tk.MustQuery("select json_array(a+b) = json_array(c) from tx1").Check(testkit.Rows("0")) } func (s *testIntegrationSuite) TestColumnInfoModified(c *C) { diff --git a/types/json/binary_functions.go b/types/json/binary_functions.go index 478aea2d2aded..30133c6e7b5ab 100644 --- a/types/json/binary_functions.go +++ b/types/json/binary_functions.go @@ -647,6 +647,16 @@ func compareInt64(x int64, y int64) int { return 1 } +func compareFloat64(x float64, y float64) int { + if x < y { + return -1 + } else if x == y { + return 0 + } + + return 1 +} + func compareUint64(x uint64, y uint64) int { if x < y { return -1 @@ -712,7 +722,7 @@ func CompareBinary(left, right BinaryJSON) int { case TypeCodeUint64: cmp = compareFloat64Uint64(left.GetFloat64(), right.GetUint64()) case TypeCodeFloat64: - cmp = compareFloat64PrecisionLoss(left.GetFloat64(), right.GetFloat64()) + cmp = compareFloat64(left.GetFloat64(), right.GetFloat64()) } case TypeCodeString: cmp = bytes.Compare(left.GetString(), right.GetString())