diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index a8db9802903cd..180bf6c6ffba4 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -3993,6 +3993,12 @@ func (s *testIntegrationSuite) TestIssue26559(c *C) { func (s *testIntegrationSuite) TestIssue27797(c *C) { tk := testkit.NewTestKit(c, s.store) + origin := tk.MustQuery("SELECT @@session.tidb_partition_prune_mode") + originStr := origin.Rows()[0][0].(string) + defer func() { + tk.MustExec("set @@session.tidb_partition_prune_mode = '" + originStr + "'") + }() + tk.MustExec("set @@session.tidb_partition_prune_mode = 'static'") tk.MustExec("use test") tk.MustExec("drop table if exists t27797") tk.MustExec("create table t27797(a int, b int, c int, d int) " + @@ -4066,6 +4072,7 @@ func (s *testIntegrationSuite) TestIssues27130(c *C) { " └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo", )) +<<<<<<< HEAD tk.MustExec("drop table if exists t2") tk.MustExec("create table t2( a enum('y','b','Abc','null'),b enum('y','b','Abc','null'),key(a, b));") tk.MustQuery(`explain format='brief' select * from t2 where a like "A%"`).Check(testkit.Rows( @@ -4078,6 +4085,34 @@ func (s *testIntegrationSuite) TestIssues27130(c *C) { "└─Selection 8000.00 cop[tikv] like(test.t2.a, \"A%\", 92), like(test.t2.b, \"A%\", 92)", " └─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo", )) +======= +func (s *testIntegrationSuite) TestIssue29705(c *C) { + tk := testkit.NewTestKit(c, s.store) + origin := tk.MustQuery("SELECT @@session.tidb_partition_prune_mode") + originStr := origin.Rows()[0][0].(string) + defer func() { + tk.MustExec("set @@session.tidb_partition_prune_mode = '" + originStr + "'") + }() + tk.MustExec("set @@session.tidb_partition_prune_mode = 'static'") + tk.MustExec("use test") + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t(id int) partition by hash(id) partitions 4;") + tk.MustExec("insert into t values(1);") + result := tk.MustQuery("SELECT COUNT(1) FROM ( SELECT COUNT(1) FROM t b GROUP BY id) a;") + result.Check(testkit.Rows("1")) +} + +func (s *testIntegrationSerialSuite) TestIssue30271(c *C) { + defer collate.SetNewCollationEnabledForTest(false) + collate.SetNewCollationEnabledForTest(true) + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a char(10), b char(10), c char(10), index (a, b, c)) collate utf8mb4_bin;") + tk.MustExec("insert into t values ('b', 'a', '1'), ('b', 'A', '2'), ('c', 'a', '3');") + tk.MustExec("set names utf8mb4 collate utf8mb4_general_ci;") + tk.MustQuery("select * from t where (a>'a' and b='a') or (b = 'A' and a < 'd') order by a,c;").Check(testkit.Rows("b a 1", "b A 2", "c a 3")) +>>>>>>> 4785dc4a6... planner: fix inconsistent schema between UnionAll and child operator (#30231) tk.MustExec("drop table if exists t3") tk.MustExec("create table t3( a int,b enum('y','b','Abc','null'), c enum('y','b','Abc','null'),key(a, b, c));") diff --git a/planner/core/rule_column_pruning.go b/planner/core/rule_column_pruning.go index 9ff7d1d3689aa..740ed7fbb721c 100644 --- a/planner/core/rule_column_pruning.go +++ b/planner/core/rule_column_pruning.go @@ -225,6 +225,23 @@ func (p *LogicalUnionAll) PruneColumns(parentUsedCols []*expression.Column) erro p.schema.Columns = append(p.schema.Columns[:i], p.schema.Columns[i+1:]...) } } + // It's possible that the child operator adds extra columns to the schema. + // Currently, (*LogicalAggregation).PruneColumns() might do this. + // But we don't need such columns, so we add an extra Projection to prune this column when this happened. + for i, child := range p.Children() { + if p.schema.Len() < child.Schema().Len() { + schema := p.schema.Clone() + exprs := make([]expression.Expression, len(p.schema.Columns)) + for j, col := range schema.Columns { + exprs[j] = col + } + proj := LogicalProjection{Exprs: exprs, AvoidColumnEvaluator: true}.Init(p.ctx, p.blockOffset) + proj.SetSchema(schema) + + proj.SetChildren(child) + p.children[i] = proj + } + } } return nil }