Repository navigation
Conversation
… in aggregate code-gen With whole-stage codegen, `AggregateCodegenSupport` (hash and sort aggregate without keys, sort aggregate with keys) and `HashAggregateExec` (regular hash map, vectorized fast hash map) eliminated the common subexpressions of the update expressions even when spark.sql.subexpressionElimination.enabled was false. `ProjectExec`, `FilterExec` and `ExpandExec` check the config, and so do the interpreted and non-whole-stage projections. The aggregate call sites have not checked it since SPARK-14951 added them. This adds `subexpressionEliminationForUpdateExprs` to `AggregateCodegenSupport`, which returns no common subexpressions when the config is off, and uses it at the three call sites.
This branch has not been deployed
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?
With whole-stage codegen,
AggregateCodegenSupportandHashAggregateExeccallctx.subexpressionEliminationForWholeStageCodegenon the update expressions of the aggregate functions without checkingspark.sql.subexpressionElimination.enabled. There are three call sites:AggregateCodegenSupport.generateAggBufferUpdateCode: hash and sort aggregates without keys, and the sort aggregate with keys.HashAggregateExec.doConsumeWithKeys: the regular hash map, and the row-based fast hash map, which reuses its code.HashAggregateExec.doConsumeWithKeys: the vectorized fast hash map.This adds
subexpressionEliminationForUpdateExprstoAggregateCodegenSupport, which returns no common subexpressions when the config is off, and uses it at the three sites.ProjectExec,FilterExecandExpandExecalready check the config, and so do the interpreted and non-whole-stage projections. The aggregate call sites have not checked it since SPARK-14951 added them.Why are the changes needed?
Turning the config off is the usual workaround when subexpression elimination evaluates an expression on a row that would otherwise skip it. For an aggregate it had no effect. With ANSI mode on and the config off, both queries below fail on 4.2.0 and master:
The first fails with
[CAST_INVALID_INPUT] The value 'bad' of the type "STRING" cannot be cast to "INT"(SPARK-62644, #61150) and the second with[ARITHMETIC_OVERFLOW] overflow(SPARK-60393, #59798). With this change they return20, 5andNULLwhen the config is off.#59798 replaces the same three blocks with a helper that calls
subexpressionEliminationForWholeStageCodegenonce for the functions without a FILTER and once for each function with one. Whichever of the two PRs merges second needs a small rebase, and both of those calls should go throughsubexpressionEliminationForUpdateExprs.The issue is also tracked as #61188 on GitHub.
Does this PR introduce any user-facing change?
Yes. With
spark.sql.subexpressionElimination.enabled=false, whole-stage codegen of an aggregate evaluates each update expression on its own instead of sharing common subexpressions, like the other operators already do. The config is internal and defaults to true, so the default behavior does not change.How was this patch tested?
New test in
WholeStageCodegenSuite, "aggregate code-gen eliminates common subexpressions only when spark.sql.subexpressionElimination.enabled is on". It runsmax(length(md5(s)))andmin(length(md5(s))), whose update expressions sharemd5(s), on six paths: hash aggregate without keys, with keys, with a row-based fast hash map and with a vectorized fast hash map, and sort aggregate without and with keys. On each it checks the plan has the code-gen'd aggregate and the result, and thatmd5Hexappears twice as often in the generated code with the config off as with it on. Without the change the counts are equal. Reverting each of the three call sites alone makes the test fail on its own path.Also ran
WholeStageCodegenSuite,DataFrameAggregateSuite,SingleLevelAggregateHashMapSuiteandTwoLevelAggregateHashMapSuite, and the new test withSPARK_ANSI_SQL_MODE=false.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)