-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
from_xml and schema_for_xml support (#429)
This is a continuation of #334 This implements a `from_xml` expression that can turn an XML string column into a parsed structured column. It also opens up `inferSchema` for a `Dataset[String]` as a necessary support function. The rest is really mild refactoring.
- Loading branch information
1 parent
d155b1b
commit ef3af6a
Showing
11 changed files
with
341 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/scala/com/databricks/spark/xml/XmlDataToCatalyst.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2019 Databricks | ||
* | ||
* Licensed 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 com.databricks.spark.xml | ||
|
||
import org.apache.spark.sql.catalyst.CatalystTypeConverters | ||
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback | ||
import org.apache.spark.sql.catalyst.expressions.{ExpectsInputTypes, Expression, UnaryExpression} | ||
import org.apache.spark.sql.catalyst.util.GenericArrayData | ||
import org.apache.spark.sql.types._ | ||
import org.apache.spark.unsafe.types.UTF8String | ||
|
||
import com.databricks.spark.xml.parsers.StaxXmlParser | ||
|
||
case class XmlDataToCatalyst( | ||
child: Expression, | ||
schema: DataType, | ||
options: XmlOptions) | ||
extends UnaryExpression with CodegenFallback with ExpectsInputTypes { | ||
|
||
override lazy val dataType: DataType = schema | ||
|
||
@transient | ||
lazy val rowSchema: StructType = schema match { | ||
case st: StructType => st | ||
case ArrayType(st: StructType, _) => st | ||
} | ||
|
||
override def nullSafeEval(xml: Any): Any = xml match { | ||
case string: UTF8String => | ||
CatalystTypeConverters.convertToCatalyst( | ||
StaxXmlParser.parseColumn(string.toString, rowSchema, options)) | ||
case string: String => | ||
StaxXmlParser.parseColumn(string.toString, rowSchema, options) | ||
case arr: GenericArrayData => | ||
CatalystTypeConverters.convertToCatalyst( | ||
arr.array.map(s => StaxXmlParser.parseColumn(s.toString, rowSchema, options))) | ||
case arr: Array[_] => | ||
arr.map(s => StaxXmlParser.parseColumn(s.toString, rowSchema, options)) | ||
case _ => null | ||
} | ||
|
||
override def inputTypes: Seq[DataType] = schema match { | ||
case _: StructType => Seq(StringType) | ||
case ArrayType(_: StructType, _) => Seq(ArrayType(StringType)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2019 Databricks | ||
* | ||
* Licensed 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 com.databricks.spark.xml | ||
|
||
import org.apache.spark.annotation.Experimental | ||
import org.apache.spark.sql.Column | ||
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser | ||
import org.apache.spark.sql.types.DataType | ||
|
||
/** | ||
* Support functions for working with XML columns directly. | ||
*/ | ||
object functions { | ||
|
||
/** | ||
* Parses a column containing a XML string into a `StructType` with the specified schema. | ||
* | ||
* @param e a string column containing XML data | ||
* @param schema the schema to use when parsing the XML string. Must be a StructType if | ||
* column is string-valued, or ArrayType[StructType] if column is an array of strings | ||
* @param options key-value pairs that correspond to those supported by [[XmlOptions]] | ||
*/ | ||
@Experimental | ||
def from_xml(e: Column, schema: DataType, options: Map[String, String] = Map.empty): Column = { | ||
val expr = CatalystSqlParser.parseExpression(e.toString()) | ||
new Column(XmlDataToCatalyst(expr, schema, XmlOptions(options))) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.