Alessandro Solimando created CALCITE-7843:
---------------------------------------------

             Summary: RelMdExpressionLineage should support Window
                 Key: CALCITE-7843
                 URL: https://issues.apache.org/jira/browse/CALCITE-7843
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.42.0
            Reporter: Alessandro Solimando
            Assignee: Alessandro Solimando


RelMdExpressionLineage has no handler for {_}Window{_}, so it falls back to the 
catch-all _RelNode_ handler 
([here|https://github.com/apache/calcite/blob/6484bed4572adbc219b171a9efbbd08a1b662f3a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdExpressionLineage.java#L101-L104]),
 which always returns null. This happens even for columns that _Window_ simply 
passes through from its input.

_RelMdTableReferences_ already supports Window 
([here|https://github.com/apache/calcite/blob/6484bed4572adbc219b171a9efbbd08a1b662f3a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdTableReferences.java#L276-L290]),
 so the two metadata types are not consistent. Any consumer of expression 
lineage, such as materialized view rewriting, fails on plans containing a 
{_}Window{_}.

Reproducer (for {_}RelMetadataTest{_}):
{code:java}
@Test void testExpressionLineageWindow() {
  final RelNode rel = sql("select empno, row_number() over (order by sal) from 
emp")
      .toRel();
  final HepProgram program = new HepProgramBuilder()
      .addRuleInstance(CoreRules.PROJECT_TO_LOGICAL_PROJECT_AND_WINDOW)
      .build();
  final HepPlanner planner = new HepPlanner(program);
  planner.setRoot(rel);
  final RelNode window = planner.findBestExp().getInput(0);
  assertThat(window, instanceOf(Window.class));

  final RelMetadataQuery mq = window.getCluster().getMetadataQuery();
  final RexNode ref = RexInputRef.of(0, window.getRowType().getFieldList());
  final Set<RexNode> r = mq.getExpressionLineage(window, ref);
  assertThat(String.valueOf(r), is("[[CATALOG, SALES, EMP].#0.$0]"));
}
{code}
The test currently fails as follows:
{noformat}
Expected: is "[[CATALOG, SALES, EMP].#0.$0]"
     but: was "null"
{noformat}
Proposed fix: add a _Window_ handler to {_}RelMdExpressionLineage{_}. The 
output of a _Window_ is all its input columns, in the same order, followed by 
one column for each window function call. If the expression references only 
input columns, the handler delegates to the input, the same way the _Sort_ 
handler does 
([here|https://github.com/apache/calcite/blob/6484bed4572adbc219b171a9efbbd08a1b662f3a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdExpressionLineage.java#L422-L425]).
 If it references a window function call column, the handler returns null.



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

Reply via email to