Skip to content

Commit 27bc425

Browse files
BUG: Fixes GH34411 _query_iterator now returns a list with an empty pd.DataFrame if chunksize is set and the resultset is empty
1 parent 506eb54 commit 27bc425

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pandas/io/sql.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,17 @@ def _query_iterator(
833833
self, result, chunksize, columns, coerce_float=True, parse_dates=None
834834
):
835835
"""Return generator through chunked result set."""
836+
has_read_data = False
836837
while True:
837838
data = result.fetchmany(chunksize)
838839
if not data:
840+
if not has_read_data:
841+
yield DataFrame.from_records(
842+
[], columns=columns, coerce_float=coerce_float
843+
)
839844
break
840845
else:
846+
has_read_data = True
841847
self.frame = DataFrame.from_records(
842848
data, columns=columns, coerce_float=coerce_float
843849
)
@@ -1227,11 +1233,21 @@ def _query_iterator(
12271233
result, chunksize, columns, index_col=None, coerce_float=True, parse_dates=None
12281234
):
12291235
"""Return generator through chunked result set"""
1236+
has_read_data = False
12301237
while True:
12311238
data = result.fetchmany(chunksize)
12321239
if not data:
1240+
if not has_read_data:
1241+
yield _wrap_result(
1242+
[],
1243+
columns,
1244+
index_col=index_col,
1245+
coerce_float=coerce_float,
1246+
parse_dates=parse_dates
1247+
)
12331248
break
12341249
else:
1250+
has_read_data = True
12351251
yield _wrap_result(
12361252
data,
12371253
columns,
@@ -1684,14 +1700,20 @@ def _query_iterator(
16841700
cursor, chunksize, columns, index_col=None, coerce_float=True, parse_dates=None
16851701
):
16861702
"""Return generator through chunked result set"""
1703+
has_read_data = False
16871704
while True:
16881705
data = cursor.fetchmany(chunksize)
16891706
if type(data) == tuple:
16901707
data = list(data)
16911708
if not data:
16921709
cursor.close()
1710+
if not has_read_data:
1711+
yield DataFrame.from_records(
1712+
[], columns=columns, coerce_float=coerce_float
1713+
)
16931714
break
16941715
else:
1716+
has_read_data = True
16951717
yield _wrap_result(
16961718
data,
16971719
columns,

pandas/tests/io/test_sql.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@ def test_read_sql_view(self):
611611
iris_frame = sql.read_sql_query("SELECT * FROM iris_view", self.conn)
612612
self._check_iris_loaded_frame(iris_frame)
613613

614+
def test_read_sql_with_chunksize_no_result(self):
615+
with_batch = sql.read_sql_query("SELECT * FROM iris_view WHERE SepalLength < 0.0", self.conn, chunksize=5)
616+
without_batch = sql.read_sql_query("SELECT * FROM iris_view WHERE SepalLength < 0.0", self.conn)
617+
tm.assert_frame_equal(
618+
pd.concat(with_batch),
619+
without_batch
620+
)
621+
614622
def test_to_sql(self):
615623
sql.to_sql(self.test_frame1, "test_frame1", self.conn)
616624
assert sql.has_table("test_frame1", self.conn)

0 commit comments

Comments
 (0)