OIiveirra commented on code in PR #71331:
URL: https://github.com/apache/doris/pull/71331#discussion_r4781005957
##########
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:
Confirmed the overflow risk. Q1 itself binds the charge SUM to
DECIMAL(38,6). Splitting alternating large positive and negative contributions
across tasks can overflow a signed Int128 subtotal even when the final sum is
zero. The scale benchmarks do not cover this case. A blanket hash fallback also
disables the optimized Q1 plan, so this remains open as a correctness blocker.
--
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]