NobiGo commented on code in PR #4201:
URL: https://github.com/apache/calcite/pull/4201#discussion_r1965351734


##########
core/src/main/java/org/apache/calcite/rel/rules/ProjectAggregateMergeRule.java:
##########
@@ -154,8 +156,22 @@ && kindCount(project.getProjects(), SqlKind.CASE) == 0) {
     builder.push(aggregate.getInput());
     builder.aggregate(
         builder.groupKey(aggregate.getGroupSet(), aggregate.groupSets), 
aggCallList);
-    builder.project(
-        RexPermuteInputsShuttle.of(mapping).visitList(projects2));
+    // If the output type of Aggregate does not match the input type required 
by original Project,
+    // CAST needs to be added
+    List<RexNode> rexInputRefs = 
RexPermuteInputsShuttle.of(mapping).visitList(projects2);
+    final ImmutableList.Builder<RexNode> selectList = ImmutableList.builder();
+    final RexShuttle rexShuttle = new RexShuttle() {
+      @Override public RexNode visitInputRef(RexInputRef inputRef) {
+        return builder.getRexBuilder().ensureType(
+            inputRef.getType(),
+            builder.fields().get(inputRef.getIndex()),
+            true);
+      }
+    };
+    for (RexNode rexInputRef : rexInputRefs) {
+      selectList.add(rexInputRef.accept(rexShuttle));
+    }
+    builder.project(selectList.build());

Review Comment:
   What you are referring to here should be `RelBuilder.convert`, which also 
requires us to build a new RowType based on Mapping, so I have not made any 
changes here.



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