diff --git a/planner/core/physical_plans.go b/planner/core/physical_plans.go index 195f746976860..ee4a226e6c2e5 100644 --- a/planner/core/physical_plans.go +++ b/planner/core/physical_plans.go @@ -1163,10 +1163,20 @@ func (p *PhysicalHashAgg) Clone() (PhysicalPlan, error) { // NewPhysicalHashAgg creates a new PhysicalHashAgg from a LogicalAggregation. func NewPhysicalHashAgg(la *LogicalAggregation, newStats *property.StatsInfo, prop *property.PhysicalProperty) *PhysicalHashAgg { + newGbyItems := make([]expression.Expression, len(la.GroupByItems)) + copy(newGbyItems, la.GroupByItems) + newAggFuncs := make([]*aggregation.AggFuncDesc, len(la.AggFuncs)) + // There's some places that rewrites the aggFunc in-place. + // I clone it first. + // It needs a well refactor to make sure that the physical optimize should not change the things of logical plan. + // It's bad for cascades + for i, aggFunc := range la.AggFuncs { + newAggFuncs[i] = aggFunc.Clone() + } agg := basePhysicalAgg{ - GroupByItems: la.GroupByItems, - AggFuncs: la.AggFuncs, - }.initForHash(la.ctx, newStats, la.blockOffset, prop) + GroupByItems: newGbyItems, + AggFuncs: newAggFuncs, + }.initForHash(la.SCtx(), newStats, la.SelectBlockOffset(), prop) return agg }