Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Bug in group_tuples_perfect, tail was not processed properly #19417

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions crates/polars-core/src/frame/group_by/perfect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ where
multithreaded &= POOL.current_num_threads() > 1;
// The latest index will be used for the null sentinel.
let len = if self.null_count() > 0 {
// we add one to store the null sentinel group
num_groups + 2
} else {
// We add one to store the null sentinel group.
num_groups + 1
} else {
num_groups
};
let null_idx = len.saturating_sub(1);

Expand All @@ -55,7 +55,11 @@ where
let ideal_offset = (t + 1) * chunk_size;
let cache_aligned_offset =
ideal_offset + groups_start.wrapping_add(ideal_offset).align_offset(128);
per_thread_offsets.push(std::cmp::min(cache_aligned_offset, len));
if t == n_threads - 1 {
per_thread_offsets.push(len);
} else {
per_thread_offsets.push(std::cmp::min(cache_aligned_offset, len));
}
}

let groups_ptr = unsafe { SyncPtr::new(groups.as_mut_ptr()) };
Expand Down Expand Up @@ -168,7 +172,7 @@ impl CategoricalChunked {
}
// on relative small tables this isn't much faster than the default strategy
// but on huge tables, this can be > 2x faster
unsafe { cats.group_tuples_perfect(cached.len() - 1, multithreaded, 0) }
unsafe { cats.group_tuples_perfect(cached.len(), multithreaded, 0) }
} else {
self.physical().group_tuples(multithreaded, sorted).unwrap()
}
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/operations/unique/test_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,11 @@ def test_unique_with_null() -> None:
{"a": [1, 2, 3, 4], "b": ["a", "b", "c", "c"], "c": [None, None, None, None]}
)
assert_frame_equal(df.unique(maintain_order=True), expected_df)


def test_categorical_unique_19409() -> None:
df = pl.DataFrame({"x": [str(n % 50) for n in range(127)]}).cast(pl.Categorical)
uniq = df.unique()
assert uniq.height == 50
assert uniq.null_count().item() == 0
assert set(uniq["x"]) == set(df["x"])