[PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-07-30 Thread via GitHub


wayneguow opened a new pull request, #47539:
URL: https://github.com/apache/spark/pull/47539

   
   
   ### What changes were proposed in this pull request?
   
   This PR aims to Migrate XML to File Data Source V2.
   
   ### Why are the changes needed?
   
   Add v2 support for XML.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No.
   
   ### How was this patch tested?
   
   Pass GA and transform `XmlSuite`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   No.


-- 
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



Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-07-30 Thread via GitHub


wayneguow commented on PR #47539:
URL: https://github.com/apache/spark/pull/47539#issuecomment-2259733836

   cc @HyukjinKwon @cloud-fan  thanks.


-- 
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



Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-08-01 Thread via GitHub


HyukjinKwon commented on PR #47539:
URL: https://github.com/apache/spark/pull/47539#issuecomment-2264287176

   cc @sandip-db WDYT?


-- 
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



Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-08-02 Thread via GitHub


sandip-db commented on code in PR #47539:
URL: https://github.com/apache/spark/pull/47539#discussion_r1702209170


##
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/xml/XmlScan.scala:
##
@@ -0,0 +1,96 @@
+/*
+ * 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.execution.datasources.v2.xml
+
+import scala.jdk.CollectionConverters._
+
+import org.apache.hadoop.fs.Path
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{Expression, ExprUtils}
+import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap
+import org.apache.spark.sql.catalyst.xml.XmlOptions
+import org.apache.spark.sql.connector.read.PartitionReaderFactory
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.execution.datasources.PartitioningAwareFileIndex
+import org.apache.spark.sql.execution.datasources.v2.TextBasedFileScan
+import org.apache.spark.sql.execution.datasources.xml.XmlDataSource
+import org.apache.spark.sql.sources.Filter
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+import org.apache.spark.util.ArrayImplicits._
+import org.apache.spark.util.SerializableConfiguration
+
+case class XmlScan(
+sparkSession: SparkSession,
+fileIndex: PartitioningAwareFileIndex,
+dataSchema: StructType,
+readDataSchema: StructType,
+readPartitionSchema: StructType,
+options: CaseInsensitiveStringMap,
+pushedFilters: Array[Filter],
+partitionFilters: Seq[Expression] = Seq.empty,
+dataFilters: Seq[Expression] = Seq.empty)
+  extends TextBasedFileScan(sparkSession, options) {
+
+  private val xmlOptions = new XmlOptions(
+CaseInsensitiveMap(options.asScala.toMap),
+sparkSession.sessionState.conf.sessionLocalTimeZone,
+sparkSession.sessionState.conf.columnNameOfCorruptRecord,
+true)
+
+  override def isSplitable(path: Path): Boolean = {
+XmlDataSource(xmlOptions).isSplitable && super.isSplitable(path)
+  }
+
+  override def getFileUnSplittableReason(path: Path): String = {
+assert(!isSplitable(path))
+if (!super.isSplitable(path)) {
+  super.getFileUnSplittableReason(path)
+} else {
+  "the xml datasource is set multiLine mode"
+}
+  }
+
+  override def createReaderFactory(): PartitionReaderFactory = {
+// Check a field requirement for corrupt records here to throw an 
exception in a driver side
+ExprUtils.verifyColumnNameOfCorruptRecord(dataSchema, 
xmlOptions.columnNameOfCorruptRecord)
+
+if (readDataSchema.length == 1 &&
+  readDataSchema.head.name == xmlOptions.columnNameOfCorruptRecord) {
+  throw 
QueryCompilationErrors.queryFromRawFilesIncludeCorruptRecordColumnError()
+}
+val caseSensitiveMap = options.asCaseSensitiveMap.asScala.toMap
+// Hadoop Configurations are case sensitive.
+val hadoopConf = 
sparkSession.sessionState.newHadoopConfWithOptions(caseSensitiveMap)
+val broadcastedConf = sparkSession.sparkContext.broadcast(
+  new SerializableConfiguration(hadoopConf))
+// The partition values are already truncated in `FileScan.partitions`.
+// We should use `readPartitionSchema` as the partition schema here.
+XmlPartitionReaderFactory(sparkSession.sessionState.conf, broadcastedConf,
+  dataSchema, readDataSchema, readPartitionSchema, xmlOptions,
+  pushedFilters.toImmutableArraySeq)
+  }
+
+  override def equals(obj: Any): Boolean = obj match {
+case j: XmlScan => super.equals(j) && dataSchema == j.dataSchema && 
options == j.options &&
+  equivalentFilters(pushedFilters, j.pushedFilters)
+case _ => false
+  }
+
+  override def hashCode(): Int = super.hashCode()
+}

Review Comment:
   Any reason to not override `getMetaData` like json/csv?



##
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/xml/XmlScanBuilder.scala:
##
@@ -0,0 +1,44 @@
+/*
+ * 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 Ap

Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-08-02 Thread via GitHub


sandip-db commented on PR #47539:
URL: https://github.com/apache/spark/pull/47539#issuecomment-2266006029

   > cc @sandip-db WDYT?
   
   It looks ok to me. Although, I have not come across anyone asking for it.


-- 
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



Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-08-02 Thread via GitHub


wayneguow commented on code in PR #47539:
URL: https://github.com/apache/spark/pull/47539#discussion_r1702432022


##
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/xml/XmlScanBuilder.scala:
##
@@ -0,0 +1,44 @@
+/*
+ * 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.execution.datasources.v2.xml
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.execution.datasources.PartitioningAwareFileIndex
+import org.apache.spark.sql.execution.datasources.v2.FileScanBuilder
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+case class XmlScanBuilder(
+sparkSession: SparkSession,
+fileIndex: PartitioningAwareFileIndex,
+schema: StructType,
+dataSchema: StructType,
+options: CaseInsensitiveStringMap)
+  extends FileScanBuilder(sparkSession, fileIndex, dataSchema) {
+  override def build(): XmlScan = {
+XmlScan(
+  sparkSession,
+  fileIndex,
+  dataSchema,
+  readDataSchema(),
+  readPartitionSchema(),
+  options,
+  pushedDataFilters,
+  partitionFilters,
+  dataFilters)
+  }
+}

Review Comment:
   Because we currently do not support push down for xml, this other topic for 
`xml` and we can separate another pr. Therefore, we can use the default method 
of the parent class currently.



-- 
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



Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-08-02 Thread via GitHub


wayneguow commented on code in PR #47539:
URL: https://github.com/apache/spark/pull/47539#discussion_r1702432161


##
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/xml/XmlScan.scala:
##
@@ -0,0 +1,96 @@
+/*
+ * 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.execution.datasources.v2.xml
+
+import scala.jdk.CollectionConverters._
+
+import org.apache.hadoop.fs.Path
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.expressions.{Expression, ExprUtils}
+import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap
+import org.apache.spark.sql.catalyst.xml.XmlOptions
+import org.apache.spark.sql.connector.read.PartitionReaderFactory
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.execution.datasources.PartitioningAwareFileIndex
+import org.apache.spark.sql.execution.datasources.v2.TextBasedFileScan
+import org.apache.spark.sql.execution.datasources.xml.XmlDataSource
+import org.apache.spark.sql.sources.Filter
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+import org.apache.spark.util.ArrayImplicits._
+import org.apache.spark.util.SerializableConfiguration
+
+case class XmlScan(
+sparkSession: SparkSession,
+fileIndex: PartitioningAwareFileIndex,
+dataSchema: StructType,
+readDataSchema: StructType,
+readPartitionSchema: StructType,
+options: CaseInsensitiveStringMap,
+pushedFilters: Array[Filter],
+partitionFilters: Seq[Expression] = Seq.empty,
+dataFilters: Seq[Expression] = Seq.empty)
+  extends TextBasedFileScan(sparkSession, options) {
+
+  private val xmlOptions = new XmlOptions(
+CaseInsensitiveMap(options.asScala.toMap),
+sparkSession.sessionState.conf.sessionLocalTimeZone,
+sparkSession.sessionState.conf.columnNameOfCorruptRecord,
+true)
+
+  override def isSplitable(path: Path): Boolean = {
+XmlDataSource(xmlOptions).isSplitable && super.isSplitable(path)
+  }
+
+  override def getFileUnSplittableReason(path: Path): String = {
+assert(!isSplitable(path))
+if (!super.isSplitable(path)) {
+  super.getFileUnSplittableReason(path)
+} else {
+  "the xml datasource is set multiLine mode"
+}
+  }
+
+  override def createReaderFactory(): PartitionReaderFactory = {
+// Check a field requirement for corrupt records here to throw an 
exception in a driver side
+ExprUtils.verifyColumnNameOfCorruptRecord(dataSchema, 
xmlOptions.columnNameOfCorruptRecord)
+
+if (readDataSchema.length == 1 &&
+  readDataSchema.head.name == xmlOptions.columnNameOfCorruptRecord) {
+  throw 
QueryCompilationErrors.queryFromRawFilesIncludeCorruptRecordColumnError()
+}
+val caseSensitiveMap = options.asCaseSensitiveMap.asScala.toMap
+// Hadoop Configurations are case sensitive.
+val hadoopConf = 
sparkSession.sessionState.newHadoopConfWithOptions(caseSensitiveMap)
+val broadcastedConf = sparkSession.sparkContext.broadcast(
+  new SerializableConfiguration(hadoopConf))
+// The partition values are already truncated in `FileScan.partitions`.
+// We should use `readPartitionSchema` as the partition schema here.
+XmlPartitionReaderFactory(sparkSession.sessionState.conf, broadcastedConf,
+  dataSchema, readDataSchema, readPartitionSchema, xmlOptions,
+  pushedFilters.toImmutableArraySeq)
+  }
+
+  override def equals(obj: Any): Boolean = obj match {
+case j: XmlScan => super.equals(j) && dataSchema == j.dataSchema && 
options == j.options &&
+  equivalentFilters(pushedFilters, j.pushedFilters)
+case _ => false
+  }
+
+  override def hashCode(): Int = super.hashCode()
+}

Review Comment:
   Since there is no `PushedFilters`, just keep the default method of the 
parent class.



-- 
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



Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-08-02 Thread via GitHub


wayneguow commented on code in PR #47539:
URL: https://github.com/apache/spark/pull/47539#discussion_r1702432423


##
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/xml/XmlDataSourceV2.scala:
##
@@ -0,0 +1,47 @@
+/*
+ * 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.execution.datasources.v2.xml
+
+import org.apache.spark.sql.connector.catalog.Table
+import org.apache.spark.sql.execution.datasources._
+import org.apache.spark.sql.execution.datasources.v2._
+import org.apache.spark.sql.execution.datasources.xml.XmlFileFormat
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+class XmlDataSourceV2 extends FileDataSourceV2 {

Review Comment:
   Yes, I referred to `JSON` and other other data source implementations that 
already have v1 and v2. Except for the push down part, the left part should be 
consistent.



-- 
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



Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-08-02 Thread via GitHub


wayneguow commented on code in PR #47539:
URL: https://github.com/apache/spark/pull/47539#discussion_r1702549729


##
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSource.scala:
##
@@ -691,7 +691,7 @@ object DataSource extends Logging {
   val internalSources = 
sources.filter(_.getClass.getName.startsWith("org.apache.spark"))
   if (provider.equalsIgnoreCase("xml") && sources.size == 2) {
 val externalSource = sources.filterNot(_.getClass.getName
-  
.startsWith("org.apache.spark.sql.execution.datasources.xml.XmlFileFormat")
+  
.startsWith("org.apache.spark.sql.execution.datasources.v2.xml.XmlDataSourceV2")

Review Comment:
   Yes, `ServiceLoader` uses 
`META-INF.services/org.apache.spark.sql.sources.DataSourceRegister`  file to 
load data source. Whether it's v1 or v2 it's the same here. 
   
   I can get your  doubts. When the `USE_V1_SOURCE_LIST ` includes `XML`, we 
finally back to using the default FileFormat here.
   
   
https://github.com/apache/spark/blob/9e35d04cb030ce2ac736914a5586d159339b4686/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSource.scala#L111



-- 
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



Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-08-02 Thread via GitHub


wayneguow commented on PR #47539:
URL: https://github.com/apache/spark/pull/47539#issuecomment-2266568096

   > > cc @sandip-db WDYT?
   > 
   > It looks ok to me. Although, I have not come across anyone asking for it.
   
   Well, the reason why I proposed this PR is that other common data sources 
have v1 and v2 implementations, so I want to add v2 for `xml`.  WDYT @cloud-fan 
, we need your help when you have time.


-- 
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



Re: [PR] [SPARK-49062][SQL] Migrate XML to File Data Source V2 [spark]

2024-08-06 Thread via GitHub


wayneguow commented on PR #47539:
URL: https://github.com/apache/spark/pull/47539#issuecomment-2271452001

   Gentle ping @cloud-fan , if you have time, please help take a look at this 
code related to v2.


-- 
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