You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2025/12/02 22:50:19 UTC

[PR] minor: Clean up shuffle transformation code in `CometExecRule` [datafusion-comet]

andygrove opened a new pull request, #2840:
URL: https://github.com/apache/datafusion-comet/pull/2840

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   N/A
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   The shuffle transformation code that I previously implemented is not very easy to follow. This PR refactors it to be more readable and concise.
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## How are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] minor: Clean up shuffle transformation code in `CometExecRule` [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #2840:
URL: https://github.com/apache/datafusion-comet/pull/2840#discussion_r2585966474


##########
spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala:
##########
@@ -394,6 +332,53 @@ case class CometExecRule(session: SparkSession) extends Rule[SparkPlan] {
     }
   }
 
+  private def operator2ProtoIfAllChildrenAreNative(op: SparkPlan): Option[Operator] = {
+    if (op.children.forall(_.isInstanceOf[CometNativeExec])) {
+      operator2Proto(op, op.children.map(_.asInstanceOf[CometNativeExec].nativeOp): _*)
+    } else {
+      None
+    }
+  }
+
+  /**
+   * Convert operator to proto and then apply a transformation to wrap the proto in a new plan.
+   */
+  private def newPlanWithProto(op: SparkPlan, fun: Operator => SparkPlan): SparkPlan = {
+    operator2ProtoIfAllChildrenAreNative(op).map(fun).getOrElse(op)
+  }
+
+  private def tryNativeShuffle(s: ShuffleExchangeExec): Option[SparkPlan] = {
+    Some(s)
+      .filter(_ => nativeShuffleSupported(s))
+      .flatMap(_ => operator2ProtoIfAllChildrenAreNative(s))
+      .map { nativeOp =>
+        // Switch to use Decimal128 regardless of precision, since Arrow native execution
+        // doesn't support Decimal32 and Decimal64 yet.

Review Comment:
   Unrelated: I'm not sure if this is true anymore. I saw a lot of code related to `Decimal32` and `Decimal64` in DataFusion these days, so we might want to revisit this assumption from the early days of Comet.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] minor: Clean up shuffle transformation code in `CometExecRule` [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #2840:
URL: https://github.com/apache/datafusion-comet/pull/2840#issuecomment-3607943436

   Thanks for the reviews @comphead @mbutrovich 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] minor: Clean up shuffle transformation code in `CometExecRule` [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #2840:
URL: https://github.com/apache/datafusion-comet/pull/2840#discussion_r2585958051


##########
spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala:
##########
@@ -183,21 +183,6 @@ case class CometExecRule(session: SparkSession) extends Rule[SparkPlan] {
    */
   // spotless:on
   private def transform(plan: SparkPlan): SparkPlan = {
-    def operator2ProtoIfAllChildrenAreNative(op: SparkPlan): Option[Operator] = {
-      if (op.children.forall(_.isInstanceOf[CometNativeExec])) {
-        operator2Proto(op, op.children.map(_.asInstanceOf[CometNativeExec].nativeOp): _*)
-      } else {
-        None
-      }
-    }
-
-    /**
-     * Convert operator to proto and then apply a transformation to wrap the proto in a new plan.
-     */
-    def newPlanWithProto(op: SparkPlan, fun: Operator => SparkPlan): SparkPlan = {
-      operator2ProtoIfAllChildrenAreNative(op).map(fun).getOrElse(op)
-    }

Review Comment:
   I moved these methods out of the `transform` method so that they can be accessed elsewhere



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] minor: Clean up shuffle transformation code in `CometExecRule` [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #2840:
URL: https://github.com/apache/datafusion-comet/pull/2840#discussion_r2583177929


##########
spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala:
##########
@@ -296,57 +281,10 @@ case class CometExecRule(session: SparkSession) extends Rule[SparkPlan] {
       case s @ ShuffleQueryStageExec(_, ReusedExchangeExec(_, _: CometShuffleExchangeExec), _) =>
         newPlanWithProto(s, CometSinkPlaceHolder(_, s, s))
 
-      // Native shuffle for Comet operators
       case s: ShuffleExchangeExec =>
-        val nativeShuffle: Option[SparkPlan] =
-          if (nativeShuffleSupported(s)) {
-            val newOp = operator2ProtoIfAllChildrenAreNative(s)
-            newOp match {
-              case Some(nativeOp) =>
-                // Switch to use Decimal128 regardless of precision, since Arrow native execution
-                // doesn't support Decimal32 and Decimal64 yet.
-                conf.setConfString(CometConf.COMET_USE_DECIMAL_128.key, "true")
-                val cometOp = CometShuffleExchangeExec(s, shuffleType = CometNativeShuffle)
-                Some(CometSinkPlaceHolder(nativeOp, s, cometOp))
-              case None =>
-                None
-            }
-          } else {
-            None
-          }
-
-        val nativeOrColumnarShuffle = if (nativeShuffle.isDefined) {
-          nativeShuffle
-        } else {
-          // Columnar shuffle for regular Spark operators (not Comet) and Comet operators
-          // (if configured).
-          // If the child of ShuffleExchangeExec is also a ShuffleExchangeExec, we should not
-          // convert it to CometColumnarShuffle,
-          if (columnarShuffleSupported(s)) {
-            val newOp = operator2Proto(s)
-            newOp match {
-              case Some(nativeOp) =>
-                s.child match {
-                  case n if n.isInstanceOf[CometNativeExec] || !n.supportsColumnar =>
-                    val cometOp =
-                      CometShuffleExchangeExec(s, shuffleType = CometColumnarShuffle)
-                    Some(CometSinkPlaceHolder(nativeOp, s, cometOp))
-                  case _ =>
-                    None
-                }
-              case None =>
-                None
-            }
-          } else {
-            None
-          }
-        }
-
-        if (nativeOrColumnarShuffle.isDefined) {
-          nativeOrColumnarShuffle.get
-        } else {
-          s
-        }
+        // try native shuffle first, then columnar shuffle, then fall back to Spark
+        // if neither are supported
+        tryNativeShuffle(s).orElse(tryColumnarShuffle(s)).getOrElse(s)

Review Comment:
   This is much easier to comprehend, IMO



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] minor: Clean up shuffle transformation code in `CometExecRule` [datafusion-comet]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #2840:
URL: https://github.com/apache/datafusion-comet/pull/2840#issuecomment-3604334840

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion-comet/pull/2840?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `0%` with `21 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 10.02%. Comparing base ([`f09f8af`](https://app.codecov.io/gh/apache/datafusion-comet/commit/f09f8af64c6599255e116a376f4f008f2fd63b43?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`960d9a0`](https://app.codecov.io/gh/apache/datafusion-comet/commit/960d9a0937910f4574e815ca10a1e64a3541ff66?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 731 commits behind head on main.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/datafusion-comet/pull/2840?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...n/scala/org/apache/comet/rules/CometExecRule.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/2840?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Frules%2FCometExecRule.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9ydWxlcy9Db21ldEV4ZWNSdWxlLnNjYWxh) | 0.00% | [21 Missing :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/2840?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@              Coverage Diff              @@
   ##               main    #2840       +/-   ##
   =============================================
   - Coverage     56.12%   10.02%   -46.11%     
   + Complexity      976      234      -742     
   =============================================
     Files           119      167       +48     
     Lines         11743    15259     +3516     
     Branches       2251     2498      +247     
   =============================================
   - Hits           6591     1529     -5062     
   - Misses         4012    13554     +9542     
   + Partials       1140      176      -964     
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/datafusion-comet/pull/2840?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] minor: Clean up shuffle transformation code in `CometExecRule` [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove merged PR #2840:
URL: https://github.com/apache/datafusion-comet/pull/2840


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org