Skip to content

Commit

Permalink
*: fix somes bugs related to the clustered index and the new collation (
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 authored Nov 12, 2020
1 parent 9af236c commit ef57bdb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion executor/mem_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (m *memTableReader) getRowData(handle kv.Handle, value []byte) ([][]byte, e
offset := colIDs[id]
if m.table.IsCommonHandle {
for i, colID := range m.pkColIDs {
if colID == col.ID {
if colID == col.ID && !types.CommonHandleNeedRestoredData(&col.FieldType) {
values[offset] = handle.EncodedCol(i)
break
}
Expand Down
31 changes: 31 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7650,6 +7650,37 @@ func (s *testIntegrationSuite) TestIssue20180(c *C) {
tk.MustQuery("select * from t where a > 1 and a = \"b\";").Check(testkit.Rows("b"))
}

func (s *testIntegrationSerialSuite) TestClusteredIndexAndNewCollation(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("set @@tidb_enable_clustered_index = 1;")
tk.MustExec("CREATE TABLE `t` (" +
"`a` char(10) COLLATE utf8mb4_unicode_ci NOT NULL," +
"`b` char(20) COLLATE utf8mb4_general_ci NOT NULL," +
"`c` int(11) NOT NULL," +
"PRIMARY KEY (`a`,`b`,`c`)," +
"KEY `idx` (`a`))")

tk.MustExec("begin")
tk.MustExec("insert into t values ('a6', 'b6', 3)")
tk.MustQuery("select * from t").Check(testkit.Rows("a6 b6 3"))
tk.MustQuery("select * from t where a='a6'").Check(testkit.Rows("a6 b6 3"))
tk.MustExec("delete from t")
tk.MustQuery("select * from t").Check(testkit.Rows())
tk.MustExec("commit")
tk.MustQuery("select * from t").Check(testkit.Rows())

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(`a` char(10) COLLATE utf8mb4_unicode_ci NOT NULL key)")
tk.MustExec("insert into t values ('&');")
tk.MustExec("replace into t values ('&');")
tk.MustQuery("select * from t").Check(testkit.Rows("&"))
}

func (s *testIntegrationSerialSuite) TestIssue20608(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
Expand Down
10 changes: 3 additions & 7 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/generatedexpr"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/stringutil"
Expand Down Expand Up @@ -878,7 +877,7 @@ func DecodeRawRowData(ctx sessionctx.Context, meta *model.TableInfo, h kv.Handle
}
continue
}
if col.IsCommonHandleColumn(meta) {
if col.IsCommonHandleColumn(meta) && !types.CommonHandleNeedRestoredData(&col.FieldType) {
pkIdx := FindPrimaryIndex(meta)
var idxOfIdx int
for i, idxCol := range pkIdx.Columns {
Expand Down Expand Up @@ -910,7 +909,7 @@ func DecodeRawRowData(ctx sessionctx.Context, meta *model.TableInfo, h kv.Handle
if col == nil {
continue
}
if col.IsPKHandleColumn(meta) || col.IsCommonHandleColumn(meta) {
if col.IsPKHandleColumn(meta) || (col.IsCommonHandleColumn(meta) && !types.CommonHandleNeedRestoredData(&col.FieldType)) {
continue
}
ri, ok := rowMap[col.ID]
Expand Down Expand Up @@ -1409,10 +1408,7 @@ func CanSkip(info *model.TableInfo, col *table.Column, value *types.Datum) bool
continue
}
canSkip := idxCol.Length == types.UnspecifiedLength
isNewCollation := collate.NewCollationEnabled() &&
col.EvalType() == types.ETString &&
!mysql.HasBinaryFlag(col.Flag)
canSkip = canSkip && !isNewCollation
canSkip = canSkip && !types.CommonHandleNeedRestoredData(&col.FieldType)
return canSkip
}
}
Expand Down
3 changes: 3 additions & 0 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ func DecodeHandleToDatumMap(handle kv.Handle, handleColIDs []int64,
if id != hid {
continue
}
if types.CommonHandleNeedRestoredData(ft) {
continue
}
d, err := decodeHandleToDatum(handle, ft, idx)
if err != nil {
return row, err
Expand Down
9 changes: 9 additions & 0 deletions types/field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pingcap/parser/mysql"
ast "github.com/pingcap/parser/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/collate"
utilMath "github.com/pingcap/tidb/util/math"
)

Expand Down Expand Up @@ -1266,3 +1267,11 @@ func SetBinChsClnFlag(ft *FieldType) {

// VarStorageLen indicates this column is a variable length column.
const VarStorageLen = ast.VarStorageLen

// CommonHandleNeedRestoredData indicates whether the column can be decoded directly from the common handle.
// If can, then returns false. Otherwise returns true.
func CommonHandleNeedRestoredData(ft *FieldType) bool {
return collate.NewCollationEnabled() &&
ft.EvalType() == ETString &&
!mysql.HasBinaryFlag(ft.Flag)
}
6 changes: 6 additions & 0 deletions util/rowcodec/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ func (decoder *ChunkDecoder) tryAppendHandleColumn(colIdx int, col *ColInfo, han
}
for i, id := range decoder.handleColIDs {
if col.ID == id {
if types.CommonHandleNeedRestoredData(col.Ft) {
return false
}
coder := codec.NewDecoder(chk, decoder.loc)
_, err := coder.DecodeOne(handle.EncodedCol(i), colIdx, col.Ft)
if err != nil {
Expand Down Expand Up @@ -419,6 +422,9 @@ func (decoder *BytesDecoder) tryDecodeHandle(values [][]byte, offset int, col *C
if handle == nil {
return false
}
if types.CommonHandleNeedRestoredData(col.Ft) {
return false
}
if col.IsPKHandle || col.ID == model.ExtraHandleID {
handleData := cacheBytes
if mysql.HasUnsignedFlag(col.Ft.Flag) {
Expand Down

0 comments on commit ef57bdb

Please sign in to comment.