|
| 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.dynamicpruning |
| 19 | + |
| 20 | +import org.apache.spark.sql.catalyst.expressions.{And, Attribute, DynamicPruningSubquery, Expression, PredicateHelper, V2ExpressionUtils} |
| 21 | +import org.apache.spark.sql.catalyst.expressions.Literal.TrueLiteral |
| 22 | +import org.apache.spark.sql.catalyst.planning.GroupBasedRowLevelOperation |
| 23 | +import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project} |
| 24 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 25 | +import org.apache.spark.sql.connector.read.SupportsRuntimeV2Filtering |
| 26 | +import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Implicits, DataSourceV2Relation, DataSourceV2ScanRelation} |
| 27 | + |
| 28 | +/** |
| 29 | + * A rule that assigns a subquery to filter groups in row-level operations at runtime. |
| 30 | + * |
| 31 | + * Data skipping during job planning for row-level operations is limited to expressions that can be |
| 32 | + * converted to data source filters. Since not all expressions can be pushed down that way and |
| 33 | + * rewriting groups is expensive, Spark allows data sources to filter group at runtime. |
| 34 | + * If the primary scan in a group-based row-level operation supports runtime filtering, this rule |
| 35 | + * will inject a subquery to find all rows that match the condition so that data sources know |
| 36 | + * exactly which groups must be rewritten. |
| 37 | + * |
| 38 | + * Note this rule only applies to group-based row-level operations. |
| 39 | + */ |
| 40 | +case class RowLevelOperationRuntimeGroupFiltering(optimizeSubqueries: Rule[LogicalPlan]) |
| 41 | + extends Rule[LogicalPlan] with PredicateHelper { |
| 42 | + |
| 43 | + import DataSourceV2Implicits._ |
| 44 | + |
| 45 | + override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown { |
| 46 | + // apply special dynamic filtering only for group-based row-level operations |
| 47 | + case GroupBasedRowLevelOperation(replaceData, cond, |
| 48 | + DataSourceV2ScanRelation(_, scan: SupportsRuntimeV2Filtering, _, _, _)) |
| 49 | + if conf.runtimeRowLevelOperationGroupFilterEnabled && cond != TrueLiteral => |
| 50 | + |
| 51 | + // use reference equality on scan to find required scan relations |
| 52 | + val newQuery = replaceData.query transformUp { |
| 53 | + case r: DataSourceV2ScanRelation if r.scan eq scan => |
| 54 | + // use the original table instance that was loaded for this row-level operation |
| 55 | + // in order to leverage a regular batch scan in the group filter query |
| 56 | + val originalTable = r.relation.table.asRowLevelOperationTable.table |
| 57 | + val relation = r.relation.copy(table = originalTable) |
| 58 | + val matchingRowsPlan = buildMatchingRowsPlan(relation, cond) |
| 59 | + |
| 60 | + val filterAttrs = scan.filterAttributes |
| 61 | + val buildKeys = V2ExpressionUtils.resolveRefs[Attribute](filterAttrs, matchingRowsPlan) |
| 62 | + val pruningKeys = V2ExpressionUtils.resolveRefs[Attribute](filterAttrs, r) |
| 63 | + val dynamicPruningCond = buildDynamicPruningCond(matchingRowsPlan, buildKeys, pruningKeys) |
| 64 | + |
| 65 | + Filter(dynamicPruningCond, r) |
| 66 | + } |
| 67 | + |
| 68 | + // optimize subqueries to rewrite them as joins and trigger job planning |
| 69 | + replaceData.copy(query = optimizeSubqueries(newQuery)) |
| 70 | + } |
| 71 | + |
| 72 | + private def buildMatchingRowsPlan( |
| 73 | + relation: DataSourceV2Relation, |
| 74 | + cond: Expression): LogicalPlan = { |
| 75 | + |
| 76 | + val matchingRowsPlan = Filter(cond, relation) |
| 77 | + |
| 78 | + // clone the relation and assign new expr IDs to avoid conflicts |
| 79 | + matchingRowsPlan transformUpWithNewOutput { |
| 80 | + case r: DataSourceV2Relation if r eq relation => |
| 81 | + val oldOutput = r.output |
| 82 | + val newOutput = oldOutput.map(_.newInstance()) |
| 83 | + r.copy(output = newOutput) -> oldOutput.zip(newOutput) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private def buildDynamicPruningCond( |
| 88 | + matchingRowsPlan: LogicalPlan, |
| 89 | + buildKeys: Seq[Attribute], |
| 90 | + pruningKeys: Seq[Attribute]): Expression = { |
| 91 | + |
| 92 | + val buildQuery = Project(buildKeys, matchingRowsPlan) |
| 93 | + val dynamicPruningSubqueries = pruningKeys.zipWithIndex.map { case (key, index) => |
| 94 | + DynamicPruningSubquery(key, buildQuery, buildKeys, index, onlyInBroadcast = false) |
| 95 | + } |
| 96 | + dynamicPruningSubqueries.reduce(And) |
| 97 | + } |
| 98 | +} |
0 commit comments