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/15 11:40:15 UTC

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

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


##########
core/src/main/java/org/apache/calcite/rel/rules/JoinDeriveIsNotNullFilterRule.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.RelOptUtil;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.plan.Strong;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Join;
+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.ImmutableBitSet;
+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();
+
+    final RexNode condition = join.getCondition();
+    final List<RexNode> nodes = RelOptUtil.conjunctions(condition);
+    final ImmutableBitSet.Builder notNullableKeys = ImmutableBitSet.builder();
+    nodes.forEach(node -> {
+      if (Strong.isStrong(node)) {
+        notNullableKeys.addAll(RelOptUtil.InputFinder.bits(node));
+      }
+    });
+
+    final List<Integer> leftKeys = new ArrayList<>();
+    final List<Integer> rightKeys = new ArrayList<>();
+
+    final int offset = join.getLeft().getRowType().getFieldCount();
+    notNullableKeys.build().asList().forEach(i -> {
+      if (i < offset) {
+        leftKeys.add(i);
+      } else {
+        rightKeys.add(i - offset);
+      }
+    });
+
+    final RelNode newLeftNode = createIsNotNullFilter(join.getLeft(), leftKeys,
+        relBuilder, mq);
+    final RelNode newRightNode = createIsNotNullFilter(join.getRight(), rightKeys,
+        relBuilder, mq);
+
+    if (newLeftNode != join.getLeft() || newRightNode != join.getRight()) {
+      final List<RelNode> inputs = new ArrayList<>();

Review Comment:
   minor: we can specify the initial capacity 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.

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

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