diff --git a/executor/partitiontest/BUILD.bazel b/executor/partitiontest/BUILD.bazel index c552e0ed55be7..a9b960066fa40 100644 --- a/executor/partitiontest/BUILD.bazel +++ b/executor/partitiontest/BUILD.bazel @@ -9,7 +9,7 @@ go_test( ], flaky = True, race = "on", - shard_count = 5, + shard_count = 6, deps = [ "//testkit", "@com_github_pingcap_failpoint//:failpoint", diff --git a/executor/partitiontest/partition_test.go b/executor/partitiontest/partition_test.go index d9614cd808ec3..bd1fa03df264e 100644 --- a/executor/partitiontest/partition_test.go +++ b/executor/partitiontest/partition_test.go @@ -503,3 +503,14 @@ func TestPartitionOnMissing(t *testing.T) { " └─TableReader(Probe) 4.00 root partition:all data:TableRangeScan", " └─TableRangeScan 4.00 cop[tikv] table:tt1 range: decided by [onmissing.tt2.listid], keep order:false")) } + +func TestIssue45757(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + + tk.MustExec("use test") + tk.MustExec("create table t(id bigint, datecode bigint, c int, index idx(id, datecode)) partition by range(datecode) (partition p0 values less than (20230501), partition p1 values less than (20230601), partition p2 values less than (20230701))") + tk.MustExec("insert into t values (111111111111111, 20230403, 0), (111111111111111, 20230503, 1), (111111111111111, 20230603, 2)") + tk.MustExec("set tidb_partition_prune_mode='static'") + tk.MustQuery("select * from t use index (idx) where id = 111111111111111 and datecode between 20230420 and 20230620 order by datecode limit 2").Check(testkit.Rows("111111111111111 20230503 1", "111111111111111 20230603 2")) +} diff --git a/planner/core/task.go b/planner/core/task.go index 6d8c297ac2f71..dc77efda97971 100644 --- a/planner/core/task.go +++ b/planner/core/task.go @@ -1302,10 +1302,19 @@ func (p *PhysicalTopN) pushPartialTopNDownToCop(copTsk *copTask) (task, bool) { Count: p.Count, } extraInfo, extraCol, hasExtraCol := tryGetPkExtraColumn(p.ctx.GetSessionVars(), tblInfo) - // TODO: sometimes it will add a duplicate `_tidb_rowid` column in ts.schema() if hasExtraCol { - idxLookup.ExtraHandleCol = extraCol ts := idxLookup.TablePlans[0].(*PhysicalTableScan) + + if !p.SCtx().GetSessionVars().IsDynamicPartitionPruneEnabled() { + extraProj := PhysicalProjection{ + Exprs: expression.Column2Exprs(ts.schema.Clone().Columns), + }.Init(p.SCtx(), p.statsInfo(), p.blockOffset, nil) + extraProj.SetSchema(ts.schema.Clone()) + extraProj.SetChildren(rootTask.p) + rootTask.p = extraProj + } + + idxLookup.ExtraHandleCol = extraCol ts.Columns = append(ts.Columns, extraInfo) ts.schema.Append(extraCol) ts.HandleIdx = []int{len(ts.Columns) - 1}