metanil commented on code in PR #59760:
URL: https://github.com/apache/spark/pull/59760#discussion_r4545838750


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/TransformExpression.scala:
##########
@@ -202,4 +401,115 @@ object TransformExpression {
     case t: TransformExpression => t.reducedWith.isDefined
     case _ => false
   }
+
+  /**
+   * `argument` at `declaredType`, or `None` when the function could not take 
it. See
+   * [[TransformExpression.evaluableFunction]].
+   *
+   * An argument goes as it is when it is stored the way the declared type is 
(`storedAlike`),
+   * which is how a function read it before this check. A numeric argument 
narrower than declared
+   * (`UpCastRule.legalNumericPrecedence`), e.g. a `SMALLINT` against an `INT` 
or an `INT` against a
+   * `DOUBLE`, is held as another JVM type, so it is cast. That gives the 
value a magic `invoke` got
+   * from Java's widening before this check, which can lose digits for a 
`BIGINT` against a
+   * `DOUBLE` as it did there, and needs no time zone. Anything else is 
refused,
+   * a `DATE` against a `TIMESTAMP` in particular, whose cast takes the 
session time zone. A call
+   * whose arguments all suit is still refused when the method it invokes 
cannot take them, see
+   * `methodLookupFailure`.
+   */
+  private def atDeclaredType(argument: Expression, declaredType: DataType): 
Option[Expression] =
+    (argument.dataType, declaredType) match {
+      case (argumentType, _) if storedAlike(argumentType, declaredType, nested 
= false) =>
+        Some(argument)
+      case (argumentType, _) if 
UpCastRule.legalNumericPrecedence(argumentType, declaredType) =>
+        Some(Cast(argument, declaredType))
+      case _ =>
+        None
+    }
+
+  /**
+   * Why the method `call` invokes does not take its arguments, or `None` when 
it does. `Invoke`
+   * and `StaticInvoke` look it up again from the arguments' classes, a 
user-defined type's being
+   * `Object`, and only among public methods, so a magic `invoke` the declared 
types found may not
+   * be reachable. Looked up here, so that such a call is refused as the 
argument mismatch it is,
+   * rather than failing in `eval`.
+   */
+  private def methodLookupFailure(call: Expression): Option[Throwable] =
+    try {
+      call match {
+        case i: objects.Invoke => i.method
+        case s: objects.StaticInvoke => s.method
+        case _ =>
+      }
+      None
+    } catch {
+      case NonFatal(e) => Some(e)
+    }
+
+  /**
+   * Whether a key of type `from` and one of type `to` holding one value are 
stored as the same
+   * bytes in an unsafe row, and order alike read as either type. A cogroup 
pairs the groups of its
+   * two sides by comparing their key rows, ordered by its left side's key 
types, so keys of other
+   * types pair their groups wrongly, or fail, see 
`KeyedShuffleSpec.createPartitioning`. As
+   * `storedAlike` for a nested value, apart from decimals: two decimals of 
one scale are stored
+   * alike, whatever their precision, when both are stored as a `long`, up to
+   * `Decimal.MAX_LONG_DIGITS` digits, or both as bytes. A row, the key itself 
or a struct field,
+   * reads a `long` at a smaller precision than its value has as it is. An 
array element, or a
+   * map's key or value, is read with a check that fails for a value of more 
digits than that
+   * precision, as one stored as bytes is anywhere. So a cogroup over such 
keys can fail, but never
+   * pairs its groups wrongly.
+   */
+  private[sql] def keysStoredAlike(from: DataType, to: DataType): Boolean = 
(from, to) match {

Review Comment:
   Done, with your flag: `keysStoredAlike(from, to)` is now `storedAlike(from, 
to, nested = true, anyPrecision = true)`.



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