Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
c48a70d
Prepare for session local timezone support.
ueshin Dec 6, 2016
1d21fec
Make Cast TimeZoneAwareExpression.
ueshin Dec 6, 2016
0763c8f
Fix DateTimeUtilsSuite to follow changes.
ueshin Dec 6, 2016
449d93d
Make some datetime expressions TimeZoneAwareExpression.
ueshin Dec 8, 2016
b59d902
Fix compiler error in sql/core.
ueshin Dec 8, 2016
3ddfae4
Add constructors without zoneId to TimeZoneAwareExpressions for Funct…
ueshin Dec 9, 2016
f58f00d
Add DateTimeUtils.threadLocalLocalTimeZone to partition-reltated Cast.
ueshin Dec 13, 2016
8f2040b
Fix timezone for Hive timestamp string.
ueshin Dec 13, 2016
63c103c
Use defaultTimeZone instead of threadLocalLocalTimeZone.
ueshin Dec 13, 2016
7066850
Add TimeZone to DateFormats.
ueshin Dec 13, 2016
1aaca29
Make `CurrentBatchTimestamp` `TimeZoneAwareExpression`.
ueshin Dec 14, 2016
e5bb246
Add tests for date functions with session local timezone.
ueshin Dec 14, 2016
32cc391
Remove unused import and small cleanup.
ueshin Dec 16, 2016
f434378
Fix tests.
ueshin Dec 16, 2016
16fd1e4
Rename `zoneId` to `timeZoneId`.
ueshin Dec 19, 2016
009c17b
Use lazy val to avoid to keep creating a new timezone object (or doin…
ueshin Dec 19, 2016
a2936ed
Modify ComputeCurrentTime to hold the same date.
ueshin Dec 19, 2016
c5ca73e
Add comments.
ueshin Dec 19, 2016
b860379
Fix `Cast.needTimeZone()` to handle complex types.
ueshin Dec 19, 2016
6746265
Fix `Dataset.showString()` to use session local timezone.
ueshin Dec 19, 2016
4b6900c
Merge branch 'master' into issues/SPARK-18350
ueshin Dec 20, 2016
4f9cc40
Modify to analyze `ResolveTimeZone` only once.
ueshin Dec 24, 2016
2ca2413
Use session local timezone for Hive string.
ueshin Dec 24, 2016
c232854
Merge branch 'master' into issues/SPARK-18350
ueshin Dec 26, 2016
5b6dd4f
Merge branch 'master' into issues/SPARK-18350
ueshin Jan 5, 2017
1ca5808
Use `addReferenceMinorObj` to avoid adding member variables.
ueshin Jan 10, 2017
702dd81
Use Option[String] for timeZoneId.
ueshin Jan 10, 2017
33a3425
Update a comment.
ueshin Jan 10, 2017
5cc93e3
Fix overloaded constructors.
ueshin Jan 11, 2017
5521165
Fix session local timezone for timezone sensitive tests.
ueshin Jan 11, 2017
bd8275e
Remove `timeZoneResolved` and use `timeZoneId.isEmpty` instead in `Re…
ueshin Jan 11, 2017
183945c
Merge branch 'master' into issues/SPARK-18350
ueshin Jan 14, 2017
22a3b6e
Remove unused parameter.
ueshin Jan 16, 2017
30d51fa
Merge branch 'master' into issues/SPARK-18350
ueshin Jan 16, 2017
043ab52
Use Cast directly instead of dsl.
ueshin Jan 16, 2017
3ba5830
Merge branch 'master' into issues/SPARK-18350
ueshin Jan 22, 2017
9ab31f0
Revert unnecessary changes.
ueshin Jan 22, 2017
b954947
Use `@` binding to simplify pattern match.
ueshin Jan 22, 2017
dbb2604
Inline a `lazy val`.
ueshin Jan 22, 2017
186cd3e
Add some TODO comments for follow-up prs.
ueshin Jan 22, 2017
6631a69
Add a config document.
ueshin Jan 22, 2017
3610465
Use an overload version of `checkAnswer`.
ueshin Jan 22, 2017
c12e596
Fix CastSuite and add some comments to describe the tests.
ueshin Jan 22, 2017
8a04e80
Use None instead of null.
ueshin Jan 22, 2017
efe3aff
Add some comments to describe the tests.
ueshin Jan 22, 2017
cdbb266
Make TimeAdd/TimeSub/MonthsBetween TimeZoneAwareExpression.
ueshin Jan 22, 2017
328399a
Add comments to explain tests.
ueshin Jan 23, 2017
7352612
Modify a test.
ueshin Jan 23, 2017
b99cf79
Refine tests.
ueshin Jan 25, 2017
a85377f
Remove unnecessary new lines.
ueshin Jan 26, 2017
f0c911b
Add newDateFormat to DateTimeUtils and use it.
ueshin Jan 26, 2017
6fa1d6a
Parameterize some tests.
ueshin Jan 26, 2017
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
Prev Previous commit
Next Next commit
Add newDateFormat to DateTimeUtils and use it.
  • Loading branch information
ueshin committed Jan 26, 2017
commit f0c911b27eea4138171ebdab7001bc7406f3f41d
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
package org.apache.spark.sql.catalyst.expressions

import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.{Calendar, Locale, TimeZone}
import java.text.DateFormat
import java.util.{Calendar, TimeZone}

import scala.util.Try
import scala.util.control.NonFatal

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodegenFallback, ExprCode}
Expand Down Expand Up @@ -464,21 +464,16 @@ case class DateFormatClass(left: Expression, right: Expression, timeZoneId: Opti
copy(timeZoneId = Option(timeZoneId))

override protected def nullSafeEval(timestamp: Any, format: Any): Any = {
val sdf = new SimpleDateFormat(format.toString, Locale.US)
sdf.setTimeZone(timeZone)
UTF8String.fromString(sdf.format(new java.util.Date(timestamp.asInstanceOf[Long] / 1000)))
val df = DateTimeUtils.newDateFormat(format.toString, timeZone)
UTF8String.fromString(df.format(new java.util.Date(timestamp.asInstanceOf[Long] / 1000)))
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val sdf = classOf[SimpleDateFormat].getName
nullSafeCodeGen(ctx, ev, (timestamp, format) => {
val tz = ctx.addReferenceMinorObj(timeZone)
val s = ctx.freshName("sdf")
s"""
$sdf $s = new $sdf($format.toString(), java.util.Locale.US);
$s.setTimeZone($tz);
${ev.value} = UTF8String.fromString($s.format(new java.util.Date($timestamp / 1000)));
"""
val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
val tz = ctx.addReferenceMinorObj(timeZone)
defineCodeGen(ctx, ev, (timestamp, format) => {
s"""UTF8String.fromString($dtu.newDateFormat($format.toString(), $tz)
.format(new java.util.Date($timestamp / 1000)))"""
})
}

Expand Down Expand Up @@ -568,12 +563,12 @@ abstract class UnixTime
override def nullable: Boolean = true

private lazy val constFormat: UTF8String = right.eval().asInstanceOf[UTF8String]
private lazy val formatter: SimpleDateFormat =
Try {
val sdf = new SimpleDateFormat(constFormat.toString, Locale.US)
sdf.setTimeZone(timeZone)
sdf
}.getOrElse(null)
private lazy val formatter: DateFormat =
try {
DateTimeUtils.newDateFormat(constFormat.toString, timeZone)
} catch {
case NonFatal(_) => null
}

override def eval(input: InternalRow): Any = {
val t = left.eval(input)
Expand All @@ -589,20 +584,25 @@ abstract class UnixTime
if (constFormat == null || formatter == null) {
null
} else {
Try(formatter.parse(
t.asInstanceOf[UTF8String].toString).getTime / 1000L).getOrElse(null)
try {
formatter.parse(
t.asInstanceOf[UTF8String].toString).getTime / 1000L
} catch {
case NonFatal(_) => null
}
}
case StringType =>
val f = right.eval(input)
if (f == null) {
null
} else {
val formatString = f.asInstanceOf[UTF8String].toString
Try {
val sdf = new SimpleDateFormat(formatString, Locale.US)
sdf.setTimeZone(timeZone)
sdf.parse(t.asInstanceOf[UTF8String].toString).getTime / 1000L
}.getOrElse(null)
try {
DateTimeUtils.newDateFormat(formatString, timeZone).parse(
t.asInstanceOf[UTF8String].toString).getTime / 1000L
} catch {
case NonFatal(_) => null
}
}
}
}
Expand All @@ -611,11 +611,11 @@ abstract class UnixTime
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
left.dataType match {
case StringType if right.foldable =>
val sdf = classOf[SimpleDateFormat].getName
val df = classOf[DateFormat].getName
if (formatter == null) {
ExprCode("", "true", ctx.defaultValue(dataType))
} else {
val formatterName = ctx.addReferenceObj("formatter", formatter, sdf)
val formatterName = ctx.addReferenceObj("formatter", formatter, df)
val eval1 = left.genCode(ctx)
ev.copy(code = s"""
${eval1.code}
Expand All @@ -631,14 +631,12 @@ abstract class UnixTime
}
case StringType =>
val tz = ctx.addReferenceMinorObj(timeZone)
val sdf = classOf[SimpleDateFormat].getName
val formatterName = ctx.freshName("formatter")
val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
nullSafeCodeGen(ctx, ev, (string, format) => {
s"""
try {
$sdf $formatterName = new $sdf($format.toString(), java.util.Locale.US);
$formatterName.setTimeZone($tz);
${ev.value} = $formatterName.parse($string.toString()).getTime() / 1000L;
${ev.value} = $dtu.newDateFormat($format.toString(), $tz)
.parse($string.toString()).getTime() / 1000L;
} catch (java.lang.IllegalArgumentException e) {
${ev.isNull} = true;
} catch (java.text.ParseException e) {
Expand Down Expand Up @@ -706,12 +704,12 @@ case class FromUnixTime(sec: Expression, format: Expression, timeZoneId: Option[
copy(timeZoneId = Option(timeZoneId))

private lazy val constFormat: UTF8String = right.eval().asInstanceOf[UTF8String]
private lazy val formatter: SimpleDateFormat =
Try {
val sdf = new SimpleDateFormat(constFormat.toString, Locale.US)
sdf.setTimeZone(timeZone)
sdf
}.getOrElse(null)
private lazy val formatter: DateFormat =
try {
DateTimeUtils.newDateFormat(constFormat.toString, timeZone)
} catch {
case NonFatal(_) => null
}

override def eval(input: InternalRow): Any = {
val time = left.eval(input)
Expand All @@ -722,31 +720,36 @@ case class FromUnixTime(sec: Expression, format: Expression, timeZoneId: Option[
if (constFormat == null || formatter == null) {
null
} else {
Try(UTF8String.fromString(formatter.format(
new java.util.Date(time.asInstanceOf[Long] * 1000L)))).getOrElse(null)
try {
UTF8String.fromString(formatter.format(
new java.util.Date(time.asInstanceOf[Long] * 1000L)))
} catch {
case NonFatal(_) => null
}
}
} else {
val f = format.eval(input)
if (f == null) {
null
} else {
Try {
val sdf = new SimpleDateFormat(f.toString, Locale.US)
sdf.setTimeZone(timeZone)
UTF8String.fromString(sdf.format(new java.util.Date(time.asInstanceOf[Long] * 1000L)))
}.getOrElse(null)
try {
UTF8String.fromString(DateTimeUtils.newDateFormat(f.toString, timeZone)
.format(new java.util.Date(time.asInstanceOf[Long] * 1000L)))
} catch {
case NonFatal(_) => null
}
}
}
}
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val sdf = classOf[SimpleDateFormat].getName
val df = classOf[DateFormat].getName
if (format.foldable) {
if (formatter == null) {
ExprCode("", "true", "(UTF8String) null")
} else {
val formatterName = ctx.addReferenceObj("formatter", formatter, sdf)
val formatterName = ctx.addReferenceObj("formatter", formatter, df)
val t = left.genCode(ctx)
ev.copy(code = s"""
${t.code}
Expand All @@ -763,13 +766,11 @@ case class FromUnixTime(sec: Expression, format: Expression, timeZoneId: Option[
}
} else {
val tz = ctx.addReferenceMinorObj(timeZone)
val formatterName = ctx.freshName("formatter")
val dtu = DateTimeUtils.getClass.getName.stripSuffix("$")
nullSafeCodeGen(ctx, ev, (seconds, f) => {
s"""
try {
$sdf $formatterName = new $sdf($f.toString(), java.util.Locale.US);
$formatterName.setTimeZone($tz);
${ev.value} = UTF8String.fromString($formatterName.format(
${ev.value} = UTF8String.fromString($dtu.newDateFormat($f.toString(), $tz).format(
new java.util.Date($seconds * 1000L)));
} catch (java.lang.IllegalArgumentException e) {
${ev.isNull} = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ object DateTimeUtils {
sdf
}

def newDateFormat(formatString: String, timeZone: TimeZone): DateFormat = {
val sdf = new SimpleDateFormat(formatString, Locale.US)
sdf.setTimeZone(timeZone)
sdf
}

// we should use the exact day as Int, for example, (year, month, day) -> day
def millisToDays(millisUtc: Long): SQLDate = {
Copy link
Contributor

Choose a reason for hiding this comment

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

now these methods without timezone parameter are only used in tests right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think so except for JSON/CSV datasources or partition value related codes, which will be fixed by the follow-up prs.

millisToDays(millisUtc, defaultTimeZone())
Expand Down