Github user budde commented on a diff in the pull request: https://github.com/apache/spark/pull/16944#discussion_r104271287 --- Diff: sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveMetastoreCatalog.scala --- @@ -217,6 +227,62 @@ private[hive] class HiveMetastoreCatalog(sparkSession: SparkSession) extends Log result.copy(expectedOutputAttributes = Some(relation.output)) } + private def inferIfNeeded( + relation: CatalogRelation, + options: Map[String, String], + fallbackSchema: StructType, + fileFormat: FileFormat, + fileIndexOpt: Option[FileIndex] = None): (StructType, CatalogTable) = { + val inferenceMode = sparkSession.sessionState.conf.caseSensitiveInferenceMode + val shouldInfer = (inferenceMode != NEVER_INFER) && !relation.tableMeta.schemaPreservesCase + val tableName = relation.tableMeta.identifier.table + if (shouldInfer) { + logInfo(s"Inferring case-sensitive schema for table $tableName (inference mode: " + + s"$inferenceMode)") + val fileIndex = fileIndexOpt.getOrElse { + val rootPath = new Path(new URI(relation.tableMeta.location)) + new InMemoryFileIndex(sparkSession, Seq(rootPath), options, None) + } + + val inferredSchema = { + val schema = fileFormat.inferSchema( + sparkSession, + options, + fileIndex.listFiles(Nil).flatMap(_.files)) + fileFormat match { + case _: ParquetFileFormat => + schema.map(ParquetFileFormat.mergeMetastoreParquetSchema(relation.tableMeta.schema, _)) + case _ => + schema + } + } + + inferredSchema match { + case Some(schema) => + if (inferenceMode == INFER_AND_SAVE) { + updateCatalogSchema(relation.tableMeta.identifier, schema) --- End diff -- Thanks, I understand the issue with this now. I'll add code to merge the partition columns back in regardless of the file format. If we want to merge any nullable fields from the metastore schema that are missing from ```inferredSchema``` (see comment above) we probably want to do this explicitly as well.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org