Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/20024#discussion_r159646388
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
---
@@ -608,6 +671,17 @@ case class Cast(child: Expression, dataType: DataType,
timeZoneId: Option[String
val tz = ctx.addReferenceObj("timeZone", timeZone)
(c, evPrim, evNull) => s"""$evPrim = UTF8String.fromString(
org.apache.spark.sql.catalyst.util.DateTimeUtils.timestampToString($c, $tz));"""
+ case ar: ArrayType =>
+ (c, evPrim, evNull) => {
+ val bufferTerm = ctx.freshName("bufferTerm")
+ val bufferClass = classOf[UTF8StringBuilder].getName
+ val writeArrayToBuffer = codegenWriteArrayToBuffer(ar, ctx)
+ s"""
+ |$bufferClass $bufferTerm = new $bufferClass();
+ |$writeArrayToBuffer($c, $bufferTerm);
+ |$evPrim = $bufferTerm.toUTF8String();
+ """.stripMargin
--- End diff --
We can simplify this too
```
val elementToStringCode = castToStringCode(et, ctx)
val funcName = ctx.freshName("elementToString")
val elementToStringFunc = ctx.addNewFunction(funcName,
s"""
private UTF8String $funcName(${ctx.dataType(et)} element) {
UTF8String elementStr = null;
${elementToStringCode("element", "elementStr", null /* resultIsNull
won't be touched */)}
return elementStr;
}
""")
...
$bufferClass $bufferTerm = new $bufferClass();
$bufferTerm.append("[");
if ($c.numElements > 0) {
if (!$c.isNullAt(0)) {
$buffer.append($elementToStringFunc(${ctx.getValue(array, et, "0")}))
}
for (int $loopIndex = 1; $loopIndex < $arTerm.numElements();
$loopIndex++) ...
}
```
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]