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/tables: fix an error message when table has no partition for value (#14099) #14107

Merged
merged 2 commits into from
Dec 18, 2019
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
22 changes: 13 additions & 9 deletions table/tables/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,20 @@ func (t *partitionedTable) locateRangePartition(ctx sessionctx.Context, pi *mode
}
if idx < 0 || idx >= len(partitionExprs) {
// The data does not belong to any of the partition returns `table has no partition for value %s`.
e, err := expression.ParseSimpleExprWithTableInfo(ctx, pi.Expr, t.meta)
if err != nil {
return 0, errors.Trace(err)
}

ret, _, err2 := e.EvalInt(ctx, chunk.MutRowFromDatums(r).ToRow())
if err2 != nil {
return 0, errors.Trace(err2)
var valueMsg string
if pi.Expr != "" {
e, err := expression.ParseSimpleExprWithTableInfo(ctx, pi.Expr, t.meta)
if err == nil {
val, _, err := e.EvalInt(ctx, chunk.MutRowFromDatums(r).ToRow())
if err == nil {
valueMsg = fmt.Sprintf("%d", val)
}
}
} else {
// When the table is partitioned by range columns.
valueMsg = "from column_list"
}
return 0, errors.Trace(table.ErrNoPartitionForGivenValue.GenWithStackByArgs(fmt.Sprintf("%d", ret)))
return 0, table.ErrNoPartitionForGivenValue.GenWithStackByArgs(valueMsg)
}
return idx, nil
}
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 @@ -292,3 +292,19 @@ func (ts *testSuite) TestGeneratePartitionExpr(c *C) {
c.Assert(expr.String(), Equals, upperBounds[i])
}
}

func (ts *testSuite) TestLocateRangePartitionErr(c *C) {
tk := testkit.NewTestKitWithInit(c, ts.store)
tk.MustExec("use test")
tk.MustExec(`CREATE TABLE t_month_data_monitor (
id int(20) NOT NULL AUTO_INCREMENT,
data_date date NOT NULL,
PRIMARY KEY (id, data_date)
) PARTITION BY RANGE COLUMNS(data_date) (
PARTITION p20190401 VALUES LESS THAN ('2019-04-02'),
PARTITION p20190402 VALUES LESS THAN ('2019-04-03')
)`)

_, err := tk.Exec("INSERT INTO t_month_data_monitor VALUES (4, '2019-04-04')")
c.Assert(table.ErrNoPartitionForGivenValue.Equal(err), IsTrue)
}