sunchao commented on code in PR #7117:
URL: https://github.com/apache/datafusion-comet/pull/7117#discussion_r4319118180


##########
spark/src/main/scala/org/apache/comet/rules/MakeDateEvaluation.scala:
##########
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.comet.rules
+
+import org.apache.spark.sql.catalyst.expressions.{Expression, MakeDate, 
SortOrder}
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.comet._
+import org.apache.spark.sql.comet.execution.shuffle.CometShuffleExchangeExec
+import org.apache.spark.sql.execution._
+import org.apache.spark.sql.execution.adaptive.{BroadcastQueryStageExec, 
QueryStageExec}
+import org.apache.spark.sql.execution.aggregate.{BaseAggregateExec, 
HashAggregateExec, ObjectHashAggregateExec}
+import org.apache.spark.sql.execution.exchange.{Exchange, ReusedExchangeExec}
+import org.apache.spark.sql.execution.joins.BaseJoinExec
+
+import org.apache.comet.shims.ShimCometWindowGroupLimit
+
+/** Keeps make_date on Spark's row path where native batching can change 
whether it throws. */
+private[rules] object MakeDateEvaluation {
+  private val sparkEvaluation = 
TreeNodeTag[String]("comet.makeDateSparkEvaluation")
+
+  def reason(plan: SparkPlan): Option[String] = 
plan.getTagValue(sparkEvaluation)
+
+  private def containsMakeDate(expressions: Seq[Expression]): Boolean =
+    expressions.exists(_.exists(_.isInstanceOf[MakeDate]))
+
+  private def copyLogicalLinks(from: SparkPlan, to: SparkPlan): Unit = {
+    if (from ne to) {
+      // setLogicalLink promotes inherited links to direct links and rewrites 
descendants.
+      // Keep each tag's role and retain the fallback plan's links when the 
source has none.
+      Seq(SparkPlan.LOGICAL_PLAN_TAG, 
SparkPlan.LOGICAL_PLAN_INHERITED_TAG).foreach { tag =>
+        from.getTagValue(tag).foreach(to.setTagValue(tag, _))
+      }
+    }
+  }
+
+  private def nativeBroadcastInput(plan: SparkPlan): Option[SparkPlan] = plan 
match {
+    case _: CometBroadcastExchangeExec => Some(plan)
+    case stage: BroadcastQueryStageExec => 
nativeBroadcastInput(stage.plan).map(_ => stage)
+    case reuse: ReusedExchangeExec => nativeBroadcastInput(reuse.child).map(_ 
=> reuse)
+    case sink: CometSinkPlaceHolder => nativeBroadcastInput(sink.child)
+    case wrapper: CometScanWrapper => 
nativeBroadcastInput(wrapper.originalPlan)
+    case _ => None
+  }
+
+  private def restoreBroadcast(plan: SparkPlan, reason: String): SparkPlan =
+    nativeBroadcastInput(plan)
+      .map { input =>
+        // Spark joins need a HashedRelation (or rows for a nested-loop join), 
not Arrow buffers.
+        // The existing transition decodes the cached broadcast and applies 
its Spark broadcast
+        // mode, including through AQE/reuse wrappers, without re-executing 
the build input.
+        val restored = CometColumnarToRowExec(input)

Review Comment:
   **[P2] Copy rows when restoring an identity broadcast** — 
[MakeDateEvaluation.scala:68](codex://review?pr=https%3A%2F%2Fgithub.com%2Fapache%2Fdatafusion-comet%2Fpull%2F7117&path=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Frules%2FMakeDateEvaluation.scala&line=68&side=right)
   
   When restoring an already-native nested-loop join, this new path uses a 
converter that reuses its row object. Spark’s `IdentityBroadcastMode` retains 
those references, corrupting the build rows:
   
   ```text
   Input:         [2024, 2025]
   Restored:      [2025, 2025]
   Copy control:  [2024, 2025]
   ```
   
   I reproduced this with the current conversion code and Spark 4.1.3. Copy 
retained rows and extend the existing broadcast-restoration test to cover 
nested-loop joins. 
[Reproducer](/tmp/comet-7117-review/identity-broadcast/IdentityBroadcastProbe.scala)
   
   **Scope:** the conversion failure is confirmed; the restoration path is 
source-traced. I have not reproduced a complete SQL/AQE query that naturally 
reaches this previously converted state. Fresh Spark broadcasts avoid it.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to