Ngone51 commented on a change in pull request #28979:
URL: https://github.com/apache/spark/pull/28979#discussion_r450271184
##########
File path: sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala
##########
@@ -504,23 +508,34 @@ 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))
+ assert(df.collect().toSeq === Seq(Row(expected)))
}
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))
+ assert(df.collect().toSeq === Seq(Row(expected)))
+ }
+
+ test("Using combined types of Instant/LocalDate in UDF") {
+ val ts = "2019-02-26T23:59:59Z"
+ val date = "2019-02-26"
+ val expectedDate = sql(s"SELECT CAST(DATE '$date' AS
STRING)").collect().head.getString(0)
+ val expectedIns = sql(s"SELECT CAST(TIMESTAMP '$ts' AS
STRING)").collect().head.getString(0)
+ spark.udf.register("toDateTime", udf((d: LocalDate, i: Instant) =>
DateTimeResult(d, i)))
Review comment:
> If udf((d: LocalDate, i: Instant) => null)), it seems
ExpressionEncoder will throw an exception?
It will since Spark does not support top-level null values.
> I think we need to test the case: udf((d: LocalDate, i: Instant) =>
DateTimeResult(null, null)).
Improved test for this case.
----------------------------------------------------------------
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]