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

RelToSql converter generates invalid code when merging rollup and sort clauses

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.32.0
    • 1.34.0
    • None

    Description

      For SQL dialects (MySQL, Hive, MsSQL) that do not support "GROUP BY ROLLUP(...)" syntax, but do support "GROUP BY ... WITH ROLLUP" syntax instead, wrong code is generated by RelToSqlConverter in the following situation: 

      • There is an Aggregate node with ROLLUP grouping
      • It has a parent Sort node with an order of fields different from the order of fields in ROLLUP Aggregation

      This can be demonstrated by the following test, that would fail if added to RelToSqlConverterTest class:

      @Test void testSelectQueryWithGroupByRollupOrderByReversed() {
        final String query = "select \"product_class_id\", \"brand_name\"\n"
            + "from \"product\"\n"
            + "group by rollup(\"product_class_id\", \"brand_name\")\n"
            + "order by 2, 1";
        final String expectedMysql = "SELECT `product_class_id`, `brand_name`\n"
            + "FROM `foodmart`.`product`\n"
            + "GROUP BY `product_class_id`, `brand_name` WITH ROLLUP";
        sql(query)
            .withMysql().ok(expectedMysql);
      }
       

      As the result we get the following SQL code:

      SELECT `product_class_id`, `brand_name
      FROM `foodmart`.`product
      GROUP BY `brand_name`, `product_class_id` WITH ROLLUP 

      It can be observed that order of fields of aggregation was changed to match the order of fields in ORDER clause, thus changing the semantics of the ROLLUP clause itself.

      Attachments

        Issue Links

          Activity

            People

              jiajunbernoulli Jiajun Xie
              lchistov1987 Leonid Chistov
              Votes:
              0 Vote for this issue
              Watchers:
              6 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 - 1h 20m
                  1h 20m