This is an automated email from the ASF dual-hosted git repository.

cloud-fan pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new dad313b2802d [SPARK-57295][SQL] Extend whitespace-only path validation 
consistency to direct file-path queries
dad313b2802d is described below

commit dad313b2802d900ea9c93baffabb9d9d8c3eb24e
Author: Anurag Kumar Dwivedi <[email protected]>
AuthorDate: Tue Jun 30 21:08:07 2026 +0800

    [SPARK-57295][SQL] Extend whitespace-only path validation consistency to 
direct file-path queries
    
    ## Description
    
    This is a follow-up to PR #56356, which improved validation consistency for 
namespace locations by treating whitespace-only values as invalid locations.
    
    ### What changes were proposed in this pull request?
    
    This PR extends the same validation behavior to direct file-path queries.
    
    Currently, direct file-path validation checks only for empty strings using 
`isEmpty`. Consequently, whitespace-only paths such as `" "`, `"\t"`, and 
`"\n"` are not recognized as empty during analysis and may fail later with 
datasource-specific errors.
    
    This PR updates the validation to use `SparkStringUtils.isBlank(...)`, 
ensuring that whitespace-only paths are treated as invalid and consistently 
fail with the standard `INVALID_EMPTY_LOCATION` error.
    
    By doing so, the change aligns direct file-path validation with the 
existing namespace location validation logic and improves consistency across 
Spark SQL location handling.
    
    ### Why are the changes needed?
    
    Currently, validation behavior differs depending on the type of location 
being processed:
    
    * Empty paths (`""`) are rejected during analysis with 
`INVALID_EMPTY_LOCATION`.
    * Whitespace-only paths (`" "`, `"\t"`, `"\n"`) may bypass analysis-time 
validation and fail later with datasource-specific errors.
    
    Using `SparkStringUtils.isBlank(...)` ensures consistent handling of all 
blank path values across Spark SQL.
    
    ### Does this PR introduce any user-facing change?
    
    Yes.
    
    Whitespace-only direct file paths are now rejected during analysis with 
`INVALID_EMPTY_LOCATION`, providing behavior consistent with namespace location 
validation.
    
    ### How was this patch tested?
    
    Added regression test coverage for blank path values, including:
    
    * `""`
    * `" "`
    * `"\t"`
    * `"\n"`
    
    and verified that they consistently fail with `INVALID_EMPTY_LOCATION`.
    
    Jira - https://issues.apache.org/jira/browse/SPARK-57295
    
    Closes #56732 from 
AnuragKDwivedi/SPARK-57295-db-location-validation-direct-file-path.
    
    Authored-by: Anurag Kumar Dwivedi <[email protected]>
    Signed-off-by: Wenchen Fan <[email protected]>
    (cherry picked from commit 41cedca6892132292cf7c2fb6db5747958068e8d)
    Signed-off-by: Wenchen Fan <[email protected]>
---
 .../org/apache/spark/sql/execution/datasources/rules.scala |  3 ++-
 .../test/scala/org/apache/spark/sql/SQLQuerySuite.scala    | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
index 7122dd52ef1a..3e20900da55b 100644
--- 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
@@ -44,6 +44,7 @@ import org.apache.spark.sql.types.{ArrayType, DataType, 
MapType, StructField, St
 import org.apache.spark.sql.util.PartitioningUtils.normalizePartitionSpec
 import org.apache.spark.sql.util.SchemaUtils
 import org.apache.spark.util.ArrayImplicits._
+import org.apache.spark.util.SparkStringUtils
 
 /**
  * Replaces [[UnresolvedRelation]]s if the plan is for direct query on files.
@@ -106,7 +107,7 @@ class ResolveSQLOnFile(sparkSession: SparkSession) extends 
Rule[LogicalPlan] {
         errorClass = "UNSUPPORTED_DATASOURCE_FOR_DIRECT_QUERY",
         messageParameters = Map("dataSourceType" -> ident.head))
     }
-    if (isFileFormat && ident.last.isEmpty) {
+    if (isFileFormat && SparkStringUtils.isBlank(ident.last)) {
       unresolved.failAnalysis(
         errorClass = "INVALID_EMPTY_LOCATION",
         messageParameters = Map("location" -> ident.last))
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala 
b/sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
index da6f6aca2adb..52cc8a3e92af 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
@@ -1697,6 +1697,20 @@ class SQLQuerySuite extends SharedSparkSession with 
AdaptiveSparkPlanHelper
     }
     assert(e.message.contains("Unsupported data source type for direct query 
on files: " +
       "org.apache.spark.sql.execution.datasources.jdbc"))
+
+    // Test for empty and whitespace-only paths
+    Seq("", " ", "\t", "\n", "\t\n", " \t ").foreach { file_path =>
+      checkError(
+        exception = intercept[AnalysisException] {
+          sql(s"select id from json.`$file_path`")
+        },
+        condition = "INVALID_EMPTY_LOCATION",
+        parameters = Map("location" -> file_path),
+        queryContext = Array(ExpectedContext(
+          fragment = s"json.`$file_path`",
+          start = 15,
+          stop = 15 + s"json.`$file_path`".length - 1)))
+    }
   }
 
   test("SortMergeJoin returns wrong results when using UnsafeRows") {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to