Skip to content

Fix read_sql empty result with chunksize bug GH34411 #34429

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

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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ I/O
- :func:`read_excel` now respects :func:`set_option` (:issue:`34252`)
- Bug in :func:`read_csv` not switching ``true_values`` and ``false_values`` for nullable ``boolean`` dtype (:issue:`34655`)
- Bug in :func:`read_json` when ``orient="split"`` does not maintain numeric string index (:issue:`28556`)
- :meth:`read_sql` returned an empty generator if ``chunksize`` was no-zero and the query returned no results. Now returns a generator with a single empty dataframe (:issue:`34411`)

Period
^^^^^^
Expand Down
22 changes: 22 additions & 0 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,11 +926,17 @@ def _query_iterator(
parse_dates=None,
):
"""Return generator through chunked result set."""
has_read_data = False
while True:
data = result.fetchmany(chunksize)
if not data:
if not has_read_data:
yield DataFrame.from_records(
[], columns=columns, coerce_float=coerce_float
)
break
else:
has_read_data = True
self.frame = DataFrame.from_records(
data, columns=columns, coerce_float=coerce_float
)
Expand Down Expand Up @@ -1343,11 +1349,21 @@ def _query_iterator(
dtype: Optional[DtypeArg] = None,
):
"""Return generator through chunked result set"""
has_read_data = False
while True:
data = result.fetchmany(chunksize)
if not data:
if not has_read_data:
yield _wrap_result(
[],
columns,
index_col=index_col,
coerce_float=coerce_float,
parse_dates=parse_dates,
)
break
else:
has_read_data = True
yield _wrap_result(
data,
columns,
Expand Down Expand Up @@ -1849,14 +1865,20 @@ def _query_iterator(
dtype: Optional[DtypeArg] = None,
):
"""Return generator through chunked result set"""
has_read_data = False
while True:
data = cursor.fetchmany(chunksize)
if type(data) == tuple:
data = list(data)
if not data:
cursor.close()
if not has_read_data:
yield DataFrame.from_records(
[], columns=columns, coerce_float=coerce_float
)
break
else:
has_read_data = True
yield _wrap_result(
data,
columns,
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,12 @@ def test_read_sql_view(self):
iris_frame = sql.read_sql_query("SELECT * FROM iris_view", self.conn)
self._check_iris_loaded_frame(iris_frame)

def test_read_sql_with_chunksize_no_result(self):
query = "SELECT * FROM iris_view WHERE SepalLength < 0.0"
with_batch = sql.read_sql_query(query, self.conn, chunksize=5)
without_batch = sql.read_sql_query(query, self.conn)
tm.assert_frame_equal(pd.concat(with_batch), without_batch)

def test_to_sql(self):
sql.to_sql(self.test_frame1, "test_frame1", self.conn)
assert sql.has_table("test_frame1", self.conn)
Expand Down