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/28 14:33:05 UTC

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

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