Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about changing stringToDate and stringToTimestamp instead?
Those functions are used only in Cast and they already handle nullcases, too.
I didn't look at the detail of this PR. The change looks a little less robust when s is null.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about change stringToDate to trimStringToDate and update trimStringToDate to:
image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ur, I'd like not to rename it. One line function document will suffice.

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