yaooqinn commented on code in PR #46591: URL: https://github.com/apache/spark/pull/46591#discussion_r1602584809
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/xmlExpressions.scala: ########## @@ -298,40 +294,40 @@ case class StructsToXml( } @transient - lazy val writer = new CharArrayWriter() - - @transient - lazy val inputSchema: StructType = child.dataType.asInstanceOf[StructType] - - @transient - lazy val gen = new StaxXmlGenerator( - inputSchema, writer, new XmlOptions(options, timeZoneId.get), false) - - // This converts rows to the XML output according to the given schema. - @transient - lazy val converter: Any => UTF8String = { - def getAndReset(): UTF8String = { - gen.flush() - val xmlString = writer.toString - writer.reset() - UTF8String.fromString(xmlString) - } - (row: Any) => - gen.write(row.asInstanceOf[InternalRow]) - getAndReset() - } + private lazy val evaluator = new StructsToXmlEvaluator( + child.dataType.asInstanceOf[StructType], options, timeZoneId) override def dataType: DataType = SQLConf.get.defaultStringType override def withTimeZone(timeZoneId: String): TimeZoneAwareExpression = copy(timeZoneId = Option(timeZoneId)) - override def nullSafeEval(value: Any): Any = converter(value) + override def nullSafeEval(value: Any): Any = { + UTF8String.fromString(evaluator.evaluate(value)) + } override def inputTypes: Seq[AbstractDataType] = StructType :: Nil override def prettyName: String = "to_xml" override protected def withNewChildInternal(newChild: Expression): StructsToXml = copy(child = newChild) + + override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { Review Comment: Simply ```scala override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { val expr = ctx.addReferenceObj("this", this) defineCodeGen(ctx, ev, input => s"(UTF8String) $expr.nullSafeEval($input)") } ``` -- 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. To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org