FrankChen021 commented on code in PR #20093:
URL: https://github.com/apache/druid/pull/20093#discussion_r3821775061
##########
sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidAggregateCaseToFilterRule.java:
##########
@@ -303,9 +303,13 @@ && isIntLiteral(arg2, BigDecimal.ZERO))) {
newProjects.add(arg1);
newProjects.add(filter);
- RelDataType newType =
typeFactory.createTypeWithNullability(call.getType(), true);
+ // Use SUM0 instead of SUM to return 0 instead of NULL when filter
never matches.
+ // This fixes the known inconsistency documented in the class javadoc
where
+ // SUM(CASE WHEN COND THEN COL1 ELSE 0 END) would return NULL instead
of 0
+ // when no rows match the condition.
+ RelDataType newType =
typeFactory.createTypeWithNullability(call.getType(), false);
return AggregateCall.create(
- call.getAggregation(),
+ SqlStdOperatorTable.SUM0,
Review Comment:
[P1] SUM0 still executes as nullable SUM
SumZeroSqlAggregator inherits SumSqlAggregator's nullable native
Long/Float/DoubleSumAggregatorFactory. With no matching filter rows, the
nullable wrapper returns NULL, so native Druid queries still produce NULL for
the empty/no-match cases changed to 0 in filtered_sum.iq.
##########
sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidAggregateCaseToFilterRule.java:
##########
@@ -303,9 +303,13 @@ && isIntLiteral(arg2, BigDecimal.ZERO))) {
newProjects.add(arg1);
newProjects.add(filter);
- RelDataType newType =
typeFactory.createTypeWithNullability(call.getType(), true);
+ // Use SUM0 instead of SUM to return 0 instead of NULL when filter
never matches.
+ // This fixes the known inconsistency documented in the class javadoc
where
+ // SUM(CASE WHEN COND THEN COL1 ELSE 0 END) would return NULL instead
of 0
+ // when no rows match the condition.
+ RelDataType newType =
typeFactory.createTypeWithNullability(call.getType(), false);
return AggregateCall.create(
- call.getAggregation(),
+ SqlStdOperatorTable.SUM0,
Review Comment:
[P2] Rewrite changes all-null semantics
When every row matches the condition and the value is NULL, SUM(CASE WHEN
condition THEN value ELSE 0 END) returns NULL, while SUM0(value) FILTER returns
0. The rewrite applies this universally; the rewrite-disabled test still
documents NULL for the 7=7,null case.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]