Skip to content

Commit

Permalink
planer: fix invalid pointer caused by a recursive CTE query
Browse files Browse the repository at this point in the history
Signed-off-by: Weizhen Wang <[email protected]>
  • Loading branch information
hawkingrei committed Jul 16, 2024
1 parent 9f1d9e6 commit 839dc05
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pkg/planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ func enumeratePhysicalPlans4Task(
if _, ok := p.Self().(*LogicalSequence); ok {
iteration = iterateChildPlan4LogicalSequence
}

if !p.SCtx().GetSessionVars().InRestrictedSQL {
fmt.Println("fuck")
}
for _, pp := range physicalPlans {
timeStampNow := p.GetLogicalTS4TaskMap()
savedPlanID := p.SCtx().GetSessionVars().PlanID.Load()
Expand Down
32 changes: 32 additions & 0 deletions pkg/planner/core/issuetest/planner_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,35 @@ func Test53726(t *testing.T) {
" └─TableReader_11 2.00 root data:TableFullScan_10",
" └─TableFullScan_10 2.00 cop[tikv] table:t7 keep order:false"))
}

func TestIssue54449(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("CREATE TABLE p ( groupid bigint(20) DEFAULT NULL, KEY k1 (groupid));")
tk.MustExec(`CREATE TABLE g (groupid bigint(20) DEFAULT NULL,parentid bigint(20) NOT NULL,KEY k1 (parentid),KEY k2 (groupid,parentid));`)
tk.MustExec(`set tidb_opt_enable_hash_join=off;`)
tk.MustExec(`WITH RECURSIVE w(gid) AS (
SELECT
groupId
FROM
p
UNION
SELECT
g.groupId
FROM
g
JOIN w ON g.parentId = w.gid
)
SELECT
1
FROM
g
WHERE
g.groupId IN (
SELECT
gid
FROM
w
);`)
}
4 changes: 3 additions & 1 deletion pkg/planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,9 @@ func physicalOptimize(logic base.LogicalPlan, planCounter *base.PlanCounterTp) (
}
}()
}

if !logic.SCtx().GetSessionVars().InRestrictedSQL {
fmt.Println("fuck")
}
logic.SCtx().GetSessionVars().StmtCtx.TaskMapBakTS = 0
t, _, err := logic.FindBestTask(prop, planCounter, opt)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package core

import (
"fmt"
"math"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -1088,6 +1089,9 @@ func (p *PhysicalUnionAll) Attach2Task(tasks ...base.Task) base.Task {

// Attach2Task implements PhysicalPlan interface.
func (sel *PhysicalSelection) Attach2Task(tasks ...base.Task) base.Task {
if tasks[0] == nil {
fmt.Println("fuck")
}
if mppTask, _ := tasks[0].(*MppTask); mppTask != nil { // always push to mpp task.
if expression.CanExprsPushDown(GetPushDownCtx(sel.SCtx()), sel.Conditions, kv.TiFlash) {
return attachPlan2Task(sel, mppTask.Copy())
Expand Down

0 comments on commit 839dc05

Please sign in to comment.