|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.comet.rules |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.optimizer.{BuildLeft, BuildRight, BuildSide, JoinSelectionHelper} |
| 23 | +import org.apache.spark.sql.catalyst.plans.{JoinType, LeftSemi} |
| 24 | +import org.apache.spark.sql.execution.{SortExec, SparkPlan} |
| 25 | +import org.apache.spark.sql.execution.joins.{ShuffledHashJoinExec, SortMergeJoinExec} |
| 26 | + |
| 27 | +/** |
| 28 | + * Adapted from equivalent rule in Apache Gluten. |
| 29 | + * |
| 30 | + * This rule replaces [[SortMergeJoinExec]] with [[ShuffledHashJoinExec]]. |
| 31 | + */ |
| 32 | +object RewriteJoin extends JoinSelectionHelper { |
| 33 | + |
| 34 | + private def getBuildSide(joinType: JoinType): Option[BuildSide] = { |
| 35 | + if (canBuildShuffledHashJoinRight(joinType)) { |
| 36 | + Some(BuildRight) |
| 37 | + } else if (canBuildShuffledHashJoinLeft(joinType)) { |
| 38 | + Some(BuildLeft) |
| 39 | + } else { |
| 40 | + None |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private def removeSort(plan: SparkPlan) = plan match { |
| 45 | + case _: SortExec => plan.children.head |
| 46 | + case _ => plan |
| 47 | + } |
| 48 | + |
| 49 | + def rewrite(plan: SparkPlan): SparkPlan = plan match { |
| 50 | + case smj: SortMergeJoinExec => |
| 51 | + getBuildSide(smj.joinType) match { |
| 52 | + case Some(BuildRight) if smj.joinType == LeftSemi => |
| 53 | + // TODO this was added as a workaround for TPC-DS q14 hanging and needs |
| 54 | + // further investigation |
| 55 | + plan |
| 56 | + case Some(buildSide) => |
| 57 | + ShuffledHashJoinExec( |
| 58 | + smj.leftKeys, |
| 59 | + smj.rightKeys, |
| 60 | + smj.joinType, |
| 61 | + buildSide, |
| 62 | + smj.condition, |
| 63 | + removeSort(smj.left), |
| 64 | + removeSort(smj.right), |
| 65 | + smj.isSkewJoin) |
| 66 | + case _ => plan |
| 67 | + } |
| 68 | + case _ => plan |
| 69 | + } |
| 70 | +} |
0 commit comments