metanil commented on code in PR #59760:
URL: https://github.com/apache/spark/pull/59760#discussion_r4463971786
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TransformExpression.scala:
##########
@@ -167,30 +175,173 @@ case class TransformExpression(
copy(children = newChildren)
/**
- * The scalar function call this transform stands for, with the bucket count
prepended as a
- * literal argument. `None` when the bound function is not a
[[ScalarFunction]], and also when a
- * join reduced this expression's keys, since then the call no longer
computes them. Evaluating it
- * to place a row would send the row to a partition it does not belong to.
That second arm is a
- * local gate. No caller reaches it today, because every consumer of a
reduced partitioning
- * refuses it first, and the write path never sees one.
+ * The bound function, when Spark could call it for this transform: a
[[ScalarFunction]], over
+ * keys no join reduced. See [[resolvedFunction]].
*/
- lazy val resolvedFunction: Option[Expression] = function match {
- case scalarFunc: ScalarFunction[_] if reducedWith.isEmpty =>
- val arguments = numBucketsOpt.fold(children)(n => Literal(n) +: children)
- Some(V2ExpressionUtils.resolveScalarFunction(scalarFunc, arguments))
+ private def scalarFunction: Option[ScalarFunction[_]] = function match {
+ case scalarFunc: ScalarFunction[_] if reducedWith.isEmpty =>
Some(scalarFunc)
case _ => None
}
- override def eval(input: InternalRow): Any = resolvedFunction match {
- case Some(fn) => fn.eval(input)
- case None => throw QueryExecutionErrors.cannotEvaluateExpressionError(this)
+ /** The arguments of the call: the bucket count, if any, as a literal, then
the children. */
+ private def callArguments: Seq[Expression] =
+ numBucketsOpt.fold(children)(n => Literal(n) +: children)
+
+ /**
+ * The scalar function call this transform stands for, over its arguments as
they are. `None`
+ * when the bound function is not a [[ScalarFunction]], and also when a join
reduced this
+ * expression's keys, since then the call no longer computes them.
Evaluating it to place a row
+ * would send the row to a partition it does not belong to. That second arm
is a local gate. No
+ * caller reaches it today, because every consumer of a reduced partitioning
refuses it first,
+ * and the write path never sees one. The write path
(`DistributionAndOrderingUtils`) casts the
+ * arguments to the function's input types itself. `eval` uses
[[evaluableFunction]] instead.
+ */
+ lazy val resolvedFunction: Option[Expression] =
+ scalarFunction.map(V2ExpressionUtils.resolveScalarFunction(_,
callArguments))
+
+ /**
+ * The call `eval` runs: [[resolvedFunction]], with each argument at the
type the function
+ * declares for it in `inputTypes()`, see `atDeclaredType` for which
arguments suit. `None` when
+ * an argument does not, when there is another number of arguments, when the
call fails to
+ * resolve, and wherever `resolvedFunction` is `None`. `eval` rethrows a
failure to resolve. A
+ * storage-partitioned join shuffles instead of evaluating such a transform,
so each case but
+ * reduced keys is logged once, see `logNotScalar`, `logArgumentsMismatch`
and
+ * `logCannotResolve`.
+ */
+ def evaluableFunction: Option[Expression] = evaluableFunctionAttempt match {
+ case Success(call) => call
+ case _ => None
+ }
+
+ /**
+ * The call, or why it failed to resolve, e.g. a `ScalarFunction` that
inherits `produceResult`
+ * from a base class, connector code that throws, or a `LinkageError` for a
connector class whose
+ * dependency is missing, which the planner would otherwise hit for a layout
it does not pick.
+ * Computed and logged once, since a `lazy val` whose initializer throws
runs it again on each
+ * access. Not serialized, so an executor resolves it again rather than
receiving the call (or
+ * the failure) the driver resolved while planning.
+ */
+ @transient private lazy val evaluableFunctionAttempt:
Try[Option[Expression]] =
+ try Success(resolveEvaluableFunction()) catch {
+ case NonFatal(e) => cannotResolve(e)
+ case e: LinkageError => cannotResolve(e)
+ }
+
+ private def cannotResolve(e: Throwable): Try[Option[Expression]] = {
+ TransformExpression.logCannotResolve(function, e)
+ Failure(e)
+ }
+
+ private def resolveEvaluableFunction(): Option[Expression] = scalarFunction
match {
+ case None =>
+ // Reduced keys are the planner's own doing, so only a function Spark
cannot call is logged.
+ if (!function.isInstanceOf[ScalarFunction[_]])
TransformExpression.logNotScalar(function)
+ None
+ case Some(scalarFunc) =>
+ val arguments = callArguments
+ val declaredTypes = scalarFunc.inputTypes().toSeq
+ val typed = arguments.zip(declaredTypes).map { case (argument,
declaredType) =>
+ TransformExpression.atDeclaredType(argument, declaredType)
+ }
+ if (arguments.length == declaredTypes.length &&
typed.forall(_.isDefined)) {
+ val call = V2ExpressionUtils.resolveScalarFunction(scalarFunc,
typed.flatten)
+ // `Invoke` and `StaticInvoke` look the method up again from the
arguments' classes, a
+ // user-defined type's being `Object`, so look it up here, where a
failure is caught and
+ // logged, rather than in `eval`.
+ call match {
Review Comment:
Good catch, thanks. The forced lookup now sits in its own small check
(`methodTakesArguments`), so a method a `UDT` argument can't reach is reported
through `logArgumentsMismatch` at `INFO` and returns `None`, as you suggested.
It no longer takes the failure: slot, so a genuine resolution failure of the
same class is still warned about. The `UDT` test now checks for a single `INFO`
line naming the mismatch, and with the old code it fails on the `WARN`.
--
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]