maropu commented on a change in pull request #28741:
URL: https://github.com/apache/spark/pull/28741#discussion_r436268072



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PushPredicateThroughJoin.scala
##########
@@ -0,0 +1,248 @@
+/*
+ * 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.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.catalyst.expressions.{And, AttributeSet, 
Expression, Not, Or, PredicateHelper}
+import org.apache.spark.sql.catalyst.plans._
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, Join, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+
+trait PushPredicateThroughJoinBase extends Rule[LogicalPlan] with 
PredicateHelper {
+  protected def enablePushingExtraPredicates: Boolean
+  /**
+   * Splits join condition expressions or filter predicates (on a given join's 
output) into three
+   * categories based on the attributes required to evaluate them. Note that 
we explicitly exclude
+   * non-deterministic (i.e., stateful) condition expressions in 
canEvaluateInLeft or
+   * canEvaluateInRight to prevent pushing these predicates on either side of 
the join.
+   *
+   * @return (canEvaluateInLeft, canEvaluateInRight, haveToEvaluateInBoth)
+   */
+  private def split(condition: Seq[Expression], left: LogicalPlan, right: 
LogicalPlan) = {
+    val (pushDownCandidates, nonDeterministic) = 
condition.partition(_.deterministic)
+    val (leftEvaluateCondition, rest) =
+      pushDownCandidates.partition(_.references.subsetOf(left.outputSet))
+    val (rightEvaluateCondition, commonCondition) =
+        rest.partition(expr => expr.references.subsetOf(right.outputSet))
+
+    // For the predicates in `commonCondition`, it is still possible to find 
sub-predicates which
+    // are able to be pushed down.
+    val leftExtraCondition = if (enablePushingExtraPredicates) {
+      commonCondition.flatMap(convertibleFilter(_, left.outputSet, 
canPartialPushDown = true))
+    } else {
+      Seq.empty
+    }
+    val rightExtraCondition = if (enablePushingExtraPredicates) {
+      commonCondition.flatMap(convertibleFilter(_, right.outputSet, 
canPartialPushDown = true))
+    } else {
+      Seq.empty
+    }
+
+    // To avoid expanding the join condition into conjunctive normal form and 
making the size
+    // of codegen much larger, `commonCondition` will be kept as original form 
in the new join
+    // condition.
+    (leftEvaluateCondition ++ leftExtraCondition, rightEvaluateCondition ++ 
rightExtraCondition,
+      commonCondition ++ nonDeterministic)
+  }
+
+  private def convertibleFilter(
+    condition: Expression,
+    outputSet: AttributeSet,
+    canPartialPushDown: Boolean): Option[Expression] = condition match {
+    // At here, it is not safe to just convert one side and remove the other 
side
+    // if we do not understand what the parent filters are.
+    //
+    // Here is an example used to explain the reason.
+    // Let's say we have NOT(a = 2 AND b in ('1')) and we do not understand 
how to

Review comment:
       nit: How about the case, `NOT(a = 2 OR b in ('1'))`? This case can be 
transformed into `NOT(a = 2) AND NOT(b in ('1'))` then I think it can be 
partially pushed down.




----------------------------------------------------------------
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to