Weihua Zhang created CALCITE-7967:
-------------------------------------
Summary: Run sub-query removal before RelDecorrelator in the
default planning pipeline
Key: CALCITE-7967
URL: https://issues.apache.org/jira/browse/CALCITE-7967
Project: Calcite
Issue Type: Improvement
Reporter: Weihua Zhang
In the default pipeline, Calcite decorrelates a plan twice, and the first pass
runs on a plan that still contains RexSubQuery expressions:
1. Prepare.prepareSql (and Planner.rel) call RelDecorrelator right after
SQL-to-rel conversion. At this point sub-queries are still RexSubQuery, so the
decorrelator removes only the Correlate nodes (for example, from LATERAL). The
nodes that declare variables for their sub-queries (Filter, Project, Join with
a non-empty variablesSet) are kept.
2. Programs.standard() runs SubQueryRemoveRule (Programs.subQuery), which turns
the sub-queries into Correlate nodes.
3. DecorrelateProgram runs RelDecorrelator again.
The first pass works on a mixed representation: some correlation is expressed
as Correlate nodes and some as RexSubQuery expressions that use $corN
variables. Several bugs come from this mix, because the decorrelator rebuilds a
node while its sub-queries still refer to its old row:
- CALCITE-7962: RelDecorrelator removes the variablesSet of a Filter, Project
or Join whose input it decorrelates. The sub-query is then treated as
uncorrelated, or fails with "Correlation variable $cor1 should be defined".
When the decorrelator moves the fields of the input, the sub-query reads the
wrong field unless its field accesses are also moved.
The new TopDownGeneralDecorrelator already requires the other order. Prepare
skips the early decorrelation when it is enabled ("TopDownGeneralDecorrelator
cannot be used until the subquerys are completely removed"), and
Programs.subQuery then removes sub-queries before decorrelating.
Proposal: In the default pipeline, remove all sub-queries first, then
decorrelate once:
SqlToRel (expand=false) -> SubQueryRemoveRule -> RelDecorrelator -> other rules
Benefits:
- The decorrelator sees a single representation of correlation (Correlate
only), so the class of bugs in CALCITE-7962 cannot occur.
- Decorrelation runs once instead of twice.
- Both decorrelators use the same pipeline.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)