Skip to content

ENH: enable server side cursors when chunksize is set #56742

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

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 3 additions & 1 deletion pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,8 @@ def run_transaction(self):
def execute(self, sql: str | Select | TextClause, params=None):
"""Simple passthrough to SQLAlchemy connectable"""
args = [] if params is None else [params]
if self.returns_generator:
self.con.execution_options(stream_results=True)
if isinstance(sql, str):
return self.con.exec_driver_sql(sql, *args)
return self.con.execute(sql, *args)
Expand Down Expand Up @@ -1814,11 +1816,11 @@ def read_query(
read_sql

"""
self.returns_generator = chunksize is not None
result = self.execute(sql, params)
columns = result.keys()

if chunksize is not None:
self.returns_generator = True
return self._query_iterator(
result,
self.exit_stack,
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,24 @@ def test_read_iris_table_chunksize(conn, request):
check_iris_frame(iris_frame)


@pytest.mark.parametrize("conn", sqlalchemy_connectable_iris)
def test_sqlalchemy_read_table_chunksize_stream_results(conn, request):
conn = request.getfixturevalue(conn)
with sql.SQLDatabase(conn) as pandasSQL:
assert list(pandasSQL.read_table("iris", chunksize=10000))
execution_options = pandasSQL.con.get_execution_options()
assert execution_options["stream_results"] is True


@pytest.mark.parametrize("conn", sqlalchemy_connectable_iris)
def test_sqlalchemy_read_sql_chunksize_stream_results(conn, request):
conn = request.getfixturevalue(conn)
with sql.SQLDatabase(conn) as pandasSQL:
assert list(pandasSQL.read_sql("SELECT * FROM iris", chunksize=10000))
execution_options = pandasSQL.con.get_execution_options()
assert execution_options["stream_results"] is True


@pytest.mark.parametrize("conn", sqlalchemy_connectable)
def test_to_sql_callable(conn, test_frame1, request):
conn = request.getfixturevalue(conn)
Expand Down