Github user liancheng commented on a diff in the pull request:
https://github.com/apache/spark/pull/5214#discussion_r27322315
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/parquet/newParquet.scala ---
@@ -775,6 +777,32 @@ private[sql] object ParquetRelation2 extends Logging {
})
}
+ /**
+ * Returns the original schema from the Parquet file with any missing
nullable fields from the
+ * Hive Metastore schema merged in.
+ *
+ * When constructing a DataFrame from a collection of structured data,
the resulting object has
+ * a schema corresponding to the union of the fields present in each
element of the collection.
+ * Spark SQL simply assigns a null value to any field that isn't present
for a particular row.
+ * In some cases, it is possible that a given table partition stored as
a Parquet file doesn't
+ * contain a particular nullable field in its schema despite that field
being present in the
+ * table schema obtained from the Hive Metastore. This method returns a
schema representing the
+ * Parquet file schema along with any additional nullable fields from
the Metastore schema
+ * merged in.
+ */
+ private[parquet] def mergeMissingNullableFields(
+ metastoreSchema: StructType,
+ parquetSchema: StructType): StructType = {
+ val fieldMap = metastoreSchema.map(f => f.name.toLowerCase -> f).toMap
+ val missingFields = metastoreSchema
+ .map(_.name.toLowerCase)
+ .diff(parquetSchema.map(_.name.toLowerCase))
+ .map(fieldMap(_))
+ .filter(_.nullable)
+ StructType(parquetSchema ++ missingFields)
--- End diff --
When the metastore schema is available, we are actually converting a
metastore Parquet table into `ParquetRelation2`. Thus, the final reconciled
schema should have exactly the same fields as the metastore schema, and simply
drop any fields only appear in the Parquet data file.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]