maropu commented on a change in pull request #28979:
URL: https://github.com/apache/spark/pull/28979#discussion_r451253446
##########
File path: sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala
##########
@@ -504,23 +510,83 @@ class UDFSuite extends QueryTest with SharedSparkSession {
}
test("Using java.time.Instant in UDF") {
- withSQLConf(SQLConf.DATETIME_JAVA8API_ENABLED.key -> "true") {
- val expected = java.time.Instant.parse("2019-02-27T00:00:00Z")
- val plusSec = udf((i: java.time.Instant) => i.plusSeconds(1))
- val df = spark.sql("SELECT TIMESTAMP '2019-02-26 23:59:59Z' as t")
- .select(plusSec('t))
- assert(df.collect().toSeq === Seq(Row(expected)))
- }
+ val dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
+ val expected = java.time.Instant.parse("2019-02-27T00:00:00Z")
+ .atZone(DateTimeUtils.getZoneId(conf.sessionLocalTimeZone))
+ .toLocalDateTime
+ .format(dtf)
+ val plusSec = udf((i: java.time.Instant) => i.plusSeconds(1))
+ val df = spark.sql("SELECT TIMESTAMP '2019-02-26 23:59:59Z' as t")
+ .select(plusSec('t).cast(StringType))
+ checkAnswer(df, Row(expected) :: Nil)
}
test("Using java.time.LocalDate in UDF") {
- withSQLConf(SQLConf.DATETIME_JAVA8API_ENABLED.key -> "true") {
- val expected = java.time.LocalDate.parse("2019-02-27")
- val plusDay = udf((i: java.time.LocalDate) => i.plusDays(1))
- val df = spark.sql("SELECT DATE '2019-02-26' as d")
- .select(plusDay('d))
- assert(df.collect().toSeq === Seq(Row(expected)))
- }
+ val expected = java.time.LocalDate.parse("2019-02-27").toString
+ val plusDay = udf((i: java.time.LocalDate) => i.plusDays(1))
+ val df = spark.sql("SELECT DATE '2019-02-26' as d")
+ .select(plusDay('d).cast(StringType))
+ checkAnswer(df, Row(expected) :: Nil)
+ }
+
+ test("Using combined types of Instant/LocalDate in UDF") {
+ val dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
+ val date = LocalDate.parse("2019-02-26")
+ val instant = Instant.parse("2019-02-26T23:59:59Z")
+ val expectedDate = date.toString
+ val expectedInstant =
+ instant.atZone(DateTimeUtils.getZoneId(conf.sessionLocalTimeZone))
+ .toLocalDateTime
+ .format(dtf)
+ val df = Seq((date, instant)).toDF("d", "i")
+
+ // test normal case
+ spark.udf.register("buildLocalDateInstantType",
+ udf((d: LocalDate, i: Instant) => LocalDateInstantType(d, i)))
+ checkAnswer(df.selectExpr(s"buildLocalDateInstantType(d, i) as di")
+ .select('di.cast(StringType)),
+ Row(s"[$expectedDate, $expectedInstant]") :: Nil)
+
+ // test null cases
+ spark.udf.register("buildLocalDateInstantType",
+ udf((d: LocalDate, i: Instant) => LocalDateInstantType(null, null)))
+ checkAnswer(df.selectExpr("buildLocalDateInstantType(d, i) as di"),
+ Row(Row(null, null)))
+
+ spark.udf.register("buildLocalDateInstantType",
+ udf((d: LocalDate, i: Instant) =>
null.asInstanceOf[LocalDateInstantType]))
+ checkAnswer(df.selectExpr("buildLocalDateInstantType(d, i) as di"),
+ Row(null))
+ }
+
+ test("Using combined types of Instant/Timestamp in UDF") {
+ val dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
+ val timestamp = Timestamp.valueOf("2019-02-26 23:59:59")
+ val instant = Instant.parse("2019-02-26T23:59:59Z")
+ val expectedTimestamp = timestamp.toLocalDateTime.format(dtf)
+ val expectedInstant =
+ instant.atZone(DateTimeUtils.getZoneId(conf.sessionLocalTimeZone))
+ .toLocalDateTime
+ .format(dtf)
+ val df = Seq((timestamp, instant)).toDF("t", "i")
+
+ // test normal case
+ spark.udf.register("buildTimestampInstantType",
+ udf((t: Timestamp, i: Instant) => TimestampInstantType(t, i)))
+ checkAnswer(df.selectExpr("buildTimestampInstantType(t, i) as ti")
+ .select('ti.cast(StringType)),
+ Row(s"[$expectedTimestamp, $expectedInstant]"))
+
+ // test null cases
+ spark.udf.register("buildTimestampInstantType",
+ udf((t: Timestamp, i: Instant) => TimestampInstantType(null, null)))
+ checkAnswer(df.selectExpr("buildTimestampInstantType(t, i) as ti"),
+ Row(Row(null, null)))
+
+ spark.udf.register("buildTimestampInstantType",
+ udf((t: Timestamp, i: Instant) =>
null.asInstanceOf[TimestampInstantType]))
+ checkAnswer(df.selectExpr("buildTimestampInstantType(t, i) as ti"),
+ Row(null))
Review comment:
Thanks for adding this test. To check the unsupported case, could you
add tests for the toplevel-null case (catch encoder exceptions)?
https://github.com/apache/spark/pull/28979#discussion_r450548418
----------------------------------------------------------------
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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]