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


##########
spark/src/main/scala/org/apache/comet/rules/MakeDateEvaluation.scala:
##########
@@ -0,0 +1,255 @@
+/*
+ * 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.{Add, Alias, And, Attribute, 
AttributeSet, BinaryComparison, Cast, ConditionalExpression, CreateArray, 
CreateNamedStruct, DateAdd, DateDiff, DateFormatClass, DateSub, EqualNullSafe, 
Expression, Greatest, HashExpression, IsNotNull, IsNull, Least, MakeDate, 
Multiply, Not, Or, SortOrder, Subtract, UnixDate}
+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.CometConf
+import org.apache.comet.serde.CometMakeDate
+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")
+
+  private def enabled(plan: SparkPlan): Boolean =
+    CometConf.COMET_EXEC_MAKE_DATE_OPERATOR_FALLBACK_ENABLED.get(plan.conf)
+
+  def reason(plan: SparkPlan): Option[String] =
+    if (enabled(plan)) plan.getTagValue(sparkEvaluation) else None
+
+  private def containsMakeDate(expressions: Seq[Expression]): Boolean =
+    expressions.exists(_.exists(_.isInstanceOf[MakeDate]))
+
+  private case class ProjectionDemand(boundary: String, deferred: AttributeSet)
+
+  // Attributes may stand for deferred expressions in a lower projection, not 
materialized values.
+  private def harmlessValue(expression: Expression): Boolean =
+    expression.references.isEmpty && 
CometMakeDate.canEvaluateEagerly(expression)
+
+  /** References that a consumer requires without exposing a potentially 
skipped sibling. */
+  private def eagerReferences(expression: Expression): AttributeSet = 
expression match {
+    case attribute: Attribute => AttributeSet(Seq(attribute))
+    case _ =>
+      val children = expression match {
+        case conditional: ConditionalExpression =>
+          val required = conditional.alwaysEvaluatedInputs
+          if 
(conditional.children.filterNot(required.contains).forall(harmlessValue)) 
required
+          else Nil
+        case _: Alias | _: Cast | _: IsNull | _: IsNotNull | _: Not | _: 
UnixDate =>
+          expression.children
+        case _: EqualNullSafe | _: CreateArray | _: CreateNamedStruct | _: 
Greatest | _: Least |
+            _: HashExpression[_] =>
+          expression.children
+        case _: And | _: Or =>
+          if (harmlessValue(expression.children(1))) 
expression.children.take(1) else Nil
+        case _: Add | _: Subtract | _: Multiply | _: BinaryComparison | _: 
DateAdd | _: DateSub |
+            _: DateDiff | _: DateFormatClass =>
+          val left = expression.children.head
+          val right = expression.children(1)
+          if (!left.nullable) expression.children
+          // A right-hand attribute can inline a throwing expression from an 
earlier projection.
+          // Only self-contained harmless values are safe without tracing that 
attribute's source.
+          else if (harmlessValue(right)) Seq(left)
+          else Nil
+        // Unknown expressions may skip inputs or catch errors raised by their 
children.
+        case _ => Nil
+      }
+      AttributeSet(children.flatMap(eagerReferences))
+  }
+
+  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)
+        restored.setTagValue(sparkEvaluation, reason)
+        copyLogicalLinks(plan, restored)
+        restored
+      }
+      .getOrElse(plan)
+
+  /**
+   * Run before bottom-up conversion and join rewriting. Reasons propagate 
back to the consuming
+   * operator, keeping the row pipeline together instead of inserting an eager 
Arrow batch between
+   * a throwing projection and its lazy consumer. Exchanges materialize their 
input independently;
+   * local sorts and aggregates do so only after their consumer requests input.
+   */
+  def preserve(plan: SparkPlan): SparkPlan = {
+    if (!enabled(plan)) return plan
+
+    def visit(
+        node: SparkPlan,
+        rowDemand: Option[String],
+        projectionDemand: Option[ProjectionDemand]): (SparkPlan, 
Option[String]) = {
+      node match {
+        // Materialized stages are independent producers. Their plans have 
already been guarded.
+        case _: QueryStageExec | _: ReusedExchangeExec => return (node, None)
+        // These wrappers do not have the child shape of their original Spark 
operator.
+        case wrapper: CometScanWrapper =>
+          val (child, childReason) = visit(wrapper.originalPlan, rowDemand, 
projectionDemand)
+          return if (childReason.isDefined) (child, childReason)
+          else (wrapper.copy(originalPlan = child), None)
+        case sink: CometSinkPlaceHolder =>
+          val (child, childReason) = visit(sink.child, rowDemand, 
projectionDemand)
+          return if (childReason.isDefined) (child, childReason)
+          else (sink.withNewChildren(Seq(child)), None)
+        case _ =>
+      }
+
+      val original = node match {
+        case scan: CometScanExec =>
+          scan.wrapped.copy(
+            partitionFilters = scan.partitionFilters,
+            dataFilters = scan.dataFilters)
+        case scan: CometNativeScanExec => scan.sparkFallback(Nil)
+        case native: CometExec => native.originalPlan
+        case shuffle: CometShuffleExchangeExec => shuffle.originalPlan
+        case broadcast: CometBroadcastExchangeExec => broadcast.originalPlan
+        case _ => node
+      }
+      val exchange = original.isInstanceOf[Exchange]
+      val join = original.isInstanceOf[BaseJoinExec]
+      val localMaterialization = original match {
+        case _: SortExec | _: HashAggregateExec | _: ObjectHashAggregateExec 
=> true
+        case _: TakeOrderedAndProjectExec => true
+        case _ => false
+      }
+      val startsLimit = original match {
+        case _: CollectLimitExec | _: LocalLimitExec | _: GlobalLimitExec => 
true
+        case topK: TakeOrderedAndProjectExec =>
+          SortOrder.orderingSatisfies(topK.child.outputOrdering, 
topK.sortOrder)
+        case _
+            if 
ShimCometWindowGroupLimit.windowGroupLimitClass.exists(_.isInstance(original)) 
=>
+          true
+        case _ => false
+      }
+      val childDemand = original match {
+        case _ if exchange => None
+        // A join can leave a local sort or aggregate completely unpolled.
+        case _ if join || rowDemand.contains("JOIN") => Some("JOIN")
+        case _ if startsLimit => Some("LIMIT")
+        case _: SampleExec => Some("SAMPLE")
+        case _: MapPartitionsExec => Some("a mapPartitions function")
+        case _ if localMaterialization => None
+        case _ => rowDemand
+      }
+      val childProjectionDemand = original match {
+        // Generated filters can defer deterministic projection outputs until 
a row is accepted.
+        case filter: FilterExec => Some(ProjectionDemand("FILTER", 
filter.child.outputSet))
+        case project: ProjectExec =>
+          val deferred = 
projectionDemand.map(_.deferred).getOrElse(AttributeSet.empty)
+          val eager = project.projectList
+            .filter(output => !output.deterministic || 
!deferred.contains(output.toAttribute))
+            .flatMap(eagerReferences)
+          // ProjectExec forces repeated inputs and nondeterministic outputs. 
Other inputs can
+          // remain inlined in a consumer's conditional, even across 
intervening aliases.
+          val skipped = project.child.outputSet -- project.usedInputs -- 
AttributeSet(eager)
+          if (skipped.isEmpty) {
+            None
+          } else {
+            
Some(ProjectionDemand(projectionDemand.map(_.boundary).getOrElse("PROJECT"), 
skipped))
+          }
+        case _ if exchange || localMaterialization || join => None
+        case _ => projectionDemand
+      }
+      val visited = node.children.map(visit(_, childDemand, 
childProjectionDemand))
+      val childReason = visited.flatMap(_._2).headOption
+      val ownReason = reason(node).orElse {
+        if (containsMakeDate(original.expressions)) {

Review Comment:
   Confirmed on `f07f57bc5089400c34ac6661f838c46e31fd1e36` with Spark 4.1.3 / 
JDK 17 and this PR's `native-lib-linux` artifact from CI run 40613167896. With 
ANSI and whole-stage codegen enabled, AQE disabled, and Parquet rows `(y, s) = 
(NULL, 'bad'), (2024, '1')`, this query retains two projections:
   
   ```sql
   SELECT r, make_date(y, m, 1) AS dt, r + 1 AS repeated_r
   FROM (
     SELECT rand(7L) AS r, y, CAST(s AS INT) AS m
     FROM t
   ) p
   ```
   
   Spark returns `NULL` and `2024-01-01` for `dt`. Comet keeps both projections 
native and throws `CAST_INVALID_INPUT` for `'bad'`.
   
   Could the regression coverage also include the following sibling case?
   
   ```sql
   SELECT r, dt, IF(dt IS NULL, 0, rhs) AS result, r + 1 AS repeated_r
   FROM (
     SELECT rand(7L) AS r, make_date(y, 1, 1) AS dt, CAST(s AS INT) AS rhs
     FROM t
   ) p
   ```
   
   Spark returns `(dt, result) = (NULL, 0), (2024-01-01, 1)`, while Comet again 
throws `CAST_INVALID_INPUT`. Here the producer does contain `MakeDate`, but 
selecting `dt` separately makes it eagerly required. Only `rhs` is deferred, so 
the check at lines 212–214 still misses the producer. Both reproductions point 
to the same need to track skipped expressions through their producers, 
including outputs that do not themselves contain `MakeDate`.
   



-- 
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