Kafka Connect: iceberg.tables.schema-force-optional only applies to top-level columns — nested struct fields are still created as required
Apache Iceberg version
1.11.0 (also reproduced on a recent 1.12.0-SNAPSHOT build)
Query engine
Kafka Connect
Please describe the bug 🐞
With iceberg.tables.schema-force-optional=true and auto-create enabled, the sink creates top-level columns as optional (as documented), but fields nested inside a struct column are still created as required when the incoming Connect schema marks them required.
Concrete case: a CDC pipeline using an SMT that attaches a metadata struct to each record (e.g. the bundled DebeziumTransform, whose _cdc.key struct carries the source table's primary-key fields). On auto-create, the struct's child fields inherit the Connect schema's required, so every table is born with required nested fields even though the operator asked for force-optional.
Why this matters: if the source table's primary key later changes (ALTER TABLE ... DROP/ADD PRIMARY KEY), the old key fields disappear from incoming records, but the Iceberg schema still requires them inside the struct. The writer then fails on every record for that table (an NPE in the writer path in our case), and because this happens inside put(), errors.tolerance=all / DLQ does not catch it — the task crash-loops and the whole connector's throughput stalls. We measured a multi-day bronze-ingest outage for one source before diagnosing; the fleet-wide fix was making ~16K nested key fields optional by hand with metadata-only schema commits.
Expected behavior: schema-force-optional=true applies recursively — every field at every nesting level created (or evolved in) by the sink should be optional, so a shrinking source schema can never make a table unwritable.
Happy to test a fix against a busy CDC deployment (thousands of auto-created tables), and to propose a patch if maintainers agree the recursive interpretation is the intended semantics.
Willingness to contribute
- [x] I can contribute a fix for this bug independently
- [x] I would be willing to contribute a fix for this bug with guidance from the Iceberg community
- [ ] I cannot contribute a fix for this bug at this time
I couldn't reproduce this on main, and the auto-create code hasn't changed since 1.11.0. With iceberg.tables.schema-force-optional=true, SchemaUtils.toIcebergType makes struct fields, list elements and map values optional at every nesting level. I ran your scenario end to end with the bundled DebeziumTransform and an in-memory catalog. The auto-created table has _cdc, _cdc.key and _cdc.key.account_id all optional. After a PK change (key {account_id} → {uuid}), the sink adds _cdc.key.uuid and keeps writing.
The NPE does reproduce when the table was created with the flag off, or outside the sink. In that case _cdc.key.account_id is required, and turning the flag on later doesn't loosen it. Evolution only makes an existing column optional when the record's schema marks it optional. Were the affected tables created before the flag was enabled?
Changing evolution to loosen every required column under this flag, as the stale #17556 did, wouldn't cover your case: a key field that has already left the records is never visited. It would also break tables with identifier fields. The sink would try to make the identifier column optional on every record, and the schema commit fails with Cannot add field ... as an identifier field: not a required field. For tables that already have required nested fields, a one-time ALTER COLUMN ... DROP NOT NULL is still the fix.