morrySnow commented on code in PR #10659: URL: https://github.com/apache/doris/pull/10659#discussion_r916497641
########## fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java: ########## @@ -112,62 +131,92 @@ public PlanFragment visit(Plan plan, PlanTranslatorContext context) { /** * Translate Agg. + * todo: support DISTINCT */ @Override - public PlanFragment visitPhysicalAggregation( - PhysicalUnaryPlan<PhysicalAggregation, Plan> agg, PlanTranslatorContext context) { - - PlanFragment inputPlanFragment = visit(agg.child(0), context); - - AggregationNode aggregationNode; - List<Slot> slotList = new ArrayList<>(); - PhysicalAggregation physicalAggregation = agg.getOperator(); - AggregateInfo.AggPhase phase = physicalAggregation.getAggPhase().toExec(); - - List<Expression> groupByExpressionList = physicalAggregation.getGroupByExprList(); + public PlanFragment visitPhysicalAggregate( + PhysicalUnaryPlan<PhysicalAggregate, Plan> aggregate, PlanTranslatorContext context) { + PlanFragment inputPlanFragment = visit(aggregate.child(0), context); + PhysicalAggregate physicalAggregate = aggregate.getOperator(); + + // TODO: stale planner generate aggregate tuple in a special way. tuple include 2 parts: + // 1. group by expressions: removing duplicate expressions add to tuple + // 2. agg functions: only removing duplicate agg functions in output expression should appear in tuple. + // e.g. select sum(v1) + 1, sum(v1) + 2 from t1 should only generate one sum(v1) in tuple + // We need: + // 1. add a project after agg, if agg function is not the top output expression. + // 2. introduce canonicalized, semanticEquals and deterministic in Expression + // for removing duplicate. + List<Expression> groupByExpressionList = physicalAggregate.getGroupByExprList(); + List<NamedExpression> outputExpressionList = physicalAggregate.getOutputExpressionList(); + + // 1. generate slot reference for each group expression + List<SlotReference> groupSlotList = Lists.newArrayList(); + for (Expression e : groupByExpressionList) { + if (e instanceof SlotReference && outputExpressionList.stream().anyMatch(o -> o.anyMatch(e::equals))) { + groupSlotList.add((SlotReference) e); + } else { + groupSlotList.add(new SlotReference(e.sql(), e.getDataType(), e.nullable(), Collections.emptyList())); + } + } ArrayList<Expr> execGroupingExpressions = groupByExpressionList.stream() - // Since output of plan doesn't contain the slots of groupBy, which is actually needed by - // the BE execution, so we have to collect them and add to the slotList to generate corresponding - // TupleDesc. - .peek(x -> slotList.addAll(x.collect(SlotReference.class::isInstance))) .map(e -> ExpressionTranslator.translate(e, context)).collect(Collectors.toCollection(ArrayList::new)); - slotList.addAll(agg.getOutput()); - TupleDescriptor outputTupleDesc = generateTupleDesc(slotList, context, null); - - List<NamedExpression> outputExpressionList = physicalAggregation.getOutputExpressionList(); - ArrayList<FunctionCallExpr> execAggExpressions = outputExpressionList.stream() - .map(e -> e.<List<AggregateFunction>>collect(AggregateFunction.class::isInstance)) + // 2. collect agg functions and generate agg function to slot reference map Review Comment: in translator, this could not happen at all -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org