OIiveirra commented on code in PR #71331:
URL: https://github.com/apache/doris/pull/71331#discussion_r4782360029


##########
fe/fe-core/src/main/java/org/apache/doris/planner/AggregationNode.java:
##########
@@ -388,6 +401,67 @@ public Pair<PlanNode, LocalExchangeType> 
enforceAndDeriveLocalExchange(
         return Pair.of(this, enforceResult.second);
     }
 
+    // Let each scan task contribute partial states when hashing a few groups 
would idle tasks.
+    // The outgoing stream routes all partial states for the same group to the 
same final reducer.
+    private boolean canDistributePartialGroups(PlanTranslatorContext context) {
+        long tasks = context.getCurrentFragmentMinInstancesPerBe();
+        if (tasks <= 1 || !Double.isFinite(estimatedGroupsForLocalExchange)
+                || estimatedGroupsForLocalExchange <= 0 || 
estimatedGroupsForLocalExchange > tasks
+                || needsFinalize || !aggInfo.isFirstPhase() || sortByGroupKey 
!= null || hasLimit()
+                || (projectList != null && !projectList.isEmpty())
+                || !(getChild(0) instanceof FileQueryScanNode) || 
getChild(0).hasLimit()
+                || 
!getChild(0).isSerialOperatorOnBe(context.getConnectContext())
+                || getFragment() == null || getFragment().getPlanRoot() != this
+                || !(getFragment().getSink() instanceof DataStreamSink)) {
+            return false;
+        }
+
+        FileQueryScanNode scan = (FileQueryScanNode) getChild(0);
+        // Grouping NDVs are cached by table and column, without a snapshot or 
scan selector.
+        if (scan.getQueryTableSnapshot() != null || scan.getScanParams() != 
null) {
+            return false;
+        }
+        DataPartition partition = getFragment().getSink().getOutputPartition();
+        List<Expr> keys = aggInfo.getGroupingExprs();
+        if (partition == null || partition.getType() != 
TPartitionType.HASH_PARTITIONED
+                || keys.size() != 2 || partition.getPartitionExprs().size() != 
keys.size()
+                || aggInfo.getOutputTupleDesc().getSlots().size() < 
keys.size()) {
+            return false;
+        }
+        for (int i = 0; i < keys.size(); i++) {
+            Expr output = partition.getPartitionExprs().get(i);
+            SlotDescriptor group = 
aggInfo.getOutputTupleDesc().getSlots().get(i);
+            if (!(keys.get(i) instanceof SlotRef) || 
!keys.get(i).getType().isStringType()
+                    || !(output instanceof SlotRef) || ((SlotRef) 
output).getDesc() == null
+                    || !group.getId().equals(((SlotRef) output).getSlotId())
+                    || !aggInfo.getOutputTupleId().equals(((SlotRef) 
output).getDesc().getParentId())) {
+                return false;
+            }
+        }
+
+        List<FunctionCallExpr> aggregates = aggInfo.getAggregateExprs();
+        return !aggregates.isEmpty() && 
aggregates.stream().allMatch(AggregationNode::canRepartitionAggregate);
+    }
+
+    private static boolean canRepartitionAggregate(FunctionCallExpr call) {
+        Function fn = call.getFn();
+        if (fn == null || fn.getBinaryType() != Function.BinaryType.BUILTIN
+                || call.getParams().isDistinct() || 
!call.getOrderByElements().isEmpty()) {
+            return false;
+        }
+        switch (call.getFnName().getFunction()) {
+            case "count":
+                return true;
+            case "sum":
+            case "avg":
+                // Floating-point overflow and DecimalV2 saturation can depend 
on how rows are split.
+                Type[] args = fn.getArgs();
+                return args.length == 1 && (args[0].isFixedPointType() || 
args[0].isDecimalV3());

Review Comment:
   Fixed in ef37ca7965f. DecimalV3 SUM and AVG now check the resolved 
accumulator type. A Decimal128 partial sum keeps the existing local hash for 
the whole aggregation node. The Iceberg regression caught the old plan even 
with a DECIMAL(10,2) input, and passed with the fix. Signed SUM/AVG results 
matched an exact decimal reference.
   
   The original Q1 SQL also falls back under the default decimal settings. I 
updated the PR description to distinguish the earlier forced-distribution 
timings from this candidate.
   



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to