Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ object Literal {
// java classes
case _ if clz == classOf[LocalDate] => DateType
case _ if clz == classOf[Date] => DateType
case _ if clz == classOf[LocalTime] => TimeType()
case _ if clz == classOf[Instant] => TimestampType
case _ if clz == classOf[Timestamp] => TimestampType
case _ if clz == classOf[LocalDateTime] => TimestampNTZType
Expand Down Expand Up @@ -199,6 +200,7 @@ object Literal {
case DateType => create(0, DateType)
case TimestampType => create(0L, TimestampType)
case TimestampNTZType => create(0L, TimestampNTZType)
case t: TimeType => create(0L, t)
case it: DayTimeIntervalType => create(0L, it)
case it: YearMonthIntervalType => create(0, it)
case CharType(length) =>
Expand Down Expand Up @@ -433,6 +435,8 @@ case class Literal (value: Any, dataType: DataType) extends LeafExpression {
dataType match {
case DateType =>
DateFormatter().format(value.asInstanceOf[Int])
case _: TimeType =>
new FractionTimeFormatter().format(value.asInstanceOf[Long])
case TimestampType =>
TimestampFormatter.getFractionFormatter(timeZoneId).format(value.asInstanceOf[Long])
case TimestampNTZType =>
Expand Down Expand Up @@ -479,7 +483,7 @@ case class Literal (value: Any, dataType: DataType) extends LeafExpression {
val jsonValue = (value, dataType) match {
case (null, _) => JNull
case (i: Int, DateType) => JString(toString)
case (l: Long, TimestampType) => JString(toString)
case (l: Long, TimestampType | _: TimeType) => JString(toString)
case (other, _) => JString(other.toString)
}
("value" -> jsonValue) :: ("dataType" -> dataType.jsonValue) :: Nil
Expand Down Expand Up @@ -563,6 +567,8 @@ case class Literal (value: Any, dataType: DataType) extends LeafExpression {
case (v: Decimal, t: DecimalType) => s"${v}BD"
case (v: Int, DateType) =>
s"DATE '$toString'"
case (_: Long, _: TimeType) =>
s"TIME '$toString'"
case (v: Long, TimestampType) =>
s"TIMESTAMP '$toString'"
case (v: Long, TimestampNTZType) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.catalyst.expressions

import java.nio.charset.StandardCharsets
import java.time.{Duration, Instant, LocalDate, LocalDateTime, Period, ZoneOffset}
import java.time.{Duration, Instant, LocalDate, LocalDateTime, LocalTime, Period, ZoneOffset}
import java.time.temporal.ChronoUnit
import java.util.TimeZone

Expand All @@ -30,6 +30,7 @@ import org.apache.spark.sql.Row
import org.apache.spark.sql.catalyst.{CatalystTypeConverters, ScalaReflection}
import org.apache.spark.sql.catalyst.encoders.ExamplePointUDT
import org.apache.spark.sql.catalyst.util.DateTimeConstants._
import org.apache.spark.sql.catalyst.util.DateTimeTestUtils.localTime
import org.apache.spark.sql.catalyst.util.DateTimeUtils
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._
Expand All @@ -51,6 +52,7 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Literal.create(null, BinaryType), null)
checkEvaluation(Literal.create(null, DecimalType.USER_DEFAULT), null)
checkEvaluation(Literal.create(null, DateType), null)
checkEvaluation(Literal.create(null, TimeType()), null)
checkEvaluation(Literal.create(null, TimestampType), null)
checkEvaluation(Literal.create(null, CalendarIntervalType), null)
checkEvaluation(Literal.create(null, YearMonthIntervalType()), null)
Expand Down Expand Up @@ -81,6 +83,7 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Literal.default(DateType), LocalDate.ofEpochDay(0))
checkEvaluation(Literal.default(TimestampType), Instant.ofEpochSecond(0))
}
checkEvaluation(Literal.default(TimeType()), LocalTime.MIDNIGHT)
checkEvaluation(Literal.default(CalendarIntervalType), new CalendarInterval(0, 0, 0L))
checkEvaluation(Literal.default(YearMonthIntervalType()), 0)
checkEvaluation(Literal.default(DayTimeIntervalType()), 0L)
Expand Down Expand Up @@ -313,6 +316,13 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
}
}

test("construct literals from arrays of java.time.LocalTime") {
val localTime0 = LocalTime.of(1, 2, 3)
checkEvaluation(Literal(Array(localTime0)), Array(localTime0))
val localTime1 = LocalTime.of(23, 59, 59, 999999000)
checkEvaluation(Literal(Array(localTime0, localTime1)), Array(localTime0, localTime1))
}

test("construct literals from java.time.Instant") {
Seq(
Instant.parse("0001-01-01T00:00:00Z"),
Expand Down Expand Up @@ -497,6 +507,11 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
}
checkEvaluation(Literal.create(duration, dt), result)
}

val time = LocalTime.of(12, 13, 14)
DataTypeTestUtils.timeTypes.foreach { tt =>
checkEvaluation(Literal.create(time, tt), localTime(12, 13, 14))
}
}

test("SPARK-37967: Literal.create support ObjectType") {
Expand Down Expand Up @@ -531,4 +546,17 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Literal.create(immArraySeq), expected)
checkEvaluation(Literal.create(immArraySeq, ArrayType(DoubleType)), expected)
}

test("TimeType toString and sql") {
Seq(
Literal.default(TimeType()) -> "00:00:00",
Literal(LocalTime.NOON) -> "12:00:00",
Literal(LocalTime.of(23, 59, 59, 100 * 1000 * 1000)) -> "23:59:59.1",
Literal(LocalTime.of(23, 59, 59, 10000)) -> "23:59:59.00001",
Literal(LocalTime.of(23, 59, 59, 999999000)) -> "23:59:59.999999"
).foreach { case (lit, str) =>
assert(lit.toString === str)
assert(lit.sql === s"TIME '$str'")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CreateViewCommand `time_view`, select '11:53:26.038344' time_str, 'HH:mm:ss.SSSS
-- !query
select time '16:39:45\t'
-- !query analysis
Project [59985000000 AS 59985000000#x]
Project [16:39:45 AS TIME '16:39:45'#x]
+- OneRowRelation


Expand Down
2 changes: 1 addition & 1 deletion sql/core/src/test/resources/sql-tests/results/time.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct<>
-- !query
select time '16:39:45\t'
-- !query schema
struct<59985000000:time(6)>
struct<TIME '16:39:45':time(6)>
-- !query output
16:39:45

Expand Down