Fokko commented on a change in pull request #26644: [SPARK-30004][SQL] Allow 
merge UserDefinedType into a native DataType
URL: https://github.com/apache/spark/pull/26644#discussion_r349918766
 
 

 ##########
 File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/types/DataTypeSuite.scala
 ##########
 @@ -478,4 +483,33 @@ class DataTypeSuite extends SparkFunSuite {
 
     assert(result === expected)
   }
+
+  test("SPARK-30004: Allow UserDefinedType to be merged into a standard 
DateType") {
+    class CustomXMLGregorianCalendarType extends 
UserDefinedType[XMLGregorianCalendar] {
+      override def sqlType: DataType = TimestampType
+
+      override def serialize(obj: XMLGregorianCalendar): Any =
+        obj.toGregorianCalendar.getTimeInMillis * 1000
+
+      override def deserialize(datum: Any): XMLGregorianCalendar = {
+        val calendar = new GregorianCalendar
+        calendar.setTimeInMillis(datum.asInstanceOf[Long])
+        DatatypeFactory.newInstance.newXMLGregorianCalendar(calendar)
+      }
+
+      override def userClass: Class[XMLGregorianCalendar] = 
classOf[XMLGregorianCalendar]
+
+      override private[sql] def jsonValue: JValue = "timestamp"
+    }
+
+    val left = StructType(
+      StructField("a", TimestampType) :: Nil)
+
+    // We should be able to merge these types since the
+    // CustomXMLGregorianCalendarType maps to a TimestampTypes
+    val right = StructType(
+      StructField("a", new CustomXMLGregorianCalendarType) :: Nil)
+
+    assert(left.merge(right) === left)
 
 Review comment:
   The current implementation isn't symmetrical. So we can convert a 
UserDefinedType to a DateType, but not the other way around. I'm hesitant to 
add this functionality because I don't see any obvious applications. Please let 
me know if you think this should be added as well. I've added a test to check 
the opposite case as well, including some additional comments to clarify the 
idea and working.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to