From db48915f874d19871252a93902eb0621b4c795b3 Mon Sep 17 00:00:00 2001
From: Yiding Cui <winoros@gmail.com>
Date: Tue, 15 Aug 2023 15:14:01 +0900
Subject: [PATCH 1/2] This is an automated cherry-pick of #46092

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
---
 planner/core/issuetest/BUILD.bazel           | 15 +++++++++++++++
 planner/core/issuetest/planner_issue_test.go |  8 ++++++++
 planner/core/logical_plan_builder.go         |  4 ++++
 3 files changed, 27 insertions(+)

diff --git a/planner/core/issuetest/BUILD.bazel b/planner/core/issuetest/BUILD.bazel
index a54a1b7361675..d65ef335d72b5 100644
--- a/planner/core/issuetest/BUILD.bazel
+++ b/planner/core/issuetest/BUILD.bazel
@@ -6,6 +6,21 @@ go_test(
     srcs = ["planner_issue_test.go"],
     flaky = True,
     race = "on",
+<<<<<<< HEAD
     shard_count = 5,
     deps = ["//testkit"],
+=======
+    shard_count = 9,
+    deps = [
+        "//parser",
+        "//planner",
+        "//planner/core",
+        "//testkit",
+        "//testkit/testdata",
+        "//testkit/testmain",
+        "//testkit/testsetup",
+        "@com_github_stretchr_testify//require",
+        "@org_uber_go_goleak//:goleak",
+    ],
+>>>>>>> a15c0177af2 (planner: DML queries might panic when CTE exists (#46092))
 )
diff --git a/planner/core/issuetest/planner_issue_test.go b/planner/core/issuetest/planner_issue_test.go
index 4978a0a580049..3b369992ab870 100644
--- a/planner/core/issuetest/planner_issue_test.go
+++ b/planner/core/issuetest/planner_issue_test.go
@@ -55,3 +55,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;")
+}
diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go
index 870c52ffb7dde..fa8c3b8be8284 100644
--- a/planner/core/logical_plan_builder.go
+++ b/planner/core/logical_plan_builder.go
@@ -4165,6 +4165,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
+			}
 			b.ctx.GetSessionVars().StmtCtx.LockTableIDs[tName.TableInfo.ID] = struct{}{}
 		}
 		p, err = b.buildSelectLock(p, l)

From aeb4077afc270bf379d74b3d4905c204f508a2a5 Mon Sep 17 00:00:00 2001
From: qw4990 <zhangyuanjia@pingcap.com>
Date: Thu, 17 Aug 2023 12:16:49 +0800
Subject: [PATCH 2/2] update readme

---
 planner/core/issuetest/BUILD.bazel | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)

diff --git a/planner/core/issuetest/BUILD.bazel b/planner/core/issuetest/BUILD.bazel
index d65ef335d72b5..411e2126d693c 100644
--- a/planner/core/issuetest/BUILD.bazel
+++ b/planner/core/issuetest/BUILD.bazel
@@ -6,21 +6,6 @@ go_test(
     srcs = ["planner_issue_test.go"],
     flaky = True,
     race = "on",
-<<<<<<< HEAD
-    shard_count = 5,
+    shard_count = 6,
     deps = ["//testkit"],
-=======
-    shard_count = 9,
-    deps = [
-        "//parser",
-        "//planner",
-        "//planner/core",
-        "//testkit",
-        "//testkit/testdata",
-        "//testkit/testmain",
-        "//testkit/testsetup",
-        "@com_github_stretchr_testify//require",
-        "@org_uber_go_goleak//:goleak",
-    ],
->>>>>>> a15c0177af2 (planner: DML queries might panic when CTE exists (#46092))
 )