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

Add compute=False option to to_snowflake #48

Merged
merged 2 commits into from
May 23, 2023
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
19 changes: 13 additions & 6 deletions dask_snowflake/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def to_snowflake(
df: dd.DataFrame,
name: str,
connection_kwargs: dict,
compute: bool = True,
):
"""Write a Dask DataFrame to a Snowflake table.

Expand All @@ -89,6 +90,10 @@ def to_snowflake(
connection_kwargs:
Connection arguments used when connecting to Snowflake with
``snowflake.connector.connect``.
compute:
Whether or not to compute immediately. If ``True``, write DataFrame
partitions to Snowflake immediately. If ``False``, return a list of
delayed objects that can be computed later. Defaults to ``True``.

Examples
--------
Expand All @@ -113,12 +118,14 @@ def to_snowflake(
# We run `ensure_db_exists` on the cluster to ensure we capture the
# right partner application ID.
ensure_db_exists(df._meta, name, connection_kwargs).compute()
dask.compute(
[
write_snowflake(partition, name, connection_kwargs)
for partition in df.to_delayed()
]
)
parts = [
write_snowflake(partition, name, connection_kwargs)
for partition in df.to_delayed()
]
if compute:
dask.compute(parts)
else:
return parts


def _fetch_batches(chunks: list[ArrowResultBatch], arrow_options: dict):
Expand Down
23 changes: 23 additions & 0 deletions dask_snowflake/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ def test_read_empty_result(table, connection_kwargs, client):
assert len(result.columns) == 0


def test_to_snowflake_compute_false(table, connection_kwargs, client):
result = to_snowflake(
ddf, name=table, connection_kwargs=connection_kwargs, compute=False
)
assert isinstance(result, list)
assert len(result) == ddf.npartitions

dask.compute(result)

ddf2 = read_snowflake(
f"SELECT * FROM {table}",
connection_kwargs=connection_kwargs,
npartitions=2,
)
# FIXME: Why does read_snowflake return lower-case columns names?
ddf2.columns = ddf2.columns.str.upper()
# FIXME: We need to sort the DataFrame because paritions are written
# in a non-sequential order.
dd.utils.assert_eq(
df, ddf2.sort_values(by="A").reset_index(drop=True), check_dtype=False
)


def test_arrow_options(table, connection_kwargs, client):
# We use a single partition Dask DataFrame to ensure the
# categories used below are always in the same order.
Expand Down