Add preserve_empty_as_null option to unnest for Spark explode_outer compatibility #19574
+131
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rationale for this change
Spark's
explode_outerfunction treats empty arrays[]the same as NULL arrays - both produce an output row with NULL. Currently, DataFusion'sunnestwithpreserve_nulls=trueonly handles NULL arrays, and empty arrays produce no output rows.This makes it tricky to achieve Spark-compatible behavior when migrating workloads.
What changes are included in this PR?
Added a new
preserve_empty_as_nullflag toUnnestOptions:false(default): empty arrays produce 0 rows (existing behavior, backwards compatible)true: empty arrays produce 1 row with NULL value (Spark's explode_outer behavior)Example with
preserve_nulls=trueandpreserve_empty_as_null=true:Input:
Output:
Files changed:
datafusion/common/src/unnest.rs- added the new option and builder methoddatafusion/physical-plan/src/unnest.rs- updatedfind_longest_length()to handle empty arraysdatafusion/proto/*- updated proto definitions for serializationHow are these changes tested?
Added new unit test
test_longest_list_length_preserve_empty_as_nullthat verifies:preserve_nullssettingAre these changes safe?
Yes - the default value is
false, so existing behavior is unchanged. Users have to explicitly opt-in to the new behavior.