[Spark] Optimize InferRebalanceAndSortOrders to support skip non-cheap keys and local sort#7551
Open
ulysses-you wants to merge 1 commit into
Open
[Spark] Optimize InferRebalanceAndSortOrders to support skip non-cheap keys and local sort#7551ulysses-you wants to merge 1 commit into
ulysses-you wants to merge 1 commit 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.
Why are the changes needed?
The
InferRebalanceAndSortOrdersoptimization infers rebalance partitioningand sort columns from the original query (e.g. join keys) to improve the
compression ratio before writing. In practice this can regress performance in
two cases:
non-trivial expressions, evaluating them during the extra shuffle and local
sort adds CPU cost that can outweigh the compression benefit.
from rebalance and do not want the additional local
Sortbefore writing.This PR makes the inference tunable so users can opt out of expensive-column
inference and skip the sort while keeping the rebalance.
Two new configs are added (default behavior is unchanged except that inference
now restricts to cheap columns by default):
spark.sql.optimizer.inferRebalanceAndSortOrdersWithCheapColumns.enabledtruespark.sql.optimizer.skipInferRebalanceAndSortOrders.enabledfalseBoth configs only take effect when
spark.sql.optimizer.inferRebalanceAndSortOrders.enabledistrue.Implementation notes:
InferRebalanceAndSortOrders.infergains anonlyInferWithCheapColumnsparameter. A new
isCheaphelper classifies an expression as cheap when it isan
Attribute/OuterReference/BoundReference, foldable, or anAlias/ExtractValuewhose children are all cheap. When the flag is set and anyinferred column is not cheap, inference returns
Noneand the rule falls backto a plain rebalance.
RebalanceBeforeWritingBase.buildRebalancereads the two new configs andskips the local
SortwhenskipInferRebalanceAndSortOrders.enabledis on.4.1,4.0, and3.5extension modules, and the rule docs table is updated.
How was this patch tested?
Skip inferring sort orders— end-to-end check that enablingskipInferRebalanceAndSortOrders.enableddrops the inferred localSortwhile keeping the
RebalancePartitions.Infer rebalance and sort orders only with cheap columns— unit-levelcheck on
InferRebalanceAndSortOrders.infer: expensive join keys(
col1 + 1) are not inferred when the cheap-column restriction is on and areinferred when it is off; cheap attribute keys are inferred regardless.
RebalanceBeforeWritingSuiteof all three modules.Ran locally (all green):
dev/reformatrun; no style changes required.Was this patch authored or co-authored using generative AI tooling?
Yes.
Assisted-by: Claude Opus 4.8