hequn8128 commented on a change in pull request #10033: 
[FLINK-14021][table-planner][table-planner-blink] Add rules to push down the 
Python ScalarFunctions contained in the join condition of Correlate node
URL: https://github.com/apache/flink/pull/10033#discussion_r352326375
 
 

 ##########
 File path: 
flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/plan/rules/logical/SplitPythonConditionFromCorrelateRule.scala
 ##########
 @@ -0,0 +1,178 @@
+/*
+ * 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.flink.table.planner.plan.rules.logical
+
+import org.apache.calcite.plan.RelOptRule.{any, operand}
+import org.apache.calcite.plan.hep.HepRelVertex
+import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall, RelOptUtil}
+import org.apache.calcite.rel.core.JoinRelType
+import org.apache.calcite.rex._
+import org.apache.flink.table.planner.plan.nodes.logical.{FlinkLogicalCalc, 
FlinkLogicalCorrelate, FlinkLogicalRel}
+import org.apache.flink.table.planner.plan.utils.PythonUtil.containsPythonCall
+import org.apache.flink.table.planner.plan.utils.RexDefaultVisitor
+
+import scala.collection.JavaConversions._
+import scala.collection.JavaConverters._
+
+/**
+  * Rule will split a [[FlinkLogicalCalc]] which is the upstream of a 
[[FlinkLogicalCorrelate]]
+  * and contains Python Functions in condition into two [[FlinkLogicalCalc]]s. 
One of the
+  * [[FlinkLogicalCalc]] without python function condition is the upstream of 
the
+  * [[FlinkLogicalCorrelate]], but the other [[[FlinkLogicalCalc]] with python 
function conditions
+  * is the downstream of the [[FlinkLogicalCorrelate]]. Currently, only inner 
join is supported.
+  *
+  * After this rule is applied, there will be no Python Functions in the 
condition of the upstream
+  * [[FlinkLogicalCalc]].
+  */
+class SplitPythonConditionFromCorrelateRule
+  extends RelOptRule(
+    operand(
+      classOf[FlinkLogicalCorrelate],
+      operand(classOf[FlinkLogicalRel], any),
+      operand(classOf[FlinkLogicalCalc], any)),
+    "SplitPythonConditionFromCorrelateRule") {
+  override def matches(call: RelOptRuleCall): Boolean = {
+    val correlate: FlinkLogicalCorrelate = 
call.rel(0).asInstanceOf[FlinkLogicalCorrelate]
+    val right: FlinkLogicalCalc = call.rel(2).asInstanceOf[FlinkLogicalCalc]
+    val joinType: JoinRelType = correlate.getJoinType
+    joinType == JoinRelType.INNER && containsPythonFunctionCondition(right)
+  }
+
+  private def containsPythonFunctionCondition(calc: FlinkLogicalCalc): Boolean 
= {
+    val program = calc.getProgram
+    val conditionExistPythonFunction = Option(program.getCondition)
+      .map(program.expandLocalRef)
+      .exists(containsPythonCall)
+    if (conditionExistPythonFunction) {
+      true
+    } else {
+      val child = calc.getInput.asInstanceOf[HepRelVertex].getCurrentRel
+      child match {
+        case calc: FlinkLogicalCalc => containsPythonFunctionCondition(calc)
+        case _ => false
+      }
+    }
+  }
+
+  private def getMergedCalc(calc: FlinkLogicalCalc): FlinkLogicalCalc = {
+    val child = calc.getInput.asInstanceOf[HepRelVertex].getCurrentRel
+    child match {
+      case logicalCalc: FlinkLogicalCalc =>
+        val bottomCalc = getMergedCalc(logicalCalc)
+        val topCalc = calc
+        val topProgram: RexProgram = topCalc.getProgram
+        val mergedProgram: RexProgram = RexProgramBuilder
+          .mergePrograms(
+            topCalc.getProgram,
+            bottomCalc.getProgram,
+            topCalc.getCluster.getRexBuilder)
+        assert(mergedProgram.getOutputRowType eq topProgram.getOutputRowType)
+        topCalc.copy(topCalc.getTraitSet, bottomCalc.getInput, mergedProgram)
+          .asInstanceOf[FlinkLogicalCalc]
+      case _ =>
+        calc
+    }
+  }
+
+  override def onMatch(call: RelOptRuleCall): Unit = {
+    val correlate: FlinkLogicalCorrelate = 
call.rel(0).asInstanceOf[FlinkLogicalCorrelate]
+    val right: FlinkLogicalCalc = call.rel(2).asInstanceOf[FlinkLogicalCalc]
+    val rexBuilder = call.builder().getRexBuilder
+    val mergedCalc = getMergedCalc(right)
+    val mergedCalcProgram = mergedCalc.getProgram
+    val input = mergedCalc.getInput
+
+    val correlateFilters = RelOptUtil
+      .conjunctions(mergedCalcProgram.getCondition)
 
 Review comment:
   There is no use to call `conjunctions` here, because the 
`mergedCalcProgram.getCondition` returns a RexLocalRef and 
`RelOptUtil.conjunctions` returns the same RexLocalRef directly, see the 
details in `RelOptUtil.conjunctions`.  This means all conditions will be pushed 
to the downstream of Correlate(include the java condition). 
   
   You can use 
`RelOptUtil.conjunctions(mergedCalcProgram.expandLocalRef(mergedCalcProgram.getCondition))`
 instead.
   
   Futhermore, you can add a test with java udf in the condition of correlate 
to verify this. For example, 
   
   ```
       val javaFunc = new JavaFunc
       val result = table
         .joinLateral(func('c) as ('s, 'l), 'l === 'a && 'c === 's && 
pyFunc('l, 'l) === 2 && javaFunc('l, 'l))
   ```
   Probably, you can reuse an existing java udf.

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


With regards,
Apache Git Services

Reply via email to