-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-53659][SQL] Infer Variant shredding schema when writing to Parquet #52406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cashmand Thanks for this awesome feature! I left a few comments.
/** | ||
* | ||
* Infer a schema when there are Variant values in the shredding schema. | ||
* Only VariantType values at the top level or nested in struct fields are replaced. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are they replaced with?
* Infer a schema when there are Variant values in the shredding schema. | ||
* Only VariantType values at the top level or nested in struct fields are replaced. | ||
* VariantType nested in arrays or maps are not modified. | ||
* @param schema The original schema containing VariantType. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this require that the schema must contain a VariantType
, and does the VariantType
have to be a top-level field?
.flatten | ||
} | ||
|
||
private def getValueAtPath(s: StructType, row: InternalRow, p: Seq[Int]): Option[VariantVal] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT:
private def getValueAtPath(s: StructType, row: InternalRow, p: Seq[Int]): Option[VariantVal] = { | |
private def getValueAtPath(schema: StructType, row: InternalRow, path: Seq[Int]): Option[VariantVal] = { |
Also, can we comment on what this will return? It looks it will return None
of the field will be null?
} | ||
|
||
/** | ||
* Update each VariantType with its inferred schema. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it return a copy of the schema (with the updates), or does it update in place?
What changes were proposed in this pull request?
When writing Variant to Parquet, we want the shredding schema to adapt to the data being written on a per-file basis. This PR adds a new output writer that buffers the first few rows before starting the write, then uses the content of those rows to determine a shredding schema, and only then creates the Parquet writer with that schema.
The heuristics for determining the shredding schema are currently fairly simple: if a field appears consistently with a consistent type, we create
value
andtyped_value
, and if it appears with an inconsistent type, we only createvalue
. We drop fields that occur in less than 10% of sampled rows, and have an upper bound of 300 total fields (countingvalue
andtyped_value
separately) to avoid creating excessively wide Parquet schemas, which can cause performance issues.Why are the changes needed?
Allows Spark to make use of the Variant shredding spec without requiring the user to manually set a shredding schema.
Does this PR introduce any user-facing change?
Only if
spark.sql.variant.inferShreddingSchema
andspark.sql.variant.writeShredding.enabled
are both set to true. They currently false by default.How was this patch tested?
Unit tests in PR.
Was this patch authored or co-authored using generative AI tooling?
No.