Skip to content

fix(spark): report aggregate DISTINCT handling - #26753

Draft
Dodothereal wants to merge 1 commit into
apache:mainfrom
Dodothereal:fix/spark-aggregate-distinct-handling
Draft

Dodothereal wants to merge 1 commit into
apache:mainfrom
Dodothereal:fix/spark-aggregate-distinct-handling

Conversation

@Dodothereal

Copy link
Copy Markdown
Contributor

Closes #25375.

The Spark aggregates all reported the default Sensitive handling for DISTINCT, even though their accumulators behave differently. collect_set always deduplicates, while collect_list, try_sum, and avg do not read the DISTINCT flag.

This declares collect_set as Insensitive and the other three as Unsupported. The optimizer can now remove a redundant DISTINCT from collect_set. For the other functions, this records the current gap; it does not make their accumulators support DISTINCT.

Making Unsupported explicit is conservative for join elimination: an aggregate with a filter no longer counts as duplicate-insensitive and therefore keeps an inner join instead of being eliminated as a semijoin. SingleDistinctToGroupBy still deduplicates single-argument forms, so their results stay the same.

The tests cover the four declarations and check through EXPLAIN that collect_set(DISTINCT a) is planned as collect_set(a). A result test with duplicates covers the same path.

Tests run:

  • cargo test -p datafusion-spark --lib aggregate
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- spark/aggregate/collect --test-threads 1
  • cargo test -p datafusion-optimizer --lib
  • cargo clippy -p datafusion-spark --lib -- -D warnings
  • cargo fmt --all -- --check

The Spark aggregates all used the default Sensitive declaration, although collect_set always deduplicates and the other three accumulators do not read DISTINCT at all. Record what each implementation actually does so the optimizer can remove a redundant modifier from collect_set.

The Unsupported declarations document the current gap for collect_list, try_sum, and avg. Nothing rejects those forms yet, so their behavior is unchanged.
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) spark Changes to datafusion-spark crate labels Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

spark Changes to datafusion-spark crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spark aggregate functions do not report distinct_handling

1 participant