[SPARK-57175][SQL] Extend nested column pruning to exists and forall over arrays of structs#56226
Open
sunchao wants to merge 2 commits into
Open
[SPARK-57175][SQL] Extend nested column pruning to exists and forall over arrays of structs#56226sunchao wants to merge 2 commits into
sunchao wants to merge 2 commits into
Conversation
…over arrays of structs
Member
Author
viirya
approved these changes
Jun 1, 2026
Member
viirya
left a comment
There was a problem hiding this comment.
Nicely scoped follow-up to SPARK-57022. I traced the shared helper extraction through both SchemaPruning.getRootFields and ProjectionOverSchema, and verified the test fixtures and expected values. The change is correct and safe.
A few things I checked that are worth noting:
ArrayExists.copy(argument =, function =)leavesfollowThreeValuedLogicuntouched, so three-valued semantics are preserved even thoughProjectionOverSchemawildcards that field in the pattern. Good.- The whole-element fallback is correct: when the lambda consumes the full element,
collectLambdaVariableFieldsreturnsNone, so the helper falls back to recursing children and the full schema is retained. The two "do not prune" tests pin this down. - Excluding
ArrayFilter/ArraySortis both correct and safe. They return the original elements (dataType = argument.dataType), so pruning the argument's element struct would corrupt the output type. And leaving them on the defaultgetRootFieldsbranch can't cause silent mis-pruning, becauseSelectedFieldterminates only at anAttribute-- aGetStructFieldchain rooted at aNamedLambdaVariableyields no narrow root field, so the argument's full schema is preserved. - The
forallover an empty array returningtrue(Contact 1 has no friends) is a nice vacuous-truth edge to have covered.
Two optional nits, neither blocking:
- In the datasource tests,
existsis called fully-qualified (org.apache.spark.sql.functions.exists) whileforallis bare. Understandable (bareexistscollides with Scala's collection method), but the asymmetry reads oddly -- a short alias import or a one-line comment would make the intent clearer. - The Catalyst-level
SchemaPruningSuiteonly covers the pruning case; the whole-element fallback is only exercised at the datasource layer. A unit-level assertion for thex -> x(returnsNone) path would round out coverage, though the datasource tests already cover it functionally.
LGTM.
peter-toth
approved these changes
Jun 2, 2026
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.
Why are the changes needed?
SPARK-57175 follows SPARK-57022, which added nested column pruning for
transformoverarray<struct>inputs. The same optimization does not currently apply to theexistsandforallhigher-order array functions.For example:
If
rule_resultscontains additional fields, Spark currently retains the full element struct in the scan schema even though the predicate only readsrule_version. This causes unnecessary Parquet and ORC input reads for wide array element schemas.What changes were proposed in this PR?
ArrayTransformwithArrayExistsandArrayForAll.GetStructFieldordinals against the projected element schema after pruning.ArrayFilterandArraySortremain out of scope because they return original input elements and require a different downstream-schema design.Does this PR introduce any user-facing change?
Yes. Eligible queries using
existsorforallover arrays of structs can read a narrower input schema. Query results and SQL APIs are unchanged.How was this patch tested?
JAVA_HOME=/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home PATH=/opt/homebrew/opt/openjdk@17/bin:$PATH build/sbt "catalyst/testOnly org.apache.spark.sql.catalyst.expressions.SchemaPruningSuite" "sql/testOnly org.apache.spark.sql.execution.datasources.parquet.ParquetV1SchemaPruningSuite org.apache.spark.sql.execution.datasources.parquet.ParquetV2SchemaPruningSuite org.apache.spark.sql.execution.datasources.orc.OrcV1SchemaPruningSuite org.apache.spark.sql.execution.datasources.orc.OrcV2SchemaPruningSuite -- -z Array"git diff --checkWas this patch authored or co-authored using generative AI tooling?
Generated-by: Codex (GPT-5)