korlov42 commented on a change in pull request #8869:
URL: https://github.com/apache/ignite/pull/8869#discussion_r595284712



##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/SortAggregateConverterRule.java
##########
@@ -29,40 +29,108 @@
 import org.apache.calcite.rel.metadata.RelMetadataQuery;
 import org.apache.calcite.util.ImmutableIntList;
 import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteConvention;
-import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteSortAggregate;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.agg.IgniteMapSortAggregate;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.agg.IgniteReduceSortAggregate;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.agg.IgniteSingleSortAggregate;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
 import org.apache.ignite.internal.util.typedef.F;
 
-/** */
-public class SortAggregateConverterRule extends 
AbstractIgniteConverterRule<LogicalAggregate> {
+/**
+ *
+ */
+public class SortAggregateConverterRule {
+    /** */
+    public static final RelOptRule SINGLE = new 
SortSingleAggregateConverterRule();
+
+    /** */
+    public static final RelOptRule MAP_REDUCE = new 
SortMapReduceAggregateConverterRule();
+
     /** */
-    public static final RelOptRule INSTANCE = new SortAggregateConverterRule();
+    private SortAggregateConverterRule() {
+        // No-op.
+    }
 
     /** */
-    public SortAggregateConverterRule() {
-        super(LogicalAggregate.class, "SortAggregateConverterRule");
+    private static class SortSingleAggregateConverterRule extends 
AbstractIgniteConverterRule<LogicalAggregate> {
+        /** */
+        SortSingleAggregateConverterRule() {
+            super(LogicalAggregate.class, "SortSingleAggregateConverterRule");
+        }
+
+        /** {@inheritDoc} */
+        @Override protected PhysicalNode convert(RelOptPlanner planner, 
RelMetadataQuery mq,
+            LogicalAggregate agg) {
+            // Applicable only for GROUP BY
+            if (F.isEmpty(agg.getGroupSet()) || agg.getGroupSets().size() > 1)
+                return null;
+
+            RelOptCluster cluster = agg.getCluster();
+            RelNode input = agg.getInput();
+
+            RelCollation collation = 
RelCollations.of(ImmutableIntList.copyOf(agg.getGroupSet().asList()));
+
+            RelTraitSet inTrait = cluster.traitSetOf(IgniteConvention.INSTANCE)
+                .replace(collation)
+                .replace(IgniteDistributions.single());
+
+            RelTraitSet outTrait = 
cluster.traitSetOf(IgniteConvention.INSTANCE)
+                .replace(collation)
+                .replace(IgniteDistributions.single());
+
+            return new IgniteSingleSortAggregate(
+                cluster,
+                outTrait,
+                convert(input, inTrait),
+                agg.getGroupSet(),
+                agg.getGroupSets(),
+                agg.getAggCallList()
+            );
+        }
     }
 
-    /** {@inheritDoc} */
-    @Override protected PhysicalNode convert(RelOptPlanner planner, 
RelMetadataQuery mq,
-        LogicalAggregate rel) {
-        // Applicable only for GROUP BY
-        if (F.isEmpty(rel.getGroupSet()) || rel.getGroupSets().size() > 1)
-            return null;
-
-        RelOptCluster cluster = rel.getCluster();
-        RelTraitSet inTrait = cluster.traitSetOf(IgniteConvention.INSTANCE);
-        RelTraitSet outTrait = cluster.traitSetOf(IgniteConvention.INSTANCE);
-        RelNode input = rel.getInput();
-
-        RelCollation collation = 
RelCollations.of(ImmutableIntList.copyOf(rel.getGroupSet().asList()));
-
-        return new IgniteSortAggregate(
-            cluster,
-            outTrait.replace(collation),
-            convert(input, inTrait.replace(collation)),
-            rel.getGroupSet(),
-            rel.getGroupSets(),
-            rel.getAggCallList()
-        );
+    /** */
+    private static class SortMapReduceAggregateConverterRule extends 
AbstractIgniteConverterRule<LogicalAggregate> {
+        /** */
+        SortMapReduceAggregateConverterRule() {
+            super(LogicalAggregate.class, 
"SortMapReduceAggregateConverterRule");
+        }
+
+        /** {@inheritDoc} */
+        @Override protected PhysicalNode convert(RelOptPlanner planner, 
RelMetadataQuery mq,
+            LogicalAggregate agg) {
+            // Applicable only for GROUP BY
+            if (F.isEmpty(agg.getGroupSet()) || agg.getGroupSets().size() > 1)
+                return null;
+
+            RelOptCluster cluster = agg.getCluster();
+            RelNode input = agg.getInput();
+
+            RelCollation collation = 
RelCollations.of(ImmutableIntList.copyOf(agg.getGroupSet().asList()));
+
+            RelTraitSet inTrait = 
cluster.traitSetOf(IgniteConvention.INSTANCE).replace(collation);
+            RelTraitSet outTrait = 
cluster.traitSetOf(IgniteConvention.INSTANCE)
+                .replace(collation);
+
+            RelNode map = new IgniteMapSortAggregate(
+                cluster,
+                outTrait,
+                convert(input, inTrait),
+                agg.getGroupSet(),
+                agg.getGroupSets(),
+                agg.getAggCallList(),
+                collation
+            );
+
+            return new IgniteReduceSortAggregate(
+                cluster,
+                outTrait.replace(IgniteDistributions.single()),
+                convert(map, inTrait.replace(IgniteDistributions.single())),

Review comment:
       the same recommendation as for hash aggregate rule




----------------------------------------------------------------
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.

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


Reply via email to