You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2019/04/19 12:54:29 UTC

[GitHub] [spark] gengliangwang commented on a change in pull request #24367: [SPARK-27457][SQL] modify bean encoder to support avro objects

gengliangwang commented on a change in pull request #24367: [SPARK-27457][SQL] modify bean encoder to support avro objects
URL: https://github.com/apache/spark/pull/24367#discussion_r276979582
 
 

 ##########
 File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/JavaTypeInference.scala
 ##########
 @@ -131,28 +132,38 @@ object JavaTypeInference {
               s"of class $other")
         }
 
-        // TODO: we should only collect properties that have getter and setter. However, some tests
-        // pass in scala case class as java bean class which doesn't have getter and setter.
-        val properties = getJavaBeanReadableProperties(other)
-        val fields = properties.map { property =>
-          val returnType = typeToken.method(property.getReadMethod).getReturnType
-          val (dataType, nullable) = inferDataType(returnType, seenTypeSet + other)
-          new StructField(property.getName, dataType, nullable)
+        val fields = getObjectProperties(other).map {
+          case (propertyName, getterMethod, setterMethod) =>
+            val (dataType, nullable) = inferDataType(
+              TypeToken.of(getterMethod.getGenericReturnType),
+              seenTypeSet + other)
+            new StructField(propertyName, dataType, nullable)
         }
         (new StructType(fields), true)
     }
   }
 
-  def getJavaBeanReadableProperties(beanClass: Class[_]): Array[PropertyDescriptor] = {
-    val beanInfo = Introspector.getBeanInfo(beanClass)
-    beanInfo.getPropertyDescriptors.filterNot(_.getName == "class")
-      .filterNot(_.getName == "declaringClass")
-      .filter(_.getReadMethod != null)
-  }
-
-  private def getJavaBeanReadableAndWritableProperties(
-      beanClass: Class[_]): Array[PropertyDescriptor] = {
-    getJavaBeanReadableProperties(beanClass).filter(_.getWriteMethod != null)
+  /**
+   * Returns: Array[(porpertyName, getterName, setterName, propertyType)]
+   *
+   * Properties of the object are defined by a getter 'propertyType [get]PropertyName()' and a
+   * setter 'void [set]PropertyName(propertyType value)' functions; where [get]PropertyName is
+   * the name of the getter function, and [set]PropertyName is the name of the setter function.
+   */
+  def getObjectProperties(beanClass: Class[_]): Array[(String, Method, Method)] = {
+    for {
+      a <- beanClass.getMethods.filter(method => method.getParameterCount == 0)
+      b <- beanClass.getMethods.filter(method => method.getReturnType == Void.TYPE &&
+        method.getParameterCount == 1)
+      if (a.getName == b.getName ||
+        (a.getName.indexOf("get") == 0 && b.getName.indexOf("set") == 0 &&
+          a.getName.substring(3) == b.getName.substring(3))) &&
 
 Review comment:
   It is a bit hacky here, compared with https://github.com/apache/spark/pull/24299

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

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