You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2020/06/18 18:19:41 UTC

[GitHub] [calcite] amaliujia commented on a change in pull request #2035: [CALCITE-4008] Implement Code generation for EnumerableSortedAggregat…

amaliujia commented on a change in pull request #2035:
URL: https://github.com/apache/calcite/pull/2035#discussion_r442416752



##########
File path: core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableSortedAggregate.java
##########
@@ -90,6 +101,133 @@ public EnumerableSortedAggregate(
   }
 
   public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
-    throw Util.needToImplement("EnumerableSortedAggregate");
+    if (getGroupType() != Group.SIMPLE
+        || aggCalls.isEmpty()) {
+      throw Util.needToImplement("EnumerableSortedAggregate");
+    }
+
+    final JavaTypeFactory typeFactory = implementor.getTypeFactory();
+    final BlockBuilder builder = new BlockBuilder();
+    final EnumerableRel child = (EnumerableRel) getInput();
+    final Result result = implementor.visitChild(this, 0, child, pref);
+    Expression childExp =
+        builder.append(
+            "child",
+            result.block);
+
+    final PhysType physType =
+        PhysTypeImpl.of(
+            typeFactory, getRowType(), pref.preferCustom());
+
+    final PhysType inputPhysType = result.physType;
+
+    ParameterExpression parameter =
+        Expressions.parameter(inputPhysType.getJavaRowType(), "a0");
+
+    final PhysType keyPhysType =
+        inputPhysType.project(groupSet.asList(), getGroupType() != Group.SIMPLE,
+            JavaRowFormat.LIST);
+    final int groupCount = getGroupCount();
+
+    final List<AggImpState> aggs = new ArrayList<>(aggCalls.size());
+    for (Ord<AggregateCall> call : Ord.zip(aggCalls)) {
+      aggs.add(new AggImpState(call.i, call.e, false));
+    }
+
+    // Function0<Object[]> accumulatorInitializer =
+    //     new Function0<Object[]>() {
+    //         public Object[] apply() {
+    //             return new Object[] {0, 0};
+    //         }
+    //     };
+    final List<Expression> initExpressions = new ArrayList<>();
+    final BlockBuilder initBlock = new BlockBuilder();
+
+    final List<Type> aggStateTypes = createAggStateTypes(
+        initExpressions, initBlock, aggs, typeFactory);
+
+    final PhysType accPhysType =
+        PhysTypeImpl.of(typeFactory,
+            typeFactory.createSyntheticType(aggStateTypes));
+
+    declareParentAccumulator(initExpressions, initBlock, accPhysType);
+
+    final Expression accumulatorInitializer =
+        builder.append("accumulatorInitializer",
+            Expressions.lambda(
+                Function0.class,
+                initBlock.toBlock()));
+
+    // Function2<Object[], Employee, Object[]> accumulatorAdder =
+    //     new Function2<Object[], Employee, Object[]>() {
+    //         public Object[] apply(Object[] acc, Employee in) {
+    //              acc[0] = ((Integer) acc[0]) + 1;
+    //              acc[1] = ((Integer) acc[1]) + in.salary;
+    //             return acc;
+    //         }
+    //     };
+    final ParameterExpression inParameter =
+        Expressions.parameter(inputPhysType.getJavaRowType(), "in");
+    final ParameterExpression acc_ =
+        Expressions.parameter(accPhysType.getJavaRowType(), "acc");
+
+    createAccumulatorAdders(
+        inParameter, aggs, accPhysType, acc_, inputPhysType, builder, implementor, typeFactory);
+
+    final ParameterExpression lambdaFactory =
+        Expressions.parameter(AggregateLambdaFactory.class,
+            builder.newName("lambdaFactory"));
+
+    implementLambdaFactory(builder, inputPhysType, aggs, accumulatorInitializer,
+        false, lambdaFactory);
+
+    final BlockBuilder resultBlock = new BlockBuilder();
+    final List<Expression> results = Expressions.list();
+    final ParameterExpression key_;
+    final Type keyType = keyPhysType.getJavaRowType();
+    key_ = Expressions.parameter(keyType, "key");
+    for (int j = 0; j < groupCount; j++) {
+      final Expression ref = keyPhysType.fieldReference(key_, j);
+      results.add(ref);
+    }
+
+    for (final AggImpState agg : aggs) {
+      results.add(
+          agg.implementor.implementResult(agg.context,
+              new AggResultContextImpl(resultBlock, agg.call, agg.state, key_,
+                  keyPhysType)));
+    }
+    resultBlock.add(physType.record(results));
+
+    final Expression keySelector_ =
+        builder.append("keySelector",
+            inputPhysType.generateSelector(parameter,
+                groupSet.asList(),
+                keyPhysType.getFormat()));
+    // Generate the appropriate key Comparator.
+    final Expression comparator = keyPhysType.generateComparator(getTraitSet().getCollation());

Review comment:
       I will need to double check and probably revise this part to make KeySelector match with KeyComparator.




----------------------------------------------------------------
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:
users@infra.apache.org