Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-1558

AggregateExpandDistinctAggregatesRule gets field mapping wrong if groupKey is used in aggregate function

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.12.0
    • Component/s: None
    • Labels:
      None

      Description

      In AggregateExpandDistinctAggregatesRule.convertSingletonDistinct,
      if the groupKey is also used in some aggregate function,
      the field mapping of top level aggregate would be messy.
      Bad cases are:

              // Equivalent SQL:
              //   SELECT deptno, COUNT(deptno), SUM(DISTINCT sal)
              //   FROM emp
              //   GROUP BY deptno
      
              //   SELECT deptno, SUM(cnt), SUM(sal)
              //   FROM
              //     SELECT deptno, COUNT(deptno) AS cnt, sal
              //     FROM emp
              //     GROUP BY deptno, sal
              //   GROUP BY deptno
      

      or a more complex case:

              // Equivalent SQL:
              //   SELECT deptno, SUM(deptno), SUM(DISTINCT sal), MAX(deptno), MAX(comm)
              //   FROM emp
              //   GROUP BY deptno
      
              //   SELECT deptno, SUM(sumOfInnerComm), SUM(sal), MAX(maxOfInnerDeptno), MAX(maxOfInnerComm)
              //   FROM
              //     SELECT deptno, sal, SUM(deptno) as sumOfInnerDeptno, MAX(deptno) as maxOfInnerDeptno, MAX(comm) AS maxOfInnerComm
              //     FROM emp
              //     GROUP BY deptno, sal
              //   GROUP BY deptno
      

      I have fixed these cases, and will provide a patch later after more tests.

      1. patch_1558.v1
        8 kB
        Zhenghua Gao

        Activity

        Hide
        docete Zhenghua Gao added a comment -

        patch for CALCITE-1558

        Show
        docete Zhenghua Gao added a comment - patch for CALCITE-1558
        Hide
        docete Zhenghua Gao added a comment -

        Created a pull request https://github.com/apache/calcite/pull/347 for this issue.

        Show
        docete Zhenghua Gao added a comment - Created a pull request https://github.com/apache/calcite/pull/347 for this issue.
        Hide
        julianhyde Julian Hyde added a comment -
        Show
        julianhyde Julian Hyde added a comment - Fixed in http://git-wip-us.apache.org/repos/asf/calcite/commit/2b966375 . Thanks for the PR, Zhenghua Gao !
        Hide
        julianhyde Julian Hyde added a comment -

        Resolved in release 1.12.0 (2017-03-24).

        Show
        julianhyde Julian Hyde added a comment - Resolved in release 1.12.0 (2017-03-24).

          People

          • Assignee:
            julianhyde Julian Hyde
            Reporter:
            docete Zhenghua Gao
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development