JacksonKjar commented on code in PR #59215:
URL: https://github.com/apache/spark/pull/59215#discussion_r4343098167
##########
sql/api/src/main/scala/org/apache/spark/sql/catalyst/JavaTypeInference.scala:
##########
@@ -186,6 +188,45 @@ object JavaTypeInference {
throw ExecutionErrors.cannotFindEncoderForTypeError(t.toString)
}
+ /**
+ * Infer an [[AgnosticEncoder]] for a Java record (`java.lang.Record`).
Fields follow component
+ * declaration order and are read through the component accessors. Like the
JavaBean case,
+ * components are resolved through `encoderFor`, which carries `seenTypeSet`
and `typeVariables`
+ * into nested components.
+ */
+ private def encoderForJavaRecord[T](
+ recordClass: Class[T],
+ seenTypeSet: Set[Class[_]],
+ typeVariables: Map[TypeVariable[_], Type]): JavaRecordEncoder[T] = {
+ if (seenTypeSet.contains(recordClass)) {
+ throw
ExecutionErrors.cannotHaveCircularReferencesInBeanClassError(recordClass)
+ }
+
+ // Type parameters are erased, so they can only be resolved when the
enclosing type binds
+ // them (e.g. a `Box<String>` component), which puts them in
`typeVariables`.
+ val unboundTypeParams =
recordClass.getTypeParameters.filterNot(typeVariables.contains)
+ if (unboundTypeParams.nonEmpty) {
+ throw ExecutionErrors.genericRecordNotSupportedError(recordClass)
+ }
+
+ val fields = recordClass.getRecordComponents.map { component =>
+ val accessor = component.getAccessor
+ val encoder = encoderFor(component.getGenericType, seenTypeSet +
recordClass, typeVariables)
+ // The existence of `javax.annotation.Nonnull` means this component is
not nullable.
+ val hasNonNull = accessor.isAnnotationPresent(classOf[Nonnull])
Review Comment:
Good catch, thanks. I applied the suggested fix in ad5bdeb and added unit
tests covering components with explicit accessors both with and without
`@Nonnull`
--
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]