[SPARK-56032][SQL] Support subexpression elimination in FilterExec whole-stage codegen#54862
Open
LuciferYang wants to merge 3 commits intoapache:masterfrom
Open
[SPARK-56032][SQL] Support subexpression elimination in FilterExec whole-stage codegen#54862LuciferYang wants to merge 3 commits intoapache:masterfrom
FilterExec whole-stage codegen#54862LuciferYang wants to merge 3 commits intoapache:masterfrom
Conversation
FilterExec whole-stage codegen
| // evaluateSubExprEliminationState must be called after predicate code generation; | ||
| // it emits the pre-computation code and marks states as consumed. | ||
| (inputVarsEvalCode, | ||
| ctx.evaluateSubExprEliminationState(subExprs.states.values), predCode) |
Member
There was a problem hiding this comment.
nit. Indentation looks a little strange. For this one, one liner might be better.
- (inputVarsEvalCode,
- ctx.evaluateSubExprEliminationState(subExprs.states.values), predCode)
+ (inputVarsEvalCode, ctx.evaluateSubExprEliminationState(subExprs.states.values), predCode)
Contributor
Author
There was a problem hiding this comment.
changed to a single line
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala
Outdated
Show resolved
Hide resolved
sql/core/src/test/scala/org/apache/spark/sql/execution/WholeStageCodegenSuite.scala
Outdated
Show resolved
Hide resolved
Member
|
cc @peter-toth , too |
Contributor
Author
|
Thank you for your review. @dongjoon-hyun |
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 subexpression elimination support to
FilterExec.doConsumein the whole-stage codegen path.The implementation follows the established CSE pattern used by
ProjectExec,HashAggregateExec, andAggregateCodegenSupport:otherPreds(excludingnotNullPreds, which are simpleIsNotNullchecks with no CSE value) and runctx.subexpressionEliminationForWholeStageCodegento discover common subexpressions.generatePredicateCodeinsidectx.withSubExprEliminationExprsso thatgenCodecalls within can look up pre-computed subexpressions.ctx.evaluateSubExprEliminationStatebefore the predicate checks.Key design decisions:
otherPredsparticipate in CSE —notNullPredsare guaranteed to beIsNotNullchecks (by FilterExec's constructor partitioning logic) with no CSE value; including them would interfere with equivalence analysis.FilterExecsetsusedInputs = AttributeSet.emptyto defer input evaluation for short-circuit optimization. However,subexpressionEliminationForWholeStageCodegen's internalgetLocalInputVariableValueshas a side effect: it clearsctx.currentVars[i].codefor input variables referenced by common subexpressions. IfnotNullPredsreference the same input variables,generatePredicateCode'sevaluateRequiredVariableswould find empty code and skip their declarations, causing "is not an rvalue" compilation errors. By pre-evaluating the input variables referenced byotherPredsbefore CSE analysis, we ensure their codes are already consumed, avoiding this conflict.notNullcheck would have short-circuited. For expensive shared expressions (e.g.,from_jsonappearing in 500 predicates), the benefit of evaluating once vs N times far outweighs the cost of losing short-circuit on the CSE portion. When there are no common subexpressions,subExprsCodeis empty and this path has zero overhead. This is safe because Spark SQL expressions handle null inputs gracefully (returning null rather than throwing).Why are the changes needed?
Support subexpression elimination in FilterExec whole-stage codegen, and with this fix, the
from_jsoncodegen (PR #48466) can be safely re-enabled in a follow-up PR.Does this PR introduce any user-facing change?
No.
How was this patch tested?
Added two new test cases in
WholeStageCodegenSuiteWas this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Sonnet 4.6