Github user liancheng commented on a diff in the pull request:
https://github.com/apache/spark/pull/5214#discussion_r27340859
--- 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 --
Ah, yeah, you're right :) Totally forgot that `mergeMetastoreParquetSchema`
already handles field reordering here. And all additional Parquet fields are
removed via [this `zip` call] [1].
[1]:
https://github.com/apache/spark/pull/5141/files#diff-c69b9e667e93b7e4693812cc72abb65fR769
---
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]