-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-19666][SQL] Skip a property without getter in Java schema inference and allow empty bean in encoder creation #17013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
9ad4789
ae4c9aa
91cee26
5808d71
ed686fa
3604855
ac5cc7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,11 +117,10 @@ object JavaTypeInference { | |
| val (valueDataType, nullable) = inferDataType(valueType) | ||
| (MapType(keyDataType, valueDataType, nullable), true) | ||
|
|
||
| case _ => | ||
| case 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 beanInfo = Introspector.getBeanInfo(typeToken.getRawType) | ||
| val properties = beanInfo.getPropertyDescriptors.filterNot(_.getName == "class") | ||
| val properties = getJavaBeanPropertiesWithGetters(other) | ||
| val fields = properties.map { property => | ||
| val returnType = typeToken.method(property.getReadMethod).getReturnType | ||
| val (dataType, nullable) = inferDataType(returnType) | ||
|
|
@@ -131,9 +130,16 @@ object JavaTypeInference { | |
| } | ||
| } | ||
|
|
||
| def getJavaBeanPropertiesWithGetters(beanClass: Class[_]): Array[PropertyDescriptor] = { | ||
| val beanInfo = Introspector.getBeanInfo(beanClass) | ||
| beanInfo.getPropertyDescriptors | ||
| .filterNot(_.getName == "class").filter(p => p.getReadMethod != null) | ||
| } | ||
|
|
||
| private def getJavaBeanProperties(beanClass: Class[_]): Array[PropertyDescriptor] = { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this call the new method above and additionally filter for a setter?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it seems so. Let me try to address this. |
||
| val beanInfo = Introspector.getBeanInfo(beanClass) | ||
| beanInfo.getPropertyDescriptors | ||
| .filterNot(_.getName == "class") | ||
| .filter(p => p.getReadMethod != null && p.getWriteMethod != null) | ||
| } | ||
|
|
||
|
|
@@ -299,8 +305,6 @@ object JavaTypeInference { | |
|
|
||
| case other => | ||
| val properties = getJavaBeanProperties(other) | ||
| assert(properties.length > 0) | ||
|
|
||
| val setters = properties.map { p => | ||
| val fieldName = p.getName | ||
| val fieldType = typeToken.method(p.getReadMethod).getReturnType | ||
|
|
@@ -418,20 +422,15 @@ object JavaTypeInference { | |
|
|
||
| case other => | ||
| val properties = getJavaBeanProperties(other) | ||
| if (properties.length > 0) { | ||
| CreateNamedStruct(properties.flatMap { p => | ||
| val fieldName = p.getName | ||
| val fieldType = typeToken.method(p.getReadMethod).getReturnType | ||
| val fieldValue = Invoke( | ||
| inputObject, | ||
| p.getReadMethod.getName, | ||
| inferExternalType(fieldType.getRawType)) | ||
| expressions.Literal(fieldName) :: serializerFor(fieldValue, fieldType) :: Nil | ||
| }) | ||
| } else { | ||
| throw new UnsupportedOperationException( | ||
| s"Cannot infer type for class ${other.getName} because it is not bean-compliant") | ||
| } | ||
| CreateNamedStruct(properties.flatMap { p => | ||
| val fieldName = p.getName | ||
| val fieldType = typeToken.method(p.getReadMethod).getReturnType | ||
| val fieldValue = Invoke( | ||
| inputObject, | ||
| p.getReadMethod.getName, | ||
| inferExternalType(fieldType.getRawType)) | ||
| expressions.Literal(fieldName) :: serializerFor(fieldValue, fieldType) :: Nil | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a brevity thing, but,
(_.getReadMethod != null)?