You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "sandip-db (via GitHub)" <gi...@apache.org> on 2024/01/07 08:34:14 UTC

Re: [PR] [SPARK-46248][SQL] XML: Support for ignoreCorruptFiles and ignoreMissingFiles options [spark]

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


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/xml/TestXmlData.scala:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.xml
+
+import java.io.{File, RandomAccessFile}
+
+import org.apache.spark.sql.{Dataset, Encoders, SparkSession}
+
+private[xml] trait TestXmlData {
+  protected def spark: SparkSession
+
+  def sampledTestData: Dataset[String] = {
+    spark
+      .range(0, 100, 1)
+      .map { index =>
+        val predefinedSample = Set[Long](3, 18, 20, 24, 50, 60, 87, 99)
+        if (predefinedSample.contains(index)) {
+          index.toString
+        } else {
+          (index.toDouble + 0.1).toString
+        }
+      }(Encoders.STRING)
+  }
+
+  def withCorruptedFile(dir: File, format: String = "gz", numBytesToCorrupt: Int = 50)(
+      f: File => Unit): Unit = {
+    // find the targeted files and corrupt the first one
+    val files = dir.listFiles().filter(file => file.isFile && file.getName.endsWith(format))
+    val raf = new RandomAccessFile(files.head.getPath, "rw")
+
+    // disable checksum verification
+    import org.apache.hadoop.fs.Path
+    val fs = new Path(dir.getPath).getFileSystem(spark.sessionState.newHadoopConf())
+    fs.setVerifyChecksum(false)
+    // delete crc files
+    val crcFiles = dir.listFiles
+      .filter(file => file.isFile && file.getName.endsWith("crc"))
+    crcFiles.foreach { file =>
+      assert(file.exists())
+      file.delete()
+      assert(!file.exists())
+    }
+    fs.close()

Review Comment:
   remove



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/xml/TestXmlData.scala:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.xml
+
+import java.io.{File, RandomAccessFile}
+
+import org.apache.spark.sql.{Dataset, Encoders, SparkSession}
+
+private[xml] trait TestXmlData {
+  protected def spark: SparkSession
+
+  def sampledTestData: Dataset[String] = {
+    spark
+      .range(0, 100, 1)
+      .map { index =>
+        val predefinedSample = Set[Long](3, 18, 20, 24, 50, 60, 87, 99)
+        if (predefinedSample.contains(index)) {
+          index.toString
+        } else {
+          (index.toDouble + 0.1).toString
+        }
+      }(Encoders.STRING)
+  }
+
+  def withCorruptedFile(dir: File, format: String = "gz", numBytesToCorrupt: Int = 50)(
+      f: File => Unit): Unit = {
+    // find the targeted files and corrupt the first one
+    val files = dir.listFiles().filter(file => file.isFile && file.getName.endsWith(format))
+    val raf = new RandomAccessFile(files.head.getPath, "rw")
+
+    // disable checksum verification
+    import org.apache.hadoop.fs.Path
+    val fs = new Path(dir.getPath).getFileSystem(spark.sessionState.newHadoopConf())
+    fs.setVerifyChecksum(false)
+    // delete crc files
+    val crcFiles = dir.listFiles
+      .filter(file => file.isFile && file.getName.endsWith("crc"))
+    crcFiles.foreach { file =>
+      assert(file.exists())
+      file.delete()
+      assert(!file.exists())
+    }
+    fs.close()
+
+    // corrupt the file
+    val fileSize = raf.length()
+    // avoid the last few bytes as it might contain crc
+    raf.seek(fileSize - numBytesToCorrupt - 100)
+    for (_ <- 1 to numBytesToCorrupt) {
+      val randomByte = (Math.random() * 256).toByte
+      raf.writeByte(randomByte)
+    }
+    raf.close()
+    f(dir)
+  }

Review Comment:
   Enable checksum at the end to prevent Parquet reader in subsequent tests from throwing leaky stream error:
   ```suggestion
       f(dir)
       fs.setVerifyChecksum(true)
     }
   ```



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org