[SPARK-58735][SQL] Prune nested fields when computing size of an array of structs - #58456
Open
hemanthboyina wants to merge 2 commits into
Open
[SPARK-58735][SQL] Prune nested fields when computing size of an array of structs#58456hemanthboyina wants to merge 2 commits into
hemanthboyina wants to merge 2 commits into
Conversation
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?
This adds a new optimizer rule, RewriteSizeOfArrayStruct.
When a query calls size() (or array_size()) on a column that is an array of structs, the rule rewrites it to take the size of a single field of that struct instead of the whole array. For example,
size(events) becomes size of events with only one inner field selected. It always picks the smallest field of the struct, following the same approach the existing GenerateOptimization rule already uses for
the explode + count case.
The rewrite runs in the operator optimization batch, just before column pruning, and only when nested schema pruning is enabled.
Why are the changes needed?
size() only needs to know how many elements are in the array. It never looks at the values inside the struct. But today, when you write size() over a whole array-of-struct column, Spark treats it as "the
entire column is needed," so nested column pruning and the Parquet/ORC schema pruning cannot kick in. As a result, every field of the struct is read from disk, even though none of them are used.
The nested pruning machinery only prunes when it sees a field being accessed in the plan. A plain column reference has no field access, so it is read in full. By rewriting the expression to access one small
field, we give the pruner the signal it needs, and it reads just that one field.
Does this PR introduce any user-facing change?
No. The query results are unchanged; only the amount of data read from disk is reduced.
How was this patch tested?
Added unit tests in NestedColumnAliasingSuite that check the rewritten plan reads only a single nested field, and that arrays of primitives and already-pruned expressions are left untouched.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Yes, used Claude Code