korlov42 commented on code in PR #6479:
URL: https://github.com/apache/ignite-3/pull/6479#discussion_r2313156681


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/agg/Accumulators.java:
##########
@@ -100,6 +102,8 @@ private Supplier<Accumulator> 
accumulatorFunctionFactory(DataContext context, Ag
                 assert lit instanceof RexLiteral : "Non-literal argument for 
LiteralAgg: " + call + ", argument: " + lit;
 
                 return LiteralVal.newAccumulator(context, (RexLiteral) lit);

Review Comment:
   what was the reason to implement grouping as accumulator? 



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/agg/AccumulatorsFactory.java:
##########
@@ -289,6 +294,7 @@ private static final class AccumulatorWrapperImpl<RowT> 
implements AccumulatorWr
             this.inAdapter = inAdapter;
             this.outAdapter = outAdapter;
             this.distinct = call.isDistinct();
+            this.grouping = call.getAggregation() == 
SqlStdOperatorTable.GROUPING;

Review Comment:
   ```suggestion
               call.getAggregation().getKind() == SqlKind.GROUPING;
   ```
   or something like this. We might override operator in 
`IgniteSqlOperatorTable`, I don't want to execution to fail in this case



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/agg/Accumulators.java:
##########
@@ -217,6 +221,62 @@ private Supplier<Accumulator> 
anyValueFactory(AggregateCall call) {
         return AnyVal.newAccumulator(type);
     }
 
+    private Supplier<Accumulator> groupingFactory(AggregateCall call) {
+        RelDataType type = call.getType();
+
+        if (type.getSqlTypeName() == ANY && !(type instanceof 
IgniteCustomType)) {
+            throw unsupportedAggregateFunction(call);
+        }
+
+        return () -> new GroupingAccumulator(call.getArgList());
+    }
+
+    /**
+     * {@code GROUPING(column [, ...])} accumulator. Pseudo accumulator that 
accepts group key columns set.
+     */
+    public static class GroupingAccumulator implements Accumulator {
+        private final List<Integer> argList;
+
+        public GroupingAccumulator(List<Integer> argList) {
+            assert argList.size() < Long.SIZE : "GROUPING function with more 
than 63 arguments is not supported.";

Review Comment:
   do we have such validation in `SqlValidator`? Please add tests on this case



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/agg/MapReduceAggregates.java:
##########
@@ -487,6 +490,28 @@ private static MapReduceAgg createSimpleAgg(AggregateCall 
call, int reduceArgume
         return new MapReduceAgg(argList, call, reduceCall, USE_INPUT_FIELD);
     }
 
+    private static MapReduceAgg createGroupingAgg(AggregateCall call, int 
reduceArgumentOffset) {
+        IntList argList = IntList.of(reduceArgumentOffset);
+
+        AggregateCall reduceCall = AggregateCall.create(
+                SqlStdOperatorTable.SINGLE_VALUE,
+                call.isDistinct(),
+                call.isApproximate(),
+                call.ignoreNulls(),
+                ImmutableList.of(),
+                argList,
+                // there is no filtering on REDUCE phase
+                -1,
+                null,
+                call.collation,
+                call.type,
+                "GROUPING" + reduceArgumentOffset);
+
+        // For aggregate that use the same aggregate function for both MAP and 
REDUCE phases
+        // use the result of an aggregate as is.
+        return new MapReduceAgg(argList, call, reduceCall, USE_INPUT_FIELD);

Review Comment:
   why do we need to compute GROUPING on MAP phase? Also, please add planner 
tests (see 
`org.apache.ignite.internal.sql.engine.planner.AbstractAggregatePlannerTest`)



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/agg/Accumulators.java:
##########
@@ -217,6 +221,62 @@ private Supplier<Accumulator> 
anyValueFactory(AggregateCall call) {
         return AnyVal.newAccumulator(type);
     }
 
+    private Supplier<Accumulator> groupingFactory(AggregateCall call) {
+        RelDataType type = call.getType();
+
+        if (type.getSqlTypeName() == ANY && !(type instanceof 
IgniteCustomType)) {
+            throw unsupportedAggregateFunction(call);
+        }
+
+        return () -> new GroupingAccumulator(call.getArgList());
+    }
+
+    /**
+     * {@code GROUPING(column [, ...])} accumulator. Pseudo accumulator that 
accepts group key columns set.
+     */
+    public static class GroupingAccumulator implements Accumulator {
+        private final List<Integer> argList;
+
+        public GroupingAccumulator(List<Integer> argList) {
+            assert argList.size() < Long.SIZE : "GROUPING function with more 
than 63 arguments is not supported.";
+            this.argList = argList;
+        }
+
+        @Override
+        public void add(AccumulatorsState state, Object[] args) {
+            assert false;
+        }
+
+        @Override
+        public void end(AccumulatorsState state, AccumulatorsState result) {

Review Comment:
   please add execution tests for this accumulator (see tests around 
`org.apache.ignite.internal.sql.engine.exec.exp.agg.SumAccumulatorTest` fro 
example)



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

Reply via email to