-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-47177][SQL] Cached SQL plan do not display final AQE plan in explain string #45282
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,8 +75,12 @@ object ExplainUtils extends AdaptiveSparkPlanHelper { | |
| * Given a input physical plan, performs the following tasks. | ||
| * 1. Generates the explain output for the input plan excluding the subquery plans. | ||
| * 2. Generates the explain output for each subquery referenced in the plan. | ||
| * | ||
| * Note that, ideally this is a no-op as different explain actions operate on different plan, | ||
| * instances but cached plan is an exception. The `InMemoryRelation#innerChildren` use a shared | ||
| * plan instance across multi-queries. Add lock for this method to avoid tag race condition. | ||
| */ | ||
| def processPlan[T <: QueryPlan[T]](plan: T, append: String => Unit): Unit = { | ||
| def processPlan[T <: QueryPlan[T]](plan: T, append: String => Unit): Unit = synchronized { | ||
|
Contributor
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. We should add more comments to explain it. Ideally this is a no-op as different explain actions operate on different plan instances, but cached plan is an exception.
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 comment |
||
| try { | ||
| // Initialize a reference-unique set of Operators to avoid accdiental overwrites and to allow | ||
| // intentional overwriting of IDs generated in previous AQE iteration | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,27 +18,42 @@ | |
| package org.apache.spark.sql.execution.columnar | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
| import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper | ||
| import org.apache.spark.sql.functions.expr | ||
| import org.apache.spark.sql.test.SharedSparkSessionBase | ||
| import org.apache.spark.storage.StorageLevel | ||
|
|
||
| class InMemoryRelationSuite extends SparkFunSuite with SharedSparkSessionBase { | ||
| test("SPARK-43157: Clone innerChildren cached plan") { | ||
| val d = spark.range(1) | ||
| val relation = InMemoryRelation(StorageLevel.MEMORY_ONLY, d.queryExecution, None) | ||
| val cloned = relation.clone().asInstanceOf[InMemoryRelation] | ||
|
|
||
| val relationCachedPlan = relation.innerChildren.head | ||
| val clonedCachedPlan = cloned.innerChildren.head | ||
|
|
||
| // verify the plans are not the same object but are logically equivalent | ||
| assert(!relationCachedPlan.eq(clonedCachedPlan)) | ||
| assert(relationCachedPlan === clonedCachedPlan) | ||
| } | ||
| class InMemoryRelationSuite extends SparkFunSuite | ||
| with SharedSparkSessionBase with AdaptiveSparkPlanHelper { | ||
|
|
||
| test("SPARK-46779: InMemoryRelations with the same cached plan are semantically equivalent") { | ||
| val d = spark.range(1) | ||
| val r1 = InMemoryRelation(StorageLevel.MEMORY_ONLY, d.queryExecution, None) | ||
| val r2 = r1.withOutput(r1.output.map(_.newInstance())) | ||
| assert(r1.sameResult(r2)) | ||
| } | ||
|
|
||
| test("SPARK-47177: Cached SQL plan do not display final AQE plan in explain string") { | ||
| def findIMRInnerChild(p: SparkPlan): SparkPlan = { | ||
| val tableCache = find(p) { | ||
| case _: InMemoryTableScanExec => true | ||
| case _ => false | ||
| } | ||
| assert(tableCache.isDefined) | ||
| tableCache.get.asInstanceOf[InMemoryTableScanExec].relation.innerChildren.head | ||
| } | ||
|
|
||
| val d1 = spark.range(1).withColumn("key", expr("id % 100")) | ||
| .groupBy("key").agg(Map("key" -> "count")) | ||
| val cached_d2 = d1.cache() | ||
| val df = cached_d2.withColumn("key2", expr("key % 10")) | ||
| .groupBy("key2").agg(Map("key2" -> "count")) | ||
|
|
||
| assert(findIMRInnerChild(df.queryExecution.executedPlan).treeString | ||
| .contains("AdaptiveSparkPlan isFinalPlan=false")) | ||
| df.collect() | ||
| assert(findIMRInnerChild(df.queryExecution.executedPlan).treeString | ||
|
Contributor
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. better to assert tree doesn't not contains any
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. it would not contain outer plan, the tree string is from InMemoryRelation.innerChildren
Contributor
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. I see, thanks! |
||
| .contains("AdaptiveSparkPlan isFinalPlan=true")) | ||
| } | ||
| } | ||
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.
why do we need a new local variable?
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.
it is possbile innerChildren is not a variable, it is defined as
def innerChildrenThere 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.
childrenisdeftoo, but I don't see we create local variables...Anyway, this is safer, I'm fine with it