From 9f51ed0697f1698efb528c27443bd8235752e5ff Mon Sep 17 00:00:00 2001
From: Jianjun Liao <36503113+Leavrth@users.noreply.github.com>
Date: Mon, 6 Mar 2023 16:07:11 +0800
Subject: [PATCH] sync_diff_inspector: fix float degree (#708)

ref pingcap/tidb-tools#699
---
 sync_diff_inspector/utils/utils.go      | 10 +++++++++-
 sync_diff_inspector/utils/utils_test.go |  2 +-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/sync_diff_inspector/utils/utils.go b/sync_diff_inspector/utils/utils.go
index 28fe0a0d..5f503173 100644
--- a/sync_diff_inspector/utils/utils.go
+++ b/sync_diff_inspector/utils/utils.go
@@ -143,7 +143,15 @@ func GetTableRowsQueryFormat(schema, table string, tableInfo *model.TableInfo, c
 
 	columnNames := make([]string, 0, len(tableInfo.Columns))
 	for _, col := range tableInfo.Columns {
-		columnNames = append(columnNames, dbutil.ColumnName(col.Name.O))
+		name := dbutil.ColumnName(col.Name.O)
+		// When col value is 0, the result is NULL.
+		// But we can use ISNULL to distinguish between null and 0.
+		if col.FieldType.GetType() == mysql.TypeFloat {
+			name = fmt.Sprintf("round(%s, 5-floor(log10(abs(%s)))) as %s", name, name, name)
+		} else if col.FieldType.GetType() == mysql.TypeDouble {
+			name = fmt.Sprintf("round(%s, 14-floor(log10(abs(%s)))) as %s", name, name, name)
+		}
+		columnNames = append(columnNames, name)
 	}
 	columns := strings.Join(columnNames, ", ")
 	if collation != "" {
diff --git a/sync_diff_inspector/utils/utils_test.go b/sync_diff_inspector/utils/utils_test.go
index 13fc00fd..39d671bc 100644
--- a/sync_diff_inspector/utils/utils_test.go
+++ b/sync_diff_inspector/utils/utils_test.go
@@ -84,7 +84,7 @@ func TestBasicTableUtilOperation(t *testing.T) {
 	require.NoError(t, err)
 
 	query, orderKeyCols := GetTableRowsQueryFormat("test", "test", tableInfo, "123")
-	require.Equal(t, query, "SELECT /*!40001 SQL_NO_CACHE */ `a`, `b`, `c`, `d` FROM `test`.`test` WHERE %s ORDER BY `a`,`b` COLLATE '123'")
+	require.Equal(t, query, "SELECT /*!40001 SQL_NO_CACHE */ `a`, `b`, round(`c`, 5-floor(log10(abs(`c`)))) as `c`, `d` FROM `test`.`test` WHERE %s ORDER BY `a`,`b` COLLATE '123'")
 	expectName := []string{"a", "b"}
 	for i, col := range orderKeyCols {
 		require.Equal(t, col.Name.O, expectName[i])