Skip to content

Commit

Permalink
planner: Comparing binary string with int caused issue in partition p…
Browse files Browse the repository at this point in the history
…runing (#52041)

close #52023
  • Loading branch information
mjonss authored Mar 25, 2024
1 parent 12af2e4 commit b8b3b92
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/planner/core/casetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ go_test(
],
data = glob(["testdata/**"]),
flaky = True,
shard_count = 20,
shard_count = 21,
deps = [
"//pkg/domain",
"//pkg/parser",
Expand Down
35 changes: 35 additions & 0 deletions pkg/planner/core/casetest/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,38 @@ func TestFixControl45132(t *testing.T) {
tk.MustExec(`set @@tidb_opt_fix_control = "45132:0"`)
tk.MustHavePlan(`select * from t where a=2`, `TableFullScan`)
}

func TestIssue52023(t *testing.T) {
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`CREATE TABLE t (
a binary(1) NOT NULL,
PRIMARY KEY (a)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE COLUMNS(a)
(PARTITION P0 VALUES LESS THAN (_binary 0x03),
PARTITION P4 VALUES LESS THAN (_binary 0xc0),
PARTITION PMX VALUES LESS THAN (MAXVALUE))`)
tk.MustExec(`insert into t values (0x5)`)
tk.MustExec(`analyze table t`)
tk.MustQuery(`select * from t`).Check(testkit.Rows("\u0005"))
tk.MustQuery(`select * from t where a = 0x5`).Check(testkit.Rows("\u0005"))
tk.MustQuery(`select * from t where a = 5`).Check(testkit.Rows())
tk.MustQuery(`select * from t where a IN (5,55)`).Check(testkit.Rows())
tk.MustQuery(`select * from t where a IN (0x5,55)`).Check(testkit.Rows("\u0005"))
tk.MustQuery(`explain select * from t where a = 0x5`).Check(testkit.Rows("Point_Get_1 1.00 root table:t, partition:P4, clustered index:PRIMARY(a) "))
tk.MustQuery(`explain format='brief' select * from t where a = 5`).Check(testkit.Rows(""+
"TableReader 0.80 root partition:all data:Selection",
"└─Selection 0.80 cop[tikv] eq(cast(test.t.a, double BINARY), 5)",
" └─TableFullScan 1.00 cop[tikv] table:t keep order:false"))
tk.MustQuery(`explain format='brief' select * from t where a IN (5,55)`).Check(testkit.Rows(""+
"TableReader 0.96 root partition:all data:Selection",
"└─Selection 0.96 cop[tikv] or(eq(cast(test.t.a, double BINARY), 5), eq(cast(test.t.a, double BINARY), 55))",
" └─TableFullScan 1.00 cop[tikv] table:t keep order:false"))
tk.MustQuery(`explain format='brief' select * from t where a IN (0x5,55)`).Check(testkit.Rows(""+
"TableReader 1.00 root partition:all data:Selection",
"└─Selection 1.00 cop[tikv] or(eq(test.t.a, \"0x05\"), eq(cast(test.t.a, double BINARY), 55))",
" └─TableFullScan 1.00 cop[tikv] table:t keep order:false"))
}
6 changes: 3 additions & 3 deletions pkg/planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1706,12 +1706,12 @@ func getNameValuePairs(ctx PlanContext, tbl *model.TableInfo, tblName model.CISt
d.SetString(d.GetString(), col.FieldType.GetCollate())
}

if col.GetType() == mysql.TypeString && col.GetCollate() == charset.CollationBin { // This type we needn't to pad `\0` in here.
return append(nvPairs, nameValuePair{colName: colName.Name.Name.L, colFieldType: &col.FieldType, value: d, con: con}), false
}
if !checkCanConvertInPointGet(col, d) {
return nil, false
}
if col.GetType() == mysql.TypeString && col.GetCollate() == charset.CollationBin { // This type we needn't to pad `\0` in here.
return append(nvPairs, nameValuePair{colName: colName.Name.Name.L, colFieldType: &col.FieldType, value: d, con: con}), false
}
dVal, err := d.ConvertTo(stmtCtx.TypeCtx(), &col.FieldType)
if err != nil {
if terror.ErrorEqual(types.ErrOverflow, err) {
Expand Down

0 comments on commit b8b3b92

Please sign in to comment.