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

RelToSql converter generates where clause with window expression

    XMLWordPrintableJSON

Details

    • Bug
    • Status: In Progress
    • Major
    • Resolution: Unresolved
    • 1.32.0
    • None
    • jdbc-adapter
    • None

    Description

      Wrong SQL code is generated when Filter (or Calc) node contains window expression.

      Example can be demonstrated by adding following code to RelToSqlConverterTest.java:

      @Test void testWindowedFilter() {
        final RelBuilder builder = relBuilder();
        final RelNode root = builder
            .scan("DEPT")
            .filter(
                builder.lessThan(
                  builder.aggregateCall(SqlStdOperatorTable.MAX, builder.field("DEPTNO"))
                      .over()
                      .partitionBy(builder.field("DNAME"))
                      .toRex(),
                    builder.literal(1)
                )
            )
            .build();
        final String expectedSql = "?";
        assertThat(toSql(root), isLinux(expectedSql));
      } 

      Generated SQL code will look like:

      SELECT *
      FROM \"scott\".\"DEPT\"
      WHERE (MAX(\"DEPTNO\") OVER (PARTITION BY \"DNAME\" RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) < 1 

      This is incorrect - window expressions are not allowed in WHERE clause by SQL standard and Calcite itself would produce following error message if this SQL code would be passed as input: 

      Windowed aggregate expression is illegal in WHERE clause 

       

      Attachments

        Issue Links

          Activity

            People

              lchistov1987 Leonid Chistov
              lchistov1987 Leonid Chistov
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated: