Skip to content

Commit f0cad7a

Browse files
EnricoMicloud-fan
authored andcommitted
[SPARK-40588] FileFormatWriter materializes AQE plan before accessing outputOrdering
### What changes were proposed in this pull request? The `FileFormatWriter` materializes an `AdaptiveQueryPlan` before accessing the plan's `outputOrdering`. This is required for Spark 3.0 to 3.3. Spark 3.4 does not need this because `FileFormatWriter` gets the final plan. ### Why are the changes needed? `FileFormatWriter` enforces an ordering if the written plan does not provide that ordering. An `AdaptiveQueryPlan` does not know its final ordering (Spark 3.0 to 3.3), in which case `FileFormatWriter` enforces the ordering (e.g. by column `"a"`) even if the plan provides a compatible ordering (e.g. by columns `"a", "b"`). In case of spilling, that order (e.g. by columns `"a", "b"`) gets broken (see SPARK-40588). ### Does this PR introduce _any_ user-facing change? This fixes SPARK-40588, which was introduced in 3.0. This restores behaviour from Spark 2.4. ### How was this patch tested? The final plan that is written to files cannot be extracted from `FileFormatWriter`. The bug explained in [SPARK-40588](https://issues.apache.org/jira/browse/SPARK-40588) can only be asserted on the result files when spilling occurs. This is very hard to control in an unit test scenario. Therefore, this was tested manually. The [example to reproduce this issue](https://issues.apache.org/jira/browse/SPARK-40588?focusedCommentId=17621032&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-17621032) given in SPARK-40588 now produces sorted files. The actual plan written into the files changed from ``` Sort [input[0, bigint, false] ASC NULLS FIRST], false, 0 +- AdaptiveSparkPlan isFinalPlan=false +- Sort [day#2L ASC NULLS FIRST, id#4L ASC NULLS FIRST], false, 0 +- Exchange hashpartitioning(day#2L, 2), REPARTITION_BY_NUM, [id=#30] +- BroadcastNestedLoopJoin BuildLeft, Inner :- BroadcastExchange IdentityBroadcastMode, [id=#28] : +- Project [id#0L AS day#2L] : +- Range (0, 2, step=1, splits=2) +- Range (0, 10000000, step=1, splits=2) ``` where `FileFormatWriter` enforces order with `Sort [input[0, bigint, false] ASC NULLS FIRST], false, 0`, to ``` *(3) Sort [day#2L ASC NULLS FIRST, id#4L ASC NULLS FIRST], false, 0 +- AQEShuffleRead coalesced +- ShuffleQueryStage 1 +- Exchange hashpartitioning(day#2L, 200), REPARTITION_BY_COL, [id=#68] +- *(2) BroadcastNestedLoopJoin BuildLeft, Inner :- BroadcastQueryStage 0 : +- BroadcastExchange IdentityBroadcastMode, [id=#42] : +- *(1) Project [id#0L AS day#2L] : +- *(1) Range (0, 2, step=1, splits=2) +- *(2) Range (0, 1000000, step=1, splits=2) ``` where the sort given by the user is the outermost sort now. Closes #38358 from EnricoMi/branch-3.3-materialize-aqe-plan. Authored-by: Enrico Minack <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent ef74381 commit f0cad7a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ case class AdaptiveSparkPlanExec(
222222
.map(_.toLong).filter(SQLExecution.getQueryExecution(_) eq context.qe)
223223
}
224224

225+
def finalPhysicalPlan: SparkPlan = withFinalPlanUpdate(identity)
226+
225227
private def getFinalPhysicalPlan(): SparkPlan = lock.synchronized {
226228
if (isFinalPlan) return currentPhysicalPlan
227229

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileFormatWriter.scala

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.apache.spark.sql.catalyst.plans.physical.HashPartitioning
4040
import org.apache.spark.sql.catalyst.util.{CaseInsensitiveMap, DateTimeUtils}
4141
import org.apache.spark.sql.errors.QueryExecutionErrors
4242
import org.apache.spark.sql.execution.{ProjectExec, SortExec, SparkPlan, SQLExecution, UnsafeExternalRowSorter}
43+
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanExec
4344
import org.apache.spark.sql.internal.SQLConf
4445
import org.apache.spark.sql.types.StringType
4546
import org.apache.spark.unsafe.types.UTF8String
@@ -187,8 +188,17 @@ object FileFormatWriter extends Logging {
187188
// We should first sort by partition columns, then bucket id, and finally sorting columns.
188189
val requiredOrdering =
189190
partitionColumns ++ writerBucketSpec.map(_.bucketIdExpression) ++ sortColumns
191+
192+
// SPARK-40588: plan may contain an AdaptiveSparkPlanExec, which does not know
193+
// its final plan's ordering, so we have to materialize that plan first
194+
def materializeAdaptiveSparkPlan(plan: SparkPlan): SparkPlan = plan match {
195+
case a: AdaptiveSparkPlanExec => a.finalPhysicalPlan
196+
case p: SparkPlan => p.withNewChildren(p.children.map(materializeAdaptiveSparkPlan))
197+
}
198+
val materializedPlan = materializeAdaptiveSparkPlan(empty2NullPlan)
199+
190200
// the sort order doesn't matter
191-
val actualOrdering = empty2NullPlan.outputOrdering.map(_.child)
201+
val actualOrdering = materializedPlan.outputOrdering.map(_.child)
192202
val orderingMatched = if (requiredOrdering.length > actualOrdering.length) {
193203
false
194204
} else {
@@ -210,7 +220,7 @@ object FileFormatWriter extends Logging {
210220

211221
try {
212222
val (rdd, concurrentOutputWriterSpec) = if (orderingMatched) {
213-
(empty2NullPlan.execute(), None)
223+
(materializedPlan.execute(), None)
214224
} else {
215225
// SPARK-21165: the `requiredOrdering` is based on the attributes from analyzed plan, and
216226
// the physical plan may have different attribute ids due to optimizer removing some
@@ -220,12 +230,12 @@ object FileFormatWriter extends Logging {
220230
val sortPlan = SortExec(
221231
orderingExpr,
222232
global = false,
223-
child = empty2NullPlan)
233+
child = materializedPlan)
224234

225235
val maxWriters = sparkSession.sessionState.conf.maxConcurrentOutputFileWriters
226236
val concurrentWritersEnabled = maxWriters > 0 && sortColumns.isEmpty
227237
if (concurrentWritersEnabled) {
228-
(empty2NullPlan.execute(),
238+
(materializedPlan.execute(),
229239
Some(ConcurrentOutputWriterSpec(maxWriters, () => sortPlan.createSorter())))
230240
} else {
231241
(sortPlan.execute(), None)

0 commit comments

Comments
 (0)