Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a bug that nullable unique index can not find null values using index. #7163

Merged
merged 6 commits into from
Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions distsql/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,22 @@ func encodeIndexKey(sc *stmtctx.StatementContext, ran *ranger.Range) ([]byte, []
if err != nil {
return nil, nil, errors.Trace(err)
}

if !ran.HighExclude {
high = []byte(kv.Key(high).PrefixNext())
}

var hasNull bool
for _, highVal := range ran.HighVal {
if highVal.IsNull() {
hasNull = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could break at once?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

break
}
}

if hasNull {
// Append 0 to make unique-key range [null, null] to be a scan rather than point-get.
high = []byte(kv.Key(high).Next())
}
return low, high, nil
}
25 changes: 25 additions & 0 deletions executor/distsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,28 @@ func (s *testSuite) TestCorColToRanges(c *C) {
// Test IndexLookUpReader.
tk.MustQuery("select t.c in (select count(*) from t s use index(idx), t t1 where s.b = t.a and s.c = t1.a) from t").Check(testkit.Rows("1", "0", "0", "0", "0", "0", "0", "0", "0"))
}

func (s *testSuite) TestUniqueKeyNullValueSelect(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
// test null in unique-key
tk.MustExec("create table t (id int default null, c varchar(20), unique id (id));")
tk.MustExec("insert t (c) values ('a'), ('b'), ('c');")
res := tk.MustQuery("select * from t where id is null;")
res.Check(testkit.Rows("<nil> a", "<nil> b", "<nil> c"))

// test null in mul unique-key
tk.MustExec("drop table t")
tk.MustExec("create table t (id int default null, b int default 1, c varchar(20), unique id_c(id, b));")
tk.MustExec("insert t (c) values ('a'), ('b'), ('c');")
res = tk.MustQuery("select * from t where id is null and b = 1;")
res.Check(testkit.Rows("<nil> 1 a", "<nil> 1 b", "<nil> 1 c"))

tk.MustExec("drop table t")
// test null in non-unique-key
tk.MustExec("create table t (id int default null, c varchar(20), key id (id));")
tk.MustExec("insert t (c) values ('a'), ('b'), ('c');")
res = tk.MustQuery("select * from t where id is null;")
res.Check(testkit.Rows("<nil> a", "<nil> b", "<nil> c"))
}
1 change: 1 addition & 0 deletions util/ranger/ranger.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func points2Ranges(sc *stmtctx.StatementContext, rangePoints []point, tp *types.
if mysql.HasNotNullFlag(tp.Flag) && endPoint.value.Kind() == types.KindNull {
continue
}

ran := &Range{
LowVal: []types.Datum{startPoint.value},
LowExclude: startPoint.excl,
Expand Down
4 changes: 4 additions & 0 deletions util/ranger/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (ran *Range) IsPoint(sc *stmtctx.StatementContext) bool {
if cmp != 0 {
return false
}

if a.IsNull() {
return false
}
}
return !ran.LowExclude && !ran.HighExclude
}
Expand Down