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


##########
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:
   I think you can achieve the same effect with one line of code: use 
`RelBuilder.cast` to convert the output row type of `aggregate` to what 
`project` expects it to be. If `cast` adds a `CAST` call then `project` will 
flatten the expression on top of it.



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