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_r352327068
########## File path: flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/plan/rules/logical/SplitPythonConditionFromCorrelateRule.scala ########## @@ -0,0 +1,175 @@ +/* + * 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.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.plan.nodes.logical.{FlinkLogicalCalc, FlinkLogicalCorrelate, FlinkLogicalRel} +import org.apache.flink.table.plan.util.PythonUtil.containsPythonCall +import org.apache.flink.table.plan.util.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) + .map(_.asInstanceOf[RexLocalRef]) + .map(mergedCalcProgram.expandLocalRef) + val remainingFilters = correlateFilters.filter(!containsPythonCall(_)) + + val bottomCalcCondition = RexUtil.composeConjunction(rexBuilder, remainingFilters) + val newBottomCalc = new FlinkLogicalCalc( + mergedCalc.getCluster, + mergedCalc.getTraitSet, + input, + RexProgram.create( + input.getRowType, + mergedCalcProgram.getProjectList, + bottomCalcCondition, + mergedCalc.getRowType, + rexBuilder)) + + val newCorrelate = new FlinkLogicalCorrelate( + correlate.getCluster, + correlate.getTraitSet, + correlate.getLeft, + newBottomCalc, + correlate.getCorrelationId, + correlate.getRequiredColumns, + correlate.getJoinType) + + val inputRefRewriter = new InputRefRewriter( + correlate.getRowType.getFieldCount - mergedCalc.getRowType.getFieldCount) + + val pythonFilters = correlateFilters + .map(_.accept(inputRefRewriter)) + .filter(containsPythonCall) + val topCalcCondition = RexUtil.composeConjunction(rexBuilder, pythonFilters) + + val rexProgram = new RexProgramBuilder(newCorrelate.getRowType, rexBuilder).getProgram + val newTopCalc = new FlinkLogicalCalc( + newCorrelate.getCluster, + newCorrelate.getTraitSet, + newCorrelate, + RexProgram.create( + newCorrelate.getRowType, + rexProgram.getExprList, + topCalcCondition, + newCorrelate.getRowType, + rexBuilder)) + + call.transformTo(newTopCalc) + } +} + +/** + * Because the inputRef is from the upstream calc node of the correlate node, Review comment: Keep the comment length 100 characters. ``` /** * Because the inputRef is from the upstream calc node of the correlate node, so after the inputRef * is pushed to the downstream calc node of the correlate node, the inputRef need to rewrite the * index. * * @param offset the start offset of the inputRef in the downstream calc. */ ``` ---------------------------------------------------------------- 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
