Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[SPARK-62677][SQL] Respect spark.sql.subexpressionElimination.enabled in aggregate code-gen - #61187

Open
rdtr wants to merge 1 commit into
apache:masterfrom
rdtr:agg-cse-config
Open

rdtr wants to merge 1 commit into
apache:masterfrom
rdtr:agg-cse-config

Conversation

@rdtr

@rdtr rdtr commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

With whole-stage codegen, AggregateCodegenSupport and HashAggregateExec call ctx.subexpressionEliminationForWholeStageCodegen on the update expressions of the aggregate functions without checking spark.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 subexpressionEliminationForUpdateExprs to AggregateCodegenSupport, which returns no common subexpressions when the config is off, and uses it at the three sites. ProjectExec, FilterExec and ExpandExec already 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:

SET spark.sql.ansi.enabled=true;
SET spark.sql.subexpressionElimination.enabled=false;

CREATE TABLE t(m map<int, int>, m2 map<int, int>, s string) USING parquet;
INSERT INTO t VALUES (NULL, NULL, 'bad'), (map(1, 10, 2, 20), map(2, 5), '2');
SELECT max(m[CAST(s AS INT)]), max(m2[CAST(s AS INT)]) FROM t;

CREATE TABLE t2 USING parquet AS SELECT * FROM VALUES (NULL, 1, 2147483647), (0, 1, 0) AS v(a, b, y);
SELECT avg(y + 1) FILTER (WHERE b < 0) FROM t2;

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 return 20, 5 and NULL when the config is off.

#59798 replaces the same three blocks with a helper that calls subexpressionEliminationForWholeStageCodegen once 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 through subexpressionEliminationForUpdateExprs.

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 runs max(length(md5(s))) and min(length(md5(s))), whose update expressions share md5(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 that md5Hex appears 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, SingleLevelAggregateHashMapSuite and TwoLevelAggregateHashMapSuite, and the new test with SPARK_ANSI_SQL_MODE=false.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

@github-actions github-actions Bot added the SQL label Dec 4, 2026
… 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.
@rdtr rdtr changed the title [SQL] Respect spark.sql.subexpressionElimination.enabled in aggregate code-gen [SPARK-62677][SQL] Respect spark.sql.subexpressionElimination.enabled in aggregate code-gen Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant