Skip to content

Commit 71c8e1e

Browse files
erjiaqingzz-jason
authored andcommitted
planner: fix wrong column calculation in ColumnPruning for LogicalUnionAll (#10384) (#10419)
1 parent 26d05fa commit 71c8e1e

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

planner/core/logical_plan_test.go

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,109 +1116,120 @@ func (s *testPlanSuite) TestColumnPruning(c *C) {
11161116
{
11171117
sql: "select count(*) from t group by a",
11181118
ans: map[int][]string{
1119-
1: {"a"},
1119+
1: {"test.t.a"},
11201120
},
11211121
},
11221122
{
11231123
sql: "select count(*) from t",
11241124
ans: map[int][]string{
1125-
1: {},
1125+
1: {"test.t._tidb_rowid"},
11261126
},
11271127
},
11281128
{
11291129
sql: "select count(*) from t a join t b where a.a < 1",
11301130
ans: map[int][]string{
1131-
1: {"a"},
1132-
2: {},
1131+
1: {"test.a.a"},
1132+
2: {"test.t._tidb_rowid"},
11331133
},
11341134
},
11351135
{
11361136
sql: "select count(*) from t a join t b on a.a = b.d",
11371137
ans: map[int][]string{
1138-
1: {"a"},
1139-
2: {"d"},
1138+
1: {"test.a.a"},
1139+
2: {"test.b.d"},
11401140
},
11411141
},
11421142
{
11431143
sql: "select count(*) from t a join t b on a.a = b.d order by sum(a.d)",
11441144
ans: map[int][]string{
1145-
1: {"a", "d"},
1146-
2: {"d"},
1145+
1: {"test.a.a", "test.a.d"},
1146+
2: {"test.b.d"},
11471147
},
11481148
},
11491149
{
11501150
sql: "select count(b.a) from t a join t b on a.a = b.d group by b.b order by sum(a.d)",
11511151
ans: map[int][]string{
1152-
1: {"a", "d"},
1153-
2: {"a", "b", "d"},
1152+
1: {"test.a.a", "test.a.d"},
1153+
2: {"test.b.a", "test.b.b", "test.b.d"},
11541154
},
11551155
},
11561156
{
11571157
sql: "select * from (select count(b.a) from t a join t b on a.a = b.d group by b.b having sum(a.d) < 0) tt",
11581158
ans: map[int][]string{
1159-
1: {"a", "d"},
1160-
2: {"a", "b", "d"},
1159+
1: {"test.a.a", "test.a.d"},
1160+
2: {"test.b.a", "test.b.b", "test.b.d"},
11611161
},
11621162
},
11631163
{
11641164
sql: "select (select count(a) from t where b = k.a) from t k",
11651165
ans: map[int][]string{
1166-
1: {"a"},
1167-
3: {"a", "b"},
1166+
1: {"test.k.a"},
1167+
3: {"test.t.a", "test.t.b"},
11681168
},
11691169
},
11701170
{
11711171
sql: "select exists (select count(*) from t where b = k.a) from t k",
11721172
ans: map[int][]string{
1173-
1: {},
1173+
1: {"test.t._tidb_rowid"},
11741174
},
11751175
},
11761176
{
11771177
sql: "select b = (select count(*) from t where b = k.a) from t k",
11781178
ans: map[int][]string{
1179-
1: {"a", "b"},
1180-
3: {"b"},
1179+
1: {"test.k.a", "test.k.b"},
1180+
3: {"test.t.b"},
11811181
},
11821182
},
11831183
{
11841184
sql: "select exists (select count(a) from t where b = k.a group by b) from t k",
11851185
ans: map[int][]string{
1186-
1: {"a"},
1187-
3: {"b"},
1186+
1: {"test.k.a"},
1187+
3: {"test.t.b"},
11881188
},
11891189
},
11901190
{
11911191
sql: "select a as c1, b as c2 from t order by 1, c1 + c2 + c",
11921192
ans: map[int][]string{
1193-
1: {"a", "b", "c"},
1193+
1: {"test.t.a", "test.t.b", "test.t.c"},
11941194
},
11951195
},
11961196
{
11971197
sql: "select a from t where b < any (select c from t)",
11981198
ans: map[int][]string{
1199-
1: {"a", "b"},
1200-
3: {"c"},
1199+
1: {"test.t.a", "test.t.b"},
1200+
3: {"test.t.c"},
12011201
},
12021202
},
12031203
{
12041204
sql: "select a from t where (b,a) != all (select c,d from t)",
12051205
ans: map[int][]string{
1206-
1: {"a", "b"},
1207-
3: {"c", "d"},
1206+
1: {"test.t.a", "test.t.b"},
1207+
3: {"test.t.c", "test.t.d"},
12081208
},
12091209
},
12101210
{
12111211
sql: "select a from t where (b,a) in (select c,d from t)",
12121212
ans: map[int][]string{
1213-
1: {"a", "b"},
1214-
3: {"c", "d"},
1213+
1: {"test.t.a", "test.t.b"},
1214+
3: {"test.t.c", "test.t.d"},
12151215
},
12161216
},
12171217
{
12181218
sql: "select a from t where a in (select a from t s group by t.b)",
12191219
ans: map[int][]string{
1220-
1: {"a"},
1221-
3: {"a"},
1220+
1: {"test.t.a"},
1221+
3: {"test.s.a"},
1222+
},
1223+
},
1224+
{
1225+
sql: "select t01.a from (select a from t t21 union all select a from t t22) t2 join t t01 on 1 left outer join t t3 on 1 join t t4 on 1",
1226+
ans: map[int][]string{
1227+
1: {"test.t22.a"},
1228+
3: {"test.t21.a"},
1229+
5: {"t2.a"},
1230+
8: {"test.t01.a"},
1231+
10: {"test.t._tidb_rowid"},
1232+
12: {"test.t._tidb_rowid"},
12221233
},
12231234
},
12241235
}
@@ -1246,9 +1257,17 @@ func checkDataSourceCols(p LogicalPlan, c *C, ans map[int][]string, comment Comm
12461257
switch p.(type) {
12471258
case *DataSource:
12481259
colList, ok := ans[p.ID()]
1249-
c.Assert(ok, IsTrue, comment)
1260+
c.Assert(ok, IsTrue, Commentf("For %v DataSource ID %d Not found", comment, p.ID()))
1261+
c.Assert(len(p.Schema().Columns), Equals, len(colList), comment)
1262+
for i, colName := range colList {
1263+
c.Assert(p.Schema().Columns[i].String(), Equals, colName, comment)
1264+
}
1265+
case *LogicalUnionAll:
1266+
colList, ok := ans[p.ID()]
1267+
c.Assert(ok, IsTrue, Commentf("For %v UnionAll ID %d Not found", comment, p.ID()))
1268+
c.Assert(len(p.Schema().Columns), Equals, len(colList), comment)
12501269
for i, colName := range colList {
1251-
c.Assert(colName, Equals, p.Schema().Columns[i].ColName.L, comment)
1270+
c.Assert(p.Schema().Columns[i].String(), Equals, colName, comment)
12521271
}
12531272
}
12541273
for _, child := range p.Children() {

planner/core/rule_column_pruning.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,17 @@ func (p *LogicalUnionAll) PruneColumns(parentUsedCols []*expression.Column) {
138138
if !hasBeenUsed {
139139
parentUsedCols = make([]*expression.Column, len(p.schema.Columns))
140140
copy(parentUsedCols, p.schema.Columns)
141+
} else {
142+
// Issue 10341: p.schema.Columns might contain table name (AsName), but p.Children()0].Schema().Columns does not.
143+
for i := len(used) - 1; i >= 0; i-- {
144+
if !used[i] {
145+
p.schema.Columns = append(p.schema.Columns[:i], p.schema.Columns[i+1:]...)
146+
}
147+
}
141148
}
142149
for _, child := range p.Children() {
143150
child.PruneColumns(parentUsedCols)
144151
}
145-
p.schema.Columns = p.children[0].Schema().Columns
146152
}
147153

148154
// PruneColumns implements LogicalPlan interface.

0 commit comments

Comments
 (0)