Skip to content

Commit 154d7f0

Browse files
authored
plan: fix a panic bug. (#1914)
1 parent 2e79d3f commit 154d7f0

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

executor/aggregate_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ func (s *testSuite) TestAggregation(c *C) {
136136
result.Check(testkit.Rows())
137137
result = tk.MustQuery("select count(*) from t a join t b where a.c < 0")
138138
result.Check(testkit.Rows("0"))
139+
result = tk.MustQuery("select sum(b.c), count(b.d), a.c from t a left join t b on a.c = b.d group by b.d order by b.d")
140+
result.Check(testkit.Rows("<nil> 0 4", "8 12 1", "5 2 3"))
139141
// This two cases prove that having always resolve name from field list firstly.
140142
result = tk.MustQuery("select 1-d as d from t having d < 0 order by d desc")
141143
result.Check(testkit.Rows("-1", "-1", "-2", "-2"))

plan/physical_plan_builder.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -630,17 +630,18 @@ func (p *Aggregation) convert2PhysicalPlanStream(prop *requiredProperty) (*physi
630630
props: make([]*columnProp, 0, len(gbyCols)),
631631
}
632632
for _, pro := range prop.props {
633-
isMatch := false
633+
var matchedCol *expression.Column
634634
for i, col := range gbyCols {
635-
if col.ColName.L == pro.col.ColName.L {
635+
if !col.Correlated && col.ColName.L == pro.col.ColName.L {
636636
isSortKey[i] = true
637-
isMatch = true
637+
matchedCol = col
638638
}
639639
}
640-
if !isMatch {
640+
if matchedCol == nil {
641641
return info, nil
642642
}
643-
newProp.props = append(newProp.props, pro)
643+
// We should add columns in aggregation in order to keep index right.
644+
newProp.props = append(newProp.props, &columnProp{col: matchedCol, desc: pro.desc})
644645
}
645646
newProp.sortKeyLen = len(newProp.props)
646647
for i, col := range gbyCols {

0 commit comments

Comments
 (0)