From d2125017cc2b2eea140db571a7aade833e27eabc Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Wed, 2 Aug 2023 18:56:25 +0800 Subject: [PATCH 1/3] executor, planner: add extraProj for indexLookUp with order by + limit + static prune --- executor/partitiontest/partition_test.go | 11 +++++++++++ planner/core/task.go | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) 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..52cc14831f84d 100644 --- a/planner/core/task.go +++ b/planner/core/task.go @@ -1302,10 +1302,20 @@ 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 findColNameByColID(ts.Columns, extraCol) != nil { + return rootTask, true + } + + idxLookup.ExtraHandleCol = extraCol + 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 + ts.Columns = append(ts.Columns, extraInfo) ts.schema.Append(extraCol) ts.HandleIdx = []int{len(ts.Columns) - 1} From 81ace8a5ca4d0815e3d06187cc8becfcaf15ada3 Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Wed, 2 Aug 2023 19:03:57 +0800 Subject: [PATCH 2/3] update --- executor/partitiontest/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 311cd03e85fa8b80d142dd61f5374de9f5083eca Mon Sep 17 00:00:00 2001 From: Jason Mo Date: Wed, 2 Aug 2023 20:36:07 +0800 Subject: [PATCH 3/3] update --- planner/core/task.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/planner/core/task.go b/planner/core/task.go index 52cc14831f84d..dc77efda97971 100644 --- a/planner/core/task.go +++ b/planner/core/task.go @@ -1304,18 +1304,17 @@ func (p *PhysicalTopN) pushPartialTopNDownToCop(copTsk *copTask) (task, bool) { extraInfo, extraCol, hasExtraCol := tryGetPkExtraColumn(p.ctx.GetSessionVars(), tblInfo) if hasExtraCol { ts := idxLookup.TablePlans[0].(*PhysicalTableScan) - if findColNameByColID(ts.Columns, extraCol) != nil { - return rootTask, true + + 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 - 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 - ts.Columns = append(ts.Columns, extraInfo) ts.schema.Append(extraCol) ts.HandleIdx = []int{len(ts.Columns) - 1}