Skip to content

Commit

Permalink
[CALCITE-6746] Optimization rule ProjectWindowTranspose is unsound
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
  • Loading branch information
mihaibudiu committed Dec 27, 2024
1 parent 51eabb7 commit 2bf0941
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ private static ImmutableBitSet findReference(final Project project,
}
}

group.lowerBound.accept(referenceFinder);
group.upperBound.accept(referenceFinder);

// Reference in Order-By
for (RelFieldCollation relFieldCollation : group.orderKeys.getFieldCollations()) {
if (relFieldCollation.getFieldIndex() < windowInputColumn) {
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,22 @@ private static boolean skipItem(RexNode expr) {
.check();
}

/**
* Test case for <a href="https://issues.apache.org/jira/projects/CALCITE/issues/CALCITE-6746">
* [CALCITE-6746] Optimization rule ProjectWindowTranspose is unsound</a>. */
@Test void testConstantWindow() {
final String sql = "with empsalary(dept, empno, salary, enroll_date) as "
+ "(VALUES ('x', 10, 5200, DATE '2007-08-01'), (NULL, NULL, NULL, NULL))\n"
+ "select sum(salary) "
+ "OVER (order by enroll_date range between INTERVAL 365 DAYS preceding and "
+ "INTERVAL 365 DAYS following),\n"
+ "salary, enroll_date FROM empsalary";
sql(sql)
.withPreRule(CoreRules.PROJECT_TO_LOGICAL_PROJECT_AND_WINDOW)
.withRule(CoreRules.PROJECT_WINDOW_TRANSPOSE)
.checkUnchanged();
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-5813">[CALCITE-5813]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,21 @@ case when cast(ename as double) < 5 then 0.0
<![CDATA[
LogicalProject(T=[CASE(<(CAST(CASE(>($1, 'abc'), $1, null:VARCHAR(20))):DOUBLE, 5.0E0), 0.0E0:DOUBLE, CASE(IS NOT NULL(CAST(CASE(>($1, 'abc'), $1, null:VARCHAR(20))):DOUBLE), CAST(CAST(CASE(>($1, 'abc'), $1, null:VARCHAR(20))):DOUBLE):DOUBLE NOT NULL, 1.0E0:DOUBLE))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testConstantWindow">
<Resource name="sql">
<![CDATA[with empsalary(dept, empno, salary, enroll_date) as (VALUES ('x', 10, 5200, DATE '2007-08-01'), (NULL, NULL, NULL, NULL))
select sum(salary) OVER (order by enroll_date range between INTERVAL 365 DAYS preceding and INTERVAL 365 DAYS following),
salary, enroll_date FROM empsalary]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalProject(EXPR$0=[CASE(>($3, 0), $4, null:INTEGER)], SALARY=[$0], ENROLL_DATE=[$1])
LogicalWindow(window#0=[window(order by [1] range between $2 PRECEDING and $2 FOLLOWING aggs [COUNT($0), SUM($0)])])
LogicalProject(EXPR$2=[$2], EXPR$3=[$3], $2=[*(365, 86400000:INTERVAL DAY)])
LogicalValues(tuples=[[{ 'x', 10, 5200, 2007-08-01 }, { null, null, null, null }]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit 2bf0941

Please sign in to comment.