spark-test commented on code in PR #60596:
URL: https://github.com/apache/spark/pull/60596#discussion_r4603079623


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/ArchiveReadSuiteBase.scala:
##########
@@ -180,6 +180,14 @@ trait ArchiveReadSuiteBase extends QueryTest with 
SharedSparkSession {
    */
   protected def supportsDecompressionCodec: Boolean = false
 
+  /**
+   * Read options that make [[format]] infer each file as one whole document 
instead of record by
+   * record (e.g. `multiLine`), or `None` when the format has no such mode. 
The shared

Review Comment:
   **Nit (P3):** XML contradicts the `None` clause. Its `multiLine` option 
defaults to true (`XmlOptions.scala:194`), so the default run already infers 
whole documents through `MultiLineXmlDataSource.inferWithArchives`. 
`XMLArchiveReadBase` keeps `None`, so XML's record-by-record inference, which 
reads archives through the Text source, never runs in this test. CSV 
contradicts the 'own code path' rationale here and the 'take different paths' 
comment on the shared test. `CSVDataSource.inferSchema` sends an archive from 
either mode to the same private `inferWithArchives`, which checks the option 
before any mode-specific tokenization, so the CSV `multiLine=true` run repeats 
the default run's path. Only JSON matches the text as written, so a reader of 
either comment would assume every archive suite checks both inference modes on 
separate paths. Rewording both comments to match the per-format behavior fixes 
this without changing the generated tests.
   
   **Verification:**
   - **Inspection:** The whole-document inference hook's documentation and the 
shared decompression inference test's comment describe the default run as 
covering each format's default inference mode, say the hook stays unset for a 
format that already infers whole documents by default such as XML, and present 
different code paths as possible rather than universal, while the generated 
tests and their names, options and assertions stay unchanged.



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/text/TextSuite.scala:
##########
@@ -446,6 +446,33 @@ abstract class TextSuite extends SharedSparkSession with 
CommonFileDataSourceSui
     }
   }
 
+  test("SPARK-60217 decompression option rejects a class that is not a 
creatable codec") {
+    withTempDir { dir =>
+      val file = new File(dir, "data.txt")
+      writeCompressed(file, "a\nb", "gzip")
+      // Each class loads, but Hadoop cannot create it as a codec: not a 
CompressionCodec, the
+      // CompressionCodec interface itself, and a codec without a no-argument 
constructor. The
+      // read must fail while planning, where `ignoreCorruptFiles` cannot turn 
the per-file
+      // failures into an empty result.
+      Seq(
+        classOf[String].getName,
+        classOf[CompressionCodec].getName,

Review Comment:
   **Nit (P3):** This list has no case that only the 
`!Modifier.isAbstract(clazz.getModifiers)` check in `isCreatableCodecClass` 
decides. `CompressionCodec` itself is also rejected by `!clazz.isInterface`, 
and an interface declares no constructors anyway. An abstract subclass of a 
concrete codec that keeps a no-argument constructor, such as `abstract class 
AbstractDecompressionCodec extends DefaultCodec`, is rejected only by the 
abstract check. If that check were dropped, the class would pass planning. 
`ReflectionUtils.newInstance` would then throw in every read task, and since 
that `RuntimeException` counts as a corrupt file, a read with 
`ignoreCorruptFiles=true` would silently return no rows, the outcome this PR 
removes, while every test here still passed. Defining such a class next to 
`ArgumentOnlyDecompressionCodec` and adding it to this list would pin the check.
   
   **Verification:**
   - **Behavior:** A text read whose decompression option names an abstract 
codec class with a no-argument constructor fails before any task runs with 
CODEC_NOT_AVAILABLE.WITH_AVAILABLE_CODECS_SUGGESTION naming that class, with 
ignoreCorruptFiles both on and off, on both the V1 and V2 text read paths.
   - **Regression:** With the abstract-modifier condition removed from the 
creatable-codec check, planning no longer rejects the abstract codec case (with 
ignoreCorruptFiles on, the read returns no rows), so the new case fails while 
the existing cases still pass.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CompressionCodecs.scala:
##########
@@ -54,9 +55,29 @@ object CompressionCodecs {
    * Read-side counterpart of [[getCodecClassName]]. Accepts the same short 
names plus `zstd` /
    * `zstandard`, since the file readers can decompress Zstandard even though 
the writers cannot
    * produce it. Used to parse the `decompression` option.
+   *
+   * Unlike the write side, a class name must also be one Hadoop can create as 
a codec: a concrete
+   * `CompressionCodec` with a no-argument constructor. The readers 
instantiate the forced codec
+   * by reflection for every file, on the executors, so anything else would 
fail there (or be
+   * skipped as a corrupt file under `ignoreCorruptFiles`) instead of at 
planning like an unknown
+   * codec name.
    */
   def getReadCodecClassName(name: String): String = {
-    getCodecClassName(name, readShortCompressionCodecNames)
+    val codecName = getCodecClassName(name, readShortCompressionCodecNames)
+    if (codecName != null && 
!isCreatableCodecClass(Utils.classForName(codecName))) {
+      throw QueryExecutionErrors.codecNotAvailableError(
+        codecName, readShortCompressionCodecNames.keys.mkString(", "))
+    }
+    codecName
+  }
+
+  // Whether `ReflectionUtils.newInstance(clazz, conf)` can produce a 
`CompressionCodec`, checked
+  // by reflection only: the class is never instantiated on the driver.

Review Comment:
   **Nit (P3):** This comment says the helper decides whether 
`ReflectionUtils.newInstance` can produce a codec, but lines 77-80 only check 
the class's shape. `UnconstructibleDecompressionCodec` in `TextSuite` passes 
every predicate, yet its constructor throws, and the existing test 'a forced 
codec that cannot be created does not leak the file stream' still expects it to 
fail in the read task. A maintainer who trusts this comment could treat 
task-side creation failures of forced codecs as unreachable, for example in the 
stream-closing path of `HadoopCodecStreams.createInputStream`, although such 
codecs still fail per file there and are skipped under `ignoreCorruptFiles`. 
Wording that states the precondition rather than success:
   
   ```suggestion
     // Whether `clazz` has the shape `ReflectionUtils.newInstance(clazz, 
conf)` needs to produce a
     // codec: a concrete `CompressionCodec` with a no-argument constructor. 
This is checked by
     // reflection only, so the class is never instantiated on the driver, and 
a class that passes can
     // still fail when the read task creates it.
   ```
   
   **Verification:**
   - **Inspection:** The comment on the creatable-codec helper names the 
checked preconditions (assignable to Hadoop's CompressionCodec, concrete, 
declaring a no-argument constructor) and says that passing them does not 
guarantee the codec can be created in the read task, while the helper's code 
stays unchanged.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/HadoopFileLinesReader.scala:
##########
@@ -61,7 +62,12 @@ class HadoopFileLinesReader(
       // HadoopLineRecordReader is LineRecordReader with some changes like 
support for specifying
       // compression codec as opposed to always infer from file extension.
       // If the line separator is `None`, it covers `\r`, `\r\n` and `\n`.
-      if (SQLConf.get.hadoopLineRecordReaderEnabled) {
+      // A forced `decompression` codec in `conf` was validated while planning 
the read, against
+      // the flag value at that time (see 
`CompressionCodecs.setReadCodecConfiguration`). The plan
+      // may run again after the flag is turned off, so follow the conf rather 
than the flag: the
+      // legacy reader would silently decompress by the file name extension 
instead.
+      if (SQLConf.get.hadoopLineRecordReaderEnabled ||
+          conf.get(HadoopCodecStreams.DECOMPRESSION_CODEC_CONF) != null) {

Review Comment:
   **Nit (P3):** The PR says reads without `decompression` still follow the 
flag, but no test turns the flag off without forcing a codec. Both flag-off 
tests in `TextSuite`, the planning-time rejection and the new rerun test, would 
still pass if this condition picked `HadoopLineRecordReader` for a flag-off 
read that recorded no codec, for example after an edit that treats an absent 
key like the empty value `none` stores. Every flag-off read without the option, 
through text, line-based CSV, JSON, XML and libsvm, would then silently lose 
the escape hatch to Hadoop's reader. An existing test already checks that the 
shared read setup unsets an inherited key, so the unpinned part is the reader 
choice itself. A plain-text file named with a `.gzip` suffix tells the readers 
apart: Hadoop's `LineRecordReader` does not know that suffix and reads the file 
as-is, while the imported reader, with no codec recorded, maps it to gzip and 
fails.
   
   **Verification:**
   - **Behavior:** With the legacy line-reader flag set to false and no 
decompression option, reading a plain-text file whose name ends in .gzip 
returns its lines unchanged on both the V1 and V2 text read paths.
   - **Regression:** If, with the flag set to false, the line-reader choice 
picked Spark's imported reader for a read that recorded no forced codec, for 
example because the forced-codec check treated an absent key like the empty 
value stored for decompression=none, the imported reader would infer gzip from 
the .gzip file name and fail on the plain text, so the new case fails.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/HadoopFileLinesReader.scala:
##########
@@ -61,7 +62,12 @@ class HadoopFileLinesReader(
       // HadoopLineRecordReader is LineRecordReader with some changes like 
support for specifying
       // compression codec as opposed to always infer from file extension.
       // If the line separator is `None`, it covers `\r`, `\r\n` and `\n`.
-      if (SQLConf.get.hadoopLineRecordReaderEnabled) {
+      // A forced `decompression` codec in `conf` was validated while planning 
the read, against
+      // the flag value at that time (see 
`CompressionCodecs.setReadCodecConfiguration`). The plan
+      // may run again after the flag is turned off, so follow the conf rather 
than the flag: the
+      // legacy reader would silently decompress by the file name extension 
instead.
+      if (SQLConf.get.hadoopLineRecordReaderEnabled ||
+          conf.get(HadoopCodecStreams.DECOMPRESSION_CODEC_CONF) != null) {

Review Comment:
   **Nit (P3):** With this disjunct, a plan built with `decompression` while 
the flag was on keeps `HadoopLineRecordReader` after the flag is turned off, as 
the new rerun test checks. The flag's description in 
`SQLConf.HADOOP_LINE_RECORD_READER_ENABLED` (SQLConf.scala:9431-9434) still 
says 'Setting the conf to false will use the LineRecordReader class from the 
hadoop jar instead of the imported one', and the exception is recorded only in 
the comment above. The 
`UNSUPPORTED_FEATURE.DECOMPRESSION_CODEC_WITH_LEGACY_LINE_READER` error names 
this flag, so its description is where users and maintainers will look, and it 
now tells them that every line-based read with the flag off uses Hadoop's 
reader. The reader choice itself is right, since Hadoop's reader would decode 
by file name. The description needs to cover the `decompression` cases: the 
option needs the flag to be true, a new read that sets it fails while the flag 
is false, and a read already planned with it keeps the imported reade
 r.
   
   **Verification:**
   - **Inspection:** The legacy line-reader flag's description states what 
turning it off does for reads without the decompression option, for newly 
planned reads with it, and for reads already planned with it, while the flag's 
key, default value, version and internal visibility stay unchanged.



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