Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: Comparing binary string with int caused issue in partition pruning #52041

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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