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


##########
core/src/main/java/org/apache/calcite/rel/core/AggregateCall.java:
##########
@@ -74,6 +74,7 @@ public class AggregateCall {
   public final int filterArg;
   public final @Nullable ImmutableBitSet distinctKeys;
   public final RelCollation collation;
+  public boolean allowChangeNullable;

Review Comment:
   For a simple SQL (such as “SELECT SUM(a) FROM tbl”), the original logic 
would infer `SUM` as nullable = TRUE. However, when there is a GROUP BY (e.g., 
“SELECT SUM(a) FROM t GROUP BY b”), SUM remains inferred as nullable = FALSE. 
There is a special case involving operations like GROUPING SETS 
((0,1,2),(0,1),(0),(0),()), where the last GROUP SET is an empty. In this 
logic, when encountering grouping, all GROUP SETS need to be fully expanded. 
Just like this:
   ```
   Union
     Aggregate(group=[{0, 1, 2}], groups=[[{0, 1, 2}]], EXPR$3=[SUM($3) NOT 
NULL])
     Aggregate(group=[{0, 1}], groups=[[{0, 1}]], EXPR$3=[SUM($3) NOT NULL])
     Aggregate(group=[{0}], groups=[[{0}]], EXPR$3=[SUM($3)] NOT NULL)
     Aggregate(group=[{0}], groups=[[{0}]], EXPR$3=[SUM($3)] NOT NULL)
     Aggregate(group=[{}]], EXPR$3=[SUM($3)]) <------ When this AGG performs 
aggregate function type inference independently, it does not know that it is 
expanded into the AGG in the UNION.
   ```
   According to the original logic, if there is a SUM function in the final 
AGG, then it would infer SUM as nullable = TRUE. However, this behavior creates 
an inconsistency with the previous AGGs. Simply checking the "groupSet" 
property in the AGG is not sufficient to determine whether this AGG was 
generated during the rewrite process. Therefore, it is necessary to set a 
marker somewhere to indicate that the nullable property of SUM functions whose 
"groupSet" is 0 and are produced by the rewrite process should not be modified.



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