yaooqinn commented on a change in pull request #28650:
URL: https://github.com/apache/spark/pull/28650#discussion_r431941232
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala
##########
@@ -1053,93 +1018,34 @@ case class FromUnixTime(sec: Expression, format:
Expression, timeZoneId: Option[
override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression =
copy(timeZoneId = Option(timeZoneId))
- private lazy val constFormat: UTF8String =
right.eval().asInstanceOf[UTF8String]
- private lazy val formatter: TimestampFormatter =
- try {
- TimestampFormatter(
- constFormat.toString,
- zoneId,
- legacyFormat = SIMPLE_DATE_FORMAT,
- needVarLengthSecondFraction = false)
- } catch {
- case e: SparkUpgradeException => throw e
- case NonFatal(_) => null
- }
-
- override def eval(input: InternalRow): Any = {
- val time = left.eval(input)
- if (time == null) {
- null
- } else {
- if (format.foldable) {
- if (constFormat == null || formatter == null) {
- null
- } else {
- try {
- UTF8String.fromString(formatter.format(time.asInstanceOf[Long] *
MICROS_PER_SECOND))
- } catch {
- case e: SparkUpgradeException => throw e
- case NonFatal(_) => null
- }
- }
- } else {
- val f = format.eval(input)
- if (f == null) {
- null
- } else {
- try {
- UTF8String.fromString(
- TimestampFormatter(
- f.toString,
- zoneId,
- legacyFormat = SIMPLE_DATE_FORMAT,
- needVarLengthSecondFraction = false)
- .format(time.asInstanceOf[Long] * MICROS_PER_SECOND))
- } catch {
- case e: SparkUpgradeException => throw e
- case NonFatal(_) => null
- }
- }
- }
- }
+ override def nullSafeEval(seconds: Any, format: Any): Any = {
+ val fmt = formatterOption.getOrElse(getFormatter(format.toString))
+ UTF8String.fromString(fmt.format(seconds.asInstanceOf[Long] *
MICROS_PER_SECOND))
}
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
- val df = classOf[TimestampFormatter].getName
- if (format.foldable) {
- if (formatter == null) {
- ExprCode.forNullValue(StringType)
- } else {
- val formatterName = ctx.addReferenceObj("formatter", formatter, df)
- val t = left.genCode(ctx)
- ev.copy(code = code"""
- ${t.code}
- boolean ${ev.isNull} = ${t.isNull};
- ${CodeGenerator.javaType(dataType)} ${ev.value} =
${CodeGenerator.defaultValue(dataType)};
- if (!${ev.isNull}) {
- try {
- ${ev.value} =
UTF8String.fromString($formatterName.format(${t.value} * 1000000L));
- } catch (java.lang.IllegalArgumentException e) {
- ${ev.isNull} = true;
- }
- }""")
- }
- } else {
- val zid = ctx.addReferenceObj("zoneId", zoneId, classOf[ZoneId].getName)
+ formatterOption.map { f =>
+ val formatterName = ctx.addReferenceObj("formatter", f)
+ defineCodeGen(ctx, ev, (seconds, _) =>
Review comment:
```scala
/**
* Short hand for generating binary evaluation code.
* If either of the sub-expressions is null, the result of this computation
* is assumed to be null.
*
* @param f accepts two variable names and returns Java code to compute
the output.
*/
protected def defineCodeGen(
ctx: CodegenContext,
ev: ExprCode,
f: (String, String) => String): ExprCode = {
nullSafeCodeGen(ctx, ev, (eval1, eval2) => {
s"${ev.value} = ${f(eval1, eval2)};"
})
}
```
it is just a wrapper for `nullSafeCodeGen`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]