[SPARK-56154][SQL] Preserve ENUM logical annotation through Parquet read-write#55047
Open
xiaoxuandev wants to merge 1 commit intoapache:masterfrom
Open
[SPARK-56154][SQL] Preserve ENUM logical annotation through Parquet read-write#55047xiaoxuandev wants to merge 1 commit intoapache:masterfrom
xiaoxuandev wants to merge 1 commit intoapache:masterfrom
Conversation
…ead-write ### What changes were proposed in this pull request? When Spark reads a Parquet file containing BINARY(ENUM) columns, the ENUM logical type annotation is lost and written back as BINARY(STRING). This patch preserves the ENUM annotation through read-write roundtrips by storing it in StructField metadata. The changes: 1. In ParquetToSparkSchemaConverter (read path): detect EnumLogicalTypeAnnotation on PrimitiveColumnIO and store a metadata tag on the StructField. This applies to OPTIONAL and REQUIRED fields. REPEATED ENUM is intentionally not supported because the write path cannot propagate element-level metadata through ArrayType conversion. 2. In SparkToParquetSchemaConverter (write path): when writing StringType columns, check for the ENUM metadata tag and emit LogicalTypeAnnotation.enumType() instead of stringType(). 3. Extract hardcoded metadata key/value strings into constants (PARQUET_LOGICAL_TYPE_KEY, PARQUET_ENUM_TYPE) in the ParquetSchemaConverter companion object. The metadata key uses the __ prefix convention for internal metadata. ### Why are the changes needed? Without this fix, Parquet files with ENUM-annotated columns lose their annotation after a Spark read-write cycle. This breaks downstream systems that depend on the ENUM annotation (e.g., Avro ecosystem tooling), causes schema inconsistency in data pipelines, and fails schema validation in schema registry scenarios. ### Does this PR introduce _any_ user-facing change? Yes. Parquet ENUM logical type annotations are now preserved when reading and writing Parquet files through Spark. Previously, all StringType columns were written as BINARY(STRING) regardless of the original annotation. ### How was this patch tested? - Roundtrip test: creates a Parquet file with BINARY(ENUM) schema, reads with Spark, verifies metadata tag, writes back, and verifies the output file retains the ENUM annotation. - REQUIRED ENUM test: verifies roundtrip preservation for required (non-nullable) ENUM fields. - Mixed columns test: verifies ENUM and STRING columns coexist correctly in the same schema without interference. - Negative test: verifies that plain StringType columns without ENUM metadata continue to write as BINARY(STRING). ### Was this patch authored or co-authored using generative AI tooling? Yes, co-authored with Kiro.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
When Spark reads a Parquet file containing BINARY(ENUM) columns, the ENUM logical type annotation is lost and written back as BINARY(STRING). This patch preserves the ENUM annotation through read-write roundtrips by storing it in StructField metadata.
The changes:
Why are the changes needed?
Without this fix, Parquet files with ENUM-annotated columns lose their annotation after a Spark read-write cycle. This breaks downstream systems that depend on the ENUM annotation (e.g., Avro ecosystem tooling), causes schema inconsistency in data pipelines, and fails schema validation in schema registry scenarios.
Does this PR introduce any user-facing change?
Yes. Parquet ENUM logical type annotations are now preserved when reading and writing Parquet files through Spark. Previously, all StringType columns were written as BINARY(STRING) regardless of the original annotation.
How was this patch tested?
Was this patch authored or co-authored using generative AI tooling?
Yes, co-authored with Kiro.