Skip to content

Commit

Permalink
executor: fix batch point get return wrong result for in(null) (#18851)
Browse files Browse the repository at this point in the history
* executor: fix batch point get return wrong result for in(null)

* fix CI

* address comment

Co-authored-by: crazycs <crazycs520@gmail.com>
Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
Co-authored-by: Evan Zhou <coocood@gmail.com>
  • Loading branch information
4 people authored Jul 30, 2020
1 parent 5dd7cd8 commit 4000174
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions executor/batch_point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ func (e *BatchPointGetExec) Next(ctx context.Context, req *chunk.Chunk) error {
return nil
}

func datumsContainNull(vals []types.Datum) bool {
for _, val := range vals {
if val.IsNull() {
return true
}
}
return false
}

func (e *BatchPointGetExec) initialize(ctx context.Context) error {
var handleVals map[string][]byte
var indexKeys []kv.Key
Expand All @@ -170,6 +179,11 @@ func (e *BatchPointGetExec) initialize(ctx context.Context) error {
dedup := make(map[hack.MutableString]struct{})
keys := make([]kv.Key, 0, len(e.idxVals))
for _, idxVals := range e.idxVals {
// For all x, 'x IN (null)' evaluate to null, so the query get no result.
if datumsContainNull(idxVals) {
continue
}

physID := getPhysID(e.tblInfo, idxVals[e.partPos].GetInt64())
idxKey, err1 := EncodeUniqueIndexKey(e.ctx, e.tblInfo, e.idxInfo, idxVals, physID)
if err1 != nil && !kv.ErrNotExist.Equal(err1) {
Expand All @@ -192,6 +206,11 @@ func (e *BatchPointGetExec) initialize(ctx context.Context) error {
}
indexKeys = keys

// SELECT * FROM t WHERE x IN (null), in this case there is no key.
if len(keys) == 0 {
return nil
}

// Fetch all handles.
handleVals, err = batchGetter.BatchGet(ctx, keys)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions executor/batch_point_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,15 @@ func (s *testBatchPointGetSuite) TestBatchPointGetCache(c *C) {
tk.MustQuery("SELECT id, token FROM test.customers WHERE id IN (28)")
tk.MustQuery("SELECT id, token FROM test.customers WHERE id IN (28, 29);").Check(testkit.Rows("28 07j", "29 03j"))
}

func (s *testBatchPointGetSuite) TestIssue18843(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t18843 ( id bigint(10) primary key, f varchar(191) default null, unique key `idx_f` (`f`))")
tk.MustExec("insert into t18843 values (1, '')")
tk.MustQuery("select * from t18843 where f in (null)").Check(testkit.Rows())

tk.MustExec("insert into t18843 values (2, null)")
tk.MustQuery("select * from t18843 where f in (null)").Check(testkit.Rows())
tk.MustQuery("select * from t18843 where f is null").Check(testkit.Rows("2 <nil>"))
}

0 comments on commit 4000174

Please sign in to comment.