|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.execution.adaptive |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.planning.ExtractSingleColumnNullAwareAntiJoin |
| 21 | +import org.apache.spark.sql.catalyst.plans.{Inner, LeftAnti, LeftSemi} |
| 22 | +import org.apache.spark.sql.catalyst.plans.logical.{Join, LocalRelation, LogicalPlan} |
| 23 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 24 | +import org.apache.spark.sql.execution.joins.HashedRelationWithAllNullKeys |
| 25 | + |
| 26 | +/** |
| 27 | + * This optimization rule detects and eliminates unnecessary Join: |
| 28 | + * 1. Join is single column NULL-aware anti join (NAAJ), and broadcasted [[HashedRelation]] |
| 29 | + * is [[HashedRelationWithAllNullKeys]]. Eliminate join to an empty [[LocalRelation]]. |
| 30 | + * |
| 31 | + * 2. Join is inner join, and either side of join is empty. Eliminate join to an empty |
| 32 | + * [[LocalRelation]]. |
| 33 | + * |
| 34 | + * 3. Join is left semi join |
| 35 | + * 3.1. Join right side is empty. Eliminate join to an empty [[LocalRelation]]. |
| 36 | + * 3.2. Join right side is non-empty and condition is empty. Eliminate join to its left side. |
| 37 | + * |
| 38 | + * 4. Join is left anti join |
| 39 | + * 4.1. Join right side is empty. Eliminate join to its left side. |
| 40 | + * 4.2. Join right side is non-empty and condition is empty. Eliminate join to an empty |
| 41 | + * [[LocalRelation]]. |
| 42 | + * |
| 43 | + * This applies to all joins (sort merge join, shuffled hash join, broadcast hash join, and |
| 44 | + * broadcast nested loop join), because sort merge join and shuffled hash join will be changed |
| 45 | + * to broadcast hash join with AQE at the first place. |
| 46 | + */ |
| 47 | +object EliminateUnnecessaryJoin extends Rule[LogicalPlan] { |
| 48 | + |
| 49 | + private def isRelationWithAllNullKeys(plan: LogicalPlan) = plan match { |
| 50 | + case LogicalQueryStage(_, stage: BroadcastQueryStageExec) |
| 51 | + if stage.resultOption.get().isDefined => |
| 52 | + stage.broadcast.relationFuture.get().value == HashedRelationWithAllNullKeys |
| 53 | + case _ => false |
| 54 | + } |
| 55 | + |
| 56 | + private def checkRowCount(plan: LogicalPlan, hasRow: Boolean): Boolean = plan match { |
| 57 | + case LogicalQueryStage(_, stage: QueryStageExec) if stage.resultOption.get().isDefined => |
| 58 | + stage.getRuntimeStatistics.rowCount match { |
| 59 | + case Some(count) => hasRow == (count > 0) |
| 60 | + case _ => false |
| 61 | + } |
| 62 | + case _ => false |
| 63 | + } |
| 64 | + |
| 65 | + def apply(plan: LogicalPlan): LogicalPlan = plan.transformDown { |
| 66 | + case j @ ExtractSingleColumnNullAwareAntiJoin(_, _) if isRelationWithAllNullKeys(j.right) => |
| 67 | + LocalRelation(j.output, data = Seq.empty, isStreaming = j.isStreaming) |
| 68 | + |
| 69 | + case j @ Join(_, _, Inner, _, _) if checkRowCount(j.left, hasRow = false) || |
| 70 | + checkRowCount(j.right, hasRow = false) => |
| 71 | + LocalRelation(j.output, data = Seq.empty, isStreaming = j.isStreaming) |
| 72 | + |
| 73 | + case j @ Join(_, _, LeftSemi, condition, _) => |
| 74 | + if (checkRowCount(j.right, hasRow = false)) { |
| 75 | + LocalRelation(j.output, data = Seq.empty, isStreaming = j.isStreaming) |
| 76 | + } else if (condition.isEmpty && checkRowCount(j.right, hasRow = true)) { |
| 77 | + j.left |
| 78 | + } else { |
| 79 | + j |
| 80 | + } |
| 81 | + |
| 82 | + case j @ Join(_, _, LeftAnti, condition, _) => |
| 83 | + if (checkRowCount(j.right, hasRow = false)) { |
| 84 | + j.left |
| 85 | + } else if (condition.isEmpty && checkRowCount(j.right, hasRow = true)) { |
| 86 | + LocalRelation(j.output, data = Seq.empty, isStreaming = j.isStreaming) |
| 87 | + } else { |
| 88 | + j |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments