Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planer: fix invalid pointer caused by a recursive CTE query #54450

Merged
merged 8 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,7 @@ func findBestTask4LogicalCTE(p *LogicalCTE, prop *property.PhysicalProperty, cou

func findBestTask4LogicalCTETable(p *LogicalCTETable, prop *property.PhysicalProperty, _ *base.PlanCounterTp, _ *optimizetrace.PhysicalOptimizeOp) (t base.Task, cntPlan int64, err error) {
if !prop.IsSortItemEmpty() {
return nil, 1, nil
return base.InvalidTask, 0, nil
}

pcteTable := PhysicalCTETable{IDForStorage: p.IDForStorage}.Init(p.SCtx(), p.StatsInfo())
Expand Down
1 change: 1 addition & 0 deletions pkg/planner/core/issuetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_test(
data = glob(["testdata/**"]),
flaky = True,
race = "on",
shard_count = 3,
deps = [
"//pkg/parser",
"//pkg/planner",
Expand Down
50 changes: 50 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,53 @@ 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.MustQuery(`explain 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
);`).Check(testkit.Rows(
"Projection_54 9990.00 root 1->Column#17",
"└─IndexJoin_59 9990.00 root inner join, inner:IndexReader_58, outer key:test.p.groupid, inner key:test.g.groupid, equal cond:eq(test.p.groupid, test.g.groupid)",
" ├─HashAgg_75(Build) 12800.00 root group by:test.p.groupid, funcs:firstrow(test.p.groupid)->test.p.groupid",
" │ └─Selection_72 12800.00 root not(isnull(test.p.groupid))",
" │ └─CTEFullScan_73 16000.00 root CTE:w data:CTE_0",
" └─IndexReader_58(Probe) 9990.00 root index:Selection_57",
" └─Selection_57 9990.00 cop[tikv] not(isnull(test.g.groupid))",
" └─IndexRangeScan_56 10000.00 cop[tikv] table:g, index:k2(groupid, parentid) range: decided by [eq(test.g.groupid, test.p.groupid)], keep order:false, stats:pseudo",
"CTE_0 16000.00 root Recursive CTE",
"├─IndexReader_24(Seed Part) 10000.00 root index:IndexFullScan_23",
"│ └─IndexFullScan_23 10000.00 cop[tikv] table:p, index:k1(groupid) keep order:false, stats:pseudo",
"└─IndexHashJoin_34(Recursive Part) 10000.00 root inner join, inner:IndexLookUp_31, outer key:test.p.groupid, inner key:test.g.parentid, equal cond:eq(test.p.groupid, test.g.parentid)",
" ├─Selection_51(Build) 8000.00 root not(isnull(test.p.groupid))",
" │ └─CTETable_52 10000.00 root Scan on CTE_0",
" └─IndexLookUp_31(Probe) 10000.00 root ",
" ├─IndexRangeScan_29(Build) 10000.00 cop[tikv] table:g, index:k1(parentid) range: decided by [eq(test.g.parentid, test.p.groupid)], keep order:false, stats:pseudo",
" └─TableRowIDScan_30(Probe) 10000.00 cop[tikv] table:g keep order:false, stats:pseudo"))
tk.MustQuery(`show warnings`).Check(testkit.Rows())
}
1 change: 1 addition & 0 deletions pkg/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestMain(m *testing.M) {

opts := []goleak.Option{
goleak.IgnoreTopFunction("github.com/dgraph-io/ristretto.(*defaultPolicy).processItems"),
goleak.IgnoreTopFunction("github.com/dgraph-io/ristretto.(*Cache).processItems"),
goleak.IgnoreTopFunction("github.com/golang/glog.(*fileSink).flushDaemon"),
goleak.IgnoreTopFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"),
goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"),
Expand Down