HyukjinKwon commented on code in PR #42462:
URL: https://github.com/apache/spark/pull/42462#discussion_r1300847390


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/xml/CreateXmlParser.scala:
##########
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.xml
+
+import java.io.{ByteArrayInputStream, InputStream, InputStreamReader, 
StringReader}
+import java.nio.channels.Channels
+import java.nio.charset.{Charset, StandardCharsets}
+import javax.xml.stream.{EventFilter, XMLEventReader, XMLInputFactory, 
XMLStreamConstants}
+import javax.xml.stream.events.XMLEvent
+
+import org.apache.hadoop.io.Text
+import sun.nio.cs.StreamDecoder
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.unsafe.types.UTF8String
+
+private[sql] object CreateXmlParser extends Serializable {
+  val filter = new EventFilter {
+    override def accept(event: XMLEvent): Boolean =
+    // Ignore comments and processing instructions
+      event.getEventType match {
+        case XMLStreamConstants.COMMENT | 
XMLStreamConstants.PROCESSING_INSTRUCTION => false
+        case _ => true
+      }
+  }
+
+  def string(xmlInputFactory: XMLInputFactory, record: String): XMLEventReader 
= {
+    // It does not have to skip for white space, since `XmlInputFormat`
+    // always finds the root tag without a heading space.
+    val eventReader = xmlInputFactory.createXMLEventReader(new 
StringReader(record))
+    xmlInputFactory.createFilteredReader(eventReader, filter)
+  }
+
+  def utf8String(xmlInputFactory: XMLInputFactory, record: UTF8String): 
XMLEventReader = {
+    val bb = record.getByteBuffer
+    assert(bb.hasArray)
+
+    val bain = new ByteArrayInputStream(
+      bb.array(), bb.arrayOffset() + bb.position(), bb.remaining())
+
+    val eventReader = xmlInputFactory.createXMLEventReader(
+      new InputStreamReader(bain, StandardCharsets.UTF_8))
+    xmlInputFactory.createFilteredReader(eventReader, filter)
+  }
+
+  def text(xmlInputFactory: XMLInputFactory, record: Text): XMLEventReader = {
+    val bs = new ByteArrayInputStream(record.getBytes, 0, record.getLength)
+
+    val eventReader = xmlInputFactory.createXMLEventReader(bs)
+    xmlInputFactory.createFilteredReader(eventReader, filter)
+  }
+
+  // Jackson parsers can be ranked according to their performance:

Review Comment:
   Let's also update the docs



-- 
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: [email protected]

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