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

Use upstream Dask for complex sorting operations #336

Merged
merged 3 commits into from
Jan 14, 2022
Merged
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
26 changes: 10 additions & 16 deletions dask_sql/physical/utils/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,27 @@ def apply_sort(
except ValueError:
pass

# Dask doesn't natively support multi-column sorting;
# we work around this by initially sorting by the first
# column then handling the rest with `map_partitions`
df = df.sort_values(
# if standard `sort_values` can't handle ascending / null position params,
# we extend it using our custom sort function
return df.sort_values(
by=sort_columns[0],
ascending=sort_ascending[0],
na_position="first" if sort_null_first[0] else "last",
sort_function=make_pickable_without_dask_sql(sort_partition_func),
sort_function_kwargs={
"sort_columns": sort_columns,
"sort_ascending": sort_ascending,
"sort_null_first": sort_null_first,
},
).persist()

# sort the remaining columns if given
if len(sort_columns) > 1:
df = df.map_partitions(
make_pickable_without_dask_sql(sort_partition_func),
meta=df,
sort_columns=sort_columns,
sort_ascending=sort_ascending,
sort_null_first=sort_null_first,
).persist()

return df


def sort_partition_func(
partition: pd.DataFrame,
sort_columns: List[str],
sort_ascending: List[bool],
sort_null_first: List[bool],
**kwargs,
):
if partition.empty:
return partition
Expand Down