Skip to content

Commit

Permalink
table: Avoid encode KindMaxValue of string type (pingcap#31743)
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 authored and wshwsh12 committed Feb 16, 2022
1 parent fe635fb commit 2506c86
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
33 changes: 18 additions & 15 deletions table/tables/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,8 @@ func (lp *ForListColumnPruning) LocatePartition(sc *stmtctx.StatementContext, v

// LocateRanges locates partition ranges by the column range
func (lp *ForListColumnPruning) LocateRanges(sc *stmtctx.StatementContext, r *ranger.Range) ([]ListPartitionLocation, error) {
var err error
var lowKey, highKey []byte
lowVal := r.LowVal[0]
if r.LowVal[0].Kind() == types.KindMinNotNull {
lowVal = types.GetMinValue(lp.ExprCol.GetType())
Expand All @@ -831,23 +833,24 @@ func (lp *ForListColumnPruning) LocateRanges(sc *stmtctx.StatementContext, r *ra
if r.HighVal[0].Kind() == types.KindMaxValue {
highVal = types.GetMaxValue(lp.ExprCol.GetType())
}
lowKey, err := lp.genKey(sc, lowVal)
if err != nil {
return nil, errors.Trace(err)
}
highKey, err := lp.genKey(sc, highVal)
if err != nil {
return nil, errors.Trace(err)
}

if lp.ExprCol.GetType().EvalType() == types.ETString {
// for string type, values returned by GetMinValue and GetMaxValue are already encoded,
// so it's unnecessary to invoke genKey to encode them.
if r.LowVal[0].Kind() == types.KindMinNotNull {
lowKey = (&lowVal).GetBytes()
// For string type, values returned by GetMinValue and GetMaxValue are already encoded,
// so it's unnecessary to invoke genKey to encode them.
if lp.ExprCol.GetType().EvalType() == types.ETString && r.LowVal[0].Kind() == types.KindMinNotNull {
lowKey = (&lowVal).GetBytes()
} else {
lowKey, err = lp.genKey(sc, lowVal)
if err != nil {
return nil, errors.Trace(err)
}
if r.HighVal[0].Kind() == types.KindMaxValue {
highKey = (&highVal).GetBytes()
}

if lp.ExprCol.GetType().EvalType() == types.ETString && r.HighVal[0].Kind() == types.KindMaxValue {
highKey = (&highVal).GetBytes()
} else {
highKey, err = lp.genKey(sc, highVal)
if err != nil {
return nil, errors.Trace(err)
}
}

Expand Down
15 changes: 15 additions & 0 deletions table/tables/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,18 @@ func TestIssue24746(t *testing.T) {
err = tk.ExecToErr("insert into t_24746 partition (p1) values(4,'ERROR, not allowed to read from partition p0',4) on duplicate key update a = a + 1, b = 'ERROR, not allowed to read from p0!'")
require.True(t, table.ErrRowDoesNotMatchGivenPartitionSet.Equal(err))
}

func TestIssue31721(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set tidb_enable_list_partition=on;")
tk.MustExec("drop tables if exists t_31721")
tk.MustExec("CREATE TABLE `t_31721` (`COL1` char(1) NOT NULL) CHARSET=utf8mb4 COLLATE=utf8mb4_bin PARTITION BY LIST COLUMNS(`COL1`) " +
"(PARTITION `P0` VALUES IN ('1')," +
"PARTITION `P1` VALUES IN ('2')," +
"PARTITION `P2` VALUES IN ('3'));")
tk.MustExec("insert into t_31721 values ('1')")
tk.MustExec("select * from t_31721 partition(p0, p1) where col1 != 2;")
}

0 comments on commit 2506c86

Please sign in to comment.