diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSource.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSource.scala index e9b8fae7cd735..3615afcf86c7a 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSource.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/DataSource.scala @@ -343,7 +343,12 @@ case class DataSource( val baseRelation = dataSource.createRelation(sparkSession.sqlContext, caseInsensitiveOptions) if (baseRelation.schema != schema) { - throw new AnalysisException(s"$className does not allow user-specified schemas.") + throw new AnalysisException( + "The user-specified schema doesn't match the actual schema: " + + s"user-specified: ${schema.toDDL}, actual: ${baseRelation.schema.toDDL}. If " + + "you're using DataFrameReader.schema API or creating a table, please do not " + + "specify the schema. Or if you're scanning an existed table, please drop " + + "it and re-create it.") } baseRelation diff --git a/sql/core/src/test/scala/org/apache/spark/sql/sources/TableScanSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/sources/TableScanSuite.scala index d4e117953942e..9a95bf770772e 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/sources/TableScanSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/sources/TableScanSuite.scala @@ -358,7 +358,7 @@ class TableScanSuite extends DataSourceTest with SharedSparkSession { // Make sure we do throw correct exception when users use a relation provider that // only implements the RelationProvider or the SchemaRelationProvider. Seq("TEMPORARY VIEW", "TABLE").foreach { tableType => - val schemaNotAllowed = intercept[Exception] { + val schemaNotMatch = intercept[Exception] { sql( s""" |CREATE $tableType relationProvierWithSchema (i int) @@ -369,7 +369,8 @@ class TableScanSuite extends DataSourceTest with SharedSparkSession { |) """.stripMargin) } - assert(schemaNotAllowed.getMessage.contains("does not allow user-specified schemas")) + assert(schemaNotMatch.getMessage.contains( + "The user-specified schema doesn't match the actual schema")) val schemaNeeded = intercept[Exception] { sql( diff --git a/sql/core/src/test/scala/org/apache/spark/sql/test/DataFrameReaderWriterSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/test/DataFrameReaderWriterSuite.scala index 55a60940a7750..fb939007697c2 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/test/DataFrameReaderWriterSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/test/DataFrameReaderWriterSuite.scala @@ -489,11 +489,10 @@ class DataFrameReaderWriterSuite extends QueryTest with SharedSparkSession with // when users do not specify the schema checkAnswer(dfReader.load(), spark.range(1, 11).toDF()) - // when users specify the schema + // when users specify a wrong schema val inputSchema = new StructType().add("s", IntegerType, nullable = false) val e = intercept[AnalysisException] { dfReader.schema(inputSchema).load() } - assert(e.getMessage.contains( - "org.apache.spark.sql.sources.SimpleScanSource does not allow user-specified schemas")) + assert(e.getMessage.contains("The user-specified schema doesn't match the actual schema")) } test("read a data source that does not extend RelationProvider") {