diff --git a/table/tables/partition.go b/table/tables/partition.go index 800e5ff3882f1..58e24385a4c2f 100644 --- a/table/tables/partition.go +++ b/table/tables/partition.go @@ -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 } diff --git a/table/tables/partition_test.go b/table/tables/partition_test.go index 06f90938a22fc..9a2cfb38eb539 100644 --- a/table/tables/partition_test.go +++ b/table/tables/partition_test.go @@ -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) +}