pingzh commented on code in PR #7117: URL: https://github.com/apache/datafusion-comet/pull/7117#discussion_r4332582389
########## spark/src/main/scala/org/apache/comet/rules/MakeDateEvaluation.scala: ########## @@ -0,0 +1,236 @@ +/* + * 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.{Alias, And, Attribute, AttributeSet, BinaryComparison, Cast, ConditionalExpression, CreateArray, CreateNamedStruct, DateAdd, DateDiff, DateFormatClass, DateSub, EqualNullSafe, Expression, IsNotNull, IsNull, MakeDate, Not, Or, 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.CometConf +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 { Review Comment: Agreed that the implementations overlap. I've opened #7142 to consolidate the guards separately, with the aim of completing that before release while preserving both sets of regression coverage. Before merging the overlapping changes, let's agree on the public configuration semantics and landing order. A date-only opt-out cannot simply become an alias for a global switch, because that would also disable protection for other functions. The shared pass also needs the additional consumers covered here, so enrolling `MakeDate` alone would not preserve the current coverage. ########## spark/src/main/scala/org/apache/comet/serde/datetime.scala: ########## @@ -436,10 +436,70 @@ object CometNextDay extends CometExpressionSerde[NextDay] with CodegenDispatchFa object CometMakeDate extends CometExpressionSerde[MakeDate] { + private val sharedArgumentReason = + "make_date requires Spark evaluation when a computed argument after a nullable " + + "argument cannot preserve Spark's shared-subexpression evaluation." + + override def getUnsupportedReasons(): Seq[String] = Seq(sharedArgumentReason) + + override def getSupportLevel(expr: MakeDate): SupportLevel = { + val firstNullable = expr.children.indexWhere(_.nullable) + val hasComputedMaskedArgument = firstNullable >= 0 && + expr.children.drop(firstNullable + 1).exists(!_.isInstanceOf[LeafExpression]) + if (hasComputedMaskedArgument && !canShortCircuitNulls(expr)) { + Unsupported(Some(sharedArgumentReason)) + } else { + Compatible() + } + } + + /** A native parent must not evaluate a date on rows where Spark skips that child. */ + private[serde] def unsupportedParentReason(expr: Expression): Option[String] = { + var pending: List[(Expression, Option[String])] = (expr, None) :: Nil + while (pending.nonEmpty) { + val (current, parent) = pending.head + pending = pending.tail + if (current.isInstanceOf[MakeDate] && parent.isDefined) { + return Some(s"make_date requires Spark evaluation under ${parent.get}") + } + def pushChildren(mask: Option[String]): Unit = { + current.children.reverseIterator.foreach(child => pending = (child, mask) :: pending) + } + if (parent.isDefined) { + pushChildren(parent) + } else { + current match { + // These native expressions already select the rows passed to their children. + case _: If | _: CaseWhen | _: MakeDate => pushChildren(None) + // COALESCE lowers to CASE and evaluates an earlier child again in its result branch. + case coalesce: Coalesce if coalesce.deterministic => pushChildren(None) + // These parents evaluate every child in Spark, including NULL values. + case _: EqualNullSafe | _: CreateArray | _: CreateNamedStruct => + pushChildren(None) + // These parents evaluate left first and skip right only when left is NULL. + case _: BinaryComparison | _: DateAdd | _: DateSub | _: DateDiff | _: DateFormatClass => + val left = current.children.head + val right = current.children(1) + val rightMask = if (left.nullable) Some(current.nodeName) else None + pending = (left, None) :: (right, rightMask) :: pending + case _: SortOrder => pushChildren(Some(current.nodeName)) + // Empty membership can skip its sole operand in Spark. + case in: In if in.list.isEmpty => pushChildren(Some(current.nodeName)) + case inSet: InSet if inSet.hset.isEmpty => pushChildren(Some(current.nodeName)) + case _ if current.children.size <= 1 => pushChildren(None) + // Other native multi-child expressions may evaluate operands that Spark skips. + case _ => pushChildren(Some(current.nodeName)) Review Comment: Agreed: source inspection confirms unnecessary fallback for this nullable filter. I've opened #7143 as a separate performance follow-up, including the suggested SQL regression. The current fallback preserves Spark's results and errors; runtime performance impact has not been measured. One detail for the fix: Spark places the date comparison on the right of the inferred NULL checks, so allowing only the eager left operand of `And`/`Or` would not make this query native. We need to recognize the redundant NULL-check shape while retaining protection for general predicates that skip an overflowing date. The operator-fallback opt-out does not disable this scalar guard. -- 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]
