diff --git a/planner/core/rule_inject_extra_projection.go b/planner/core/rule_inject_extra_projection.go index 9b629b894040f..a2bcbcf28e3eb 100644 --- a/planner/core/rule_inject_extra_projection.go +++ b/planner/core/rule_inject_extra_projection.go @@ -99,9 +99,10 @@ func injectProjBelowAgg(aggPlan PhysicalPlan, aggFuncs []*aggregation.AggFuncDes } projExprs = append(projExprs, arg) newArg := &expression.Column{ - RetType: arg.GetType(), - ColName: model.NewCIStr(fmt.Sprintf("col_%d", len(projSchemaCols))), - Index: cursor, + UniqueID: aggPlan.context().GetSessionVars().AllocPlanColumnID(), + RetType: arg.GetType(), + ColName: model.NewCIStr(fmt.Sprintf("col_%d", len(projSchemaCols))), + Index: cursor, } projSchemaCols = append(projSchemaCols, newArg) f.Args[i] = newArg diff --git a/planner/core/task.go b/planner/core/task.go index 8c210bc6c2336..36e23245b67ab 100644 --- a/planner/core/task.go +++ b/planner/core/task.go @@ -509,18 +509,27 @@ func (p *basePhysicalAgg) newPartialAggregate() (partial, final PhysicalPlan) { func (p *PhysicalStreamAgg) attach2Task(tasks ...task) task { t := tasks[0].copy() if cop, ok := t.(*copTask); ok { - partialAgg, finalAgg := p.newPartialAggregate() - if partialAgg != nil { - if cop.tablePlan != nil { - partialAgg.SetChildren(cop.tablePlan) - cop.tablePlan = partialAgg - } else { - partialAgg.SetChildren(cop.indexPlan) - cop.indexPlan = partialAgg + // We should not push agg down across double read, since the data of second read is ordered by handle instead of index. + // The `doubleReadNeedProj` is always set if the double read needs to keep order. So we just use it to decided + // whether the following plan is double read with order reserved. + if !cop.doubleReadNeedProj { + partialAgg, finalAgg := p.newPartialAggregate() + if partialAgg != nil { + if cop.tablePlan != nil { + cop.finishIndexPlan() + partialAgg.SetChildren(cop.tablePlan) + cop.tablePlan = partialAgg + } else { + partialAgg.SetChildren(cop.indexPlan) + cop.indexPlan = partialAgg + } } + t = finishCopTask(p.ctx, cop) + attachPlan2Task(finalAgg, t) + } else { + t = finishCopTask(p.ctx, cop) + attachPlan2Task(p, t) } - t = finishCopTask(p.ctx, cop) - attachPlan2Task(finalAgg, t) } else { attachPlan2Task(p, t) }