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 10:07:52 UTC

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

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


##########
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,
+      RelBuilder relBuilder, RelMetadataQuery mq) {
+    final RelOptPredicateList relOptPredicateList = mq.getPulledUpPredicates(input);
+    final RexExecutor executor =
+        Util.first(input.getCluster().getPlanner().getExecutor(), RexUtil.EXECUTOR);
+    final RexBuilder rexBuilder = input.getCluster().getRexBuilder();
+    final RexSimplify simplify = new RexSimplify(rexBuilder, relOptPredicateList, executor);
+    List<RexNode> nodes = new ArrayList<>();
+    keys.forEach(i -> {
+      final RexNode expr = relBuilder
+          .call(SqlStdOperatorTable.IS_NOT_NULL, rexBuilder.makeInputRef(input, i));
+      RexNode simplified = simplify.simplify(expr);
+      if (!simplified.isAlwaysTrue()) {
+        nodes.add(expr);
+      }
+    });

Review Comment:
   I love lambdas, but in cases like this I find them less readable than a traditional for each loop, WDYT?



##########
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,

Review Comment:
   Nitpick: please try to be consistent with the `final` modifier, if you have a mixed usage like in this case, one would expect the variables without `final` to be reassigned, but this does not happen and it's confusing.
   
   I noticed it also in the `createIsNotNullFilter` method.



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