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

kate edited comment on CALCITE-6516 at 8/6/24 12:40 PM:
--------------------------------------------------------

I'm guessing it has something to do with this part of the code in the 
AggregateReduceFunctionsRule.
{code:java}
RexNode numeratorRef =
    rexBuilder.addAggCall(sumCall,
        nGroups,
        newCalls,
        aggCallMapping,
        oldAggRel.getInput()::fieldIsNullable);
final RexNode denominatorRef =
    rexBuilder.addAggCall(countCall,
        nGroups,
        newCalls,
        aggCallMapping,
        oldAggRel.getInput()::fieldIsNullable);

final RelDataTypeFactory typeFactory = oldAggRel.getCluster().getTypeFactory();
final RelDataType avgType =
    typeFactory.createTypeWithNullability(oldCall.getType(),
        numeratorRef.getType().isNullable());
numeratorRef = rexBuilder.ensureType(avgType, numeratorRef, true);{code}
Maybe for the sum type here we should use deriveSumType() from typeSystem  
instead of avgType ?  I'm not sure if that creates other problems

 


was (Author: JIRAUSER301334):
I'm guessing it has something to do with this part of the code in the 
AggregateReduceFunctionsRule.

 
{code:java}
RexNode numeratorRef =
    rexBuilder.addAggCall(sumCall,
        nGroups,
        newCalls,
        aggCallMapping,
        oldAggRel.getInput()::fieldIsNullable);
final RexNode denominatorRef =
    rexBuilder.addAggCall(countCall,
        nGroups,
        newCalls,
        aggCallMapping,
        oldAggRel.getInput()::fieldIsNullable);

final RelDataTypeFactory typeFactory = oldAggRel.getCluster().getTypeFactory();
final RelDataType avgType =
    typeFactory.createTypeWithNullability(oldCall.getType(),
        numeratorRef.getType().isNullable());
numeratorRef = rexBuilder.ensureType(avgType, numeratorRef, true);{code}
 

Maybe for the sum type here we should use deriveSumType() from typeSystem  
instead of avgType ?  I'm not sure if that creates other problems

 

> AVG to SUM+COUNT transformation inserts invalid CAST call
> ---------------------------------------------------------
>
>                 Key: CALCITE-6516
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6516
>             Project: Calcite
>          Issue Type: Bug
>    Affects Versions: 1.37.0
>            Reporter: Leonid Chistov
>            Priority: Major
>
> As described in https://issues.apache.org/jira/browse/CALCITE-4911.
> In Calcite:
> {code:java}
> select avg(comm) as a from "scott".emp{code}
> This SQL physical plan is:
> {noformat}
> EnumerableCalc(expr#0..1=[{inputs}], expr#2=[0], expr#3=[=($t1, $t2)], 
> expr#4=[null:DECIMAL(19, 2)], expr#5=[CASE($t3, $t4, $t0)], 
> expr#6=[CAST($t5):DECIMAL(7, 2)], expr#7=[/($t6, $t1)], 
> expr#8=[CAST($t7):DECIMAL(7, 2)], A=[$t8])
>  EnumerableAggregate(group=[{}], agg#0=[$SUM0($6)], agg#1=[COUNT($6)])
>    EnumerableTableScan(table=[[scott, EMP]]){noformat}
> Problem here is that cast of SUM result to DECIMAL(7, 2) is not correct and 
> may cause overflow error at runtime or result in not expected data truncation.



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

Reply via email to