Repository: pingcap/tidb-tools (go)

Issue:
sync-diff-inspector: sql-hint-use-index in random splitter can't find the right index in some cases
## Bug Report

Please answer these questions before submitting your issue. Thanks!

1. What did you do?
If possible, provide a recipe for reproducing the error.
The sql-hint-use-index may not pick the index in random splitter if the index cols order are not same as the original col order. The bug is in this [line](https://github.com/pingcap/tidb-tools/blob/ac97e96991cf66e7a81bc35e1238c084875f4e5f/sync_diff_inspector/splitter/random.go#L85C1-L88C4) where the cols in iFields is not sorted, but the MatchesIndex assumed it is.

E.g. for table
```
CREATE TABLE test1  (
  `id_second` INT NOT NULL,
   `id_first` INT NOT NULL,
    `v` VARCHAR(20)
  PRIMARY KEY (`id_first`,`id_second`)
);
```

2. What did you expect to see?
The right index is picked

3. What did you see instead?
No index is picked.

4. What version of TiDB are you using (`tidb-server -V` or run `select tidb_version();` on TiDB)?

5. which tool are you using?

6. what versionof tool are you using (`pump -V` or `tidb-lightning -V` or `syncer -V`)?

Pull request:
sync-diff-inspector: Fix the indexColumns logic in random splitter to find matching index without sort
<!--
Thank you for contributing to TiDB! Please read TiDB's [CONTRIBUTING](https://github.com/pingcap/tidb/blob/master/CONTRIBUTING.md) document **BEFORE** filing this PR.
-->

### What problem does this PR solve?
<!--
Please create an issue first to describe the problem.

There MUST be one line starting with "Issue Number:  " and 
linking the relevant issues via the "close" or "ref".

For more info, check https://pingcap.github.io/tidb-dev-guide/contribute-to-tidb/contribute-code.html#referring-to-an-issue.
-->

Issue Number: close #858

### What is changed and how it works?
In random splitter when picking the index name for query hint, the iFields.MatchesIndex(index) sort the index cols before comparison, however we should use the exact index col order here, as the `fields` column list is already containing the ordered columns used for constructing the chunks.

This PR fixes this bug by not using MatchesIndex

### Check List <!--REMOVE the items that are not applicable-->

Tests <!-- At least one of them must be included. -->

 - Unit test
 - Manual test (add detailed scripts or steps below)




Patch:
diff --git a/sync_diff_inspector/splitter/random.go b/sync_diff_inspector/splitter/random.go
index de80274..ba8cb59 100644
--- a/sync_diff_inspector/splitter/random.go
+++ b/sync_diff_inspector/splitter/random.go
@@ -64,7 +64,7 @@ func NewRandomIteratorWithCheckpoint(ctx context.Context, progressID string, tab
 
 	// Below logic is modified from BucketIterator
 	// It's used to find the index which can match the split fields in RandomIterator.
-	iFields := &indexFields{cols: fields, tableInfo: table.Info}
+	fieldNames := utils.GetColumnNames(fields)
 	var indices = dbutil.FindAllIndex(table.Info)
 NEXTINDEX:
 	for _, index := range indices {
@@ -82,8 +82,7 @@ NEXTINDEX:
 			continue
 		}
 
-		if !iFields.MatchesIndex(index) {
-			// We are enforcing user configured "index-fields" settings.
+		if !utils.IsIndexMatchingColumns(index, fieldNames) {
 			continue
 		}
 


Test patch:
diff --git a/sync_diff_inspector/splitter/splitter_test.go b/sync_diff_inspector/splitter/splitter_test.go
index 70c22d6..50087e1 100644
--- a/sync_diff_inspector/splitter/splitter_test.go
+++ b/sync_diff_inspector/splitter/splitter_test.go
@@ -996,6 +996,10 @@ func TestRandomSpliterHint(t *testing.T) {
 			"create table `test`.`test`(`a` int, `b` int, `c` int, primary key(`a`, `b`), unique key i1(`c`))",
 			[]model.CIStr{model.NewCIStr("a"), model.NewCIStr("b")},
 		},
+		{
+			"create table `test`.`test`(`a` int, `b` int, `c` int, primary key(`b`, `a`), unique key i1(`c`))",
+			[]model.CIStr{model.NewCIStr("b"), model.NewCIStr("a")},
+		},
 		{
 			"create table `test`.`test`(`a` int, `b` int, `c` int, unique key i1(`c`), key i2(`b`))",
 			[]model.CIStr{model.NewCIStr("c")},