Skip to content

Commit 2326917

Browse files
author
Patrick Ribbsaeter
authored
fix: adapt input batches with stricter nested nullability to planned schema in aggregation (apache#24394)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> Closes apache#24069. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> In DataFusion, in-memory table sources such as `MemTable::try_new` accept `RecordBatch`es whose schemas are stricter than the table's declared schema via `Schema::contains(&batches_schema)` (e.g. nullable nested fields declared on the table vs non-nullable nested fields in the input batches). However, `MemoryStream` previously advertised the declared table schema while emitting the underlying stricter `RecordBatch`es without adapting them. When downstream operators (such as `AggregateExec` with `array_agg` or distinct aggregation) received batches with stricter nested schemas, runtime type mismatch errors occurred (e.g. apache#24069). ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> 1. **`datafusion_common::nested_struct::adapt_batch_to_schema`**: - Adapts `RecordBatch`es whose nested schemas are stricter than a target schema. - Recursively reconstructs compatible nested Struct/List types. - Explicitly handles Dense and Sparse Union schema conformance while preserving type IDs and dense offsets without copying buffer data. - Requires exact Union type-ID sets and matching modes. - Does not broaden generic SQL CAST semantics (`requires_nested_struct_cast` remains untouched). 2. **`MemoryStream` Producer Boundary Normalization (`datafusion-physical-plan/src/memory.rs`)**: - Fixes the producer-side invariant exposed by apache#24069. - When batches have stricter schemas accepted by `MemTable::try_new`, `MemoryStream::poll_next` normalizes emitted batches using `adapt_batch_to_schema(batch, &self.schema)` whenever the runtime batch schema differs from `self.schema` and `self.schema.contains(batch.schema())`. - Ensures all `RecordBatch`es emitted by `MemoryStream` conform exactly to `stream.schema()`. 3. **Regression Coverage**: - Direct `MemoryStream` regressions in `memory.rs` verifying emitted batches match the advertised schema, including projection handling. - Unit tests in `nested_struct.rs` covering nested Struct and Dense/Sparse Union adaptation, unpacked scalar values, type IDs, offsets, reordered IDs, and incompatible Union layouts. - End-to-end SQL aggregation integration tests in `nested_nullability.rs` covering standard, DISTINCT, and spilling aggregations for apache#24069. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes: - `datafusion-common` unit tests for `adapt_batch_to_schema` and Union adaptation (`test_adapt_batch_to_schema_*`). - `datafusion-physical-plan` unit tests for `MemoryStream` emitted batch schema conformance and projection (`test_memory_stream_emitted_batch_matches_declared_schema*`). - `datafusion` core SQL integration tests in `datafusion/core/tests/sql/aggregates/nested_nullability.rs`. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> No. Queries aggregating in-memory tables whose batches have stricter nested nullability than the table schema now succeed as expected.
1 parent 482d1fd commit 2326917

4 files changed

Lines changed: 1093 additions & 3 deletions

File tree

0 commit comments

Comments
 (0)