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


##########
spark/src/main/scala/org/apache/comet/rules/MakeDateEvaluation.scala:
##########
@@ -0,0 +1,384 @@
+/*
+ * 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, GetDateField, Greatest, HashExpression, IsNotNull, IsNull, Least, 
MakeDate, Multiply, Not, Or, SortOrder, Subtract, UnixDate}
+import 
org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, Count}
+import org.apache.spark.sql.catalyst.optimizer.BuildLeft
+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, 
BroadcastHashJoinExec, BroadcastNestedLoopJoinExec}
+
+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 def containsDeferredAggregateDate(expression: Expression): Boolean = 
expression match {
+    // COUNT checks its sole nullable argument on every input row. Filters, 
other nullable
+    // arguments, and a non-nullable argument can let Spark skip evaluating 
the date.
+    case AggregateExpression(Count(Seq(child)), _, _, None, _) if 
child.nullable => false

Review Comment:
   [P2] With ANSI enabled, `COUNT(date_add(make_date(y, 1, 1), CAST(s AS 
INT)))` can throw `CAST_INVALID_INPUT` where Spark returns `1`. On Parquet rows 
`(NULL, 'bad')` and `(2024, '1')`, Spark skips the days operand on the first 
row because the date is NULL. The COUNT exemption accepts the entire nullable 
argument, and `CometDateAdd` serializes both operands without null 
short-circuiting. The same expression is eligible as a native hash partition 
key below a limit. Guard `DateAdd` when a nullable left date can hide a 
throwing right operand, while preserving native eligibility for bare 
`make_date`.



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