From ec077a067b01a1395ad8af6ce424e2eba5cf90c8 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 13 Jan 2023 15:33:47 +0800 Subject: [PATCH] distsql: fix a bug of ranger.Range to kv.KeyRange conversion (#40204) (#40552) close pingcap/tidb#40158 --- distsql/request_builder.go | 15 --------------- executor/executor_issue_test.go | 12 ++++++++++++ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/distsql/request_builder.go b/distsql/request_builder.go index 8c74d08915b14..e215bcd74acc8 100644 --- a/distsql/request_builder.go +++ b/distsql/request_builder.go @@ -735,20 +735,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/executor_issue_test.go b/executor/executor_issue_test.go index b4c453c3efd9f..08e4e14031ebd 100644 --- a/executor/executor_issue_test.go +++ b/executor/executor_issue_test.go @@ -1302,3 +1302,15 @@ func TestIssue33214(t *testing.T) { } } } + +func TestIssue40158(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + 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()) +}