From d093a5faaf313fe4ec32b18d7ca814bb4fd54f13 Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Wed, 3 Mar 2021 16:22:55 +0800 Subject: [PATCH] cherry pick #22938 to release-4.0 Signed-off-by: ti-srebot --- executor/executor_test.go | 17 +++++++++++++++++ planner/core/rule_partition_processor.go | 11 ++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index e310abc017c96..be1613bcf3d60 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -4064,6 +4064,23 @@ func (s *testSuiteP1) TestSelectPartition(c *C) { tk.MustQuery("select a, b from th where b>10").Check(testkit.Rows("11 11")) tk.MustExec("commit") tk.MustQuery("select a, b from th where b>10").Check(testkit.Rows("11 11")) + + // test partition function is scalar func + tk.MustExec("drop table if exists tscalar") + tk.MustExec(`create table tscalar (c1 int) partition by range (c1 % 30) ( + partition p0 values less than (0), + partition p1 values less than (10), + partition p2 values less than (20), + partition pm values less than (maxvalue));`) + tk.MustExec("insert into tscalar values(0), (10), (40), (50), (55)") + // test IN expression + tk.MustExec("insert into tscalar values(-0), (-10), (-40), (-50), (-55)") + tk.MustQuery("select * from tscalar where c1 in (55, 55)").Check(testkit.Rows("55")) + tk.MustQuery("select * from tscalar where c1 in (40, 40)").Check(testkit.Rows("40")) + tk.MustQuery("select * from tscalar where c1 in (40)").Check(testkit.Rows("40")) + tk.MustQuery("select * from tscalar where c1 in (-40)").Check(testkit.Rows("-40")) + tk.MustQuery("select * from tscalar where c1 in (-40, -40)").Check(testkit.Rows("-40")) + tk.MustQuery("select * from tscalar where c1 in (-1)").Check(testkit.Rows()) } func (s *testSuiteP1) TestDeletePartition(c *C) { diff --git a/planner/core/rule_partition_processor.go b/planner/core/rule_partition_processor.go index 12b7d53ab9f80..cc570d2577e84 100644 --- a/planner/core/rule_partition_processor.go +++ b/planner/core/rule_partition_processor.go @@ -509,7 +509,16 @@ func partitionRangeForInExpr(sctx sessionctx.Context, args []expression.Expressi default: return pruner.fullRange() } - val, err := constExpr.Value.ToInt64(sctx.GetSessionVars().StmtCtx) + + var val int64 + var err error + if pruner.partFn != nil { + // replace fn(col) to fn(const) + partFnConst := replaceColumnWithConst(pruner.partFn, constExpr) + val, _, err = partFnConst.EvalInt(sctx, chunk.Row{}) + } else { + val, err = constExpr.Value.ToInt64(sctx.GetSessionVars().StmtCtx) + } if err != nil { return pruner.fullRange() }