Skip to content

Spark: Support writing shredded variant in Iceberg-Spark#14297

Merged
huaxingao merged 34 commits intoapache:mainfrom
aihuaxu:spark-write-iceberg-variant
May 6, 2026
Merged

Spark: Support writing shredded variant in Iceberg-Spark#14297
huaxingao merged 34 commits intoapache:mainfrom
aihuaxu:spark-write-iceberg-variant

Conversation

@aihuaxu
Copy link
Copy Markdown
Contributor

@aihuaxu aihuaxu commented Oct 11, 2025

What it does

This PR adds support for writing shredded variants from Spark into Iceberg tables. Variant shredding extracts commonly-typed fields from semi-structured VARIANT columns into dedicated typed Parquet columns (typed_value), enabling predicate pushdown, column pruning, and better read performance.

Key design: Buffered schema inference

Because the shredded schema isn't known at Spark's planning time (DSv2 creates DataWriterFactory on the driver before seeing data), the PR uses a lazy/buffered approach:

  1. A new BufferedFileAppender buffers the first N rows.
  2. A VariantShreddingAnalyzer analyzes the buffered rows to infer the shredded schema.
  3. Once the schema is determined, the real Parquet writer is created and the buffer is flushed.

Shredding heuristics

  • Most common type wins: for each field, the type that appears most frequently becomes the typed_value type.
  • Frequency pruning: fields appearing in less than 10% of sampled rows are dropped.
  • Field cap: maximum 300 shredded fields.
  • Deterministic tie-breaking: explicit priority maps to ensure stable schemas regardless of record order.
  • Decimal special handling: precision/scale must be consistent; if not, decimal is not shredded.
  • Null fields are skipped: JSON null values ({"field": null}) don't create shredded columns.

Co-Authored by: @nssalian

@aihuaxu aihuaxu force-pushed the spark-write-iceberg-variant branch from 16b7a09 to dc4f72e Compare October 11, 2025 21:03
@aihuaxu aihuaxu marked this pull request as ready for review October 11, 2025 21:15
@aihuaxu aihuaxu force-pushed the spark-write-iceberg-variant branch 3 times, most recently from 97851f0 to b87e999 Compare October 13, 2025 16:47
@aihuaxu
Copy link
Copy Markdown
Contributor Author

aihuaxu commented Oct 15, 2025

@amogh-jahagirdar @Fokko @huaxingao Can you help take a look at this PR and if we have better approach for this?

@aihuaxu
Copy link
Copy Markdown
Contributor Author

aihuaxu commented Oct 21, 2025

cc @RussellSpitzer, @pvary and @rdblue Seems it's better to have the implementation with new File Format proposal but want to check if this is acceptable approach as an interim solution or you see a better alternative.

Comment thread parquet/src/main/java/org/apache/iceberg/parquet/ParquetWriter.java Outdated
@pvary
Copy link
Copy Markdown
Contributor

pvary commented Oct 21, 2025

@aihuaxu: Don't we want to do the same but instead of wrapping the ParquetWriter, we could wrap the DataWriter. The schema would be created near the SparkWrite.WriterFactory and it would be easier to move to the new API when it is ready. The added benefit would be that when other formats implement the Variant, we could reuse the code.

Would this be prohibitively complex?

@huaxingao
Copy link
Copy Markdown
Contributor

In Spark DSv2, planning/validation happens on the driver. BatchWrite#createBatchWriterFactory runs on the driver and returns a DataWriterFactory that is serialized to executors. That factory must already carry the write schema the executors will use when they create DataWriters.

For shredded variant, we don’t know the shredded schema at planning time. We have to inspect some records to derive it. Doing a read on the driver during createBatchWriterFactory would mean starting a second job inside planning, which is not how DSv2 is intended to work.

Because of that, the current proposed Spark approach is: put the logical variant in the writer factory, on the executor, buffer the first N rows, infer the shredded schema from data, then initialize the concrete writer and flush the buffer. I believe this PR follow the same approach, which seems like a practical solution to me given DSV2's constraints.

@pvary
Copy link
Copy Markdown
Contributor

pvary commented Oct 22, 2025

Thanks for the explanation, @huaxingao! I see several possible workarounds for the DataWriterFactory serialization issue, but I have some more fundamental concerns about the overall approach.
I believe shredding should be driven by future reader requirements rather than by the actual data being written. Ideally, it should remain relatively stable across data files within the same table and originate from a writer job configuration—or even better, from a table-level configuration.

Even if we accept that the written data should dictate the shredding logic, Spark’s implementation—while dependent on input order—is at least somewhat stable. It drops rarely used fields, handles inconsistent types, and limits the number of columns.
I understand this is only a PoC implementation for shredding, but I’m concerned that the current simplifications make it very unstable. If I’m interpreting correctly, the logic infers the type from the first occurrence of each field and creates a column for every field. This could lead to highly inconsistent column layouts within a table, especially in IoT scenarios where multiple sensors produce vastly different data.
Did I miss anything?

@aihuaxu
Copy link
Copy Markdown
Contributor Author

aihuaxu commented Oct 24, 2025

Thanks @huaxingao and @pvary for reviewing, and thanks to Huaxin for explaining how the writer works in Spark.

Regarding the concern about unstable schemas, Spark's approach makes sense:

  • If a field appears consistently with a consistent type, create both value and typed_value
  • If a field appears with inconsistent types, create only value
  • Drop fields that occur in less than 10% of sampled rows
  • Cap the total at 300 fields (counting value and typed_value separately)

We could implement similar heuristics. Additionally, making the shredded schema configurable would allow users to choose which fields to shred at write time based on their read patterns.

For this POC, I'd like any feedback on whether there are any significant high-level design options to consider first and if this approach is acceptable. This seems hacky. I may have missed big picture on how the writers work across Spark + Iceberg + Parquet and we may have better way.

@github-actions
Copy link
Copy Markdown

This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Nov 24, 2025
@Tishj
Copy link
Copy Markdown

Tishj commented Nov 30, 2025

This PR caught my eye, as I've implemented the equivalent in DuckDB: duckdb/duckdb#19336

The PR description doesn't give much away, but I think the approach is similar to the proposed (interim) solution here: buffer the first rowgroup, infer the shredded schema from this, then finalize the file schema and start writing data.

We've opted to create a typed_value even though the type isn't 100% consistent within the buffered data, as long as it's the most common. I think you're losing potential compression by not doing that.

We've also added a copy option to force the shredded schema, for debugging purposes and for power users.

As for DECIMAL, it's kind of a special case in the shredding inference. We only shred on a DECIMAL type if all the decimal values we've seen for a column/field have the same width+scale, if any decimal value differs, DECIMAL won't be considered anymore when determining the shredded type of the column/field

@github-actions github-actions Bot removed the stale label Dec 1, 2025
@yguy-ryft
Copy link
Copy Markdown
Contributor

This PR is super exciting!
Does this rely on variant shredding support in Spark? Is it supported in Spark 4.1 already, or planned for future releases?

Regarding the heuristics - I'd like to propose adding table properties as hints for variant shredding.
Similarly to properties used for bloom filters, it could be good to introduce something like write.parquet.variant-shredding-enabled.column.col1, which will hint to the writer that this column is important for shredding.
Many variants have important fields for which shredding should be enforced, and other fields which are less central and can be managed with simpler heuristics.
Would love to hear your thoughts!

@aihuaxu
Copy link
Copy Markdown
Contributor Author

aihuaxu commented Jan 9, 2026

This PR caught my eye, as I've implemented the equivalent in DuckDB: duckdb/duckdb#19336

The PR description doesn't give much away, but I think the approach is similar to the proposed (interim) solution here: buffer the first rowgroup, infer the shredded schema from this, then finalize the file schema and start writing data.

That is correct.

We've opted to create a typed_value even though the type isn't 100% consistent within the buffered data, as long as it's the most common. I think you're losing potential compression by not doing that.

I'm still trying to improve the heuristics to use the most common one as shredding type rather than the first one and probably cap the number of shredded fields, etc. but it doesn't need 100% consistent type to be shredded.

We've also added a copy option to force the shredded schema, for debugging purposes and for power users.

Yeah. I think that makes sense for advanced user to determine the shredded schema since they may know the read pattern.

As for DECIMAL, it's kind of a special case in the shredding inference. We only shred on a DECIMAL type if all the decimal values we've seen for a column/field have the same width+scale, if any decimal value differs, DECIMAL won't be considered anymore when determining the shredded type of the column/field

Why is DECIMAL special here? If we determine DECIMAL4 to be shredded type, then we may shred as DECIMAL4 or not shred if they cannot fit in DECIMAL4, right?

@aihuaxu
Copy link
Copy Markdown
Contributor Author

aihuaxu commented Jan 9, 2026

This PR is super exciting! Does this rely on variant shredding support in Spark? Is it supported in Spark 4.1 already, or planned for future releases?

Regarding the heuristics - I'd like to propose adding table properties as hints for variant shredding. Similarly to properties used for bloom filters, it could be good to introduce something like write.parquet.variant-shredding-enabled.column.col1, which will hint to the writer that this column is important for shredding. Many variants have important fields for which shredding should be enforced, and other fields which are less central and can be managed with simpler heuristics. Would love to hear your thoughts!

Yeah. I'm also thinking of that too. Will address that separately. Basically based on read pattern, the user can specify the shredding schema.

Copy link
Copy Markdown

@gkpanda4 gkpanda4 left a comment

Choose a reason for hiding this comment

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

When processing JSON objects containing null field values (e.g., {"field": null}), the variant shredding creates schema columns for these null fields instead of omitting them entirely. This would cause schema bloat.

Adding a null check in ParquetVariantUtil.java:386 in the object() method should fix it.

@aihuaxu aihuaxu force-pushed the spark-write-iceberg-variant branch 2 times, most recently from 2e81d79 to 7e1b608 Compare January 15, 2026 19:35
@aihuaxu
Copy link
Copy Markdown
Contributor Author

aihuaxu commented Jan 15, 2026

When processing JSON objects containing null field values (e.g., {"field": null}), the variant shredding creates schema columns for these null fields instead of omitting them entirely. This would cause schema bloat.

Adding a null check in ParquetVariantUtil.java:386 in the object() method should fix it.

I addressed this null value check in VariantShreddingAnalyzer.java instead. If it's NULL, then we will not add the shredded field.

@aihuaxu aihuaxu force-pushed the spark-write-iceberg-variant branch 4 times, most recently from 7c805f6 to 67dbe97 Compare January 15, 2026 22:50
@qlong
Copy link
Copy Markdown

qlong commented Apr 30, 2026

I looked at VariantShreddingAnalyzer and SparkVariantShreddingAnalyzer, implementation looks good, just minor nit.

The current strategy is to shred aggressively, including fields with multiple incompatible types by picking the most common one. When a field has mixed types, the shredded typed_value is only populated for rows whose value matches the chosen type; other rows still carry the full binary value. This means bounded column reads are not available for mixed-type fields, and the performance gain relative to the added column overhead is not clear.

I am not suggesting the current design is flawed. Shredding parameters like MIN_FIELD_FREQUENCY and MAX_SHREDDED_FIELDS can be tuned or new strategies introduced in follow-ups without breaking existing files. But more performance testing on real query patterns would help inform whether these thresholds need to be user-tunable. I would not block merge on this, assuming the community agrees.

private static final String VALUE = "value";
private static final String ELEMENT = "element";
private static final double MIN_FIELD_FREQUENCY = 0.10;
private static final int MAX_SHREDDED_FIELDS = 300;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe we can make those shredding params configurable in the future, or after more performance testing?


private static class PathNode {
private final String fieldName;
private final Map<String, PathNode> objectChildren = Maps.newTreeMap();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Performance nit: this map is on the hot path, tree map requires string comparison. Maybe change it to hashmap since ordering is not important until after pruning createObjectTypedValue? We do sort there:

// createObjectTypedValue: sort once here
private static Type createObjectTypedValue(PathNode node) {
    List<PathNode> sorted = Lists.newArrayList(node.objectChildren.values());
    sorted.sort(Comparator.comparing(child -> child.fieldName));
    ...
}

@nssalian
Copy link
Copy Markdown
Contributor

Thanks for the reviews @steveloughran @qlong - all great points. I'd like to land this PR as-is and I can follow up with a PR to address these since the PR is already large. I summarized here:

  • Configurable shredding parameters for workload tuning
  • TreeMap to HashMap optimization in PathNode, sort once at schema build time
  • TIE_BREAK_PRIORITY javadoc + reorder STRING above BINARY
  • Debug logging in buildShreddedAppender
  • Switch statement in ParquetFormatModel.set()
  • Docs: qualify query performance claim

None of these affect correctness. Happy to open the follow-up immediately after merge if there is agreement.

Copy link
Copy Markdown

@qlong qlong left a comment

Choose a reason for hiding this comment

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

I focused on shredding analyzer and it looks good to me

Comment thread core/src/main/java/org/apache/iceberg/io/BufferedFileAppender.java Outdated
Comment thread core/src/main/java/org/apache/iceberg/TableProperties.java Outdated
Comment thread core/src/main/java/org/apache/iceberg/TableProperties.java Outdated
Comment thread spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/SparkSQLProperties.java Outdated
Comment thread parquet/src/main/java/org/apache/iceberg/parquet/ParquetFormatModel.java Outdated
@nssalian
Copy link
Copy Markdown
Contributor

nssalian commented May 5, 2026

Will address @huaxingao's comments in an upcoming commit. I also realized that this PR was originally only on Spark 4.1. I'll can add the changes to Spark 4.0 too. Or should I do that in a follow up PR after this is merged?
The sequence would be


GroupType typedValue = variantGroup.getType("typed_value").asGroupType();
assertThat(typedValue.containsField("a")).isTrue();
assertThat(typedValue.containsField("b")).isTrue();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test verifies the shredded schema and the data round-trip. Should we also verify the data is in the typed columns to prove the data is really shredded?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Updated the test with check for the data in the typed_value

// Verify data is in typed columns by reading raw Parquet groups
try (ParquetReader<Group> rawReader =
ParquetReader.builder(
new GroupReadSupport(), new org.apache.hadoop.fs.Path(outputFile.location()))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: import org.apache.hadoop.fs.Path. You can fix this in the followup PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I saw that in another test too here and the TestParquetDataWriter has import java.nio.file.Path so it would conflict. I'm not sure if there is a better way.

Copy link
Copy Markdown
Contributor

@huaxingao huaxingao left a comment

Choose a reason for hiding this comment

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

LGTM

@huaxingao huaxingao merged commit e2a119c into apache:main May 6, 2026
38 checks passed
@huaxingao
Copy link
Copy Markdown
Contributor

Thanks @aihuaxu @nssalian for the PR! Thanks every one for the review!

@nssalian nssalian deleted the spark-write-iceberg-variant branch May 6, 2026 20:15
@nssalian
Copy link
Copy Markdown
Contributor

nssalian commented May 7, 2026

I'll open a follow-up PR to address the pending items here after @pvary's backport PR goes in for Spark 4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.