From 9e0daaeba024ff50634bc5ecbbc36464ffca6e46 Mon Sep 17 00:00:00 2001 From: Anurag-Dwivedi Date: Wed, 24 Jun 2026 14:05:21 +0530 Subject: [PATCH] [SPARK-57295][SQL] Improve validation for empty/blank file paths in direct SQL queries --- .../apache/spark/sql/execution/datasources/rules.scala | 3 ++- .../test/scala/org/apache/spark/sql/SQLQuerySuite.scala | 9 +++++++++ 2 files changed, 11 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 7122dd52ef1a5..3e20900da55b9 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 da6f6aca2adbf..c638281abbf0c 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,15 @@ 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 { location => + val e = intercept[AnalysisException] { + sql(s"select id from json.`$location`") + } + assert(e.message.contains("The location name cannot be empty string")) + assert(e.message.contains(s"`$location`")) + } } test("SortMergeJoin returns wrong results when using UnsafeRows") {