@@ -339,33 +339,15 @@ case class DataSource(
339339 dataSource.createRelation(sparkSession.sqlContext, caseInsensitiveOptions)
340340 case (_ : SchemaRelationProvider , None ) =>
341341 throw new AnalysisException (s " A schema needs to be specified when using $className. " )
342- case (dataSource : RelationProvider , Some (specifiedSchema )) =>
342+ case (dataSource : RelationProvider , Some (schema )) =>
343343 val baseRelation =
344344 dataSource.createRelation(sparkSession.sqlContext, caseInsensitiveOptions)
345- val persistentSchema = baseRelation.schema
346- val persistentSize = persistentSchema.size
347- val specifiedSize = specifiedSchema.size
348- if (persistentSize == specifiedSize) {
349- val (persistentFields, specifiedFields) = persistentSchema.zip(specifiedSchema)
350- .filter { case (existedField, userField) => existedField != userField }
351- .unzip
352- if (persistentFields.nonEmpty) {
353- val errorMsg =
354- s " Mismatched fields detected between persistent schema and user specified schema: " +
355- s " persistentFields: ${persistentFields.map(_.toDDL).mkString(" , " )}, " +
356- s " specifiedFields: ${specifiedFields.map(_.toDDL).mkString(" , " )}. " +
357- s " This happens either you make a mistake in schema or type mapping between Spark " +
358- s " and external data sources have been updated while your specified schema still " +
359- s " using the old schema. Please either correct the schema or just do not specify " +
360- s " the schema since a specified schema for $className is not necessary. "
361- throw new AnalysisException (errorMsg)
362- }
363- } else {
364- val errorMsg =
365- s " The number of fields between persistent schema and user specified schema " +
366- s " mismatched: expect $persistentSize fields, but got $specifiedSize fields. " +
367- s " Please either correct the schema or just do not specify the schema since " +
368- s " a specified schema for $className is not necessary. "
345+ if (baseRelation.schema != schema) {
346+ val errorMsg = " The user-specified schema doesn't match the actual schema: " +
347+ s " user-specified: ${schema.toDDL}, actual: ${baseRelation.schema.toDDL}. If " +
348+ s " you're using DataFrameReader.schema API or creating a table, please do not " +
349+ s " specify the schema. Or if you're scanning an existed table, please drop " +
350+ s " it and re-create it. "
369351 throw new AnalysisException (errorMsg)
370352 }
371353 baseRelation
0 commit comments