-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-41183][SQL] Add an extension API to do plan normalization for caching #38692
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 |
|---|---|---|
|
|
@@ -105,12 +105,29 @@ class QueryExecution( | |
| case other => other | ||
| } | ||
|
|
||
| // The plan that has been normalized by custom rules, so that it's more likely to hit cache. | ||
| lazy val normalized: LogicalPlan = { | ||
|
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. Perhaps adding some comments for this. |
||
| val normalizationRules = sparkSession.sessionState.planNormalizationRules | ||
| if (normalizationRules.isEmpty) { | ||
| commandExecuted | ||
| } else { | ||
| val planChangeLogger = new PlanChangeLogger[LogicalPlan]() | ||
| val normalized = normalizationRules.foldLeft(commandExecuted) { (p, rule) => | ||
| val result = rule.apply(p) | ||
| planChangeLogger.logRule(rule.ruleName, p, result) | ||
| result | ||
| } | ||
| planChangeLogger.logBatch("Plan Normalization", commandExecuted, normalized) | ||
| normalized | ||
| } | ||
| } | ||
|
|
||
| lazy val withCachedData: LogicalPlan = sparkSession.withActive { | ||
| assertAnalyzed() | ||
| assertSupported() | ||
| // clone the plan to avoid sharing the plan instance between different stages like analyzing, | ||
| // optimizing and planning. | ||
| sparkSession.sharedState.cacheManager.useCachedData(commandExecuted.clone()) | ||
| sparkSession.sharedState.cacheManager.useCachedData(normalized.clone()) | ||
| } | ||
|
|
||
| def assertCommandExecuted(): Unit = commandExecuted | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,6 +192,23 @@ class SparkSessionExtensionSuite extends SparkFunSuite with SQLHelper { | |
| testInjectColumnar(false) | ||
| } | ||
|
|
||
| test("inject plan normalization rules") { | ||
| val extensions = create { extensions => | ||
| extensions.injectPlanNormalizationRules { session => | ||
| org.apache.spark.sql.catalyst.optimizer.PushDownPredicates | ||
| } | ||
| } | ||
| withSession(extensions) { session => | ||
| import session.implicits._ | ||
| val df = Seq((1, "a"), (2, "b")).toDF("i", "s") | ||
| df.select("i").filter($"i" > 1).cache() | ||
| assert(df.filter($"i" > 1).select("i").queryExecution.executedPlan.find { | ||
| case _: org.apache.spark.sql.execution.columnar.InMemoryTableScanExec => true | ||
| case _ => false | ||
|
Comment on lines
+204
to
+207
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. So without the added rule, caching is unable to apply here, right?
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. yup
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. Should we add a negative test that verifies this? Might be overkill... |
||
| }.isDefined) | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-39991: AQE should retain column statistics from completed query stages") { | ||
| val extensions = create { extensions => | ||
| extensions.injectColumnar(_ => | ||
|
|
||
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.
nit: isn't
.apply(...)redundant with just(...)?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.
I'm following the existing code style in this file. I assume the reason is people who are not familiar with Scala may be confused when reading the code
.map(_(session))