[SPARK-57179][SQL] Skip statically-dead null branches in GenerateOrdering for non-nullable sort keys#56230
Open
gengliangwang wants to merge 1 commit into
Open
Conversation
…ring for non-nullable sort keys
### What changes were proposed in this pull request?
In `GenerateOrdering.genComparisons`, each order key emits a four-way branch:
`if (l.isNull && r.isNull) {} else if (l.isNull) {..} else if (r.isNull) {..}
else { compare }`. When the order key is statically non-nullable, both
`l.isNull` and `r.isNull` are `FalseLiteral`, so the three null-handling
branches are dead and only the `else { compare }` ever runs.
This patch detects that case (`l.isNull == FalseLiteral && r.isNull ==
FalseLiteral`, the same idiom used by `GenerateUnsafeProjection`) and emits only
the value comparison, wrapped in a block so `comp` stays scoped when
`splitExpressions` concatenates multiple comparisons. The nullable path is
unchanged.
### Why are the changes needed?
Part of SPARK-56908 (umbrella). `GenerateOrdering` backs `SortExec`, range
partitioning, window, and sort-merge join; sort keys are very commonly
non-nullable, so this removes dead branches from a hot, widely-generated
comparator.
### Does this PR introduce _any_ user-facing change?
No. The compiled behavior is identical; only the emitted Java source text changes.
### How was this patch tested?
Added `GenerateOrdering with non-nullable keys: <type>` cases to `OrderingSuite`
(atomic + complex types, mixed asc/desc to cover both `comp`/`-comp`, two keys
to cover `comp` scoping), cross-checked against `InterpretedOrdering`.
```
build/sbt "catalyst/testOnly *OrderingSuite" # 89/89
build/sbt "sql/testOnly org.apache.spark.sql.execution.SortSuite" # 255/255
```
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)
Co-authored-by: Isaac
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?
In
GenerateOrdering.genComparisons, each order key emits a four-way branch:if (l.isNull && r.isNull) {} else if (l.isNull) {..} else if (r.isNull) {..} else { compare }. When the order key is statically non-nullable, bothl.isNullandr.isNullareFalseLiteral, so the three null-handling branches are dead and only theelse { compare }ever runs.This patch detects that case (
l.isNull == FalseLiteral && r.isNull == FalseLiteral-- the same idiomGenerateUnsafeProjectionuses) and emits only the value comparison, wrapped in a block socompstays scoped whensplitExpressionsconcatenates multiple comparisons. The nullable path is unchanged.Why are the changes needed?
Part of SPARK-56908 (umbrella).
GenerateOrderingbacksSortExec, range partitioning, window, and sort-merge join; sort keys are very commonly non-nullable, so this removes dead branches from a hot, widely-generated comparator (helping with the JVM 64KB method / constant-pool limits, Janino compile time, and JIT work).Does this PR introduce any user-facing change?
No. The compiled behavior is identical; only the emitted Java source text changes.
How was this patch tested?
Added
GenerateOrdering with non-nullable keys: <type>cases toOrderingSuite(atomic + complex types, mixed asc/desc to cover both thecompand-compemissions, two keys to cover thecompscoping), cross-checked againstInterpretedOrdering.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)