akshatshenoi-db commented on code in PR #57238:
URL: https://github.com/apache/spark/pull/57238#discussion_r3581401246


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/SupportsArchiveFormat.scala:
##########
@@ -0,0 +1,363 @@
+/*
+ * 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
+
+import java.io.{Closeable, File, FileOutputStream, InputStream}
+import java.util.Locale
+import java.util.regex.Pattern
+import java.util.zip.GZIPInputStream
+
+import scala.util.control.NonFatal
+
+import org.apache.commons.compress.archivers.{ArchiveEntry, ArchiveInputStream}
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
+import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream
+import org.apache.commons.io.ByteOrderMark
+import org.apache.commons.io.input.{BOMInputStream, CloseShieldInputStream}
+import org.apache.hadoop.conf.Configuration
+import org.apache.hadoop.fs.Path
+import org.apache.hadoop.io.Text
+import org.apache.hadoop.util.LineReader
+
+import org.apache.spark.{SparkEnv, TaskContext}
+import org.apache.spark.internal.Logging
+import org.apache.spark.paths.SparkPath
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.util.{HadoopFSUtils, Utils}
+
+/**
+ * Mixed into a file-based data source to let it read tar/zip archives of its 
files, treating an
+ * archive like a directory of the entries it contains. Schema inference is 
left to each format.
+ */
+trait SupportsArchiveFormat extends Logging {
+
+  /**
+   * Splits one already-decompressed archive entry's bytes into lines (`None` 
separator = detect
+   * line breaks), yielding each line as [[Text]] without the trailing 
separator. Does not close the
+   * entry stream (the reader owns it).
+   */
+  protected def lineIterator(
+      in: InputStream,
+      lineSeparatorInRead: Option[Array[Byte]]): Iterator[Text] = {
+    // Strip a leading BOM (LineReader does not) so lines match the 
non-archive read path.
+    val bomInputStream = BOMInputStream.builder()
+      .setInputStream(in)
+      .setByteOrderMarks(
+        ByteOrderMark.UTF_8,
+        ByteOrderMark.UTF_16LE,
+        ByteOrderMark.UTF_16BE,
+        ByteOrderMark.UTF_32LE,
+        ByteOrderMark.UTF_32BE)
+      .setInclude(false)
+      .get()
+    val reader = lineSeparatorInRead match {
+      case Some(sep) => new LineReader(bomInputStream, sep)
+      case _ => new LineReader(bomInputStream)
+    }
+    new Iterator[Text] {
+      private val text = new Text()
+      private var finished = false
+      private var hasValue = false
+
+      override def hasNext: Boolean = {
+        if (!finished && !hasValue) {
+          finished = reader.readLine(text) == 0
+          hasValue = !finished
+        }
+        !finished
+      }
+
+      override def next(): Text = {
+        if (!hasNext) throw new NoSuchElementException
+        hasValue = false
+        text
+      }
+    }
+  }
+
+  /** Which archive entries are data files of this format; others are skipped. 
*/
+  protected def archiveEntryFilter(name: String): Boolean =
+    throw new UnsupportedOperationException(
+      s"${getClass.getName} does not support random-access archive reads")
+
+  /**
+   * Materializes each kept entry (those passing [[archiveEntryFilter]]) to a 
temp file under
+   * `localDir`, lazily one at a time, so only one entry occupies disk at once.
+   *
+   * @param path     the archive path
+   * @param conf     Hadoop configuration used to open the archive
+   * @param localDir directory the per-entry temp files are created under
+   * @return an iterator of `(entryName, localFile)` for the kept entries, in 
archive order
+   */
+  private def localizeEntries(
+      path: Path,
+      conf: Configuration,
+      localDir: File): Iterator[(String, File)] =
+    SupportsArchiveFormat.readArchiveEntries(path, conf) { (name, in) =>
+      if (archiveEntryFilter(name)) {
+        Iterator.single((name, SupportsArchiveFormat.copyEntryToLocalFile(in, 
localDir, name)))
+      } else {
+        Iterator.empty
+      }
+    }
+
+  /**
+   * Reads an archive by unpacking each entry to a temp file and applying 
`readEntry`, for a
+   * format that needs a complete file on disk (random access).
+   *
+   * @param file       the archive as a [[PartitionedFile]]
+   * @param conf       Hadoop configuration used to open the archive
+   * @param tempPrefix prefix for the per-task temp dir the entries are 
unpacked into
+   * @param readEntry  reads one unpacked entry file into rows
+   * @return iterator of rows across all entries
+   */
+  protected def readLocalizedEntries(

Review Comment:
   Left as part of this PR since Im currently working on follow up PRs for ORC, 
Parquet and Excel (DBR only) that need the surface, the code wont be uncalled 
or untested for more than a day or 2 max if thats ok



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