[
https://issues.apache.org/jira/browse/CALCITE-7916?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Atharv Rastogi updated CALCITE-7916:
------------------------------------
Description:
{{RelMdPredicates}} currently has no {{getPredicates(Window,
RelMetadataQuery)}} handler. A {{Window}} therefore uses the generic
{{RelNode}} handler, which returns {{{}RelOptPredicateList.EMPTY{}}}, losing
facts already known about its input.
{{LogicalWindow}} preserves the input columns at their original positions and
appends the window-function results. Predicates already established on those
input columns therefore remain valid above the Window.
For example:
{{SELECT CASE WHEN deptno = 10 THEN rn ELSE -1 END AS result FROM ( SELECT
deptno, ROW_NUMBER() OVER (ORDER BY empno) AS rn FROM emp WHERE deptno = 10 )
ranked;}}
Once the window expression has been converted to a {{{}LogicalWindow{}}}, the
relevant plan shape is:
{code:java}
{{Project(result = CASE WHEN deptno = 10 THEN rn ELSE -1 END)
Window(rn = ROW_NUMBER() OVER (ORDER BY empno))
Filter(deptno = 10)
Scan(emp)}}{code}
Every row entering and leaving the Window has {{{}deptno = 10{}}}. However,
querying the Window’s pulled-up predicates currently returns an empty list,
preventing consumers of this metadata from using that fact.
With the predicate preserved, the existing project expression-reduction rule
should be able to simplify the plan to:
{code:java}
{{Project(result = rn)
Window(rn = ROW_NUMBER() OVER (ORDER BY empno))
Filter(deptno = 10)
Scan(emp)}}{code}
The same metadata can also expose redundant filters above a Window.
Proposed change:
Add a Window handler to {{RelMdPredicates}} that preserves the input’s
pulled-up predicates and their derived constant information. This applies to
constants, ranges, and other predicates on the unchanged input columns. It
should not infer new facts about the window-function results.
The returned metadata should contain the predicates applicable to the Window’s
output; join-specific {{leftInferredPredicates}} and
{{rightInferredPredicates}} from an input Join should not be carried over as
Window metadata.
Tests should cover constant and non-constant input predicates, nullable
columns, unchanged column positions, and expression simplification above a
Window.
Relevant source:
*
[RelMdPredicates.java|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/rel/metadata/RelMdPredicates.java]:
missing Window handler and empty generic fallback.
*
[LogicalWindow.java|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/rel/logical/LogicalWindow.java]:
output construction preserves input fields before appending window results.
*
[ReduceExpressionsRule.java|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java]:
Filter and Project reduction consume their input’s pulled-up predicates.
was:
{{RelMdPredicates}} currently has no {{getPredicates(Window,
RelMetadataQuery)}} handler. A {{Window}} therefore uses the generic
{{RelNode}} handler, which returns {{{}RelOptPredicateList.EMPTY{}}}, losing
facts already known about its input.
{{LogicalWindow}} preserves the input columns at their original positions and
appends the window-function results. Predicates already established on those
input columns therefore remain valid above the Window.
For example:
{{SELECT CASE WHEN deptno = 10 THEN rn ELSE -1 END AS result FROM ( SELECT
deptno, ROW_NUMBER() OVER (ORDER BY empno) AS rn FROM emp WHERE deptno = 10 )
ranked;}}
Once the window expression has been converted to a {{{}LogicalWindow{}}}, the
relevant plan shape is:
{code:java}
{{Project(result = CASE WHEN deptno = 10 THEN rn ELSE -1 END)
Window(rn = ROW_NUMBER() OVER (ORDER BY empno))
Filter(deptno = 10)
Scan(emp)}}{code}
Every row entering and leaving the Window has {{{}deptno = 10{}}}. However,
querying the Window’s pulled-up predicates currently returns an empty list,
preventing consumers of this metadata from using that fact.
With the predicate preserved, the existing project expression-reduction rule
should be able to simplify the plan to:
{code:java}
{{Project(result = rn)
Window(rn = ROW_NUMBER() OVER (ORDER BY empno))
Filter(deptno = 10)
Scan(emp)}}{code}
The same metadata can also expose redundant filters above a Window.
Proposed change:
Add a Window handler to {{RelMdPredicates}} that preserves the input’s
pulled-up predicates and their derived constant information. This applies to
constants, ranges, and other predicates on the unchanged input columns. It
should not infer new facts about the window-function results.
The returned metadata should contain the predicates applicable to the Window’s
output; join-specific {{leftInferredPredicates}} and
{{rightInferredPredicates}} from an input Join should not be carried over as
Window metadata.
Tests should cover constant and non-constant input predicates, nullable
columns, unchanged column positions, and expression simplification above a
Window.
Relevant source:
*
[RelMdPredicates.java|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/rel/metadata/RelMdPredicates.java]:
missing Window handler and empty generic fallback.
*
[LogicalWindow.java|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/rel/logical/LogicalWindow.java]:
output construction preserves input fields before appending window results.
*
[ReduceExpressionsRule.java|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java]:
Filter and Project reduction consume their input’s pulled-up predicates.
> RelMdPredicates should preserve input predicates through Window
> ---------------------------------------------------------------
>
> Key: CALCITE-7916
> URL: https://issues.apache.org/jira/browse/CALCITE-7916
> Project: Calcite
> Issue Type: Bug
> Reporter: Atharv Rastogi
> Priority: Minor
>
> {{RelMdPredicates}} currently has no {{getPredicates(Window,
> RelMetadataQuery)}} handler. A {{Window}} therefore uses the generic
> {{RelNode}} handler, which returns {{{}RelOptPredicateList.EMPTY{}}}, losing
> facts already known about its input.
> {{LogicalWindow}} preserves the input columns at their original positions and
> appends the window-function results. Predicates already established on those
> input columns therefore remain valid above the Window.
> For example:
>
>
> {{SELECT CASE WHEN deptno = 10 THEN rn ELSE -1 END AS result FROM ( SELECT
> deptno, ROW_NUMBER() OVER (ORDER BY empno) AS rn FROM emp WHERE deptno = 10 )
> ranked;}}
>
>
> Once the window expression has been converted to a {{{}LogicalWindow{}}}, the
> relevant plan shape is:
>
> {code:java}
> {{Project(result = CASE WHEN deptno = 10 THEN rn ELSE -1 END)
> Window(rn = ROW_NUMBER() OVER (ORDER BY empno))
> Filter(deptno = 10)
> Scan(emp)}}{code}
>
> Every row entering and leaving the Window has {{{}deptno = 10{}}}. However,
> querying the Window’s pulled-up predicates currently returns an empty list,
> preventing consumers of this metadata from using that fact.
> With the predicate preserved, the existing project expression-reduction rule
> should be able to simplify the plan to:
>
> {code:java}
> {{Project(result = rn)
> Window(rn = ROW_NUMBER() OVER (ORDER BY empno))
> Filter(deptno = 10)
> Scan(emp)}}{code}
>
> The same metadata can also expose redundant filters above a Window.
> Proposed change:
> Add a Window handler to {{RelMdPredicates}} that preserves the input’s
> pulled-up predicates and their derived constant information. This applies to
> constants, ranges, and other predicates on the unchanged input columns. It
> should not infer new facts about the window-function results.
> The returned metadata should contain the predicates applicable to the
> Window’s output; join-specific {{leftInferredPredicates}} and
> {{rightInferredPredicates}} from an input Join should not be carried over as
> Window metadata.
> Tests should cover constant and non-constant input predicates, nullable
> columns, unchanged column positions, and expression simplification above a
> Window.
> Relevant source:
> *
> [RelMdPredicates.java|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/rel/metadata/RelMdPredicates.java]:
> missing Window handler and empty generic fallback.
> *
> [LogicalWindow.java|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/rel/logical/LogicalWindow.java]:
> output construction preserves input fields before appending window results.
> *
> [ReduceExpressionsRule.java|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java]:
> Filter and Project reduction consume their input’s pulled-up predicates.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)