| /* |
| * 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.nodes.logical |
| |
| import org.apache.calcite.plan._ |
| import org.apache.calcite.plan.volcano.RelSubset |
| import org.apache.calcite.rel.RelNode |
| import org.apache.calcite.rel.convert.ConverterRule |
| import org.apache.calcite.rel.core._ |
| import org.apache.calcite.rel.logical.LogicalJoin |
| import org.apache.calcite.rex.RexNode |
| import org.apache.flink.table.plan.nodes.FlinkConventions |
| |
| class FlinkLogicalJoin( |
| cluster: RelOptCluster, |
| traitSet: RelTraitSet, |
| left: RelNode, |
| right: RelNode, |
| condition: RexNode, |
| joinType: JoinRelType) |
| extends FlinkLogicalJoinBase( |
| cluster, |
| traitSet, |
| left, |
| right, |
| condition, |
| joinType) { |
| |
| override def copy( |
| traitSet: RelTraitSet, |
| conditionExpr: RexNode, |
| left: RelNode, |
| right: RelNode, |
| joinType: JoinRelType, |
| semiJoinDone: Boolean): Join = { |
| |
| new FlinkLogicalJoin(cluster, traitSet, left, right, conditionExpr, joinType) |
| } |
| } |
| |
| private class FlinkLogicalJoinConverter |
| extends ConverterRule( |
| classOf[LogicalJoin], |
| Convention.NONE, |
| FlinkConventions.LOGICAL, |
| "FlinkLogicalJoinConverter") { |
| |
| override def matches(call: RelOptRuleCall): Boolean = { |
| val join: LogicalJoin = call.rel(0).asInstanceOf[LogicalJoin] |
| val joinInfo = join.analyzeCondition |
| |
| hasEqualityPredicates(joinInfo) || isSingleRowJoin(join) |
| } |
| |
| override def convert(rel: RelNode): RelNode = { |
| val join = rel.asInstanceOf[LogicalJoin] |
| val traitSet = rel.getTraitSet.replace(FlinkConventions.LOGICAL) |
| val newLeft = RelOptRule.convert(join.getLeft, FlinkConventions.LOGICAL) |
| val newRight = RelOptRule.convert(join.getRight, FlinkConventions.LOGICAL) |
| |
| new FlinkLogicalJoin( |
| rel.getCluster, |
| traitSet, |
| newLeft, |
| newRight, |
| join.getCondition, |
| join.getJoinType) |
| } |
| |
| private def hasEqualityPredicates(joinInfo: JoinInfo): Boolean = { |
| // joins require an equi-condition or a conjunctive predicate with at least one equi-condition |
| !joinInfo.pairs().isEmpty |
| } |
| |
| private def isSingleRowJoin(join: LogicalJoin): Boolean = { |
| join.getJoinType match { |
| case JoinRelType.INNER if isSingleRow(join.getRight) || isSingleRow(join.getLeft) => true |
| case JoinRelType.LEFT if isSingleRow(join.getRight) => true |
| case JoinRelType.RIGHT if isSingleRow(join.getLeft) => true |
| case _ => false |
| } |
| } |
| |
| /** |
| * Recursively checks if a [[RelNode]] returns at most a single row. |
| * Input must be a global aggregation possibly followed by projections or filters. |
| */ |
| private def isSingleRow(node: RelNode): Boolean = { |
| node match { |
| case ss: RelSubset => isSingleRow(ss.getOriginal) |
| case lp: Project => isSingleRow(lp.getInput) |
| case lf: Filter => isSingleRow(lf.getInput) |
| case lc: Calc => isSingleRow(lc.getInput) |
| case la: Aggregate => la.getGroupSet.isEmpty |
| case _ => false |
| } |
| } |
| } |
| |
| object FlinkLogicalJoin { |
| val CONVERTER: ConverterRule = new FlinkLogicalJoinConverter() |
| } |