[SPARK-58614][SQL] Support DISTINCT aggregates in unbounded preceding window frames - #57815
Open
zml1206 wants to merge 9 commits into
Open
[SPARK-58614][SQL] Support DISTINCT aggregates in unbounded preceding window frames#57815zml1206 wants to merge 9 commits into
zml1206 wants to merge 9 commits into
Conversation
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 support for distinct aggregate window functions whose lower frame bound is
UNBOUNDED PRECEDING, including:UNBOUNDED FOLLOWING.ROWSandRANGEframes.COUNT(DISTINCT ...).LISTAGG(DISTINCT ...).The implementation:
BytesToBytesMapto deduplicate binary-stable keys in memory.UnsafeKVExternalSorterwhen the hash threshold is reached ormemory allocation fails.
spark.sql.windowExec.distinct.hash.fallbackThresholdconfiguration.Bounded and sliding distinct window frames remain unsupported.
Why are the changes needed?
Spark currently rejects all distinct aggregate window functions, including common expressions
such as:
A simple in-memory hash set can consume unbounded memory for partitions with many distinct
values. This implementation combines hash-based deduplication with an external-sort fallback,
providing fast execution for normal workloads while allowing large partitions to spill to disk.
Does this PR introduce any user-facing change?
Yes.
Users can now execute distinct aggregate window functions when the lower frame bound is
UNBOUNDED PRECEDING.
Distinct window functions with bounded lower frames, such as sliding windows, continue to
produce the existing DISTINCT_WINDOW_FUNCTION_UNSUPPORTED analysis error.
Python UDAFs and distinct inputs without a supported ordering remain unsupported.
How was this patch tested?
The test coverage includes:
Growing ROWS and RANGE frames.
Full-partition frames.
COUNT, SUM, AVG, COLLECT_LIST, and LISTAGG with DISTINCT.
Multiple distinct arguments, filters, and null values.
Hash fallback triggered by entry-count and memory thresholds.
External sorter spills and spill metric reporting.
Binary-unstable collations, normalized floating-point values, and empty distinct keys.
Negative tests for unsupported bounded frames.
The relevant targeted SQL window tests and scalastyle checks pass locally. The LISTAGG
SQL golden results were regenerated to cover the newly supported behavior.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex (GPT-5)