18
18
package org .apache .spark .sql .catalyst .expressions
19
19
20
20
import java .nio .charset .StandardCharsets
21
+ import java .time .{Instant , LocalDate }
21
22
22
23
import scala .reflect .runtime .universe .{typeTag , TypeTag }
23
24
@@ -26,6 +27,7 @@ import org.apache.spark.sql.Row
26
27
import org .apache .spark .sql .catalyst .{CatalystTypeConverters , ScalaReflection }
27
28
import org .apache .spark .sql .catalyst .encoders .ExamplePointUDT
28
29
import org .apache .spark .sql .catalyst .util .DateTimeUtils
30
+ import org .apache .spark .sql .internal .SQLConf
29
31
import org .apache .spark .sql .types ._
30
32
import org .apache .spark .unsafe .types .CalendarInterval
31
33
@@ -64,8 +66,14 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
64
66
checkEvaluation(Literal .default(BinaryType ), " " .getBytes(StandardCharsets .UTF_8 ))
65
67
checkEvaluation(Literal .default(DecimalType .USER_DEFAULT ), Decimal (0 ))
66
68
checkEvaluation(Literal .default(DecimalType .SYSTEM_DEFAULT ), Decimal (0 ))
67
- checkEvaluation(Literal .default(DateType ), DateTimeUtils .toJavaDate(0 ))
68
- checkEvaluation(Literal .default(TimestampType ), DateTimeUtils .toJavaTimestamp(0L ))
69
+ withSQLConf(SQLConf .DATETIME_JAVA8API_EANBLED .key -> " false" ) {
70
+ checkEvaluation(Literal .default(DateType ), DateTimeUtils .toJavaDate(0 ))
71
+ checkEvaluation(Literal .default(TimestampType ), DateTimeUtils .toJavaTimestamp(0L ))
72
+ }
73
+ withSQLConf(SQLConf .DATETIME_JAVA8API_EANBLED .key -> " true" ) {
74
+ checkEvaluation(Literal .default(DateType ), LocalDate .ofEpochDay(0 ))
75
+ checkEvaluation(Literal .default(TimestampType ), Instant .ofEpochSecond(0 ))
76
+ }
69
77
checkEvaluation(Literal .default(CalendarIntervalType ), new CalendarInterval (0 , 0L ))
70
78
checkEvaluation(Literal .default(ArrayType (StringType )), Array ())
71
79
checkEvaluation(Literal .default(MapType (IntegerType , StringType )), Map ())
@@ -228,4 +236,47 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
228
236
checkEvaluation(Literal ('\u0000 ' ), " \u0000 " )
229
237
checkEvaluation(Literal .create('\n ' ), " \n " )
230
238
}
239
+
240
+ test(" construct literals from java.time.LocalDate" ) {
241
+ Seq (
242
+ LocalDate .of(1 , 1 , 1 ),
243
+ LocalDate .of(1582 , 10 , 1 ),
244
+ LocalDate .of(1600 , 7 , 30 ),
245
+ LocalDate .of(1969 , 12 , 31 ),
246
+ LocalDate .of(1970 , 1 , 1 ),
247
+ LocalDate .of(2019 , 3 , 20 ),
248
+ LocalDate .of(2100 , 5 , 17 )).foreach { localDate =>
249
+ checkEvaluation(Literal (localDate), localDate)
250
+ }
251
+ }
252
+
253
+ test(" construct literals from arrays of java.time.LocalDate" ) {
254
+ withSQLConf(SQLConf .DATETIME_JAVA8API_EANBLED .key -> " true" ) {
255
+ val localDate0 = LocalDate .of(2019 , 3 , 20 )
256
+ checkEvaluation(Literal (Array (localDate0)), Array (localDate0))
257
+ val localDate1 = LocalDate .of(2100 , 4 , 22 )
258
+ checkEvaluation(Literal (Array (localDate0, localDate1)), Array (localDate0, localDate1))
259
+ }
260
+ }
261
+
262
+ test(" construct literals from java.time.Instant" ) {
263
+ Seq (
264
+ Instant .parse(" 0001-01-01T00:00:00Z" ),
265
+ Instant .parse(" 1582-10-01T01:02:03Z" ),
266
+ Instant .parse(" 1970-02-28T11:12:13Z" ),
267
+ Instant .ofEpochMilli(0 ),
268
+ Instant .parse(" 2019-03-20T10:15:30Z" ),
269
+ Instant .parse(" 2100-12-31T22:17:31Z" )).foreach { instant =>
270
+ checkEvaluation(Literal (instant), instant)
271
+ }
272
+ }
273
+
274
+ test(" construct literals from arrays of java.time.Instant" ) {
275
+ withSQLConf(SQLConf .DATETIME_JAVA8API_EANBLED .key -> " true" ) {
276
+ val instant0 = Instant .ofEpochMilli(0 )
277
+ checkEvaluation(Literal (Array (instant0)), Array (instant0))
278
+ val instant1 = Instant .parse(" 2019-03-20T10:15:30Z" )
279
+ checkEvaluation(Literal (Array (instant0, instant1)), Array (instant0, instant1))
280
+ }
281
+ }
231
282
}
0 commit comments