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

Julian Hyde commented on CALCITE-4652:
--------------------------------------

The {{boolean needTopCast}} variable, and the logic surrounding it, might be 
superfluous. You could call {{RelBuilder.convert}}. If the desired row type is 
the same, that method will no-op.

Your {{testDistinctCountWithExpandSumType}} could use some comments. It's not 
clear what it is testing, and what you hope to happen.

{{Tester.withTypeFactory(RelDataTypeFactory)}} is pretty good. 
{{Tester.withTypeFactorySupplier(Supplier<RelDataTypeFactory>)}} might be 
slightly better. Then Tester would have a field{code}final 
Supplier<RelDataTypeFactory> typeFactorySupplier{code} initialized to be a 
memoizing supplier (Guava {{Suppliers.memoize}}) and all calls to 
{{getTypeFactory}} could be replaced with {{typeFactorySupplier.get()}}.

> AggregateExpandDistinctAggregatesRule must cast top aggregates to original 
> type
> -------------------------------------------------------------------------------
>
>                 Key: CALCITE-4652
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4652
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.27.0
>            Reporter: Taras Ledkov
>            Assignee: Taras Ledkov
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 1.28.0
>
>          Time Spent: 1h
>  Remaining Estimate: 0h
>
> The query {{SELECT COUNT(comm), COUNT(DISTINCT comm) FROM emp}} produces the 
> plan below:
> {code}
> LogicalAggregate(group=[{}], EXPR$0=[$SUM0($1)], EXPR$1=[COUNT($0)])
>   LogicalAggregate(group=[{0}], EXPR$0=[COUNT()])
>     LogicalProject(COMM=[$6])
>       LogicalTableScan(table=[[CATALOG, SALES, EMP]]) 
> {code}
> In case SUM type is expanded by override {{RelDataTypeSystem#deriveSumType}} 
> the {{AggregateExpandDistinctAggregatesRule}} fails with assertion below:
> {code}
> Error:
> java.lang.AssertionError: type mismatch:
> aggCall type:
> BIGINT NOT NULL
> inferred type:
> DECIMAL(19, 0) NOT NULL
>     at org.apache.calcite.util.Litmus$1.fail(Litmus.java:32)
>     at org.apache.calcite.plan.RelOptUtil.eq(RelOptUtil.java:2211)
>     at 
> org.apache.calcite.rel.core.Aggregate.typeMatchesInferred(Aggregate.java:443)
>     at org.apache.calcite.rel.core.Aggregate.<init>(Aggregate.java:170)
>     at 
> org.apache.calcite.rel.logical.LogicalAggregate.<init>(LogicalAggregate.java:72)
>     at 
> org.apache.calcite.rel.logical.LogicalAggregate.copy(LogicalAggregate.java:154)
>     at 
> org.apache.calcite.rel.logical.LogicalAggregate.copy(LogicalAggregate.java:48)
>     at 
> org.apache.calcite.rel.rules.AggregateExpandDistinctAggregatesRule.convertSingletonDistinct(AggregateExpandDistinctAggregatesRule.java:401)
>     ... 
> {code}
> *Root cause:*
> the type of SUM aggregate doesn't match original type of the COUNT.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to