From 27a83568636b656512e2537094f412d34cd20296 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 17 Dec 2019 17:42:41 +0800 Subject: [PATCH 1/2] table/tables: fix an error message when table has no partition for value --- table/tables/partition.go | 16 ++++++++-------- table/tables/partition_test.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/table/tables/partition.go b/table/tables/partition.go index 800e5ff3882f1..6fb2fc3da12f6 100644 --- a/table/tables/partition.go +++ b/table/tables/partition.go @@ -293,14 +293,14 @@ 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 ret int64 + expr := rangePartitionString(pi) + e, err := expression.ParseSimpleExprWithTableInfo(ctx, expr, t.meta) + if err == nil { + ret, _, err = e.EvalInt(ctx, chunk.MutRowFromDatums(r).ToRow()) + if err != nil { + logutil.BgLogger().Warn("locateRangePartition extrace index fail", zap.Error(err)) + } } return 0, errors.Trace(table.ErrNoPartitionForGivenValue.GenWithStackByArgs(fmt.Sprintf("%d", ret))) } 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) +} From 3257a9eed8a61bda06bb94ea6233f982d2a9b0ec Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 17 Dec 2019 20:04:47 +0800 Subject: [PATCH 2/2] tiny update --- table/tables/partition.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/table/tables/partition.go b/table/tables/partition.go index 6fb2fc3da12f6..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`. - var ret int64 - expr := rangePartitionString(pi) - e, err := expression.ParseSimpleExprWithTableInfo(ctx, expr, t.meta) - if err == nil { - ret, _, err = e.EvalInt(ctx, chunk.MutRowFromDatums(r).ToRow()) - if err != nil { - logutil.BgLogger().Warn("locateRangePartition extrace index fail", zap.Error(err)) + 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 }