Github user maropu commented on a diff in the pull request:

    https://github.com/apache/spark/pull/20756#discussion_r173067172
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala
 ---
    @@ -1261,8 +1261,39 @@ case class InitializeJavaBean(beanInstance: 
Expression, setters: Map[String, Exp
       override def children: Seq[Expression] = beanInstance +: 
setters.values.toSeq
       override def dataType: DataType = beanInstance.dataType
     
    -  override def eval(input: InternalRow): Any =
    -    throw new UnsupportedOperationException("Only code-generated 
evaluation is supported.")
    +  private lazy val resolvedSetters = {
    +    val ObjectType(beanClass) = beanInstance.dataType
    +    setters.map {
    +      case (name, expr) =>
    +        // Looking for known type mapping first, then using Class attached 
in `ObjectType`.
    +        // Finally also looking for general `Object`-type parameter for 
generic methods.
    +        val paramTypes = 
CallMethodViaReflection.typeMapping.getOrElse(expr.dataType,
    +            Seq(expr.dataType.asInstanceOf[ObjectType].cls)) ++ 
Seq(classOf[Object])
    +        val methods = paramTypes.flatMap { fieldClass =>
    +          try {
    +            Some(beanClass.getDeclaredMethod(name, fieldClass))
    +          } catch {
    +            case e: NoSuchMethodException => None
    +          }
    +        }
    +        if (methods.isEmpty) {
    +          throw new NoSuchMethodException(s"""A method named "$name" is 
not declared """ +
    +            "in any enclosing class nor any supertype, nor through a 
static import")
    +        }
    +        methods.head -> expr
    +    }
    +  }
    +
    +  override def eval(input: InternalRow): Any = {
    +    val instance = beanInstance.eval(input).asInstanceOf[Object]
    --- End diff --
    
    super nit: better to put the cast inside `if` for avoiding unnecessary 
casts in case of many null cases?;
    ```
        val instance = beanInstance.eval(input)
        if (instance != null) {
          val obj = instance.asInstanceOf[Object]
          resolvedSetters.foreach {
            case (setter: Method, expr) =>
              setter.invoke(obj, expr.eval(input).asInstanceOf[Object])
          }
        }
        instance
    ```


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to