-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-46170][SQL] Support inject adaptive query post planner strategy rules in SparkSessionExtensions #44074
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 |
|---|---|---|
|
|
@@ -29,15 +29,17 @@ import org.apache.spark.sql.catalyst.{FunctionIdentifier, InternalRow, TableIden | |
| import org.apache.spark.sql.catalyst.catalog.BucketSpec | ||
| import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.{Final, Partial} | ||
| import org.apache.spark.sql.catalyst.parser.{CatalystSqlParser, ParserInterface} | ||
| import org.apache.spark.sql.catalyst.plans.SQLHelper | ||
| import org.apache.spark.sql.catalyst.plans.logical.{ColumnStat, Limit, LocalRelation, LogicalPlan, Statistics, UnresolvedHint} | ||
| import org.apache.spark.sql.catalyst.plans.physical.Partitioning | ||
| import org.apache.spark.sql.catalyst.plans.physical.{Partitioning, SinglePartition} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.catalyst.trees.TreeNodeTag | ||
| import org.apache.spark.sql.connector.write.WriterCommitMessage | ||
| import org.apache.spark.sql.execution._ | ||
| import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanExec, AdaptiveSparkPlanHelper, AQEShuffleReadExec, QueryStageExec, ShuffleQueryStageExec} | ||
| import org.apache.spark.sql.execution.aggregate.HashAggregateExec | ||
| import org.apache.spark.sql.execution.datasources.{FileFormat, WriteFilesExec, WriteFilesSpec} | ||
| import org.apache.spark.sql.execution.exchange.{BroadcastExchangeExec, BroadcastExchangeLike, ShuffleExchangeExec, ShuffleExchangeLike, ShuffleOrigin} | ||
| import org.apache.spark.sql.execution.vectorized.OnHeapColumnVector | ||
|
|
@@ -516,6 +518,31 @@ class SparkSessionExtensionSuite extends SparkFunSuite with SQLHelper with Adapt | |
| } | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-46170: Support inject adaptive query post planner strategy rules in " + | ||
| "SparkSessionExtensions") { | ||
| val extensions = create { extensions => | ||
| extensions.injectQueryPostPlannerStrategyRule(_ => MyQueryPostPlannerStrategyRule) | ||
| } | ||
| withSession(extensions) { session => | ||
| assert(session.sessionState.adaptiveRulesHolder.queryPostPlannerStrategyRules | ||
| .contains(MyQueryPostPlannerStrategyRule)) | ||
| import session.implicits._ | ||
| withSQLConf(SQLConf.SHUFFLE_PARTITIONS.key -> "3", | ||
| SQLConf.COALESCE_PARTITIONS_ENABLED.key -> "false") { | ||
| val input = Seq(10, 20, 10).toDF("c1") | ||
| val df = input.groupBy("c1").count() | ||
| df.collect() | ||
|
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 tried it, and even if
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 is used to make sure we are checking the final plan |
||
| assert(df.rdd.partitions.length == 1) | ||
| assert(collectFirst(df.queryExecution.executedPlan) { | ||
| case s: ShuffleExchangeExec if s.outputPartitioning == SinglePartition => true | ||
|
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. How about Additionally, a personal opinion unrelated to this pr: If there is an |
||
| }.isDefined) | ||
| assert(collectFirst(df.queryExecution.executedPlan) { | ||
| case _: SortExec => true | ||
| }.isDefined) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| case class MyRule(spark: SparkSession) extends Rule[LogicalPlan] { | ||
|
|
@@ -1190,3 +1217,14 @@ object RequireAtLeaseTwoPartitions extends Rule[SparkPlan] { | |
| } | ||
| } | ||
| } | ||
|
|
||
| object MyQueryPostPlannerStrategyRule extends Rule[SparkPlan] { | ||
| override def apply(plan: SparkPlan): SparkPlan = { | ||
| plan.transformUp { | ||
| case h: HashAggregateExec if h.aggregateExpressions.map(_.mode).contains(Partial) => | ||
| ShuffleExchangeExec(SinglePartition, h) | ||
| case h: HashAggregateExec if h.aggregateExpressions.map(_.mode).contains(Final) => | ||
| SortExec(h.groupingExpressions.map(k => SortOrder.apply(k, Ascending)), false, h) | ||
| } | ||
| } | ||
| } | ||
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.
ditto. Shall we put this before
private[sql] def buildQueryStagePrepRules?