Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-36680][SQL][FOLLOWUP] Files with options should be put into resolveDataSource function #47370

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package org.apache.spark.sql.execution.datasources

import java.util.Locale

import scala.jdk.CollectionConverters._

import org.apache.spark.sql.{AnalysisException, SaveMode, SparkSession}
import org.apache.spark.sql.catalyst.analysis._
import org.apache.spark.sql.catalyst.catalog._
Expand Down Expand Up @@ -50,7 +52,11 @@ class ResolveSQLOnFile(sparkSession: SparkSession) extends Rule[LogicalPlan] {

private def resolveDataSource(unresolved: UnresolvedRelation): DataSource = {
val ident = unresolved.multipartIdentifier
val dataSource = DataSource(sparkSession, paths = Seq(ident.last), className = ident.head)
val dataSource = DataSource(
sparkSession,
paths = Seq(ident.last),
className = ident.head,
options = unresolved.options.asScala.toMap)
// `dataSource.providingClass` may throw ClassNotFoundException, the caller side will try-catch
// it and return the original plan, so that the analyzer can report table not found later.
val isFileFormat = classOf[FileFormat].isAssignableFrom(dataSource.providingClass)
Expand Down
22 changes: 21 additions & 1 deletion sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
import org.apache.spark.sql.execution.aggregate._
import org.apache.spark.sql.execution.columnar.InMemoryTableScanExec
import org.apache.spark.sql.execution.command.DataWritingCommandExec
import org.apache.spark.sql.execution.datasources.{InsertIntoHadoopFsRelationCommand, LogicalRelation}
import org.apache.spark.sql.execution.datasources.{HadoopFsRelation, InsertIntoHadoopFsRelationCommand, LogicalRelation}
import org.apache.spark.sql.execution.datasources.v2.BatchScanExec
import org.apache.spark.sql.execution.datasources.v2.orc.OrcScan
import org.apache.spark.sql.execution.datasources.v2.parquet.ParquetScan
Expand Down Expand Up @@ -4856,6 +4856,26 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
|"""
)
}

test("SPARK-36680: Files hint options should be put into resolveDataSource function") {
val df1 = spark.range(100).toDF()
withTempPath { f =>
df1.write.json(f.getCanonicalPath)
val df2 = sql(
s"""
|SELECT id
|FROM json.`${f.getCanonicalPath}`
|WITH (`key1` = 1, `key2` = 2)
""".stripMargin
)
checkAnswer(df2, df1)
val relations = df2.queryExecution.analyzed.collect {
case LogicalRelation(fs: HadoopFsRelation, _, _, _) => fs
}
assert(relations.size == 1)
assert(relations.head.options == Map("key1" -> "1", "key2" -> "2"))
}
}
}

case class Foo(bar: Option[String])