Mihai Budiu created CALCITE-7966:
------------------------------------

             Summary: Many optimizer rules produce invalid plans when a 
Project, Filter or Join has a correlated sub-query
                 Key: CALCITE-7966
                 URL: https://issues.apache.org/jira/browse/CALCITE-7966
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.42.0
            Reporter: Mihai Budiu


A Project, Filter or Join whose expressions contain a correlated sub-query 
({{RexSubQuery}}) declares the variable of the sub-query in its 
{{variablesSet}}. The variable refers to the input row of the node. For a Join, 
it refers to the joined row. {{SubQueryRemoveRule}} uses {{variablesSet}} to 
find the variables that a node declares.

Many rules were written for plans without sub-queries. When such a rule matches 
a node that declares a variable, it moves, copies or rebuilds the expressions 
that contain the sub-query, but it does not move, copy or rebuild the 
declaration of the variable. The result is an invalid plan:
# After the rule, the node that holds the sub-query does not declare the 
variable, but the sub-query still uses it. Usually the rule builds the new node 
with {{RelBuilder}} ({{filter}}, {{project}}, {{join}}) without the 
{{variablesSet}}.
# Some rules also change the input row of the node. Then the index of a field 
access, such as {{$cor0.DEPTNO0}}, refers to a field that does not exist, or to 
a different field.

h3. Problematic rules

||Rule||What the rule does with the sub-query||
|{{PROJECT_FILTER_TRANSPOSE}}, {{PROJECT_FILTER_TRANSPOSE_WHOLE_EXPRESSIONS}}, 
{{PROJECT_FILTER_TRANSPOSE_WHOLE_PROJECT_EXPRESSIONS}}|Rebuilds the Filter 
without the variable. The new Project below it can remove a field that the 
sub-query reads|
|{{FILTER_PROJECT_TRANSPOSE}}|Copies the sub-query from the Project into a new 
Filter below it, without a variable|
|{{FILTER_AGGREGATE_TRANSPOSE}}|Moves the Filter below the Aggregate, without 
the variable|
|{{FILTER_SET_OP_TRANSPOSE}}|Copies the Filter into each input of the Union, 
without the variable|
|{{FILTER_REDUCE_EXPRESSIONS}}|Rebuilds the Filter without the variable|
|{{PROJECT_JOIN_TRANSPOSE}}|The Join keeps the variable, but its inputs get 
fewer fields. {{$cor0.DEPTNO0}} (field 8) then refers to a row with 4 fields|
|{{JOIN_EXTRACT_FILTER}}|Moves the ON condition into a Filter above the Join. 
The Join keeps the variable, and the Filter has none|
|{{JOIN_ADD_REDUNDANT_SEMI_JOIN}}|Copies the ON condition into a semi-join, 
without a variable|
|{{JOIN_ON_UNIQUE_TO_SEMI_JOIN}}|Rebuilds the Project without the variable. The 
semi-join below it has no fields of the right input, but the sub-query reads 
one|
|{{JOIN_TO_CORRELATE}}|The Correlate declares a new variable for the left row. 
The ON sub-query still uses the variable of the Join|
|{{JOIN_TO_MULTI_JOIN}}, {{JOIN_TO_HYPER_GRAPH}}|{{MultiJoin}} and 
{{HyperGraph}} cannot declare a variable|

h3. Proposed fix

A rule that does not keep the variables must not match a Project, Filter or 
Join whose {{variablesSet}} is not empty. Some rules do this already. 
{{ProjectMergeRule}} and {{JoinUnionTransposeRule}} check {{variablesSet}}. 
{{FilterToCalcRule}}, {{ProjectToCalcRule}}, {{FilterCalcMergeRule}} and 
{{ProjectCalcMergeRule}} do not match nodes with sub-queries.

Found while working on CALCITE-7340.





--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to