Rafay A created CALCITE-6750:
--------------------------------

             Summary: ProjectWindowTransposeRule does not consider references 
in bounds
                 Key: CALCITE-6750
                 URL: https://issues.apache.org/jira/browse/CALCITE-6750
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.38.0
            Reporter: Rafay A


ProjectWindowTransposeRule does not find reference in lower/upper bounds and 
pushes an incorrect project past Window causing IOOBE when combined with other 
rules later in the planning.
We ran into this use case by a different combinations of rules, and i cannot 
recreate the same scenario, but i am constructing a rel plan here. Consider 
this test:
{code:java}
@Test void testProjectWindowTransposeRuleWithReferenceInBounds() {
  fixture()
      .withVolcanoPlanner(false, p -> {
        p.addRelTraitDef(RelDistributionTraitDef.INSTANCE);
        RelOptUtil.registerDefaultRules(p, false, false);
      })
      .withDynamicTable()
      .relFn(b -> {
        final RelDataTypeFactory typeFactory = b.getTypeFactory();
        final RelDataType bigIntType =
            typeFactory.createTypeWithNullability(
                typeFactory.createSqlType(SqlTypeName.BIGINT), true);
        final RelDataType intType =
            typeFactory.createTypeWithNullability(
                typeFactory.createSqlType(SqlTypeName.INTEGER), true);
        final RexBuilder rb = b.getRexBuilder();
        return b.scan("EMP")
            .project(b.field("DEPTNO"),
                b.alias(
                    rb.makeOver(bigIntType,
                        SqlStdOperatorTable.COUNT,
                        ImmutableList.of(b.field(1)),
                        ImmutableList.of(b.field(2)),
                        ImmutableList.of(
                            new RexFieldCollation(b.field(0),
                                ImmutableSet.of())),
                        RexWindowBounds.preceding(rb
                            .makeAbstractCast(intType, b.literal(10), false)),
                        RexWindowBounds.CURRENT_ROW,
                        true, true, false, false, false),
                    "x"))
            .filter(b.call(SqlStdOperatorTable.EQUALS, b.field(0), 
b.literal(10)))
            .project(b.field(0), b.field(1), b.literal(100))
            .build();
      })
      .withRule(CoreRules.PROJECT_TO_LOGICAL_PROJECT_AND_WINDOW,
          CoreRules.PROJECT_WINDOW_TRANSPOSE)
      .checkUnchanged();
} {code}
This output after ProjectToWindowRule is:
{code:java}
LogicalProject(DEPTNO=[$3], $1=[$5])
  LogicalWindow(window#0=[window(partition {2} order by [0] rows between $4 
PRECEDING and CURRENT ROW aggs [COUNT($1)])])
    LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], DEPTNO=[$7], 
$4=[CAST(10):INTEGER])
      LogicalTableScan(table=[[scott, EMP]]) {code}
You can see that the window is referencing a constant from the project below 
(its a cast, so is not in the constants list in the LogicalWindow). But after 
ProjectWindowTransposeRule, the output becomes:
{code:java}
LogicalProject(DEPTNO=[$3], $1=[$4])
  LogicalWindow(window#0=[window(partition {2} order by [0] rows between $4 
PRECEDING and CURRENT ROW aggs [COUNT($1)])])
    LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], DEPTNO=[$3])
      LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], DEPTNO=[$7], 
$4=[CAST(10):INTEGER])
        LogicalTableScan(table=[[scott, EMP]]) {code}
Which is incorrect. $4 in the LogicalWindow will run into IOOBE because the 
project below only has 3 fields.
 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to