Skip to content

Fix null sorting for single partition dataframes - #8225

Merged
jsignell merged 2 commits into
dask:mainfrom
charlesbluca:single-partition-null-sorting
Oct 13, 2021
Merged

jsignell merged 2 commits into
dask:mainfrom
charlesbluca:single-partition-null-sorting

Conversation

@charlesbluca

@charlesbluca charlesbluca commented Oct 6, 2021 •

Copy link
Copy Markdown
Member

With #8167, we now remove duplicates from divisions when we are using rearrange_by_divisions for sorting purposes. One case that wasn't considered here was the sorting of a single partition dataframe with nulls - in this case, it is possible for len(divisions) == 1, which can cause all sorts of problems such as passing npartitions=1 to rearrange_by_column and assigning rows to non-existent partitions in set_partitions_pre.

This PR adds handling for the following cases:

  • df.npartitions == 1; we now call map_partitions(M.sort_values, ...) and skip Dask's sort_values altogether
  • len(divisions) == 2 before dropping duplicates; this implies that we are going to repartition to a single partition, which can be done using repartition without entering rearrange_by_divisions

With these changes, we should avoid the failing cases listed above.

  • Closes #xxxx
  • Tests added / passed
  • Passes black dask / flake8 dask / isort dask

@charlesbluca

charlesbluca commented Oct 6, 2021 •

Copy link
Copy Markdown
Member Author

I'm also realizing now that null handling in general seems to fly in the face of the npartitions kwarg for sort_values, as by changing the length of the divisions computed here:

divisions, mins, maxes = _calculate_divisions(
df, sort_by_col, repartition, npartitions, upsample, partition_size
)

We are essentially setting the output's npartitions to whatever makes the most contextual sense during the sort, similar to "auto".

Does this kwarg have a specific reason for being here - i.e. is it important that we uphold it? I would assume no since these changes haven't seemed to break anything within Dask, but am unsure of if this behavior will cause user confusion.

EDIT:

Another reason this question is relevant is because if we don't care much about the behavior of npartitions here it would probably be performant in this situation to just call map_partitions(M.sort_values, ...) on single-partition dataframes.

@jsignell

Copy link
Copy Markdown
Member

My understanding is that npartitions doesn't guarantee the number of partitions, it just sort of sets the intention. For example if your dataframe is too small and you do not necessarily get the number of partitions set by npartitions. So I think it's ok to not always return exactly the same number of partitions. I'm not quite sure I follow the argument about shuffle not honoring npartitions. There is a npartitions kwarg in the _calculate_divisions call and that determines (to some extent) the output divisions.

@charlesbluca

Copy link
Copy Markdown
Member Author

My bad, I mean to say that _calculate_divisions does respect the npartitions kwarg, but when we reach this drop_duplicates call in rearrange_by_divisions:

if not duplicates:
divisions = divisions.drop_duplicates()

There is a chance that divisions will change, causing npartitions to no longer be respected when sorting.

If it's okay that we don't match the npartitions passed in as a kwarg, then I think the best way to handle this is to just call map_partitions(M.sort_values, ...) for the single-partition case.

Comment thread dask/dataframe/shuffle.py
if len(divisions) == 2:
return df.repartition(npartitions=1).map_partitions(
M.sort_values, by, ascending=ascending, na_position=na_position
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a simpler way of handling the edge case where sort_values needs to repartition to a single partition; rather than attempt to rearrange_by_divisions and make wasteful calls to set_partitions_pre (which would just assign all rows to the same single partition), we just immediately repartition and proceed with the sort.

@jsignell jsignell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants