diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/DecorrelateInnerUnnestWithGlobalAggregation.java b/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/DecorrelateInnerUnnestWithGlobalAggregation.java index b21137418df51..20d232eb4ec5a 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/DecorrelateInnerUnnestWithGlobalAggregation.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/DecorrelateInnerUnnestWithGlobalAggregation.java @@ -219,8 +219,7 @@ private static boolean isGlobalAggregation(PlanNode node) } AggregationNode aggregationNode = (AggregationNode) node; - return aggregationNode.hasEmptyGroupingSet() && - aggregationNode.getGroupingSetCount() == 1 && + return aggregationNode.hasSingleGlobalAggregation() && aggregationNode.getStep() == SINGLE; } diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/DecorrelateLeftUnnestWithGlobalAggregation.java b/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/DecorrelateLeftUnnestWithGlobalAggregation.java index fd95414d8ae11..146ea0c9afa7f 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/DecorrelateLeftUnnestWithGlobalAggregation.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/DecorrelateLeftUnnestWithGlobalAggregation.java @@ -185,8 +185,7 @@ private static boolean isGlobalAggregation(PlanNode node) } AggregationNode aggregationNode = (AggregationNode) node; - return aggregationNode.hasEmptyGroupingSet() && - aggregationNode.getGroupingSetCount() == 1 && + return aggregationNode.hasSingleGlobalAggregation() && aggregationNode.getStep() == SINGLE; } diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/RemoveEmptyGlobalAggregation.java b/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/RemoveEmptyGlobalAggregation.java index 3552feeac032a..74f5edc8f9251 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/RemoveEmptyGlobalAggregation.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/RemoveEmptyGlobalAggregation.java @@ -32,8 +32,7 @@ public class RemoveEmptyGlobalAggregation .matching(node -> // no aggregate functions node.getAggregations().isEmpty() && - // global aggregation - node.hasEmptyGroupingSet() && node.getGroupingSetCount() == 1); + node.hasSingleGlobalAggregation()); @Override public Pattern getPattern() diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/optimizations/QueryCardinalityUtil.java b/core/trino-main/src/main/java/io/trino/sql/planner/optimizations/QueryCardinalityUtil.java index 0034ffdaf0784..1cfd95a1178a3 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/optimizations/QueryCardinalityUtil.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/optimizations/QueryCardinalityUtil.java @@ -119,7 +119,7 @@ public Range visitEnforceSingleRow(EnforceSingleRowNode node, Void context @Override public Range visitAggregation(AggregationNode node, Void context) { - if (node.hasEmptyGroupingSet() && node.getGroupingSetCount() == 1) { + if (node.hasSingleGlobalAggregation()) { // only single default aggregation which will produce exactly single row return Range.singleton(1L); } diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/plan/AggregationNode.java b/core/trino-main/src/main/java/io/trino/sql/planner/plan/AggregationNode.java index df41262bf2284..e44376f09753d 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/plan/AggregationNode.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/plan/AggregationNode.java @@ -119,6 +119,15 @@ public GroupingSetDescriptor getGroupingSets() return groupingSets; } + /** + * @return true if the aggregation collapses all rows into a single global group (e.g., as a result of a GROUP BY () query). + * Otherwise, false. + */ + public boolean hasSingleGlobalAggregation() + { + return hasEmptyGroupingSet() && getGroupingSetCount() == 1; + } + /** * @return whether this node should produce default output in case of no input pages. * For example for query: