Skip to content

Commit

Permalink
planner: fix possible inconsistent output cols among union's children
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Nov 22, 2023
1 parent 9062834 commit f0fce05
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pkg/planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ func (p *LogicalUnionAll) PruneColumns(parentUsedCols []*expression.Column, opt
if !hasBeenUsed {
parentUsedCols = make([]*expression.Column, len(p.schema.Columns))
copy(parentUsedCols, p.schema.Columns)
for i := range used {
used[i] = true
}
}
for _, child := range p.Children() {
err := child.PruneColumns(parentUsedCols, opt, p)
Expand All @@ -284,16 +287,14 @@ func (p *LogicalUnionAll) PruneColumns(parentUsedCols []*expression.Column, opt
}

prunedColumns := make([]*expression.Column, 0)
if hasBeenUsed {
// keep the schema of LogicalUnionAll same as its children's
used := expression.GetUsedList(p.SCtx(), p.children[0].Schema().Columns, p.schema)
for i := len(used) - 1; i >= 0; i-- {
if !used[i] {
prunedColumns = append(prunedColumns, p.schema.Columns[i])
p.schema.Columns = append(p.schema.Columns[:i], p.schema.Columns[i+1:]...)
}
for i := len(used) - 1; i >= 0; i-- {
if !used[i] {
prunedColumns = append(prunedColumns, p.schema.Columns[i])
p.schema.Columns = append(p.schema.Columns[:i], p.schema.Columns[i+1:]...)
}
appendColumnPruneTraceStep(p, prunedColumns, opt)
}
appendColumnPruneTraceStep(p, prunedColumns, opt)
if hasBeenUsed {
// 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,81 @@ LEFT JOIN tmp3 c3 ON c3.id = '1';
id id
1 1
1 1
drop table if exists t;
create table t(a int, b int);
set @@tidb_max_chunk_size = 32;
insert into t values(1, 1);
insert into t select a+1, a+1 from t;
insert into t select a+2, a+2 from t;
insert into t select a+4, a+4 from t;
insert into t select a+8, a+8 from t;
insert into t select a+16, a+16 from t;
insert into t select a+32, a+32 from t;
select a from (select 100 as a, 100 as b union all select * from t) t where b != 0;
a
100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
set @@tidb_max_chunk_size = default;
15 changes: 15 additions & 0 deletions tests/integrationtest/t/planner/core/issuetest/planner_issue.test
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,18 @@ FROM
t2 db
LEFT JOIN tmp3 c2 ON c2.id = '1'
LEFT JOIN tmp3 c3 ON c3.id = '1';

# https://github.com/pingcap/tidb/issues/48755
drop table if exists t;
create table t(a int, b int);
set @@tidb_max_chunk_size = 32;
# insert into more than 32 rows to the table.
insert into t values(1, 1);
insert into t select a+1, a+1 from t;
insert into t select a+2, a+2 from t;
insert into t select a+4, a+4 from t;
insert into t select a+8, a+8 from t;
insert into t select a+16, a+16 from t;
insert into t select a+32, a+32 from t;
select a from (select 100 as a, 100 as b union all select * from t) t where b != 0;
set @@tidb_max_chunk_size = default;

0 comments on commit f0fce05

Please sign in to comment.