Fixed runtime type mismatch errors in Apache DataFusion when in-memory table sources had stricter nested nullability than the declared table schema. Added recursive Struct/List/Union schema adaptation at the MemoryStream producer boundary, ensuring all emitted RecordBatches conform exactly to the advertised schema.
Role
Category
Query Engine & Arrow Runtime
Engineering discipline
UPSTREAM SHIPPED

Patrick Ribbsaeter · Independent open-source contribution
Engineering objective
MemTable::try_new accepts RecordBatches with stricter schemas than the declared table schema via Schema::contains. However, MemoryStream advertised the declared schema while emitting the underlying stricter RecordBatches without adaptation. Downstream operators such as AggregateExec with array_agg or distinct aggregation received batches with stricter nested schemas, causing runtime type mismatch errors.
Constraints
Approach
Implemented adapt_batch_to_schema in datafusion_common::nested_struct for recursive schema adaptation.
Reconstructed compatible nested Struct/List types, explicitly handling Dense and Sparse Union conformance.
Normalized batches at the MemoryStream producer boundary in poll_next when runtime batch schema differs from self.schema.
Added regression coverage: unit tests for adapt_batch_to_schema, MemoryStream emission tests, and end-to-end SQL aggregation integration tests.
Technical validation
Observable engineering evidence. Discipline: UPSTREAM SHIPPED
Public PR
PR #24394 · shipped
Issue Resolved
#24069
Review Comments
8 comments
Labels
core, common, physical-plan
Test Coverage
Unit + Integration
Release
v55.1.0 · Sep 2026
Public evidence
Outcome
Queries aggregating in-memory tables with stricter nested nullability now succeed as expected
Recursive Struct/List/Union schema adaptation handles all nested type combinations
MemoryStream producer boundary guarantees schema conformance for all downstream operators
Merged upstream and shipped in Apache DataFusion v55.1.0 release with 8 review comments and 4 labels (core, common, physical-plan)
Technologies
Next engineering system
MICROSOFT OPEN SOURCE
Have an ambitious AI system to build?
I work across AI architecture, product engineering, private inference, automation, SaaS infrastructure, and production hardening.