Skip to content

Commit

Permalink
executor: correct range calculation for CHAR column (#10124)
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason authored May 14, 2019
1 parent 7d7c146 commit ba7134d
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 7 deletions.
1 change: 1 addition & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var _ = Suite(&testSuite3{})
var _ = Suite(&testBypassSuite{})
var _ = Suite(&testUpdateSuite{})
var _ = Suite(&testOOMSuite{})
var _ = Suite(&testPointGetSuite{})

type testSuite struct {
cluster *mocktikv.Cluster
Expand Down
16 changes: 11 additions & 5 deletions executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/ranger"
)

func (b *executorBuilder) buildPointGet(p *plannercore.PointGetPlan) Executor {
Expand Down Expand Up @@ -86,7 +87,7 @@ func (e *PointGetExecutor) Next(ctx context.Context, req *chunk.RecordBatch) err
}
if e.idxInfo != nil {
idxKey, err1 := e.encodeIndexKey()
if err1 != nil {
if err1 != nil && !kv.ErrNotExist.Equal(err1) {
return err1
}

Expand Down Expand Up @@ -132,16 +133,21 @@ func (e *PointGetExecutor) Next(ctx context.Context, req *chunk.RecordBatch) err
return e.decodeRowValToChunk(val, req.Chunk)
}

func (e *PointGetExecutor) encodeIndexKey() ([]byte, error) {
func (e *PointGetExecutor) encodeIndexKey() (_ []byte, err error) {
sc := e.ctx.GetSessionVars().StmtCtx
for i := range e.idxVals {
colInfo := e.tblInfo.Columns[e.idxInfo.Columns[i].Offset]
casted, err := table.CastValue(e.ctx, e.idxVals[i], colInfo)
if colInfo.Tp == mysql.TypeString || colInfo.Tp == mysql.TypeVarString || colInfo.Tp == mysql.TypeVarchar {
e.idxVals[i], err = ranger.HandlePadCharToFullLength(sc, &colInfo.FieldType, e.idxVals[i])
} else {
e.idxVals[i], err = table.CastValue(e.ctx, e.idxVals[i], colInfo)
}
if err != nil {
return nil, err
}
e.idxVals[i] = casted
}
encodedIdxVals, err := codec.EncodeKey(e.ctx.GetSessionVars().StmtCtx, nil, e.idxVals...)

encodedIdxVals, err := codec.EncodeKey(sc, nil, e.idxVals...)
if err != nil {
return nil, err
}
Expand Down
287 changes: 286 additions & 1 deletion executor/point_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,57 @@
package executor_test

import (
"fmt"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/util/testkit"
)

func (s *testSuite1) TestPointGet(c *C) {
type testPointGetSuite struct {
store kv.Storage
dom *domain.Domain
cli *checkRequestClient
}

func (s *testPointGetSuite) SetUpSuite(c *C) {
cli := &checkRequestClient{}
hijackClient := func(c tikv.Client) tikv.Client {
cli.Client = c
return cli
}
s.cli = cli

var err error
s.store, err = mockstore.NewMockTikvStore(
mockstore.WithHijackClient(hijackClient),
)
c.Assert(err, IsNil)
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
s.dom.SetStatsUpdating(true)
}

func (s *testPointGetSuite) TearDownSuite(c *C) {
s.dom.Close()
s.store.Close()
}

func (s *testPointGetSuite) TearDownTest(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
r := tk.MustQuery("show tables")
for _, tb := range r.Rows() {
tableName := tb[0]
tk.MustExec(fmt.Sprintf("drop table %v", tableName))
}
}

func (s *testPointGetSuite) TestPointGet(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table point (id int primary key, c int, d varchar(10), unique c_d (c, d))")
Expand Down Expand Up @@ -56,3 +102,242 @@ func (s *testSuite1) TestPointGet(c *C) {
`5 5 6 5 6 7 6 7 7`,
))
}

func (s *testPointGetSuite) TestPointGetCharPK(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a char(2) primary key, b char(2));`)
tk.MustExec(`insert into t values("aa", "bb");`)

// Test truncate without sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="";`)
tk.MustPointGet(`select * from t where a = "aa";`).Check(testkit.Rows(`aa bb`))
tk.MustPointGet(`select * from t where a = "aab";`).Check(testkit.Rows())

// Test truncate with sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustPointGet(`select * from t where a = "aa";`).Check(testkit.Rows(`aa bb`))
tk.MustPointGet(`select * from t where a = "aab";`).Check(testkit.Rows())

tk.MustExec(`truncate table t;`)
tk.MustExec(`insert into t values("a ", "b ");`)

// Test trailing spaces without sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows(`a b`))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())

// Test trailing spaces with sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())

// // Test CHAR BINARY.
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a char(2) binary primary key, b char(2));`)
tk.MustExec(`insert into t values(" ", " ");`)
tk.MustExec(`insert into t values("a ", "b ");`)

// Test trailing spaces without sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows(`a b`))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))

// Test trailing spaces with sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows(`a b`))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
}

func (s *testPointGetSuite) TestIndexLookupCharPK(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a char(2) primary key, b char(2));`)
tk.MustExec(`insert into t values("aa", "bb");`)

// Test truncate without sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="";`)
tk.MustIndexLookup(`select * from t tmp where a = "aa";`).Check(testkit.Rows(`aa bb`))
tk.MustIndexLookup(`select * from t tmp where a = "aab";`).Check(testkit.Rows())

// Test truncate with sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustIndexLookup(`select * from t tmp where a = "aa";`).Check(testkit.Rows(`aa bb`))
tk.MustIndexLookup(`select * from t tmp where a = "aab";`).Check(testkit.Rows())

tk.MustExec(`truncate table t;`)
tk.MustExec(`insert into t values("a ", "b ");`)

// Test trailing spaces without sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="";`)
tk.MustIndexLookup(`select * from t tmp where a = "a";`).Check(testkit.Rows(`a b`))
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())

// Test trailing spaces with sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustIndexLookup(`select * from t tmp where a = "a";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())

// Test CHAR BINARY.
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a char(2) binary primary key, b char(2));`)
tk.MustExec(`insert into t values(" ", " ");`)
tk.MustExec(`insert into t values("a ", "b ");`)

// Test trailing spaces without sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="";`)
tk.MustIndexLookup(`select * from t tmp where a = "a";`).Check(testkit.Rows(`a b`))
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustIndexLookup(`select * from t tmp where a = " ";`).Check(testkit.Rows(` `))
tk.MustIndexLookup(`select * from t tmp where a = " ";`).Check(testkit.Rows(` `))
tk.MustIndexLookup(`select * from t tmp where a = " ";`).Check(testkit.Rows(` `))

// Test trailing spaces with sql mode `PAD_CHAR_TO_FULL_LENGTH`.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustIndexLookup(`select * from t tmp where a = "a";`).Check(testkit.Rows(`a b`))
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows(`a b`))
tk.MustIndexLookup(`select * from t tmp where a = " ";`).Check(testkit.Rows(` `))
tk.MustIndexLookup(`select * from t tmp where a = " ";`).Check(testkit.Rows(` `))
tk.MustIndexLookup(`select * from t tmp where a = " ";`).Check(testkit.Rows(` `))
}

func (s *testPointGetSuite) TestPointGetVarcharPK(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a varchar(2) primary key, b varchar(2));`)
tk.MustExec(`insert into t values("aa", "bb");`)

// Test truncate without sql mode `PAD_CHAR_TO_FULL_LENGTH`.
// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustExec(`set @@sql_mode="";`)
tk.MustPointGet(`select * from t where a = "aa";`).Check(testkit.Rows(`aa bb`))
tk.MustPointGet(`select * from t where a = "aab";`).Check(testkit.Rows())

// Test truncate with sql mode `PAD_CHAR_TO_FULL_LENGTH`.
// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustPointGet(`select * from t where a = "aa";`).Check(testkit.Rows(`aa bb`))
tk.MustPointGet(`select * from t where a = "aab";`).Check(testkit.Rows())

tk.MustExec(`truncate table t;`)
tk.MustExec(`insert into t values("a ", "b ");`)

// Test trailing spaces without sql mode `PAD_CHAR_TO_FULL_LENGTH`.
// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustExec(`set @@sql_mode="";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())

// Test trailing spaces with sql mode `PAD_CHAR_TO_FULL_LENGTH`.
// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())

// // Test VARCHAR BINARY.
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a varchar(2) binary primary key, b varchar(2));`)
tk.MustExec(`insert into t values(" ", " ");`)
tk.MustExec(`insert into t values("a ", "b ");`)

// Test trailing spaces without sql mode `PAD_CHAR_TO_FULL_LENGTH`.
// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustExec(`set @@sql_mode="";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))

// Test trailing spaces with sql mode `PAD_CHAR_TO_FULL_LENGTH`.
// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Rows(` `))
}

func (s *testPointGetSuite) TestPointGetBinaryPK(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a binary(2) primary key, b binary(2));`)
tk.MustExec(`insert into t values("a", "b");`)

tk.MustExec(`set @@sql_mode="";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a\0";`).Check(testkit.Rows("a\x00 b\x00"))

// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a\0";`).Check(testkit.Rows("a\x00 b\x00"))

tk.MustExec(`insert into t values("a ", "b ");`)
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())

// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Rows())
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Rows())
}

func (s *testPointGetSuite) TestIndexLookupBinaryPK(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a binary(2) primary key, b binary(2));`)
tk.MustExec(`insert into t values("a", "b");`)

tk.MustExec(`set @@sql_mode="";`)
tk.MustIndexLookup(`select * from t tmp where a = "a";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a\0";`).Check(testkit.Rows("a\x00 b\x00"))

// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustExec(`set @@sql_mode="PAD_CHAR_TO_FULL_LENGTH";`)
tk.MustIndexLookup(`select * from t tmp where a = "a";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a\0";`).Check(testkit.Rows("a\x00 b\x00"))

tk.MustExec(`insert into t values("a ", "b ");`)
tk.MustIndexLookup(`select * from t tmp where a = "a";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())

// `PAD_CHAR_TO_FULL_LENGTH` should not affect the result.
tk.MustIndexLookup(`select * from t tmp where a = "a";`).Check(testkit.Rows())
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows(`a b `))
tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Rows())
}
Loading

0 comments on commit ba7134d

Please sign in to comment.