Skip to content

Commit

Permalink
trim() the string when cast stringToTimestamp and stringToDate
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyum committed Nov 5, 2018
1 parent c71db43 commit d297817
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
// TimestampConverter
private[this] def castToTimestamp(from: DataType): Any => Any = from match {
case StringType =>
buildCast[UTF8String](_, utfs => DateTimeUtils.stringToTimestamp(utfs, timeZone).orNull)
buildCast[UTF8String](_, s => DateTimeUtils.stringToTimestamp(s.trim(), timeZone).orNull)
case BooleanType =>
buildCast[Boolean](_, b => if (b) 1L else 0)
case LongType =>
Expand Down Expand Up @@ -402,7 +402,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
// DateConverter
private[this] def castToDate(from: DataType): Any => Any = from match {
case StringType =>
buildCast[UTF8String](_, s => DateTimeUtils.stringToDate(s).orNull)
buildCast[UTF8String](_, s => DateTimeUtils.stringToDate(s.trim()).orNull)
case TimestampType =>
// throw valid precision more than seconds, according to Hive.
// Timestamp.nanos is in 0 to 999,999,999, no more than a second.
Expand Down Expand Up @@ -907,7 +907,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
val intOpt = ctx.freshVariable("intOpt", classOf[Option[Integer]])
(c, evPrim, evNull) => code"""
scala.Option<Integer> $intOpt =
org.apache.spark.sql.catalyst.util.DateTimeUtils.stringToDate($c);
org.apache.spark.sql.catalyst.util.DateTimeUtils.stringToDate($c.trim());
if ($intOpt.isDefined()) {
$evPrim = ((Integer) $intOpt.get()).intValue();
} else {
Expand Down Expand Up @@ -1010,7 +1010,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
(c, evPrim, evNull) =>
code"""
scala.Option<Long> $longOpt =
org.apache.spark.sql.catalyst.util.DateTimeUtils.stringToTimestamp($c, $tz);
org.apache.spark.sql.catalyst.util.DateTimeUtils.stringToTimestamp($c.trim(), $tz);
if ($longOpt.isDefined()) {
$evPrim = ((Long) $longOpt.get()).longValue();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
c = Calendar.getInstance()
c.set(2015, 2, 18, 0, 0, 0)
c.set(Calendar.MILLISECOND, 0)
checkEvaluation(Cast(Literal("2015-03-18"), DateType), new Date(c.getTimeInMillis))
checkEvaluation(Cast(Literal("2015-03-18 "), DateType), new Date(c.getTimeInMillis))

Seq("2015-03-18", " 2015-03-18", "2015-03-18 ", " 2015-03-18 ").foreach { s =>
checkEvaluation(Cast(Literal(s), DateType), new Date(c.getTimeInMillis))
}
checkEvaluation(Cast(Literal("2015-03-18 123142"), DateType), new Date(c.getTimeInMillis))
checkEvaluation(Cast(Literal("2015-03-18T123123"), DateType), new Date(c.getTimeInMillis))
checkEvaluation(Cast(Literal("2015-03-18T"), DateType), new Date(c.getTimeInMillis))
Expand Down Expand Up @@ -130,9 +132,9 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
c = Calendar.getInstance(tz)
c.set(2015, 2, 18, 0, 0, 0)
c.set(Calendar.MILLISECOND, 0)
checkCastStringToTimestamp("2015-03-18", new Timestamp(c.getTimeInMillis))
checkCastStringToTimestamp("2015-03-18 ", new Timestamp(c.getTimeInMillis))
checkCastStringToTimestamp("2015-03-18T", new Timestamp(c.getTimeInMillis))
Seq("2015-03-18", " 2015-03-18", "2015-03-18 ", " 2015-03-18 ", "2015-03-18T").foreach { s =>
checkCastStringToTimestamp(s, new Timestamp(c.getTimeInMillis))
}

c = Calendar.getInstance(tz)
c.set(2015, 2, 18, 12, 3, 17)
Expand Down

0 comments on commit d297817

Please sign in to comment.