Skip to content

Commit

Permalink
sync_diff_inspector: fix float degree (#708)
Browse files Browse the repository at this point in the history
ref #699
  • Loading branch information
Leavrth authored Mar 6, 2023
1 parent 12273bb commit 9f51ed0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion sync_diff_inspector/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
2 changes: 1 addition & 1 deletion sync_diff_inspector/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 9f51ed0

Please sign in to comment.