Github user cloud-fan commented on a diff in the pull request: https://github.com/apache/spark/pull/16944#discussion_r104270713 --- 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 -- the `inferredSchema` is data schema(excluding partition schema), and `updateCatalogSchema` will just update the table to this schema, which means we will lose partition schema after this.
--- 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