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

Optimize Spark Dask take function #530

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions fugue_dask/execution_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,17 @@ def _partition_take(partition, n, presort):
).head(n)

else:
if len(_presort.keys()) == 0 and n == 1:
return DaskDataFrame(
d.drop_duplicates(
subset=partition_spec.partition_by,
ignore_index=True,
keep="first",
),
df.schema,
type_safe=False,
)

d = (
d.groupby(partition_spec.partition_by, dropna=False)
.apply(_partition_take, n=n, presort=_presort, meta=meta)
Expand Down
5 changes: 5 additions & 0 deletions fugue_spark/execution_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,11 @@ def _presort_to_col(_col: str, _asc: bool) -> Any:

# If partition exists
else:
if len(_presort.keys()) == 0 and n == 1:
return self._to_spark_df(
d.dropDuplicates(subset=partition_spec.partition_by), df.schema
)

w = Window.partitionBy([col(x) for x in partition_spec.partition_by])

if len(_presort.keys()) > 0:
Expand Down
29 changes: 29 additions & 0 deletions fugue_test/execution_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,35 @@ def test_take(self):
"a:str,b:int,c:long",
throw=True,
)
a = fa.as_fugue_engine_df(
e,
[
["a", 2, 3],
[None, 4, 2],
[None, 2, 1],
],
"a:str,b:int,c:long",
)
i = fa.take(a, n=1, partition="a", presort=None)
case1 = df_eq(
i,
[
["a", 2, 3],
[None, 4, 2],
],
"a:str,b:int,c:long",
throw=False,
)
case2 = df_eq(
i,
[
["a", 2, 3],
[None, 2, 1],
],
"a:str,b:int,c:long",
throw=False,
)
assert case1 or case2
raises(ValueError, lambda: fa.take(a, n=0.5, presort=None))

def test_sample_n(self):
Expand Down
Loading