Skip to content

Commit

Permalink
cherry pick pingcap#24204 to release-5.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
eurekaka committed May 8, 2021
1 parent 1145e34 commit 934c9aa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
19 changes: 19 additions & 0 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1693,3 +1693,22 @@ func (s *testPlanSuite) TestNthPlanHintWithExplain(c *C) {
// hold in the future, you may need to modify this.
tk.MustQuery("explain format = 'brief' select * from test.tt where a=1 and b=1").Check(testkit.Rows(output[1].Plan...))
}

func (s *testPlanSuite) TestPossibleProperties(c *C) {
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer func() {
dom.Close()
store.Close()
}()
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists student, sc")
tk.MustExec("create table student(id int primary key auto_increment, name varchar(4) not null)")
tk.MustExec("create table sc(id int primary key auto_increment, student_id int not null, course_id int not null, score int not null)")
tk.MustExec("insert into student values (1,'s1'), (2,'s2')")
tk.MustExec("insert into sc (student_id, course_id, score) values (1,1,59), (1,2,57), (1,3,76), (2,1,99), (2,2,100), (2,3,100)")
tk.MustQuery("select /*+ stream_agg() */ a.id, avg(b.score) as afs from student a join sc b on a.id = b.student_id where b.score < 60 group by a.id having count(b.course_id) >= 2").Check(testkit.Rows(
"1 58.0000",
))
}
33 changes: 28 additions & 5 deletions planner/core/property_cols_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,27 @@ func (p *LogicalProjection) PreparePossibleProperties(schema *expression.Schema,
return childProperties
}

func clonePossibleProperties(props [][]*expression.Column) [][]*expression.Column {
res := make([][]*expression.Column, len(props))
for i, prop := range props {
clonedProp := make([]*expression.Column, len(prop))
for j, col := range prop {
clonedProp[j] = col.Clone().(*expression.Column)
}
res[i] = clonedProp
}
return res
}

// PreparePossibleProperties implements LogicalPlan PreparePossibleProperties interface.
func (p *LogicalJoin) PreparePossibleProperties(schema *expression.Schema, childrenProperties ...[][]*expression.Column) [][]*expression.Column {
leftProperties := childrenProperties[0]
rightProperties := childrenProperties[1]
// TODO: We should consider properties propagation.
p.leftProperties = leftProperties
p.rightProperties = rightProperties
// Clone the Columns in the property before saving them, otherwise the upper Projection may
// modify them and lead to unexpected results.
p.leftProperties = clonePossibleProperties(leftProperties)
p.rightProperties = clonePossibleProperties(rightProperties)
if p.JoinType == LeftOuterJoin || p.JoinType == LeftOuterSemiJoin {
rightProperties = nil
} else if p.JoinType == RightOuterJoin {
Expand Down Expand Up @@ -200,13 +214,22 @@ func (la *LogicalAggregation) PreparePossibleProperties(schema *expression.Schem
return nil
}
resultProperties := make([][]*expression.Column, 0, len(childProps))
clonedProperties := make([][]*expression.Column, 0, len(childProps))
groupByCols := la.GetGroupByCols()
for _, possibleChildProperty := range childProps {
sortColOffsets := getMaxSortPrefix(possibleChildProperty, groupByCols)
if len(sortColOffsets) == len(groupByCols) {
resultProperties = append(resultProperties, possibleChildProperty[:len(groupByCols)])
prop := possibleChildProperty[:len(groupByCols)]
resultProperties = append(resultProperties, prop)
// Clone the Columns in the property before saving them, otherwise the upper Projection may
// modify them and lead to unexpected results.
clonedProp := make([]*expression.Column, len(prop))
for i, col := range prop {
clonedProp[i] = col.Clone().(*expression.Column)
}
clonedProperties = append(clonedProperties, clonedProp)
}
}
la.possibleProperties = resultProperties
return la.possibleProperties
la.possibleProperties = clonedProperties
return resultProperties
}

0 comments on commit 934c9aa

Please sign in to comment.