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 2022/05/10 17:19:56 UTC

[GitHub] [calcite] julianhyde commented on a diff in pull request #2800: [CALCITE-3890] Derive IS NOT NULL filter from inner join

julianhyde commented on code in PR #2800:
URL: https://github.com/apache/calcite/pull/2800#discussion_r869499838


##########
core/src/main/java/org/apache/calcite/rel/rules/JoinDeriveIsNotNullFilterRule.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.RelOptPredicateList;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinInfo;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexExecutor;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexSimplify;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableIntList;
+import org.apache.calcite.util.Util;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Planner rule that derives is not null filter from inner join.
+ */
+@Value.Enclosing
+public class JoinDeriveIsNotNullFilterRule
+    extends RelRule<JoinDeriveIsNotNullFilterRule.Config> implements TransformationRule {
+
+  public JoinDeriveIsNotNullFilterRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final Join join = call.rel(0);
+    final RelBuilder relBuilder = call.builder();
+    final RelMetadataQuery mq = call.getMetadataQuery();
+    JoinInfo joinInfo = join.analyzeCondition();
+    RelNode newLeftNode = createIsNotNullFilter(join.getLeft(), joinInfo.leftKeys,
+        relBuilder, mq);
+    RelNode newRightNode = createIsNotNullFilter(join.getRight(), joinInfo.rightKeys,
+        relBuilder, mq);
+
+    if (newLeftNode != join.getLeft() || newRightNode != join.getRight()) {
+      List<RelNode> inputs = new ArrayList<>();
+      inputs.add(newLeftNode);
+      inputs.add(newRightNode);
+      RelNode newJoin = join.copy(join.getTraitSet(), inputs);
+      call.transformTo(newJoin);
+    }
+  }
+
+  private RelNode createIsNotNullFilter(RelNode input, ImmutableIntList keys,

Review Comment:
   This method would be simpler if it didn't return a RelNode but just operated on the RelBuilder. You wouldn't need to call simplify, or check whether the condition is true. It's quite possible it would be just one line.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

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