Skip to content

Commit

Permalink
planner: DML queries might panic when CTE exists
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Aug 15, 2023
1 parent d4a50aa commit 25764bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion planner/core/issuetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ go_test(
data = glob(["testdata/**"]),
flaky = True,
race = "on",
shard_count = 8,
shard_count = 9,
deps = [
"//parser",
"//planner",
Expand Down
8 changes: 8 additions & 0 deletions planner/core/issuetest/planner_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,11 @@ func TestIssue45758(t *testing.T) {
// result ok
tk.MustExec("UPDATE tb1, (SELECT code AS cid, code, MAX(class) AS class FROM tb2 GROUP BY code) tb3 SET tb1.cid = tb3.cid, tb1.code = tb3.code, tb1.class = tb3.class")
}

func TestIssue46083(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("CREATE TEMPORARY TABLE v0(v1 int)")
tk.MustExec("INSERT INTO v0 WITH ta2 AS (TABLE v0) TABLE ta2 FOR UPDATE OF ta2;")
}
4 changes: 4 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4466,6 +4466,10 @@ func (b *PlanBuilder) buildSelect(ctx context.Context, sel *ast.SelectStmt) (p L
b.ctx.GetSessionVars().StmtCtx.AppendWarning(err)
}
for _, tName := range l.Tables {
// CTE has no *model.TableInfo, we need to skip it.
if tName.TableInfo == nil {
continue

Check warning on line 4471 in planner/core/logical_plan_builder.go

View check run for this annotation

Codecov / codecov/patch

planner/core/logical_plan_builder.go#L4469-L4471

Added lines #L4469 - L4471 were not covered by tests
}
b.ctx.GetSessionVars().StmtCtx.LockTableIDs[tName.TableInfo.ID] = struct{}{}
}
p, err = b.buildSelectLock(p, l)
Expand Down

0 comments on commit 25764bc

Please sign in to comment.