[SPARK-48701][SQL] Make PandasMode collation-aware - #58233
Open
vinodkc wants to merge 1 commit into
Open
Conversation
Co-authored-by: Isaac <no-reply@databricks.com>
uros-b
approved these changes
Aug 24, 2026
Member
|
Thank you @vinodkc! cc @HyukjinKwon for Pandas |
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?
Make the internal
PandasModeaggregate (pandas_mode) collation-aware.This PR extracts the folding logic into a shared
ModeCollationAwaretrait mixed into bothModeandPandasMode, and callsgetCollationAwareBufferinPandasMode.eval(with a null-key guard, sincePandasModestores a null key whenignoreNA = false). Eval-only change;Mode's behavior is unchanged.Why are the changes needed?
pandas_mode(behind Pandas API on SparkSeries.mode()/DataFrame.mode()) silently returns wrong results on non-binary collated strings: collation-equal values (e.g.'b'and'B'underUTF8_LCASE) are counted as separate keys.Example : a
UTF8_LCASEcolumn with valuesa, a, b, B, B(so'b'and'B'are equal: countsa=2,b/B=3, mode should be the singleb/Bgroup):Before: ['B', 'a'] # wrong: a=2 ties B=2, b counted separately (2 modes)
After: ['b'] # correct: b/B fold to one group of count 3, winning (1 mode)
Does this PR introduce any user-facing change?
Yes. pandas_mode (Pandas API on Spark Series.mode() / DataFrame.mode()) now returns the correct mode(s) on columns with a non-binary collation, instead of counting collation-equal strings as distinct values. Results on default (UTF8_BINARY) collation and non-string types are unchanged.
How was this patch tested?
Added unit tests in
CollationAggregationSuiteWas this patch authored or co-authored using generative AI tooling?
Yes. Generated-by: Claude code.