Skip to content

Conversation

cashmand
Copy link
Contributor

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 and typed_value, and if it appears with an inconsistent type, we only create value. We drop fields that occur in less than 10% of sampled rows, and have an upper bound of 300 total fields (counting value and typed_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 and spark.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.

@github-actions github-actions bot added the SQL label Sep 22, 2025
@cashmand cashmand changed the title [SPARK-53659] Infer Variant shredding schema when writing to Parquet [SPARK-53659][SQL] Infer Variant shredding schema when writing to Parquet Sep 22, 2025
Copy link
Contributor

@gene-db gene-db left a 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.
Copy link
Contributor

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.
Copy link
Contributor

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] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT:

Suggested change
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.
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants