[SPARK-59147][SQL] Preserve struct nullness when SELECT * EXCEPT drops a nested field - #58477
Open
Vivek1106-04 wants to merge 1 commit into
Open
[SPARK-59147][SQL] Preserve struct nullness when SELECT * EXCEPT drops a nested field#58477Vivek1106-04 wants to merge 1 commit into
Vivek1106-04 wants to merge 1 commit into
Conversation
…s a nested field Nested-field `SELECT * EXCEPT (col.field)` rebuilds the enclosing struct with `CreateStruct`, which is never nullable. A NULL struct was therefore rewritten into a non-NULL struct whose remaining fields are NULL, silently changing query results for `IS NULL` checks, filters, joins and serialized output. Guard the reconstruction with `If(IsNull(col), Literal(null, dataType), ...)` when the original column is nullable, matching what `UpdateFields.evalExpr` already does for `DropField`. The pipe `|> DROP <col>.<field>` operator shares this code path and is fixed as well.
uros-b
reviewed
Sep 3, 2026
uros-b
left a comment
Member
There was a problem hiding this comment.
The fix seems correct, minimal, and well-tested to me. Thank you @Vivek1106-04
Please ping @viirya and/or @cloud-fan for additional review!
Author
|
@viirya ! and @cloud-fan - requesting for a review. Thank You |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changes were proposed in this pull request?
UnresolvedStarExceptOrReplace.filterColumnsrewrites a struct column when the EXCEPT listnames a nested field: it extracts the retained fields and wraps them back into a
CreateStruct.CreateStructresolves toCreateNamedStruct, which is never nullable, sothe rebuilt struct is always non-NULL even when the original struct expression evaluated to
NULL.
This PR guards the reconstruction with
If(IsNull(col), Literal(null, dataType), ...)whenthe original column is nullable, the same way
UpdateFields.evalExpralready does forDropField.Why are the changes needed?
Dropping a nested field silently turns a NULL struct into a non-NULL struct whose remaining
fields are NULL. This is a silent correctness bug: nullable structs commonly come from
parsed JSON and from the null-producing side of outer joins, and afterwards
IS NULLchecks, filters, joins,
COALESCEand serialized output all behave differently.Before:
After:
The same path backs the pipe
|> DROP <col>.<field>operator, which had the same problem.Does this PR introduce any user-facing change?
Yes.
SELECT * EXCEPT (col.field)(and|> DROP col.field) now returns NULL for a row whosecolis NULL, instead of a struct of NULL fields. The result type is unchanged. Thiscorrects wrong results; there is no behavior change for non-nullable structs or for rows
whose struct is not NULL.
How was this patch tested?
Added cases to
selectExcept.sqlcovering a NULL top-level struct, a NULL nested struct, andstruct expansion (
SELECT data.* EXCEPT (s2.c)), and regenerated the golden files. The newcases fail on master and pass with the fix.
pipe-operators.sqlanalyzer results wereregenerated to reflect the added null guard; its query results are unchanged.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5)