Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-45562][SQL] XML: Make 'rowTag' a required option #43389

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -63,8 +63,8 @@ private[sql] class XmlOptions(
}

val compressionCodec = parameters.get(COMPRESSION).map(CompressionCodecs.getCodecClassName)
val rowTag = parameters.getOrElse(ROW_TAG, XmlOptions.DEFAULT_ROW_TAG)
require(rowTag.nonEmpty, s"'$ROW_TAG' option should not be empty string.")
val rowTag = parameters.getOrElse(ROW_TAG, XmlOptions.DEFAULT_ROW_TAG).trim
require(rowTag.nonEmpty, s"'$ROW_TAG' option should not be an empty string.")
require(!rowTag.startsWith("<") && !rowTag.endsWith(">"),
s"'$ROW_TAG' should not include angle brackets")
val rootTag = parameters.getOrElse(ROOT_TAG, XmlOptions.DEFAULT_ROOT_TAG)
Original file line number Diff line number Diff line change
@@ -42,6 +42,8 @@ class XmlFileFormat extends TextBasedFileFormat with DataSourceRegister {
def getXmlOptions(
sparkSession: SparkSession,
parameters: Map[String, String]): XmlOptions = {
val rowTagOpt = parameters.get(XmlOptions.ROW_TAG)
require(rowTagOpt.isDefined, s"'${XmlOptions.ROW_TAG}' option is required.")
new XmlOptions(parameters,
sparkSession.sessionState.conf.sessionLocalTimeZone,
sparkSession.sessionState.conf.columnNameOfCorruptRecord)
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ private Path getEmptyTempDir() throws IOException {
public void testXmlParser() {
Map<String, String> options = new HashMap<>();
options.put("rowTag", booksFileTag);
Dataset<Row> df = spark.read().options(options).format("xml").load(booksFile);
Dataset<Row> df = spark.read().options(options).xml(booksFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to shortened version for better code readability.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this change is not related to the theme, it is not recommended to modify it. But it does simplify the code a bit and is also OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

String prefix = XmlOptions.DEFAULT_ATTRIBUTE_PREFIX();
long result = df.select(prefix + "id").count();
Assertions.assertEquals(result, numBooks);
@@ -92,7 +92,7 @@ public void testXmlParser() {
public void testLoad() {
Map<String, String> options = new HashMap<>();
options.put("rowTag", booksFileTag);
Dataset<Row> df = spark.read().options(options).format("xml").load(booksFile);
Dataset<Row> df = spark.read().options(options).xml(booksFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.

long result = df.select("description").count();
Assertions.assertEquals(result, numBooks);
}
@@ -103,10 +103,10 @@ public void testSave() throws IOException {
options.put("rowTag", booksFileTag);
Path booksPath = getEmptyTempDir().resolve("booksFile");

Dataset<Row> df = spark.read().options(options).format("xml").load(booksFile);
df.select("price", "description").write().format("xml").save(booksPath.toString());
Dataset<Row> df = spark.read().options(options).xml(booksFile);
df.select("price", "description").write().options(options).xml(booksPath.toString());

Dataset<Row> newDf = spark.read().format("xml").load(booksPath.toString());
Dataset<Row> newDf = spark.read().options(options).xml(booksPath.toString());
long result = newDf.select("price").count();
Assertions.assertEquals(result, numBooks);
}
Loading