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

In a query with ROLLUP, validator wrongly infers that a column is NOT NULL

    XMLWordPrintableJSON

Details

    Description

      CALCITE throws exception during the stage of sql_to_rel,  when executing SELECT statement  after the statement of ROLLUP, and grouping's item exists in the SELECT statement.

       

      Error message:

      Conversion to relational algebra failed to preserve datatypes:
      validated type:
      RecordType(INTEGER DEPTNO, INTEGER NOT NULL EXPR$1) NOT NULL
      converted type:
      RecordType(INTEGER DEPTNO, INTEGER EXPR$1) NOT NULL
      rel:
      LogicalProject(DEPTNO=[$0], EXPR$1=[CASE(=($2, 0), $0, 1)])
        LogicalAggregate(group=[{0, 1}], groups=[[{0, 1}, {0}, {}]], agg#0=[GROUPING($0)])
          LogicalProject(DEPTNO=[$7], JOB=[$2])
            LogicalTableScan(table=[[CATALOG, SALES, EMP]])java.lang.AssertionError: Conversion to relational algebra failed to preserve datatypes:
      validated type:
      RecordType(INTEGER DEPTNO, INTEGER NOT NULL EXPR$1) NOT NULL
      converted type:
      RecordType(INTEGER DEPTNO, INTEGER EXPR$1) NOT NULL
      rel:
      LogicalProject(DEPTNO=[$0], EXPR$1=[CASE(=($2, 0), $0, 1)])
        LogicalAggregate(group=[{0, 1}], groups=[[{0, 1}, {0}, {}]], agg#0=[GROUPING($0)])
          LogicalProject(DEPTNO=[$7], JOB=[$2])
            LogicalTableScan(table=[[CATALOG, SALES, EMP]])    at org.apache.calcite.sql2rel.SqlToRelConverter.checkConvertedType(SqlToRelConverter.java:492)
          at org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(SqlToRelConverter.java:607)
          at org.apache.calcite.sql.test.AbstractSqlTester.convertSqlToRel2(AbstractSqlTester.java:536)
          at org.apache.calcite.sql.test.AbstractSqlTester.assertSqlConvertsTo(AbstractSqlTester.java:477)
          at org.apache.calcite.sql.test.AbstractSqlTester.assertConvertsTo(AbstractSqlTester.java:455)
          at org.apache.calcite.test.SqlToRelFixture.convertsTo(SqlToRelFixture.java:106)
          at org.apache.calcite.test.SqlToRelFixture.ok(SqlToRelFixture.java:94)
          at org.apache.calcite.test.SqlToRelConverterTest.testCaseWhenGroupingSet(SqlToRelConverterTest.java:4687) 

       

      Test case:(org.apache.calcite.test.SqlToRelConverterTest#testCaseWhenGroupingSet)

      // JAVA: org.apache.calcite.test.SqlToRelConverterTest#testCaseWhenGroupingSet
      
      @Test void testCaseWhenGroupingSet() {
        final String sql = "select deptno, case when grouping(deptno) = 0 then deptno else 1 end\n"
            + "from emp\n"
            + "group by rollup(deptno, job)";
        sql(sql).ok();
      }
      
      // XML: org/apache/calcite/test/SqlToRelConverterTest.xml:456 
        <TestCase name="testCaseWhenGroupingSet">
          <Resource name="sql">
            <![CDATA[SELECT deptno, CASE WHEN grouping(deptno) = 0 THEN deptno ELSE 1 END
      FROM emp
      GROUP BY ROLLUP(deptno, job)]]>
          </Resource>
          <Resource name="plan">
            <![CDATA[
      LogicalProject(DEPTNO=[$0], EXPR$1=[CASE(=($2, 0), $0, 1)])
        LogicalAggregate(group=[{0, 1}], groups=[[{0, 1}, {0}, {}]], agg#0=[GROUPING($0)])
          LogicalProject(DEPTNO=[$7], JOB=[$2])
            LogicalTableScan(table=[[CATALOG, SALES, EMP]])
      ]]>
          </Resource>
        </TestCase>

       

       

      Attachments

        1. screenshot-1.png
          103 kB
          Benchao Li
        2. image-2022-09-29-16-31-04-642.png
          277 kB
          Xurenhe

        Issue Links

          Activity

            People

              wojustme Xurenhe
              wojustme Xurenhe
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 50m
                  50m