diff --git a/distsql/request_builder.go b/distsql/request_builder.go index 09ab4094ab732..fd88867cafe80 100644 --- a/distsql/request_builder.go +++ b/distsql/request_builder.go @@ -782,20 +782,5 @@ func EncodeIndexKey(sc *stmtctx.StatementContext, ran *ranger.Range) ([]byte, [] if !ran.HighExclude { high = kv.Key(high).PrefixNext() } - - var hasNull bool - for _, highVal := range ran.HighVal { - if highVal.IsNull() { - hasNull = true - break - } - } - - // NOTE: this is a hard-code operation to avoid wrong results when accessing unique index with NULL; - // Please see https://github.com/pingcap/tidb/issues/29650 for more details - if hasNull { - // Append 0 to make unique-key range [null, null] to be a scan rather than point-get. - high = kv.Key(high).Next() - } return low, high, nil } diff --git a/executor/issuetest/executor_issue_test.go b/executor/issuetest/executor_issue_test.go index 8dcbf251cdf89..945b8cd1d2c5f 100644 --- a/executor/issuetest/executor_issue_test.go +++ b/executor/issuetest/executor_issue_test.go @@ -1339,3 +1339,14 @@ PARTITION BY LIST COLUMNS(c_int) tk.MustExec("delete t1, t2 from t1, t2 where t1.c_enum in ('blue');") tk.MustExec("commit") } + +func TestIssue40158(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + + tk.MustExec("use test") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t1 (_id int PRIMARY KEY, c1 char, index (c1));") + tk.MustExec("insert into t1 values (1, null);") + tk.MustQuery("select * from t1 where c1 is null and _id < 1;").Check(testkit.Rows()) +}