Repository: pingcap/tispark (language: python)

Issue:
[BUG] Return empty data
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->

**What did you do**
SQL
```
CREATE TABLE  `test`.`s4` (
`LISTID` int(5) NOT NULL,
`INST_NO` varchar(5) NOT NULL,
UNIQUE KEY `idx_listinst` (`LISTID`,`INST_NO`)
) 

insert into s4 values (1,'1')
```

TiSpark

spark.sql("select * from tidb_catalog.test.s4 where LISTID = 1").show()

return empty

TiSpark version : v3.1.2



Discussion / hints:

Fix patch:
diff --git a/tikv-client/src/main/java/com/pingcap/tikv/meta/TiDAGRequest.java b/tikv-client/src/main/java/com/pingcap/tikv/meta/TiDAGRequest.java
index c18cd3e21e..be32501615 100644
--- a/tikv-client/src/main/java/com/pingcap/tikv/meta/TiDAGRequest.java
+++ b/tikv-client/src/main/java/com/pingcap/tikv/meta/TiDAGRequest.java
@@ -304,10 +304,10 @@ private void addIndexReaderIndexRangeScanExecutorCols(
 
   private void addIndexColsToScanBuilder(
       IndexScan.Builder indexScanBuilder, Map<String, Integer> colOffsetInFieldMap) {
-    if (indexInfo.isUnique()) {
-      indexScanBuilder.setUnique(true);
-    }
     for (TiIndexColumn indexColumn : indexInfo.getIndexColumns()) {
+      // We can optimize performance by pass unique into index scan.
+      // Refer to
+      // https://github.com/pingcap/tidb/blob/4ae5be190b0017675deedd9af8b80d8868e03f0e/planner/core/plan_to_pb.go#L271 to see if we can pass unique into index scan.
       TiColumnInfo tableInfoColumn = tableInfo.getColumn(indexColumn.getName());
       // already add this col before.
       ColumnInfo.Builder colBuild = ColumnInfo.newBuilder(tableInfoColumn.toProto(tableInfo));


Test patch:
diff --git a/core/src/test/scala/org/apache/spark/sql/IssueTestSuite.scala b/core/src/test/scala/org/apache/spark/sql/IssueTestSuite.scala
index 7d8197705c..38bac47e56 100644
--- a/core/src/test/scala/org/apache/spark/sql/IssueTestSuite.scala
+++ b/core/src/test/scala/org/apache/spark/sql/IssueTestSuite.scala
@@ -22,6 +22,17 @@ import org.apache.spark.sql.functions.{col, sum}
 
 class IssueTestSuite extends BaseTiSparkTest {
 
+  test("test issue 2649") {
+    val dbTable = "tispark_test.mutil_uniq"
+    tidbStmt.execute(s"drop table if exists $dbTable")
+    tidbStmt.execute(
+      s"CREATE TABLE $dbTable (`a` int(5) NOT NULL,`b` int(5) NOT NULL,UNIQUE KEY `idx_ab` (`a`,`b`))")
+    tidbStmt.execute(s"insert into $dbTable values(0, 0),(1,1)")
+    val df = spark.sql(s"select * from $dbTable where a=1")
+    val row1 = Row(1, 1)
+    checkAnswer(df, Seq(row1))
+  }
+
   //https://github.com/pingcap/tispark/issues/2268
   test("show rowid in commonhandle") {
     spark.sqlContext.setConf(TiConfigConst.SHOW_ROWID, "true")