dongjoon-hyun commented on code in PR #60101:
URL: https://github.com/apache/spark/pull/60101#discussion_r4472819675


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/TextBasedFileScan.scala:
##########
@@ -32,7 +32,7 @@ abstract class TextBasedFileScan(
     sparkSession: SparkSession,
     options: CaseInsensitiveStringMap) extends FileScan {
   @transient private lazy val codecFactory: CompressionCodecFactory = new 
CompressionCodecFactory(
-    sparkSession.sessionState.newHadoopConfWithOptions(options.asScala.toMap))
+    
sparkSession.sessionState.newHadoopConfWithOptions(options.asCaseSensitiveMap.asScala.toMap))

Review Comment:
   **1. [Medium] Other DSv2 paths still pass lowercased keys**
   
   This line now uses `asCaseSensitiveMap`, but other DSv2 callers still 
convert the `CaseInsensitiveStringMap` with `options.asScala.toMap`, whose keys 
are already lowercased. Such a plain map skips the new `CaseInsensitiveMap` 
branch in `newHadoopConfWithOptions`, so these confs still lose mixed-case keys:
   
   - `FileTable.fileIndex` builds `MetadataLogFileIndex` from 
`options.asScala.toMap` ([FileTable.scala 
L68-L69](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/FileTable.scala#L68-L69)).
 The description lists `MetadataLogFileIndex` as fixed, which holds for DSv1 
but not here. The `InMemoryFileIndex` branch right below already gets 
`caseSensitiveMap`.
   - Schema inference in 
[`OrcTable`](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/orc/OrcTable.scala#L44),
 
[`ParquetTable`](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/parquet/ParquetTable.scala#L44),
 
[`AvroTable`](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/connector/avro/src/main/scala/org/apache/spark/sql/v2/avro/AvroTable.scala#L43),
 
[`JsonTable`](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/json/JsonTable.scala#L44-L45)
 and 
[`CSVTable`](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/csv/CSVTable.scala#L44-L45)
 passes `options.asScala.toMap`.
  It reaches `newHadoopConfWithOptions` through `OrcUtils.readSchema`, 
`SchemaMergeUtils.mergeSchemasInParallel`, `AvroUtils.inferSchema` and the 
multiLine `createBaseRdd` of JSON and CSV.
   
   So with `spark.sql.sources.useV1SourceList` set to empty, a mixed-case key 
such as `fs.azure.skipUserGroupMetadataDuringInitialization` still does not 
reach schema inference or the streaming-sink file index. Could these call sites 
use `options.asCaseSensitiveMap.asScala.toMap` too? If you prefer to keep DSv2 
out of this PR, the description could say so.



##########
sql/core/src/main/scala/org/apache/spark/sql/internal/SessionState.scala:
##########
@@ -120,8 +121,14 @@ private[sql] class SessionState(
 
   def newHadoopConfWithOptions(options: Map[String, String]): Configuration = {
     val hadoopConf = newHadoopConf()
-    options.foreach { case (k, v) =>
-      if ((v ne null) && k != "path" && k != "paths") {
+    // CaseInsensitiveMap.iterator yields lowercased keys, but Hadoop 
Configuration is
+    // case-sensitive. Use the original map to preserve the user's key casing.
+    val rawOptions = options match {
+      case ci: CaseInsensitiveMap[String @unchecked] => ci.originalMap

Review Comment:
   **2. [Medium] Mixed-case spellings of lowercase Hadoop keys stop applying**
   
   Before this PR, a `CaseInsensitiveMap` passed here was iterated through its 
lowercased view, so a key written as `Parquet.Enable.Dictionary` landed in the 
conf as `parquet.enable.dictionary` and took effect. That covers every path 
that passes a `CaseInsensitiveMap`: DSv1 scans (`relation.options`), DSv1 
schema inference, and `InsertIntoHadoopFsRelationCommand` for an `INSERT` into 
a data source table, which gets 
[`t.options`](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSourceStrategy.scala#L222-L229).
 After this PR only the spelling the user wrote reaches the conf, so such an 
option is now ignored without an error. For example, I expect this to write 
without dictionary encoding today and with it after this PR:
   
   ```sql
   CREATE TABLE t (id INT) USING parquet OPTIONS ('Parquet.Enable.Dictionary' 
'false');
   INSERT INTO t VALUES (1);
   ```
   
   Hadoop keys are case-sensitive, so the new behavior is the right one and 
matches DSv2. Still, it can change what existing tables and jobs do. Since the 
description marks this as a user-facing change, could you add an item to 
`docs/sql-migration-guide.md`?



##########
sql/core/src/main/scala/org/apache/spark/sql/internal/SessionState.scala:
##########
@@ -120,8 +121,14 @@ private[sql] class SessionState(
 
   def newHadoopConfWithOptions(options: Map[String, String]): Configuration = {
     val hadoopConf = newHadoopConf()
-    options.foreach { case (k, v) =>
-      if ((v ne null) && k != "path" && k != "paths") {
+    // CaseInsensitiveMap.iterator yields lowercased keys, but Hadoop 
Configuration is
+    // case-sensitive. Use the original map to preserve the user's key casing.
+    val rawOptions = options match {
+      case ci: CaseInsensitiveMap[String @unchecked] => ci.originalMap
+      case other => other
+    }
+    rawOptions.foreach { case (k, v) =>

Review Comment:
   **3. [Low] Read options can now override Spark SQL configs that executors 
read from the Hadoop conf**
   
   A camelCase `spark.sql.*` key in DSv1 read options used to reach the conf 
lowercased, so it had no effect. Now it reaches the conf as written. 
`ParquetFileFormat.setupHadoopConf` 
([L129-L172](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala#L129-L172))
 resets most reader flags from `SQLConf` afterwards. It does not reset these 
three, which executors read from the conf ([ParquetReadSupport.scala 
L132-L135](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetReadSupport.scala#L132-L135),
 [ParquetSchemaConverter.scala 
L95-L96](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetSchemaConverter.scala#L95-L96)):
   
   - `spark.sql.parquet.fieldId.read.enabled`
   - `spark.sql.parquet.fieldId.read.ignoreMissing`
   - `spark.sql.parquet.ignoreVariantAnnotation`
   
   So `.option("spark.sql.parquet.fieldId.read.enabled", "true")` now switches 
one DSv1 read to field-ID matching, as DSv2 already allows. Is that intended? 
If so, the migration note could mention it. If not, `setupHadoopConf` could set 
these from `SQLConf` like the keys around them.



##########
sql/core/src/main/scala/org/apache/spark/sql/internal/SessionState.scala:
##########
@@ -120,8 +121,14 @@ private[sql] class SessionState(
 
   def newHadoopConfWithOptions(options: Map[String, String]): Configuration = {
     val hadoopConf = newHadoopConf()
-    options.foreach { case (k, v) =>
-      if ((v ne null) && k != "path" && k != "paths") {
+    // CaseInsensitiveMap.iterator yields lowercased keys, but Hadoop 
Configuration is
+    // case-sensitive. Use the original map to preserve the user's key casing.
+    val rawOptions = options match {
+      case ci: CaseInsensitiveMap[String @unchecked] => ci.originalMap
+      case other => other
+    }
+    rawOptions.foreach { case (k, v) =>
+      if ((v ne null) && !k.equalsIgnoreCase("path") && 
!k.equalsIgnoreCase("paths")) {

Review Comment:
   **4. [Low] `path` and `paths` are now filtered differently from 
`mergeWriteOptionsIntoHadoopConf`**
   
   The description says this matches 
`DataSourceUtils.mergeWriteOptionsIntoHadoopConf`, but that method still 
compares case-sensitively ([DataSourceUtils.scala 
L65](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSourceUtils.scala#L65)).
 Within one DSv1 write, the two now disagree. With `df.write.option("PATH", 
"/x").format("parquet").save()`, the conf of 
`InsertIntoHadoopFsRelationCommand` drops `PATH`, while `FileFormatWriter` 
merges it into the job conf. Before this PR both kept it. It is harmless in 
practice, but could `mergeWriteOptionsIntoHadoopConf` use the same 
`equalsIgnoreCase` check? Item 6 would remove the second copy.



##########
sql/core/src/main/scala/org/apache/spark/sql/internal/SessionState.scala:
##########
@@ -120,8 +121,14 @@ private[sql] class SessionState(
 
   def newHadoopConfWithOptions(options: Map[String, String]): Configuration = {
     val hadoopConf = newHadoopConf()
-    options.foreach { case (k, v) =>
-      if ((v ne null) && k != "path" && k != "paths") {
+    // CaseInsensitiveMap.iterator yields lowercased keys, but Hadoop 
Configuration is
+    // case-sensitive. Use the original map to preserve the user's key casing.
+    val rawOptions = options match {
+      case ci: CaseInsensitiveMap[String @unchecked] => ci.originalMap
+      case other => other

Review Comment:
   **5. [Nit] Only an unmodified `CaseInsensitiveMap` gets its original keys**
   
   Every other map falls through here as is. A `CaseInsensitiveMap` that went 
through `filter` or `map`, or a `CaseInsensitiveStringMap` converted with 
`asScala`, arrives as a plain map whose keys are already lowercased. So the fix 
depends on every caller passing the `CaseInsensitiveMap` instance itself, and 
item 1 lists callers that do not. Fixing the callers to pass case-preserving 
maps would be more robust, as `ResolveDataSource` already does with 
`originalMap` 
([L200](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveDataSource.scala#L200))
 and DSv2 does with `asCaseSensitiveMap`. Keeping this branch as a safety net 
is fine, but the comment could say that callers are expected to pass the 
original keys.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/TextBasedFileScan.scala:
##########
@@ -32,7 +32,7 @@ abstract class TextBasedFileScan(
     sparkSession: SparkSession,
     options: CaseInsensitiveStringMap) extends FileScan {
   @transient private lazy val codecFactory: CompressionCodecFactory = new 
CompressionCodecFactory(

Review Comment:
   **7. [Nit] `codecFactory` and `newReadHadoopConf` build the same conf**
   
   After this change both call 
`newHadoopConfWithOptions(options.asCaseSensitiveMap.asScala.toMap)`, here and 
in `newReadHadoopConf` (L56-L57). This bug came from the two copies drifting 
apart, so a small private helper used by both would keep them in sync.



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/DataSourceSuite.scala:
##########
@@ -240,6 +241,25 @@ class DataSourceSuite extends SharedSparkSession with 
PrivateMethodTester {
       parameters = Map("paths" -> "/path1, /path2"))
     )
   }
+
+  test("SPARK-60887: newHadoopConfWithOptions preserves key casing for 
CaseInsensitiveMap") {

Review Comment:
   **8. [Low] No test goes through a real read, and nothing covers 
`TextBasedFileScan`**
   
   This test calls `newHadoopConfWithOptions` directly. It would still pass if 
a caller stopped passing the `CaseInsensitiveMap` instance (item 5), and 
reverting the `TextBasedFileScan` change fails no test.
   
   `CommonFileDataSourceSuite` already checks this end to end for every file 
format, in both the V1 and V2 suites 
([L41-L65](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/CommonFileDataSourceSuite.scala#L41-L65)):
 
[`FakeFileSystemRequiringDSOption`](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/test/scala/org/apache/spark/sql/FileBasedDataSourceSuite.scala#L1884-L1889)
 fails to initialize unless the conf has `ds_option`. That key is lowercase, so 
it passes with or without this PR. With a mixed-case key such as `ds_Option`, I 
expect the V1 suites to fail on master and pass with this PR, while the V2 
suites would keep failing until item 1 is fixed. A case for DSv1 ORC 
`mergeSchema` with `archivePathFilter`, which this PR fixes as a side effect, 
would also help.



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/DataSourceSuite.scala:
##########
@@ -240,6 +241,25 @@ class DataSourceSuite extends SharedSparkSession with 
PrivateMethodTester {
       parameters = Map("paths" -> "/path1, /path2"))
     )
   }
+
+  test("SPARK-60887: newHadoopConfWithOptions preserves key casing for 
CaseInsensitiveMap") {
+    val options = CaseInsensitiveMap(Map(
+      "fs.azure.skipUserGroupMetadataDuringInitialization" -> "true",
+      "mapreduce.input.pathFilter.class" -> "org.example.CustomPathFilter",
+      "path" -> "/tmp/a",
+      "Path" -> "/tmp/b",
+      "paths" -> "/tmp/c",
+      "PATHS" -> "/tmp/d"

Review Comment:
   **9. [Nit] The test map holds case duplicates that a reader does not 
produce**
   
   `DataFrameReader` and `DataFrameWriter` keep one spelling per key, because 
`CaseInsensitiveMap.updated` drops the other spellings. Meanwhile the behavior 
that did change is not covered: a plain map (for example from DSv2 
`asCaseSensitiveMap`) with a `Path` or `PATHS` key used to put that key into 
the conf and now drops it. Could the test use one spelling per key and add a 
plain-map case?



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/DataSourceSuite.scala:
##########
@@ -240,6 +241,25 @@ class DataSourceSuite extends SharedSparkSession with 
PrivateMethodTester {
       parameters = Map("paths" -> "/path1, /path2"))
     )
   }
+
+  test("SPARK-60887: newHadoopConfWithOptions preserves key casing for 
CaseInsensitiveMap") {
+    val options = CaseInsensitiveMap(Map(
+      "fs.azure.skipUserGroupMetadataDuringInitialization" -> "true",
+      "mapreduce.input.pathFilter.class" -> "org.example.CustomPathFilter",

Review Comment:
   **10. [Nit] `mapreduce.input.pathFilter.class` already reaches the batch 
file listing**
   
   The description cites this key as one that was ignored. For a batch 
`spark.read` load, `ResolveDataSource` passes `finalOptions.originalMap` 
([L200](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveDataSource.scala#L200)),
 and `DataSource.createInMemoryFileIndex` builds the index from that map 
([L613-L617](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSource.scala#L613-L617)).
 So `InMemoryFileIndex` already sees this key with its casing on master. What 
this PR changes is the conf of the read tasks in `DataSourceScanExec`, schema 
inference, `InsertIntoHadoopFsRelationCommand`, and `MetadataLogFileIndex` in 
DSv1. Could the description name those paths, so that readers do not expect a 
change in file listing?



##########
sql/core/src/main/scala/org/apache/spark/sql/internal/SessionState.scala:
##########
@@ -120,8 +121,14 @@ private[sql] class SessionState(
 
   def newHadoopConfWithOptions(options: Map[String, String]): Configuration = {
     val hadoopConf = newHadoopConf()
-    options.foreach { case (k, v) =>
-      if ((v ne null) && k != "path" && k != "paths") {
+    // CaseInsensitiveMap.iterator yields lowercased keys, but Hadoop 
Configuration is

Review Comment:
   **6. [Nit] This duplicates 
`DataSourceUtils.mergeWriteOptionsIntoHadoopConf`**
   
   The unwrap, the loop and this comment are copied from 
`mergeWriteOptionsIntoHadoopConf` ([DataSourceUtils.scala 
L56-L69](https://github.com/apache/spark/blob/80d8c2837e17a9b8ad9a74e0cf55ff12d7c91fbf/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSourceUtils.scala#L56-L69)),
 and the copies already differ in the `path` check (item 4). Could one call the 
other, or both call a shared helper? For example, after aligning the `path` 
check there:
   
   ```scala
     def newHadoopConfWithOptions(options: Map[String, String]): Configuration 
= {
       val hadoopConf = newHadoopConf()
       DataSourceUtils.mergeWriteOptionsIntoHadoopConf(options, hadoopConf)
       hadoopConf
     }
   ```
   
   The helper may then deserve a name without "Write".



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