[SPARK-32087][SQL] Allow UserDefinedType to use encoder to deserialize rows in ScalaUDF as well #28920
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR tries to address the comment: #28645 (comment)
It changes
canUpCast/canCastto allow cast from sub UDT to base UDT, in order to achieve the goal to allow UserDefinedType to useExpressionEncoderto deserialize rows in ScalaUDF as well.One thing that needs to mention is, even we allow cast from sub UDT to base UDT, it doesn't really do the cast in
Cast. Because, yet, sub UDT and base UDT are considered as the same type(because of #16660), see:spark/sql/catalyst/src/main/scala/org/apache/spark/sql/types/UserDefinedType.scala
Lines 81 to 86 in 5264164
spark/sql/catalyst/src/main/scala/org/apache/spark/sql/types/UserDefinedType.scala
Lines 92 to 95 in 5264164
Therefore, the optimize rule
SimplifyCastwill eliminate the cast at the end.Why are the changes needed?
Reduce the special case caused by
UserDefinedTypeinResolveEncodersInUDFandScalaUDF.Does this PR introduce any user-facing change?
No.
How was this patch tested?
It should be covered by the test of
SPARK-19311, which is also updated a little in this PR.