Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix possible inconsistent output cols among union's children #48775

Merged
merged 6 commits into from
Nov 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
planner: fix possible inconsistent output cols among union's children
winoros committed Nov 22, 2023
commit f0fce0552f7c32183e39c4efcb9b3febae01a562
19 changes: 10 additions & 9 deletions pkg/planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
@@ -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)
@@ -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)
}
Comment on lines +290 to +295
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we can use slices.Delete or slices.DeleteFunc to make it cleaner.

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.
Original file line number Diff line number Diff line change
@@ -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
@@ -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;