Skip to content

Conversation

@somasays
Copy link
Contributor

@somasays somasays commented Dec 8, 2025

Summary

Root Cause

When writing data to a table with an optional map field, the schema compatibility check incorrectly failed if the data was missing that field. This happened because the validator descended into the map's internal key field (which is always required=True per Iceberg spec) even when the parent map field was optional and missing.

The Fix

Modified _SchemaCompatibilityVisitor.field() in pyiceberg/schema.py to check if the field exists in the provided schema before descending into children. If an optional parent field is missing, we skip child validation entirely.

Test plan

  • All 328 schema tests pass
  • Lint passes (make lint)
  • New tests cover:
    • Optional map field missing (should pass) - main fix
    • Required map field missing (should fail)
    • Optional list field missing (should pass)
    • Optional struct field missing (should pass)
    • Optional map field present (should pass)

When writing data to a table with an optional map field, the schema
compatibility check incorrectly failed if the data was missing that
field. This happened because the validator descended into the map's
internal key field (which is always required) even when the parent
map field was optional and missing.

The fix modifies _SchemaCompatibilityVisitor.field() to skip child
validation when an optional parent field is missing from the provided
schema.

Closes apache#2684
@somasays somasays force-pushed the fix-optional-map-field-write branch from 76084a8 to c7ba4b1 Compare December 8, 2025 10:53
Copy link
Contributor

@Fokko Fokko left a comment

Choose a reason for hiding this comment

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

This makes sense to me, thanks for fixing this edge case 👍

@Fokko Fokko merged commit 65ba595 into apache:main Dec 8, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Writing to a table with an optional map field fails if data is missing map field

2 participants