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

Jiajun Xie commented on CALCITE-6214:
-------------------------------------

1. Changed `COUNT` to `aggregate function`.
2. RelBuilder not support merge two aggregate that have different groupset. So 
there are two aggregate in plan.
{code:java}
LogicalAggregate(group=[{0}], SDS=[SUM(DISTINCT $1)], SS=[SUM($1)])
  LogicalAggregate(group=[{0, 1}]) -- We can optimize it.
    LogicalProject(DEPTNO=[$7], SAL=[$5])
      LogicalTableScan(table=[[CATALOG, SALES, EMP]])
{code}
RelBuilder yield better plan for the sql that have no aggregate function.
{code:java}
SELECT deptno, sal ss
FROM (
   SELECT DISTINCT deptno, sal
   FROM emp)
GROUP BY deptno, sal 


LogicalProject(DEPTNO=[$0], SS=[$1])
  LogicalProject(DEPTNO=[$1], SAL=[$0])
    LogicalAggregate(group=[{5, 7}]) -- Only one aggregate
      LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}
 I need some time to improve it, maybe I will add one commit that via 
RelBuilder on the weekend.

3. Removed backticks.

> Remove DISTINCT in aggregate function if field is unique
> --------------------------------------------------------
>
>                 Key: CALCITE-6214
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6214
>             Project: Calcite
>          Issue Type: Improvement
>          Components: core
>            Reporter: Jiajun Xie
>            Assignee: Jiajun Xie
>            Priority: Minor
>              Labels: pull-request-available
>
> For the sql
> {code:java}
> select count(distinct x) cnt
> from(
>    select distinct sal x from emp
> ) t  {code}
> The distinct that in count can be removed.
> {code:java}
> LogicalAggregate(group=[{}], CNT=[COUNT($0)])
>   LogicalAggregate(group=[{0}])
>     LogicalProject(X=[$5])
>       LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}
> But `CoreRules#AGGREGATE_REMOVE` not support it, so there are two DISTINCT.
> {code:java}
> LogicalAggregate(group=[{}], CNT=[COUNT(DISTINCT $0)])
>   LogicalAggregate(group=[{0}])
>     LogicalProject(X=[$5])
>       LogicalTableScan(table=[[CATALOG, SALES, EMP]]) {code}



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

Reply via email to