Skip to content

[SPARK-57179][SQL] Skip statically-dead null branches in GenerateOrdering for non-nullable sort keys#56230

Open
gengliangwang wants to merge 1 commit into
apache:masterfrom
gengliangwang:spark-ordering-deadnull-codegen
Open

[SPARK-57179][SQL] Skip statically-dead null branches in GenerateOrdering for non-nullable sort keys#56230
gengliangwang wants to merge 1 commit into
apache:masterfrom
gengliangwang:spark-ordering-deadnull-codegen

Conversation

@gengliangwang
Copy link
Copy Markdown
Member

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 GenerateUnsafeProjection uses) 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 (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 to OrderingSuite (atomic + complex types, mixed asc/desc to cover both the comp and -comp emissions, two keys to cover the 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)

…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
@gengliangwang gengliangwang requested a review from LuciferYang May 31, 2026 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant