Skip to content

Commit

Permalink
cherry pick pingcap#34449 to release-6.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
time-and-fate authored and ti-srebot committed May 9, 2022
1 parent 36a9810 commit 979453a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
36 changes: 35 additions & 1 deletion planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,40 @@ END:
}

func (p *LogicalMemTable) findBestTask(prop *property.PhysicalProperty, planCounter *PlanCounterTp, opt *physicalOptimizeOp) (t task, cntPlan int64, err error) {
if prop.MPPPartitionTp != property.AnyType {
return invalidTask, 0, nil
}

// If prop.CanAddEnforcer is true, the prop.SortItems need to be set nil for p.findBestTask.
// Before function return, reset it for enforcing task prop.
oldProp := prop.CloneEssentialFields()
if prop.CanAddEnforcer {
// First, get the bestTask without enforced prop
prop.CanAddEnforcer = false
cnt := int64(0)
t, cnt, err = p.findBestTask(prop, planCounter, opt)
if err != nil {
return nil, 0, err
}
prop.CanAddEnforcer = true
if t != invalidTask {
cntPlan = cnt
return
}
// Next, get the bestTask with enforced prop
prop.SortItems = []property.SortItem{}
}
defer func() {
if err != nil {
return
}
if prop.CanAddEnforcer {
*prop = *oldProp
t = enforceProperty(prop, t, p.basePlan.ctx)
prop.CanAddEnforcer = true
}
}()

if !prop.IsEmpty() || planCounter.Empty() {
return invalidTask, 0, nil
}
Expand Down Expand Up @@ -740,7 +774,7 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter
return
}
var cnt int64
// If prop.enforced is true, the prop.cols need to be set nil for ds.findBestTask.
// If prop.CanAddEnforcer is true, the prop.SortItems need to be set nil for ds.findBestTask.
// Before function return, reset it for enforcing task prop and storing map<prop,task>.
oldProp := prop.CloneEssentialFields()
if prop.CanAddEnforcer {
Expand Down
42 changes: 42 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6430,3 +6430,45 @@ func TestIssue33042(t *testing.T) {
),
)
}
<<<<<<< HEAD
=======

func TestIssue29663(t *testing.T) {
store, _, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1 (a int, b int)")
tk.MustExec("create table t2 (c int, d int)")
tk.MustExec("insert into t1 values(1, 1), (1,2),(2,1),(2,2)")
tk.MustExec("insert into t2 values(1, 3), (1,4),(2,5),(2,6)")

tk.MustQuery("explain select one.a from t1 one order by (select two.d from t2 two where two.c = one.b)").Check(testkit.Rows(
"Projection_16 10000.00 root test.t1.a",
"└─Sort_17 10000.00 root test.t2.d",
" └─Apply_20 10000.00 root CARTESIAN left outer join",
" ├─TableReader_22(Build) 10000.00 root data:TableFullScan_21",
" │ └─TableFullScan_21 10000.00 cop[tikv] table:one keep order:false, stats:pseudo",
" └─MaxOneRow_23(Probe) 1.00 root ",
" └─TableReader_26 2.00 root data:Selection_25",
" └─Selection_25 2.00 cop[tikv] eq(test.t2.c, test.t1.b)",
" └─TableFullScan_24 2000.00 cop[tikv] table:two keep order:false, stats:pseudo"))
}

func TestIssue31609(t *testing.T) {
store, _, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

tk.MustQuery("explain select rank() over (partition by table_name) from information_schema.tables").Check(testkit.Rows(
"Projection_7 10000.00 root Column#27",
"└─Shuffle_11 10000.00 root execution info: concurrency:5, data sources:[MemTableScan_9]",
" └─Window_8 10000.00 root rank()->Column#27 over(partition by Column#3)",
" └─Sort_10 10000.00 root Column#3",
" └─MemTableScan_9 10000.00 root table:TABLES ",
))
}
>>>>>>> 1f4fd0720... planner: enable memtable operator support enforced property (#34449)

0 comments on commit 979453a

Please sign in to comment.