[ 
https://issues.apache.org/jira/browse/CALCITE-6834?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17931992#comment-17931992
 ] 

xiong duan edited comment on CALCITE-6834 at 3/24/25 3:19 AM:
--------------------------------------------------------------

[~julianhyde] I tried to perform type conversion when generating COALESCE, but 
we converted `COALESSE` to `CASE WHEN` before validating it, so we couldn't add 
a type.

Therefore, I tried to convert `COALESCE` to `CASE WHEN` in 
StandardConvertletTable and remove the `rewriteCall` in `SqlCoalesceFunction`, 
but this change had a significant impact.

My PR 
[https://github.com/NobiGo/calcite/tree/CALCITE-6834-NEW|https://github.com/NobiGo/calcite/tree/CALCITE-6834-NEW]
 I think the current PR  is better. What do you think?


was (Author: nobigo):
[~julianhyde] I tried to perform type conversion when generating COALESCE, but 
we converted `COALESSE` to `CASE WHEN` before validating it, so we couldn't add 
a type.

Therefore, I tried to convert `COALESCE` to `CASE WHEN` in 
StandardConvertletTable and remove the `rewriteCall` in `SqlCoalesceFunction`, 
but this change had a significant impact.

My PR 
[https://github.com/NobiGo/calcite/tree/CALCITE-6834-NEW|https://github.com/NobiGo/calcite/tree/CALCITE-6834-NEW.]
 I think the current PR  is better. What do you think?

> In query that applies COALESCE to nullable SUM, EnumerableProjectToCalcRule 
> throws AssertionError
> -------------------------------------------------------------------------------------------------
>
>                 Key: CALCITE-6834
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6834
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: Julian Hyde
>            Assignee: xiong duan
>            Priority: Major
>              Labels: pull-request-available
>
> In a query that applies COALESCE to nullable SUM, EnumerableProjectToCalcRule 
> throws AssertionError. Query:
> {code}
> select coalesce(sum(mgr), 0) as m
> from emp;
> {code}
> Stack trace:
> {code}
> java.lang.AssertionError: Cannot add expression of different type to set:
> set type is RecordType(INTEGER NOT NULL $f0) NOT NULL
> expression type is RecordType(SMALLINT NOT NULL $f0) NOT NULL
> set is 
> rel#5531:EnumerableProject.ENUMERABLE.[](input=HepRelVertex#5530,inputs=0)
> expression is EnumerableCalc(expr#0=[{inputs}], $f0=[$t0])
>   EnumerableAggregate(group=[{}], agg#0=[$SUM0($3)])
>     EnumerableTableScan(table=[[scott, EMP]])
> Type mismatch:
> rowtype of original rel: RecordType(INTEGER NOT NULL $f0) NOT NULL
> rowtype of new rel: RecordType(SMALLINT NOT NULL $f0) NOT NULL
> Difference:
> $f0: INTEGER NOT NULL -> SMALLINT NOT NULL
>       at 
> org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:433)
>       at 
> org.apache.calcite.plan.hep.HepRuleCall.transformTo(HepRuleCall.java:58)
>       at 
> org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:273)
>       at 
> org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:288)
>       at 
> org.apache.calcite.adapter.enumerable.EnumerableProjectToCalcRule.onMatch(EnumerableProjectToCalcRule.java:54)
>       at 
> org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:350)
>       at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:556)
> {code}
> Here is a patch to reproduce:
> {noformat}
> diff --git a/core/src/test/resources/sql/agg.iq 
> b/core/src/test/resources/sql/agg.iq
> index 7322b84da..26b2a4b5c 100644
> --- a/core/src/test/resources/sql/agg.iq
> +++ b/core/src/test/resources/sql/agg.iq
> @@ -723,6 +723,17 @@ group by deptno;
>  
>  !use scott
>  
> +select coalesce(sum(mgr), 0) as m
> +from emp;
> ++--------+
> +|    M   |
> ++--------+
> +| 100611 |
> ++--------+
> +(1 row)
> +
> +!ok
> +
>  # GROUPING in SELECT clause of CUBE query
> {noformat}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to