You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2021/04/19 17:22:03 UTC

[GitHub] [calcite] zabetak opened a new pull request #2406: [CALCITE-4560]: Wrong plan when decorrelating EXISTS subquery with COALESCE in the predicate

zabetak opened a new pull request #2406:
URL: https://github.com/apache/calcite/pull/2406


   1. Add plan transformations before starting the core RelDecorrelator logic
   to bring the plan into an equivalent but more convenient form that can be
   decorrelated into more efficient and correct plans.
   
   2. Based on the changes above many plans with subqueries become more
   efficient since the value generator is no longer necessary and it is dropped.
   
   3. Add test case in SqlToRelConverter reproducing the problem (bad plan)
   and update existing tests based on the changes.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] zabetak commented on pull request #2406: [CALCITE-4560]: Wrong plan when decorrelating EXISTS subquery with COALESCE in the predicate

Posted by GitBox <gi...@apache.org>.
zabetak commented on pull request #2406:
URL: https://github.com/apache/calcite/pull/2406#issuecomment-828509885


   > I am not sure this approach will be robust enough to handle more general cases such as ($1 * $COR1) > ($2 - $COR2)
   
   I agree that it is not general but I think it captures the most common cases without hurting the rest. We can treat more complex cases when the time comes and if there is need to do so :) 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] zabetak commented on a change in pull request #2406: [CALCITE-4560]: Wrong plan when decorrelating EXISTS subquery with COALESCE in the predicate

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2406:
URL: https://github.com/apache/calcite/pull/2406#discussion_r622243891



##########
File path: core/src/main/java/org/apache/calcite/rel/rules/FilterFlattenCorrelatedConditionRule.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Planner rule that matches a {@link Filter} expression with correlated variables, and rewrites the
+ * condition in a simpler form that is more convenient for the decorrelation logic.
+ *
+ * Uncorrelated calls below a comparison operator are turned into input references by extracting the
+ * computation in a {@link org.apache.calcite.rel.core.Project} expression. An additional projection
+ * may be added on top of the new filter to retain expression equivalence.
+ *
+ * Sub-plan before
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalFilter(condition=[=($cor0.DEPTNO, +($7, 30))])
+ *     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ *
+ * Sub-plan after
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+ *     LogicalFilter(condition=[=($cor0.DEPTNO, $9)])
+ *       LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., SLACKER=[$8], $f9=[+($7, 30)])
+ *         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ */
+public final class FilterFlattenCorrelatedConditionRule
+    extends RelRule<FilterFlattenCorrelatedConditionRule.Config> {
+
+  public FilterFlattenCorrelatedConditionRule(final Config config) {
+    super(config);
+  }
+
+  @Override public boolean matches(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    return RexUtil.containsCorrelation(filter.getCondition());
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    RelBuilder b = call.builder();
+    b.push(filter.getInput());
+    final int proj = b.fields().size();
+    List<RexNode> projOperands = new ArrayList<>();
+    RexNode newCondition = filter.getCondition().accept(new RexShuttle() {
+      @Override public RexNode visitCall(RexCall call) {
+        switch (call.getKind()) {
+        case EQUALS:
+        case NOT_EQUALS:
+        case GREATER_THAN:
+        case GREATER_THAN_OR_EQUAL:
+        case LESS_THAN:
+        case LESS_THAN_OR_EQUAL:
+          RexNode op0 = call.operands.get(0);
+          RexNode op1 = call.operands.get(1);
+          final int replaceIndex;
+          if (RexUtil.containsCorrelation(op1) && isUncorrelatedCall(op0)) {
+            replaceIndex = 0;
+          } else if (RexUtil.containsCorrelation(op0) && isUncorrelatedCall(op1)) {
+            replaceIndex = 1;
+          } else {
+            // Structure does not match, do not replace
+            replaceIndex = -1;
+          }
+          if (replaceIndex != -1) {
+            List<RexNode> copyOperands = new ArrayList<>(call.operands);
+            RexNode oldOp = call.operands.get(replaceIndex);
+            RexNode newOp = b.getRexBuilder()
+                .makeInputRef(oldOp.getType(), proj + projOperands.size());
+            projOperands.add(oldOp);
+            copyOperands.set(replaceIndex, newOp);
+            return call.clone(call.type, copyOperands);
+          }
+          return call;
+        case AND:

Review comment:
       The rule is tightly connected with the current implementation in `RelDecorrelator#findCorrelationEquivalent`. If we cannot handle NOT there then we don't gain much by handling more kinds of expressions in this rule. I added some comments and extra javadoc in the code to better reflect the purpose.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] zabetak closed pull request #2406: [CALCITE-4560]: Wrong plan when decorrelating EXISTS subquery with COALESCE in the predicate

Posted by GitBox <gi...@apache.org>.
zabetak closed pull request #2406:
URL: https://github.com/apache/calcite/pull/2406


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] jamesstarr commented on pull request #2406: [CALCITE-4560]: Wrong plan when decorrelating EXISTS subquery with COALESCE in the predicate

Posted by GitBox <gi...@apache.org>.
jamesstarr commented on pull request #2406:
URL: https://github.com/apache/calcite/pull/2406#issuecomment-827089688


   I am not sure this approach will be robust enough to handle more general cases such as ($1 * $COR1) > ($2 - $COR2)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] jamesstarr commented on a change in pull request #2406: [CALCITE-4560]: Wrong plan when decorrelating EXISTS subquery with COALESCE in the predicate

Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2406:
URL: https://github.com/apache/calcite/pull/2406#discussion_r620542827



##########
File path: core/src/main/java/org/apache/calcite/rel/rules/FilterFlattenCorrelatedConditionRule.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Planner rule that matches a {@link Filter} expression with correlated variables, and rewrites the
+ * condition in a simpler form that is more convenient for the decorrelation logic.
+ *
+ * Uncorrelated calls below a comparison operator are turned into input references by extracting the
+ * computation in a {@link org.apache.calcite.rel.core.Project} expression. An additional projection
+ * may be added on top of the new filter to retain expression equivalence.
+ *
+ * Sub-plan before
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalFilter(condition=[=($cor0.DEPTNO, +($7, 30))])
+ *     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ *
+ * Sub-plan after
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+ *     LogicalFilter(condition=[=($cor0.DEPTNO, $9)])
+ *       LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., SLACKER=[$8], $f9=[+($7, 30)])
+ *         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ */
+public final class FilterFlattenCorrelatedConditionRule
+    extends RelRule<FilterFlattenCorrelatedConditionRule.Config> {
+
+  public FilterFlattenCorrelatedConditionRule(final Config config) {
+    super(config);
+  }
+
+  @Override public boolean matches(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    return RexUtil.containsCorrelation(filter.getCondition());
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    RelBuilder b = call.builder();
+    b.push(filter.getInput());
+    final int proj = b.fields().size();
+    List<RexNode> projOperands = new ArrayList<>();
+    RexNode newCondition = filter.getCondition().accept(new RexShuttle() {
+      @Override public RexNode visitCall(RexCall call) {
+        switch (call.getKind()) {
+        case EQUALS:

Review comment:
       Should this include IS_DISTINCT_FROM and IS_NOT_DISTINCT_FROM? 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] jamesstarr commented on a change in pull request #2406: [CALCITE-4560]: Wrong plan when decorrelating EXISTS subquery with COALESCE in the predicate

Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2406:
URL: https://github.com/apache/calcite/pull/2406#discussion_r620538609



##########
File path: core/src/main/java/org/apache/calcite/rel/rules/FilterFlattenCorrelatedConditionRule.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Planner rule that matches a {@link Filter} expression with correlated variables, and rewrites the
+ * condition in a simpler form that is more convenient for the decorrelation logic.
+ *
+ * Uncorrelated calls below a comparison operator are turned into input references by extracting the
+ * computation in a {@link org.apache.calcite.rel.core.Project} expression. An additional projection
+ * may be added on top of the new filter to retain expression equivalence.
+ *
+ * Sub-plan before
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalFilter(condition=[=($cor0.DEPTNO, +($7, 30))])
+ *     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ *
+ * Sub-plan after
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+ *     LogicalFilter(condition=[=($cor0.DEPTNO, $9)])
+ *       LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., SLACKER=[$8], $f9=[+($7, 30)])
+ *         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ */
+public final class FilterFlattenCorrelatedConditionRule
+    extends RelRule<FilterFlattenCorrelatedConditionRule.Config> {
+
+  public FilterFlattenCorrelatedConditionRule(final Config config) {
+    super(config);
+  }
+
+  @Override public boolean matches(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    return RexUtil.containsCorrelation(filter.getCondition());
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    RelBuilder b = call.builder();
+    b.push(filter.getInput());
+    final int proj = b.fields().size();
+    List<RexNode> projOperands = new ArrayList<>();
+    RexNode newCondition = filter.getCondition().accept(new RexShuttle() {
+      @Override public RexNode visitCall(RexCall call) {
+        switch (call.getKind()) {
+        case EQUALS:
+        case NOT_EQUALS:
+        case GREATER_THAN:
+        case GREATER_THAN_OR_EQUAL:
+        case LESS_THAN:
+        case LESS_THAN_OR_EQUAL:
+          RexNode op0 = call.operands.get(0);
+          RexNode op1 = call.operands.get(1);
+          final int replaceIndex;
+          if (RexUtil.containsCorrelation(op1) && isUncorrelatedCall(op0)) {
+            replaceIndex = 0;
+          } else if (RexUtil.containsCorrelation(op0) && isUncorrelatedCall(op1)) {
+            replaceIndex = 1;
+          } else {
+            // Structure does not match, do not replace
+            replaceIndex = -1;
+          }
+          if (replaceIndex != -1) {
+            List<RexNode> copyOperands = new ArrayList<>(call.operands);
+            RexNode oldOp = call.operands.get(replaceIndex);
+            RexNode newOp = b.getRexBuilder()
+                .makeInputRef(oldOp.getType(), proj + projOperands.size());
+            projOperands.add(oldOp);
+            copyOperands.set(replaceIndex, newOp);
+            return call.clone(call.type, copyOperands);
+          }
+          return call;
+        case AND:

Review comment:
       I believe you also not want here.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] zabetak commented on a change in pull request #2406: [CALCITE-4560]: Wrong plan when decorrelating EXISTS subquery with COALESCE in the predicate

Posted by GitBox <gi...@apache.org>.
zabetak commented on a change in pull request #2406:
URL: https://github.com/apache/calcite/pull/2406#discussion_r622240277



##########
File path: core/src/main/java/org/apache/calcite/rel/rules/FilterFlattenCorrelatedConditionRule.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Planner rule that matches a {@link Filter} expression with correlated variables, and rewrites the
+ * condition in a simpler form that is more convenient for the decorrelation logic.
+ *
+ * Uncorrelated calls below a comparison operator are turned into input references by extracting the
+ * computation in a {@link org.apache.calcite.rel.core.Project} expression. An additional projection
+ * may be added on top of the new filter to retain expression equivalence.
+ *
+ * Sub-plan before
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalFilter(condition=[=($cor0.DEPTNO, +($7, 30))])
+ *     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ *
+ * Sub-plan after
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+ *     LogicalFilter(condition=[=($cor0.DEPTNO, $9)])
+ *       LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., SLACKER=[$8], $f9=[+($7, 30)])
+ *         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ */
+public final class FilterFlattenCorrelatedConditionRule
+    extends RelRule<FilterFlattenCorrelatedConditionRule.Config> {
+
+  public FilterFlattenCorrelatedConditionRule(final Config config) {
+    super(config);
+  }
+
+  @Override public boolean matches(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    return RexUtil.containsCorrelation(filter.getCondition());
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    RelBuilder b = call.builder();
+    b.push(filter.getInput());
+    final int proj = b.fields().size();
+    List<RexNode> projOperands = new ArrayList<>();
+    RexNode newCondition = filter.getCondition().accept(new RexShuttle() {
+      @Override public RexNode visitCall(RexCall call) {
+        switch (call.getKind()) {
+        case EQUALS:

Review comment:
       Makes sense, although it will not make a huge difference for Calcite itself.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [calcite] jamesstarr commented on a change in pull request #2406: [CALCITE-4560]: Wrong plan when decorrelating EXISTS subquery with COALESCE in the predicate

Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2406:
URL: https://github.com/apache/calcite/pull/2406#discussion_r620537008



##########
File path: core/src/main/java/org/apache/calcite/rel/rules/FilterFlattenCorrelatedConditionRule.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Planner rule that matches a {@link Filter} expression with correlated variables, and rewrites the
+ * condition in a simpler form that is more convenient for the decorrelation logic.
+ *
+ * Uncorrelated calls below a comparison operator are turned into input references by extracting the
+ * computation in a {@link org.apache.calcite.rel.core.Project} expression. An additional projection
+ * may be added on top of the new filter to retain expression equivalence.
+ *
+ * Sub-plan before
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalFilter(condition=[=($cor0.DEPTNO, +($7, 30))])
+ *     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ *
+ * Sub-plan after
+ * <pre>
+ * LogicalProject($f0=[true])
+ *   LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., COMM=[$6], DEPTNO=[$7], SLACKER=[$8])
+ *     LogicalFilter(condition=[=($cor0.DEPTNO, $9)])
+ *       LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2],..., SLACKER=[$8], $f9=[+($7, 30)])
+ *         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ */
+public final class FilterFlattenCorrelatedConditionRule
+    extends RelRule<FilterFlattenCorrelatedConditionRule.Config> {
+
+  public FilterFlattenCorrelatedConditionRule(final Config config) {
+    super(config);
+  }
+
+  @Override public boolean matches(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    return RexUtil.containsCorrelation(filter.getCondition());
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    Filter filter = call.rel(0);
+    RelBuilder b = call.builder();
+    b.push(filter.getInput());
+    final int proj = b.fields().size();
+    List<RexNode> projOperands = new ArrayList<>();
+    RexNode newCondition = filter.getCondition().accept(new RexShuttle() {
+      @Override public RexNode visitCall(RexCall call) {
+        switch (call.getKind()) {
+        case EQUALS:
+        case NOT_EQUALS:
+        case GREATER_THAN:
+        case GREATER_THAN_OR_EQUAL:
+        case LESS_THAN:
+        case LESS_THAN_OR_EQUAL:
+          RexNode op0 = call.operands.get(0);
+          RexNode op1 = call.operands.get(1);
+          final int replaceIndex;
+          if (RexUtil.containsCorrelation(op1) && isUncorrelatedCall(op0)) {
+            replaceIndex = 0;
+          } else if (RexUtil.containsCorrelation(op0) && isUncorrelatedCall(op1)) {
+            replaceIndex = 1;
+          } else {
+            // Structure does not match, do not replace
+            replaceIndex = -1;
+          }
+          if (replaceIndex != -1) {
+            List<RexNode> copyOperands = new ArrayList<>(call.operands);
+            RexNode oldOp = call.operands.get(replaceIndex);
+            RexNode newOp = b.getRexBuilder()
+                .makeInputRef(oldOp.getType(), proj + projOperands.size());
+            projOperands.add(oldOp);
+            copyOperands.set(replaceIndex, newOp);
+            return call.clone(call.type, copyOperands);
+          }
+          return call;
+        case AND:

Review comment:
       I believe you also want NOT here. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org