cloud-fan commented on a change in pull request #32365:
URL: https://github.com/apache/spark/pull/32365#discussion_r621916208



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
##########
@@ -2649,3 +2652,84 @@ case class Sentences(
     copy(str = newFirst, language = newSecond, country = newThird)
 
 }
+
+case class ToPrettyString(expr: Expression, timeZoneId: Option[String] = None)
+  extends UnaryExpression with ImplicitCastInputTypes with 
TimeZoneAwareExpression{
+  import ToPrettyString._
+
+  require(children.nonEmpty, s"$prettyName() should take at least 1 argument")
+  override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression =
+    copy(timeZoneId = Option(timeZoneId))
+  override def child: Expression = expr
+  override def foldable: Boolean = child.foldable
+  override def nullable: Boolean = child.nullable
+  override def dataType: DataType = StringType
+  override def inputTypes: Seq[AbstractDataType] = Seq(AnyDataType)
+
+  private val timeFormatters: TimeFormatters =
+    TimeFormatters(DateFormatter(zoneId), 
TimestampFormatter.getFractionFormatter(zoneId))
+
+  override def nullSafeEval(input: Any): Any = {
+    UTF8String.fromString(toHiveString((input, expr.dataType), false, 
timeFormatters))
+  }
+
+  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    nullSafeCodeGen(ctx, ev, eval => {
+      val toHiveString = ToPrettyString.getClass.getName.stripSuffix("$")
+      val tuple2 = Tuple2.getClass.getName.stripSuffix("$")
+      val dataType = JavaCode.global(
+        ctx.addReferenceObj("dataType", expr.dataType),
+        expr.dataType.getClass)
+      val formatter = JavaCode.global(
+        ctx.addReferenceObj("dateFormatter", timeFormatters),
+        timeFormatters.getClass)
+      s"""${ev.value} = UTF8String.fromString($toHiveString.toHiveString(
+         |$tuple2.apply($eval, ${dataType}), false, 
$formatter));""".stripMargin
+    })
+  }
+
+  override def prettyName: String = getTagValue(
+    FunctionRegistry.FUNC_ALIAS).getOrElse("to_hive_string")
+
+  override protected def withNewChildInternal(newChild: Expression): 
Expression =
+    ToPrettyString(newChild)
+}
+
+object ToPrettyString {
+  case class TimeFormatters(date: DateFormatter, timestamp: TimestampFormatter)
+
+  def toHiveString(
+    a: (Any, DataType),

Review comment:
       nit: 4 spaces indentation




-- 
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]

Reply via email to