[ 
https://issues.apache.org/jira/browse/CALCITE-3893?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Shuo Cheng updated CALCITE-3893:
--------------------------------
    Description: 
Consider the following SQL:
{code:java}
select 
  deptno, group_id() as g, count(*) as c
from emp 
group by grouping sets (deptno, (), ())
{code}
the plan after SqlToRel is:
{code:java}
LogicalUnion(all=[true])
  LogicalProject(DEPTNO=[$0], G=[0:BIGINT], C=[$1])
    LogicalAggregate(group=[{0}], groups=[[{0}, {}]], C=[COUNT()])
      LogicalProject(DEPTNO=[$7])
        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
  LogicalProject(DEPTNO=[$0], G=[1:BIGINT], C=[$1])
    LogicalAggregate(group=[{0}], groups=[[{}]], C=[COUNT()])
      LogicalProject(DEPTNO=[$7])
        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
{code}
I'm afraid there's some semantic problems here. As `groups` of the second 
Aggregate is empty, then what is `$0` meaning in the Project above it. Maybe 
that we want is:
{code:java}
LogicalUnion(all=[true])
  LogicalProject(DEPTNO=[$0], G=[0:BIGINT], C=[$1])
    LogicalAggregate(group=[{0}], groups=[[{0}, {}]], C=[COUNT()])
      LogicalProject(DEPTNO=[$7])
        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
  LogicalProject(DEPTNO=[$0], G=[1:BIGINT], C=[$1])
    LogicalAggregate(group=[{0}], groups=[[{0}]], C=[COUNT()])
      LogicalProject(DEPTNO=[null])
        LogicalTableScan(table=[[CATALOG, SALES, EMP]]){code}
I noticed this is introduced by CALCITE-1824, cc [~donnyzone], 
[~vladimirsitnikov].

  was:
Consider the following SQL:
{code:java}
select 
  deptno, group_id() as g, count(*) as c
from emp 
group by grouping sets (deptno, (), ())
{code}
the plan after SqlToRel is:
{code:java}
LogicalUnion(all=[true])
  LogicalProject(DEPTNO=[$0], G=[0:BIGINT], C=[$1])
    LogicalAggregate(group=[{0}], groups=[[{0}, {}]], C=[COUNT()])
      LogicalProject(DEPTNO=[$7])
        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
  LogicalProject(DEPTNO=[$0], G=[1:BIGINT], C=[$1])
    LogicalAggregate(group=[{0}], groups=[[{}]], C=[COUNT()])
      LogicalProject(DEPTNO=[$7])
        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
{code}
I'm afraid there's some semantic problems here. As `groups` of the aggregate is 
empty, then what is `$0` meaning in the project above the aggregate. Maybe that 
we want is:
{code:java}

LogicalUnion(all=[true])
  LogicalProject(DEPTNO=[$0], G=[0:BIGINT], C=[$1])
    LogicalAggregate(group=[{0}], groups=[[{0}, {}]], C=[COUNT()])
      LogicalProject(DEPTNO=[$7])
        LogicalTableScan(table=[[CATALOG, SALES, EMP]])
  LogicalProject(DEPTNO=[$0], G=[1:BIGINT], C=[$1])
    LogicalAggregate(group=[{0}], groups=[[{0}]], C=[COUNT()])
      LogicalProject(DEPTNO=[null])
        LogicalTableScan(table=[[CATALOG, SALES, EMP]]){code}
I noticed this is introduced by CALCITE-1824, cc [~donnyzone], 
[~vladimirsitnikov].


> SQL with GROUP_ID may generate wrong plan
> -----------------------------------------
>
>                 Key: CALCITE-3893
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3893
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.22.0
>            Reporter: Shuo Cheng
>            Priority: Major
>             Fix For: 1.23.0
>
>
> Consider the following SQL:
> {code:java}
> select 
>   deptno, group_id() as g, count(*) as c
> from emp 
> group by grouping sets (deptno, (), ())
> {code}
> the plan after SqlToRel is:
> {code:java}
> LogicalUnion(all=[true])
>   LogicalProject(DEPTNO=[$0], G=[0:BIGINT], C=[$1])
>     LogicalAggregate(group=[{0}], groups=[[{0}, {}]], C=[COUNT()])
>       LogicalProject(DEPTNO=[$7])
>         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
>   LogicalProject(DEPTNO=[$0], G=[1:BIGINT], C=[$1])
>     LogicalAggregate(group=[{0}], groups=[[{}]], C=[COUNT()])
>       LogicalProject(DEPTNO=[$7])
>         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
> {code}
> I'm afraid there's some semantic problems here. As `groups` of the second 
> Aggregate is empty, then what is `$0` meaning in the Project above it. Maybe 
> that we want is:
> {code:java}
> LogicalUnion(all=[true])
>   LogicalProject(DEPTNO=[$0], G=[0:BIGINT], C=[$1])
>     LogicalAggregate(group=[{0}], groups=[[{0}, {}]], C=[COUNT()])
>       LogicalProject(DEPTNO=[$7])
>         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
>   LogicalProject(DEPTNO=[$0], G=[1:BIGINT], C=[$1])
>     LogicalAggregate(group=[{0}], groups=[[{0}]], C=[COUNT()])
>       LogicalProject(DEPTNO=[null])
>         LogicalTableScan(table=[[CATALOG, SALES, EMP]]){code}
> I noticed this is introduced by CALCITE-1824, cc [~donnyzone], 
> [~vladimirsitnikov].



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

Reply via email to