[SPARK-57241][SQL] Skip statically-dead null checks in StringLocate codegen for non-nullable children#56292
Open
LuciferYang wants to merge 1 commit into
Open
[SPARK-57241][SQL] Skip statically-dead null checks in StringLocate codegen for non-nullable children#56292LuciferYang wants to merge 1 commit into
LuciferYang wants to merge 1 commit into
Conversation
…odegen for non-nullable children StringLocate.doGenCode always emitted a null check for each of its three children (substr, str, start) plus a `boolean isNull = false` declaration, even when a child is non-nullable and its codegen isNull is statically false. This produces dead Java: the 2-arg `locate(substr, str)` form defaults start to a non-null literal, so its guard was an always-true dead branch. Emit each null check only when the corresponding child is nullable, and when the whole expression is non-nullable drop the isNull declaration and return isNull = FalseLiteral so parents can skip their own null checks. Behavior is unchanged: eval and the nesting (a null start returns 0 without evaluating substr/str) are preserved.
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 is a sub-task of SPARK-56908 (reduce generated Java size in whole-stage codegen).
StringLocate.doGenCodealways emitted a null check for each of its three children (substr,str,start), plus aboolean isNull = falsedeclaration, regardless of whether a child can actually be null. For a non-nullable child the codegenisNullis staticallyfalse, so these checks are dead code. In particular the 2-arglocate(substr, str)/position(substr IN str)form defaultsstartto a non-null literal, so theif (!start.isNull)guard was an always-true dead branch on every call.This PR emits each null check only when the corresponding child is nullable, and when the whole expression is non-nullable (i.e. both
substrandstrare non-nullable) it drops theisNulldeclaration and returnsisNull = FalseLiteralso parent expressions can skip their own null check too.The behavior is unchanged.
evaland the existing nesting are preserved: a nullstartreturns0without evaluatingsubstr/str(to conform with Hive), and the result is null iff a non-nullstartmeets a nullsubstrorstr. When all three children are nullable the generated code is identical to before.For example,
locate(a, b)on non-nullable columns goes from:to:
This follows the same pattern as the merged sibling sub-tasks (e.g.
If,NaNvl,AtLeastNNonNulls,CreateNamedStruct).Why are the changes needed?
To reduce the size of the generated Java in whole-stage codegen, as tracked by SPARK-56908. The skipped guards are statically dead, and
locate/positionare common functions, so eliminating the always-truestartguard and the dead null checks shrinks generated code on the hot path with no behavior change.Does this PR introduce any user-facing change?
No. This is a codegen-only change;
eval,nullable,dataType, and results are unchanged, so SQL output and golden files are unaffected.How was this patch tested?
Existing
StringExpressionsSuite"LOCATE" coverage plus newcheckEvaluationcases covering every combination of nullable/non-nullablesubstr/str/start(so each generated codegen template is exercised;checkEvaluationruns interpreted and codegen, with whole-stage codegen both on and off). Also ranorg.apache.spark.sql.StringFunctionsSuite(locate/position).Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 4.8)