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

sync_diff_inspector: prevent float/double 0 turned into NULL #843

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
41 changes: 27 additions & 14 deletions sync_diff_inspector/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb-tools/sync_diff_inspector/chunk"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/parser/types"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -135,6 +136,27 @@ func GetColumnsFromIndex(index *model.IndexInfo, tableInfo *model.TableInfo) []*
return indexColumns
}

// formatColumn gets the SQL expression to format the column for comparison.
//
// Normally it would just return `name`, but for floating-point types it would
// round the result to 6 (single) or 15 (double) digits.
// See <https://github.com/pingcap/tidb-tools/pull/700#discussion_r1122787571>
// for details.
func formatColumn(name string, fieldType *types.FieldType) string {
var k, epsilon string
switch fieldType.GetType() {
case mysql.TypeFloat:
k = "5"
epsilon = "1e-45"
case mysql.TypeDouble:
k = "14"
epsilon = "5e-324"
default:
return name
}
return fmt.Sprintf("round(%[1]s, %[2]s-floor(log10(greatest(abs(%[1]s), %[3]s))))", name, k, epsilon)
}

// GetTableRowsQueryFormat returns a rowsQuerySQL template for the specific table.
//
// e.g. SELECT /*!40001 SQL_NO_CACHE */ `a`, `b` FROM `schema`.`table` WHERE %s ORDER BY `a`.
Expand All @@ -148,14 +170,11 @@ func GetTableRowsQueryFormat(schema, table string, tableInfo *model.TableInfo, c
}

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)
expr := formatColumn(name, &col.FieldType)
if expr != name {
expr += " as " + name
}
columnNames = append(columnNames, name)
columnNames = append(columnNames, expr)
}
columns := strings.Join(columnNames, ", ")
if collation != "" {
Expand Down Expand Up @@ -785,13 +804,7 @@ func GetCountAndMd5Checksum(ctx context.Context, db *sql.DB, schemaName, tableNa
continue
}
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))))", name, name)
} else if col.FieldType.GetType() == mysql.TypeDouble {
name = fmt.Sprintf("round(%s, 14-floor(log10(abs(%s))))", name, name)
}
name = formatColumn(name, &col.FieldType)
columnNames = append(columnNames, name)
columnIsNull = append(columnIsNull, fmt.Sprintf("ISNULL(%s)", name))
}
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`, round(`c`, 5-floor(log10(abs(`c`)))) as `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(greatest(abs(`c`), 1e-45)))) 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