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

table: fix insert value into hash partition table which not int (#21182) #21238

Merged
merged 3 commits into from
Nov 25, 2020
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
13 changes: 12 additions & 1 deletion table/tables/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,18 @@ func (t *partitionedTable) locateRangePartition(ctx sessionctx.Context, pi *mode
// TODO: supports linear hashing
func (t *partitionedTable) locateHashPartition(ctx sessionctx.Context, pi *model.PartitionInfo, r []types.Datum) (int, error) {
if col, ok := t.partitionExpr.Expr.(*expression.Column); ok {
ret := r[col.Index].GetInt64()
var data types.Datum
switch r[col.Index].Kind() {
case types.KindInt64, types.KindUint64:
data = r[col.Index]
default:
var err error
data, err = r[col.Index].ConvertTo(ctx.GetSessionVars().StmtCtx, types.NewFieldType(mysql.TypeLong))
if err != nil {
return 0, err
}
}
ret := data.GetInt64()
ret = ret % int64(t.meta.Partition.Num)
if ret < 0 {
ret = -ret
Expand Down
16 changes: 16 additions & 0 deletions table/tables/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,19 @@ partition p4 values less than (9223372036854775806))`)
tk.MustQuery("select * from t_int where id = -4294967294").Check(testkit.Rows())
tk.MustQuery("select * from t_int where id < -12345 order by id desc").Check(testkit.Rows("-429496729312", "-9223372036854775803"))
}

func (ts *testSuite) TestHashPartitionInsertValue(c *C) {
tk := testkit.NewTestKitWithInit(c, ts.store)
tk.MustExec("use test")
tk.MustExec("drop tables if exists t4")
tk.MustExec(`CREATE TABLE t4(
a bit(1) DEFAULT NULL,
b int(11) DEFAULT NULL
) PARTITION BY HASH(a)
PARTITIONS 3`)
defer tk.MustExec("drop tables if exists t4")
tk.MustExec("INSERT INTO t4 VALUES(0, 0)")
tk.MustExec("INSERT INTO t4 VALUES(1, 1)")
result := tk.MustQuery("SELECT * FROM t4 WHERE a = 1")
result.Check(testkit.Rows("\x01 1"))
}