-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-27747][SQL] add a logical plan link in the physical plan #24626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * 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.spark.sql.execution | ||
|
|
||
| import scala.reflect.ClassTag | ||
|
|
||
| import org.apache.spark.sql.TPCDSQuerySuite | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, Complete, Final} | ||
| import org.apache.spark.sql.catalyst.plans.QueryPlan | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Generate, Join, LocalRelation, LogicalPlan, Range, Sample, Union, Window} | ||
| import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec, SortAggregateExec} | ||
| import org.apache.spark.sql.execution.columnar.{InMemoryRelation, InMemoryTableScanExec} | ||
| import org.apache.spark.sql.execution.datasources.LogicalRelation | ||
| import org.apache.spark.sql.execution.datasources.v2.{BatchScanExec, DataSourceV2Relation} | ||
| import org.apache.spark.sql.execution.exchange.{BroadcastExchangeExec, ReusedExchangeExec, ShuffleExchangeExec} | ||
| import org.apache.spark.sql.execution.joins._ | ||
| import org.apache.spark.sql.execution.window.WindowExec | ||
|
|
||
| class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite { | ||
|
|
||
| override protected def checkGeneratedCode(plan: SparkPlan): Unit = { | ||
| super.checkGeneratedCode(plan) | ||
| checkLogicalPlanTag(plan) | ||
| } | ||
|
|
||
| private def isFinalAgg(aggExprs: Seq[AggregateExpression]): Boolean = { | ||
| // TODO: aggregate node without aggregate expressions can also be a final aggregate, but | ||
| // currently the aggregate node doesn't have a final/partial flag. | ||
| aggExprs.nonEmpty && aggExprs.forall(ae => ae.mode == Complete || ae.mode == Final) | ||
| } | ||
|
|
||
| // A scan plan tree is a plan tree that has a leaf node under zero or more Project/Filter nodes. | ||
| private def isScanPlanTree(plan: SparkPlan): Boolean = plan match { | ||
| case p: ProjectExec => isScanPlanTree(p.child) | ||
| case f: FilterExec => isScanPlanTree(f.child) | ||
| case _: LeafExecNode => true | ||
| case _ => false | ||
| } | ||
|
|
||
| private def checkLogicalPlanTag(plan: SparkPlan): Unit = { | ||
| plan match { | ||
| case _: HashJoin | _: BroadcastNestedLoopJoinExec | _: CartesianProductExec | ||
| | _: ShuffledHashJoinExec | _: SortMergeJoinExec => | ||
| assertLogicalPlanType[Join](plan) | ||
|
|
||
| // There is no corresponding logical plan for the physical partial aggregate. | ||
| case agg: HashAggregateExec if isFinalAgg(agg.aggregateExpressions) => | ||
| assertLogicalPlanType[Aggregate](plan) | ||
| case agg: ObjectHashAggregateExec if isFinalAgg(agg.aggregateExpressions) => | ||
| assertLogicalPlanType[Aggregate](plan) | ||
| case agg: SortAggregateExec if isFinalAgg(agg.aggregateExpressions) => | ||
| assertLogicalPlanType[Aggregate](plan) | ||
|
|
||
| case _: WindowExec => | ||
| assertLogicalPlanType[Window](plan) | ||
|
|
||
| case _: UnionExec => | ||
| assertLogicalPlanType[Union](plan) | ||
|
|
||
| case _: SampleExec => | ||
| assertLogicalPlanType[Sample](plan) | ||
|
|
||
| case _: GenerateExec => | ||
| assertLogicalPlanType[Generate](plan) | ||
|
|
||
| // The exchange related nodes are created after the planning, they don't have corresponding | ||
| // logical plan. | ||
| case _: ShuffleExchangeExec | _: BroadcastExchangeExec | _: ReusedExchangeExec => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add ReusedSubqueryExec?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added below |
||
| assert(!plan.tags.contains(SparkPlan.LOGICAL_PLAN_TAG_NAME)) | ||
|
|
||
| // The subquery exec nodes are just wrappers of the actual nodes, they don't have | ||
| // corresponding logical plan. | ||
| case _: SubqueryExec | _: ReusedSubqueryExec => | ||
| assert(!plan.tags.contains(SparkPlan.LOGICAL_PLAN_TAG_NAME)) | ||
|
|
||
| case _ if isScanPlanTree(plan) => | ||
| // The strategies for planning scan can remove or add FilterExec/ProjectExec nodes, | ||
| // so it's not simple to check. Instead, we only check that the origin LogicalPlan | ||
| // contains the corresponding leaf node of the SparkPlan. | ||
| // a strategy might remove the filter if it's totally pushed down, e.g.: | ||
| // logical = Project(Filter(Scan A)) | ||
| // physical = ProjectExec(ScanExec A) | ||
| // we only check that leaf modes match between logical and physical plan. | ||
| val logicalLeaves = getLogicalPlan(plan).collectLeaves() | ||
| val physicalLeaves = plan.collectLeaves() | ||
| assert(logicalLeaves.length == 1) | ||
| assert(physicalLeaves.length == 1) | ||
| physicalLeaves.head match { | ||
| case _: RangeExec => logicalLeaves.head.isInstanceOf[Range] | ||
| case _: DataSourceScanExec => logicalLeaves.head.isInstanceOf[LogicalRelation] | ||
| case _: InMemoryTableScanExec => logicalLeaves.head.isInstanceOf[InMemoryRelation] | ||
| case _: LocalTableScanExec => logicalLeaves.head.isInstanceOf[LocalRelation] | ||
| case _: ExternalRDDScanExec[_] => logicalLeaves.head.isInstanceOf[ExternalRDD[_]] | ||
| case _: BatchScanExec => logicalLeaves.head.isInstanceOf[DataSourceV2Relation] | ||
| case _ => | ||
| } | ||
| // Do not need to check the children recursively. | ||
| return | ||
|
|
||
| case _ => | ||
| } | ||
|
|
||
| plan.children.foreach(checkLogicalPlanTag) | ||
| plan.subqueries.foreach(checkLogicalPlanTag) | ||
| } | ||
|
|
||
| private def getLogicalPlan(node: SparkPlan): LogicalPlan = { | ||
| assert(node.tags.contains(SparkPlan.LOGICAL_PLAN_TAG_NAME), | ||
| node.getClass.getSimpleName + " does not have a logical plan link") | ||
| node.tags(SparkPlan.LOGICAL_PLAN_TAG_NAME).asInstanceOf[LogicalPlan] | ||
| } | ||
|
|
||
| private def assertLogicalPlanType[T <: LogicalPlan : ClassTag](node: SparkPlan): Unit = { | ||
| val logicalPlan = getLogicalPlan(node) | ||
| val expectedCls = implicitly[ClassTag[T]].runtimeClass | ||
| assert(expectedCls == logicalPlan.getClass) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here after applying the
rule, we need to carry over the tags, if needed, too.Since carrying the tags in both if and else branches, so maybe: