[
https://issues.apache.org/jira/browse/CALCITE-4345?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17218744#comment-17218744
]
Julian Hyde edited comment on CALCITE-4345 at 10/22/20, 4:58 AM:
-----------------------------------------------------------------
2. You are correct about 'sum' evaluating to null when all values are null or
filter is always false. I had forgotten that.
I think there is still a benefit to all of these patterns becoming some kind of
count. What do you think of the following:
* {{sum(case when b then 1 end)}} (and anything else that may return null)
translates to {{nullif(count\(*) filter (where b)}};
* {{sum(case when b then 1 else 0 end)}} (and anything else that returns 0}}
translates to {{count\(*) filter (where b)}}.
Count is easier to think about and compute than sum. Using the {{nullif}}
wrapper makes similar things look similar, which is a good thing.
was (Author: julianhyde):
2. You are correct about 'sum' evaluating to null when all values are null or
filter is always false. I had forgotten that.
I think there is still a benefit to all of these patterns becoming some kind of
count. What do you think of the following:
* {{sum(case when b then 1 end)}} (and anything else that may return null)
translates to {{nullif(count(*) filter (where b)}};
* {{sum(case when b then 1 else 0 end)}} (and anything else that returns 0}}
translates to {{count(*) filter (where b)}}.
Count is easier to think about and compute than sum. Using the {{nullif}}
wrapper makes similar things look similar, which is a good thing.
> SUM CASE WHEN xxx THEN 1 without "ELSE" throws NPE in
> AggregateCaseToFilterRule
> -------------------------------------------------------------------------------
>
> Key: CALCITE-4345
> URL: https://issues.apache.org/jira/browse/CALCITE-4345
> Project: Calcite
> Issue Type: Bug
> Components: core
> Reporter: Jiatao Tao
> Assignee: Jiatao Tao
> Priority: Major
> Labels: pull-request-available
> Attachments: image-2020-10-19-16-59-44-900.png
>
> Time Spent: 2h
> Remaining Estimate: 0h
>
> Add a test in CsvTest:
>
> {code:java}
> @Test void testSumCaseWhenWithoutElse() throws SQLException {
> sql("model", "select sum( CASE WHEN empno=10 THEN 1 END ) from EMPS").ok();
> }
> {code}
> Will throw NPE
> java.lang.NullPointerExceptionjava.lang.NullPointerException at
> org.apache.calcite.rex.RexLiteral.intValue(RexLiteral.java:1152) at
> org.apache.calcite.rel.rules.AggregateCaseToFilterRule.transform(AggregateCaseToFilterRule.java:234)
> at
> org.apache.calcite.rel.rules.AggregateCaseToFilterRule.onMatch(AggregateCaseToFilterRule.java:115)
> at
> org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:333)
> at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:542) at
> org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:407) at
> org.apache.calcite.plan.hep.HepPlanner.executeInstruction(HepPlanner.java:243)
> at
> org.apache.calcite.plan.hep.HepInstruction$RuleInstance.execute(HepInstruction.java:127)
> at
> org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:202) at
> org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:189) at
> org.apache.calcite.test.RelOptTestBase.checkPlanning(RelOptTestBase.java:122)
> at org.apache.calcite.test.RelOptTestBase.access$000(RelOptTestBase.java:64)
> at org.apache.calcite.test.RelOptTestBase$Sql.check(RelOptTestBase.java:303)
> at org.apache.calcite.test.RelOptTestBase$Sql.check(RelOptTestBase.java:282)
> at
> org.apache.calcite.test.RelOptRulesTest.testAggregateCaseToFilter(RelOptRulesTest.java:3833)
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)