[Fix](nereids)make agg output unchanged after normalized repeat (#36367)

cherry-pick #36207 to branch-2.1

Co-authored-by: feiniaofeiafei <moailing@selectdb.com>
This commit is contained in:
feiniaofeiafei
2024-06-19 12:23:56 +08:00
committed by GitHub
parent df22344550
commit bdba954e1f
3 changed files with 87 additions and 0 deletions

View File

@ -42,6 +42,7 @@ import org.apache.doris.nereids.util.ExpressionUtils;
import org.apache.doris.nereids.util.PlanUtils.CollectNonWindowedAggFuncs;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@ -191,6 +192,8 @@ public class NormalizeRepeat extends OneAnalysisRuleFactory {
.addAll(groupingSetsUsedSlot)
.addAll(allVirtualSlots)
.build();
normalizedAggOutput = getExprIdUnchangedNormalizedAggOutput(normalizedAggOutput, repeat.getOutputExpressions());
return new LogicalAggregate<>(normalizedAggGroupBy, (List) normalizedAggOutput,
Optional.of(normalizedRepeat), normalizedRepeat);
}
@ -451,4 +454,25 @@ public class NormalizeRepeat extends OneAnalysisRuleFactory {
return hasNewChildren ? windowExpression.withChildren(newChildren) : windowExpression;
}
}
private static List<NamedExpression> getExprIdUnchangedNormalizedAggOutput(
List<NamedExpression> normalizedAggOutput, List<NamedExpression> originalAggOutput) {
Builder<NamedExpression> builder = new ImmutableList.Builder<>();
for (int i = 0; i < originalAggOutput.size(); i++) {
NamedExpression e = normalizedAggOutput.get(i);
// process Expression like Alias(SlotReference#0)#0
if (e instanceof Alias && e.child(0) instanceof SlotReference) {
SlotReference slotReference = (SlotReference) e.child(0);
if (slotReference.getExprId().equals(e.getExprId())) {
e = slotReference;
}
}
// Make the output ExprId unchanged
if (!e.getExprId().equals(originalAggOutput.get(i).getExprId())) {
e = new Alias(originalAggOutput.get(i).getExprId(), e, originalAggOutput.get(i).getName());
}
builder.add(e);
}
return builder.build();
}
}