diff --git a/pkg/diff/chunk.go b/pkg/diff/chunk.go index f51ede381..5731b34dc 100644 --- a/pkg/diff/chunk.go +++ b/pkg/diff/chunk.go @@ -281,27 +281,30 @@ func GenerateCheckJob(table *TableInstance, splitField, limits string, chunkSize chunks = chunks[1:] args := make([]interface{}, 0, 2) - var condition1, condition2 string + var ( + condition1 = "TRUE" + condition2 = "TRUE" + ) if !chunk.noBegin { + format := "`%s`%s > ?" if chunk.containBegin { - condition1 = fmt.Sprintf("`%s`%s >= ?", column.Name, collation) - } else { - condition1 = fmt.Sprintf("`%s`%s > ?", column.Name, collation) + format = "`%s`%s >= ?" } + + condition1 = fmt.Sprintf(format, column.Name, collation) args = append(args, chunk.begin) - } else { - condition1 = "TRUE" } + if !chunk.noEnd { + format := "`%s`%s < ?" if chunk.containEnd { - condition2 = fmt.Sprintf("`%s`%s <= ?", column.Name, collation) - } else { - condition2 = fmt.Sprintf("`%s`%s < ?", column.Name, collation) + format = "`%s`%s <= ?" } + + condition2 = fmt.Sprintf(format, column.Name, collation) args = append(args, chunk.end) - } else { - condition2 = "TRUE" } + where := fmt.Sprintf("(%s AND %s AND %s)", condition1, condition2, limits) log.Debugf("%s.%s create dump job, where: %s, begin: %v, end: %v", table.Schema, table.Table, where, chunk.begin, chunk.end) diff --git a/pkg/diff/diff.go b/pkg/diff/diff.go index c17bf9d07..b7a4beb09 100644 --- a/pkg/diff/diff.go +++ b/pkg/diff/diff.go @@ -506,30 +506,36 @@ func compareData(map1, map2 map[string][]byte, null1, null2 map[string]bool, ord return false, 0, errors.Errorf("don't have key %s", col.Name.O) } if needQuotes(col.FieldType) { - if string(data1) > string(data2) { - cmp = 1 - break - } else if string(data1) < string(data2) { - cmp = -1 - break - } else { + + strData1 := string(data1) + strData2 := string(data2) + + if len(strData1) == len(strData2) && strData1 == strData2 { continue } + + cmp = -1 + if strData1 > strData2 { + cmp = 1 + } + break + } else { num1, err1 := strconv.ParseFloat(string(data1), 64) num2, err2 := strconv.ParseFloat(string(data2), 64) if err1 != nil || err2 != nil { return false, 0, errors.Errorf("convert %s, %s to float failed, err1: %v, err2: %v", string(data1), string(data2), err1, err2) } + + if num1 == num2 { + continue + } + + cmp = -1 if num1 > num2 { cmp = 1 - break - } else if num1 < num2 { - cmp = -1 - break - } else { - continue } + break } } diff --git a/pkg/diff/merge.go b/pkg/diff/merge.go index 610ec8656..7e4894f1c 100644 --- a/pkg/diff/merge.go +++ b/pkg/diff/merge.go @@ -41,11 +41,10 @@ func (r RowDatas) Less(i, j int) bool { data1 = r.Rows[i].Data[col.Name.O] data2 = r.Rows[j].Data[col.Name.O] if needQuotes(col.FieldType) { - if string(data1) > string(data2) { - return false - } else if string(data1) < string(data2) { - return true - } else { + strData1 := string(data1) + strData2 := string(data2) + + if strData1 == strData2 { // `NULL` is less than "" if r.Rows[i].Null[col.Name.O] { return true @@ -55,20 +54,25 @@ func (r RowDatas) Less(i, j int) bool { } continue } - } else { - num1, err1 := strconv.ParseFloat(string(data1), 64) - num2, err2 := strconv.ParseFloat(string(data2), 64) - if err1 != nil || err2 != nil { - log.Fatalf("convert %s, %s to float failed, err1: %v, err2: %v", string(data1), string(data2), err1, err2) - } - if num1 > num2 { + if strData1 > strData2 { return false - } else if num1 < num2 { - return true - } else { - continue } + return true + } + num1, err1 := strconv.ParseFloat(string(data1), 64) + num2, err2 := strconv.ParseFloat(string(data2), 64) + if err1 != nil || err2 != nil { + log.Fatalf("convert %s, %s to float failed, err1: %v, err2: %v", string(data1), string(data2), err1, err2) } + + if num1 == num2 { + continue + } + if num1 > num2 { + return false + } + return true + } return true