[SPARK-58063][SQL] Add column pruning for BIN BY#57157
Open
vranes wants to merge 3 commits into
Open
Conversation
cloud-fan
reviewed
Jul 9, 2026
cloud-fan
left a comment
Contributor
There was a problem hiding this comment.
0 blocking, 1 non-blocking, 0 nits. Correct and result-safe; one non-blocking design suggestion on the pruning mechanism.
Design / architecture (1)
Optimizer.scala:1139:unrequiredChildIndexmachinery buys only <=16 bytes/row and theGenerateanalogy doesn't transfer toBinBy— see inline
Verification
Traced the Project(_, BinBy) rewrite: the pruning Project (prunedChild) keeps every kernel-read input (requiredAttrs ⊇ b.references), and the unrequired set reduces to {rangeStart, rangeEnd} -- p.references — DISTRIBUTE columns are gated out, so no kernel input is ever pruned. Result-equivalent for empty / single / many-row input, and the synthesized Project forwards child attributes with unchanged nullability/dataType/exprId (no metadata mismatch). The rewrite is correct; the finding below is a simplification, not a correctness concern.
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 PR adds column pruning to the logical
BinByoperator, so aProjectoverBIN BYtrims fully-unused pass-through columns from the operator's child.A
ColumnPruningarm forProject(_, b: BinBy)rewrites the child viaprunedChild, keeping the range and DISTRIBUTE inputs the kernel reads (requiredAttrs = p.references -- b.producedAttributes ++ b.references):This is the plain
prunedChildform used byExpand/MergeRows, and it adds no field toBinBy. It is enabled by the produced-attributes shape from SPARK-57858: the scaled DISTRIBUTE columns and appended columns are produced attributes with freshExprIds, soBinBy.referenceslists exactly the child inputs the kernel reads, andprunedChildcan never prune those away.An earlier revision followed the
Generateshape: aunrequiredChildIndex: Seq[Int]field plus arequiredChildOutputlazy val and anoutputoverride, so the range columns could stay in the child (read for bin math) while dropping fromoutput. Per review, that was dropped: the set the field can ever exclude is provably{rangeStart, rangeEnd} -- p.references(at most two fixed-width timestamps, and only when the range columns are not selected, which the outerProjecttrims anyway), so the field bought at most ~16 bytes/row and added positional operator surface. TheGenerateanalogy does not transfer becauseGenerateexcludesgenerator.references, which are arbitrary-width.BinBy.outputstayschild.output.map(replace) ++ appendedAttributes.Why are the changes needed?
BIN BYis row-multiplying, so forwarding unused pass-through columns through the operator wastes work proportional to the fan-out. Pruning them at the child, asGenerate/Expandalready do, avoids carrying columns the query does not use.Does this PR introduce any user-facing change?
No.
BIN BYis gated off by default (spark.sql.binByRelationOperator.enabled, SPARK-57440). This is an optimizer-only change that does not alter query results.How was this patch tested?
ColumnPruningSuite: aProjectoverBinByprunes the unused pass-through column while keeping the range and DISTRIBUTE inputs in the child. Uses a newbinBytest DSL helper (dsl/package.scala, alongsidegenerate).ResolveBinBySuite: existing analyzer coverage, green (the pruning arm is optimizer-only;BinByis unchanged frommaster).Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Anthropic)