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

SNOW-1887904:add batch fetching logic #2970

Draft
wants to merge 1 commit into
base: dev/data-source
Choose a base branch
from
Draft
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
21 changes: 19 additions & 2 deletions src/snowflake/snowpark/dataframe_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
from snowflake.snowpark._internal.type_utils import (
ColumnOrName,
convert_sf_to_sp_type,
Connection,
# Connection,
convert_sp_to_sf_type,
)
from pyodbc import Connection
from snowflake.snowpark._internal.utils import (
INFER_SCHEMA_FORMAT_TYPES,
SNOWFLAKE_PATH_PREFIXES,
Expand Down Expand Up @@ -1033,6 +1034,7 @@ def dbapi(
num_partitions: Optional[int] = None,
max_workers: Optional[int] = None,
query_timeout: Optional[int] = 0,
fetch_size: Optional[int] = 0,
) -> DataFrame:
conn = create_connection()
# this is specified to pyodbc, need other way to manage timeout on other drivers
Expand Down Expand Up @@ -1107,6 +1109,7 @@ def dbapi(
i,
tmp_dir,
query_timeout,
fetch_size,
)
for i, query in enumerate(partitioned_queries)
]
Expand Down Expand Up @@ -1322,11 +1325,24 @@ def _task_fetch_from_data_source(
i: int,
tmp_dir: str,
query_timeout: int = 0,
fetch_size: int = 0,
) -> str:
conn = create_connection()
# this is specified to pyodbc, need other way to manage timeout on other drivers
conn.timeout = query_timeout
result = conn.cursor().execute(query).fetchall()
result = []
if fetch_size == 0:
result = conn.cursor().execute(query).fetchall()
elif fetch_size > 0:
cursor = conn.cursor()
cursor.arraysize = fetch_size
rows = cursor.execute(query).fetchmany(cursor.arraysize)
while rows:
result.extend(rows)
rows = cursor.execute(query).fetchmany(cursor.arraysize)
else:
raise ValueError("fetch size cannot be smaller than 0")

columns = [col[0] for col in schema]
df = pd.DataFrame.from_records(result, columns=columns)
df = df.map(
Expand All @@ -1346,6 +1362,7 @@ def task_fetch_from_data_source_with_retry(
i: int,
tmp_dir: str,
query_timeout: int = 0,
fetch_size: int = 0,
) -> Union[str, Exception]:
retry_count = 0
error = None
Expand Down
Loading