From 5d3b53132128cd0073b72dcf0e17b0fe51f3d379 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Fri, 20 Oct 2023 17:35:05 +0800 Subject: [PATCH 1/3] planner: add test for issue 47331 Signed-off-by: hi-rustin --- pkg/executor/explain_test.go | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pkg/executor/explain_test.go b/pkg/executor/explain_test.go index 3c720f836f4e0..ce33ef64a4cf9 100644 --- a/pkg/executor/explain_test.go +++ b/pkg/executor/explain_test.go @@ -189,6 +189,57 @@ func checkMemoryInfo(t *testing.T, tk *testkit.TestKit, sql string) { } } +func TestIssue47331(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1") + tk.MustExec(`create table t1( + id1 varchar(2) DEFAULT '00', + id2 varchar(30) NOT NULL, + id3 datetime DEFAULT NULL, + id4 varchar(100) NOT NULL DEFAULT 'ecifdata', + id5 datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + id6 int(11) DEFAULT NULL, + id7 int(11) DEFAULT NULL, + UNIQUE KEY UI_id2 (id2), + KEY ix_id1 (id1) + )`) + tk.MustExec("drop table if exists t2") + tk.MustExec(`create table t2( + id10 varchar(40) NOT NULL, + id2 varchar(30) NOT NULL, + KEY IX_id2 (id2), + PRIMARY KEY (id10) + )`) + tk.MustExec("drop table if exists t3") + tk.MustExec(`create table t3( + id20 varchar(40) DEFAULT NULL, + UNIQUE KEY IX_id20 (id20) + )`) + tk.MustExec(` + explain + UPDATE t1 a + SET a.id1 = '04', + a.id3 = CURRENT_TIMESTAMP, + a.id4 = SUBSTRING_INDEX(USER(), '@', 1), + a.id5 = CURRENT_TIMESTAMP + WHERE a.id1 = '03' + AND a.id6 - IFNULL(a.id7, 0) = + ( + SELECT COUNT(1) + FROM t2 b, t3 c + WHERE b.id10 = c.id20 + AND b.id2 = a.id2 + AND b.id2 in ( + SELECT rn.id2 + FROM t1 rn + WHERE rn.id1 = '03' + ) + ); + `) +} + func TestMemoryAndDiskUsageAfterClose(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) From 63a07c2eadf275a2ab529ea4a1987690fc83001e Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 22 Nov 2023 12:14:11 +0800 Subject: [PATCH 2/3] Clone the originalSchema Signed-off-by: hi-rustin --- pkg/planner/core/rule_join_reorder.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/planner/core/rule_join_reorder.go b/pkg/planner/core/rule_join_reorder.go index 17e598b9d473c..56da9e4bad143 100644 --- a/pkg/planner/core/rule_join_reorder.go +++ b/pkg/planner/core/rule_join_reorder.go @@ -316,7 +316,8 @@ func (s *joinReOrderSolver) optimizeRecursive(ctx sessionctx.Context, p LogicalP proj := LogicalProjection{ Exprs: expression.Column2Exprs(originalSchema.Columns), }.Init(p.SCtx(), p.SelectBlockOffset()) - proj.SetSchema(originalSchema) + // Clone the schema here, because the schema may be changed by the projection optimization later. + proj.SetSchema(originalSchema.Clone()) proj.SetChildren(p) p = proj } From 93fb2269239b7e0bbd9df9273a691a1952eb0320 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 22 Nov 2023 14:36:12 +0800 Subject: [PATCH 3/3] Better comment Signed-off-by: hi-rustin --- pkg/planner/core/rule_join_reorder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/planner/core/rule_join_reorder.go b/pkg/planner/core/rule_join_reorder.go index 56da9e4bad143..337c63457d540 100644 --- a/pkg/planner/core/rule_join_reorder.go +++ b/pkg/planner/core/rule_join_reorder.go @@ -316,7 +316,7 @@ func (s *joinReOrderSolver) optimizeRecursive(ctx sessionctx.Context, p LogicalP proj := LogicalProjection{ Exprs: expression.Column2Exprs(originalSchema.Columns), }.Init(p.SCtx(), p.SelectBlockOffset()) - // Clone the schema here, because the schema may be changed by the projection optimization later. + // Clone the schema here, because the schema may be changed by column pruning rules. proj.SetSchema(originalSchema.Clone()) proj.SetChildren(p) p = proj