Skip to content

Commit

Permalink
executor, planner: add extraProj for indexLookUp with order by + limi…
Browse files Browse the repository at this point in the history
…t + static prune (#45771)

close #45757
  • Loading branch information
Defined2014 authored Aug 9, 2023
1 parent c620def commit a923897
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion executor/partitiontest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ go_test(
],
flaky = True,
race = "on",
shard_count = 5,
shard_count = 6,
deps = [
"//testkit",
"@com_github_pingcap_failpoint//:failpoint",
Expand Down
11 changes: 11 additions & 0 deletions executor/partitiontest/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
13 changes: 11 additions & 2 deletions planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit a923897

Please sign in to comment.