From 993cadf0879294cadc6a8f39579d20e3725447ab Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Fri, 10 Sep 2021 16:38:39 +0800 Subject: [PATCH] planner: fix wrong plan caused by shallow copy schema columns (#27798) (#27836) --- planner/core/integration_test.go | 30 ++++++++++++++++++++++ planner/core/rule_aggregation_push_down.go | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 4957c29223fbb..8ef77e8eebf26 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -2095,3 +2095,33 @@ func (s *testIntegrationSuite) TestIssue26559(c *C) { tk.MustExec("insert into t values('2020-07-29 09:07:01', '2020-07-27 16:57:36');") tk.MustQuery("select greatest(a, b) from t union select null;").Sort().Check(testkit.Rows("2020-07-29 09:07:01", "")) } + +func (s *testIntegrationSuite) TestIssue27797(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t27797") + tk.MustExec("create table t27797(a int, b int, c int, d int) " + + "partition by range columns(d) (" + + "partition p0 values less than (20)," + + "partition p1 values less than(40)," + + "partition p2 values less than(60));") + tk.MustExec("insert into t27797 values(1,1,1,1), (2,2,2,2), (22,22,22,22), (44,44,44,44);") + tk.MustExec("set sql_mode='';") + result := tk.MustQuery("select count(*) from (select a, b from t27797 where d > 1 and d < 60 and b > 0 group by b, c) tt;") + result.Check(testkit.Rows("3")) + + tk.MustExec("drop table if exists IDT_HP24172") + tk.MustExec("CREATE TABLE `IDT_HP24172` ( " + + "`COL1` mediumint(16) DEFAULT NULL, " + + "`COL2` varchar(20) DEFAULT NULL, " + + "`COL4` datetime DEFAULT NULL, " + + "`COL3` bigint(20) DEFAULT NULL, " + + "`COL5` float DEFAULT NULL, " + + "KEY `UM_COL` (`COL1`,`COL3`) " + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin " + + "PARTITION BY HASH( `COL1`+`COL3` ) " + + "PARTITIONS 8;") + tk.MustExec("insert into IDT_HP24172(col1) values(8388607);") + result = tk.MustQuery("select col2 from IDT_HP24172 where col1 = 8388607 and col1 in (select col1 from IDT_HP24172);") + result.Check(testkit.Rows("")) +} diff --git a/planner/core/rule_aggregation_push_down.go b/planner/core/rule_aggregation_push_down.go index 0c796e4b0c3f4..8668efc2138dd 100644 --- a/planner/core/rule_aggregation_push_down.go +++ b/planner/core/rule_aggregation_push_down.go @@ -384,7 +384,7 @@ func (a *aggregationPushDownSolver) tryAggPushDownForUnion(union *LogicalUnionAl } newChildren = append(newChildren, newChild) } - union.SetSchema(expression.NewSchema(newChildren[0].Schema().Columns...)) + union.SetSchema(expression.NewSchema(newChildren[0].Schema().Clone().Columns...)) union.SetChildren(newChildren...) return nil }