Skip to content

Commit

Permalink
table/tables: fix an error message when table has no partition for va…
Browse files Browse the repository at this point in the history
…lue (#14099) (#14107)
  • Loading branch information
sre-bot authored and ngaut committed Dec 18, 2019
1 parent d0502a6 commit 1866f27
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
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)
}

0 comments on commit 1866f27

Please sign in to comment.