@@ -22,17 +22,18 @@ import org.apache.gluten.execution.VeloxResizeBatchesExec
2222import org .apache .spark .sql .catalyst .rules .Rule
2323import org .apache .spark .sql .execution .{ColumnarShuffleExchangeExec , SparkPlan }
2424import org .apache .spark .sql .execution .adaptive .{AQEShuffleReadExec , ShuffleQueryStageExec }
25- import org .apache .spark .sql .execution .exchange .ReusedExchangeExec
2625
2726/**
2827 * Try to append [[VeloxResizeBatchesExec ]] for shuffle input and output to make the batch sizes in
2928 * good shape.
3029 */
31- case class AppendBatchResizeForShuffleInputAndOutput () extends Rule [SparkPlan ] {
30+ case class AppendBatchResizeForShuffleInputAndOutput (isAdaptiveContext : Boolean )
31+ extends Rule [SparkPlan ] {
3232 override def apply (plan : SparkPlan ): SparkPlan = {
3333 if (VeloxConfig .get.enableColumnarCudf) {
3434 return plan
3535 }
36+
3637 val resizeBatchesShuffleInputEnabled = VeloxConfig .get.veloxResizeBatchesShuffleInput
3738 val resizeBatchesShuffleOutputEnabled = VeloxConfig .get.veloxResizeBatchesShuffleOutput
3839 if (! resizeBatchesShuffleInputEnabled && ! resizeBatchesShuffleOutputEnabled) {
@@ -41,65 +42,58 @@ case class AppendBatchResizeForShuffleInputAndOutput() extends Rule[SparkPlan] {
4142
4243 val range = VeloxConfig .get.veloxResizeBatchesShuffleInputOutputRange
4344 val preferredBatchBytes = VeloxConfig .get.veloxPreferredBatchBytes
45+
46+ val newPlan = if (resizeBatchesShuffleInputEnabled) {
47+ addResizeBatchesForShuffleInput(plan, range.min, range.max, preferredBatchBytes)
48+ } else {
49+ plan
50+ }
51+
52+ val resultPlan = if (isAdaptiveContext && resizeBatchesShuffleOutputEnabled) {
53+ addResizeBatchesForShuffleOutput(newPlan, range.min, range.max, preferredBatchBytes)
54+ } else {
55+ newPlan
56+ }
57+
58+ resultPlan
59+ }
60+
61+ private def addResizeBatchesForShuffleInput (
62+ plan : SparkPlan ,
63+ min : Int ,
64+ max : Int ,
65+ preferredBatchBytes : Long ): SparkPlan = {
4466 plan.transformUp {
4567 case shuffle : ColumnarShuffleExchangeExec
46- if resizeBatchesShuffleInputEnabled &&
47- shuffle.shuffleWriterType.requiresResizingShuffleInput =>
68+ if shuffle.shuffleWriterType.requiresResizingShuffleInput =>
4869 val appendBatches =
49- VeloxResizeBatchesExec (shuffle.child, range. min, range. max, preferredBatchBytes)
70+ VeloxResizeBatchesExec (shuffle.child, min, max, preferredBatchBytes)
5071 shuffle.withNewChildren(Seq (appendBatches))
51- case a @ AQEShuffleReadExec (
52- ShuffleQueryStageExec (_, shuffle : ColumnarShuffleExchangeExec , _),
53- _)
54- if resizeBatchesShuffleOutputEnabled &&
55- shuffle.shuffleWriterType.requiresResizingShuffleOutput =>
56- VeloxResizeBatchesExec (a, range.min, range.max, preferredBatchBytes)
57- case a @ AQEShuffleReadExec (
58- ShuffleQueryStageExec (
59- _,
60- ReusedExchangeExec (_, shuffle : ColumnarShuffleExchangeExec ),
61- _),
62- _)
63- if resizeBatchesShuffleOutputEnabled &&
64- shuffle.shuffleWriterType.requiresResizingShuffleOutput =>
65- VeloxResizeBatchesExec (a, range.min, range.max, preferredBatchBytes)
66- // Since it's transformed in a bottom to up order, so we may first encounter
67- // ShuffeQueryStageExec, which is transformed to VeloxResizeBatchesExec(ShuffeQueryStageExec),
68- // then we see AQEShuffleReadExec
69- case a @ AQEShuffleReadExec (
70- VeloxResizeBatchesExec (
71- s @ ShuffleQueryStageExec (_, shuffle : ColumnarShuffleExchangeExec , _),
72- _,
73- _,
74- _),
75- _)
76- if resizeBatchesShuffleOutputEnabled &&
77- shuffle.shuffleWriterType.requiresResizingShuffleOutput =>
78- VeloxResizeBatchesExec (a.copy(child = s), range.min, range.max, preferredBatchBytes)
79- case a @ AQEShuffleReadExec (
80- VeloxResizeBatchesExec (
81- s @ ShuffleQueryStageExec (
82- _,
83- ReusedExchangeExec (_, shuffle : ColumnarShuffleExchangeExec ),
84- _),
85- _,
86- _,
87- _),
88- _)
89- if resizeBatchesShuffleOutputEnabled &&
90- shuffle.shuffleWriterType.requiresResizingShuffleOutput =>
91- VeloxResizeBatchesExec (a.copy(child = s), range.min, range.max, preferredBatchBytes)
92- case s @ ShuffleQueryStageExec (_, shuffle : ColumnarShuffleExchangeExec , _)
93- if resizeBatchesShuffleOutputEnabled &&
94- shuffle.shuffleWriterType.requiresResizingShuffleOutput =>
95- VeloxResizeBatchesExec (s, range.min, range.max, preferredBatchBytes)
96- case s @ ShuffleQueryStageExec (
97- _,
98- ReusedExchangeExec (_, shuffle : ColumnarShuffleExchangeExec ),
99- _)
100- if resizeBatchesShuffleOutputEnabled &&
101- shuffle.shuffleWriterType.requiresResizingShuffleOutput =>
102- VeloxResizeBatchesExec (s, range.min, range.max, preferredBatchBytes)
72+ }
73+ }
74+
75+ private def addResizeBatchesForShuffleOutput (
76+ plan : SparkPlan ,
77+ min : Int ,
78+ max : Int ,
79+ preferredBatchBytes : Long ): SparkPlan = {
80+ plan match {
81+ case s : ShuffleQueryStageExec if requiresResizingShuffleOutput(s) =>
82+ VeloxResizeBatchesExec (s, min, max, preferredBatchBytes)
83+ case a @ AQEShuffleReadExec (s @ ShuffleQueryStageExec (_, _, _), _)
84+ if requiresResizingShuffleOutput(s) =>
85+ VeloxResizeBatchesExec (a, min, max, preferredBatchBytes)
86+ case other =>
87+ other.mapChildren(addResizeBatchesForShuffleOutput(_, min, max, preferredBatchBytes))
88+ }
89+ }
90+
91+ private def requiresResizingShuffleOutput (s : ShuffleQueryStageExec ): Boolean = {
92+ s.shuffle match {
93+ case c : ColumnarShuffleExchangeExec
94+ if c.shuffleWriterType.requiresResizingShuffleOutput =>
95+ true
96+ case _ => false
10397 }
10498 }
10599}
0 commit comments