xiedeyantu commented on code in PR #4495:
URL: https://github.com/apache/calcite/pull/4495#discussion_r2261608902


##########
core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java:
##########
@@ -227,7 +227,8 @@ public static SqlCall stripSeparator(SqlCall call) {
         @Override public RelDataType
         inferReturnType(SqlOperatorBinding opBinding) {
           final RelDataType type = super.inferReturnType(opBinding);
-          if (opBinding.getGroupCount() == 0 || opBinding.hasFilter()) {
+          if (opBinding.allowChangeNullable()

Review Comment:
   This is not the direct cause. The direct cause lies in the expansion of 
groupsets. For example, agg(group={0,1,2}, groups=[{0,1,2}, {}]) would, under 
the original logic, be expanded into agg(group={0,1,2}, groups=[{0,1,2}]) and 
agg(group={0,1,2}, groups=[{}]). However, this is incorrect. It should be 
expanded into agg(group={0,1,2}, groups=[{0,1,2}]) and agg(group={}, 
groups=[{}]). This discrepancy is what leads to the subsequent issue.
   For simple SQL statements (such as select sum(a) from t), the original logic 
would infer the sum as nullable = true. However, when there is a group by (such 
as select sum(a) from t group by b), the sum remains inferred as nullable = 
false. There is a special case involving operations like rollup (i.e., {0,1,2}, 
{0,1}, {0}, {}), where the last grouping set is an empty grouping set. In this 
logic, when encountering grouping, all group sets need to be fully expanded 
(otherwise it’s impossible to correctly compute the grouping value for each 
set), and then connected with union. According to the above logic, the sum in 
the final empty grouping set will be inferred as nullable = true, which is 
inconsistent with the other grouping sets where nullable = false. This 
inconsistency can cause issues in subsequent rule executions (such as 
ProjectMergeRule, ProjectToCalcRule, etc.). Therefore, in this special case, an 
additional parameter is introduced to force certain aggregate functions to 
 automatically infer nullable as true. I hope this explanation is clear.



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