Skip to content

Commit 57fb8a9

Browse files
ai: apply changes for #1007 (1 review thread)
Addresses: - #4418300468 at src/databricks/sql/result_set.py:206 Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
1 parent a0b3c9f commit 57fb8a9

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎src/databricks/sql/result_set.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727

2828
logger = logging.getLogger(__name__)
2929

30+
# Upper bound on how many rows a fetchone() refill pulls and converts at once. The
31+
# refill uses ``arraysize`` but is capped here so the first fetchone() on a cursor
32+
# with a large arraysize (default 100000) doesn't materialise a full page just to
33+
# return one row.
34+
_FETCHONE_BUFFER_MAX_ROWS = 1000
35+
3036

3137
class ResultSet(ABC):
3238
"""
@@ -194,7 +200,10 @@ def _row_buffer_take_raw(self, n: Optional[int] = None) -> Any:
194200
def _fetchone_buffered(
195201
self, fetch_raw: Callable[[int], Any], convert: Callable[[Any], List[Row]]
196202
) -> Optional[Row]:
197-
"""Serve fetchone() from the row buffer, refilling ``arraysize`` rows at a time.
203+
"""Serve fetchone() from the row buffer, refilling in small batches.
204+
205+
Each refill fetches ``arraysize`` rows, capped at
206+
``_FETCHONE_BUFFER_MAX_ROWS``.
198207
199208
``_next_row_index`` keeps counting rows handed to the caller: rows parked in
200209
the buffer are subtracted on refill and added back as they are taken. The
@@ -203,7 +212,7 @@ def _fetchone_buffered(
203212
``expected_row_start_offset``).
204213
"""
205214
if self._row_buffer_count() == 0:
206-
raw = fetch_raw(max(self.arraysize, 1))
215+
raw = fetch_raw(max(min(self.arraysize, _FETCHONE_BUFFER_MAX_ROWS), 1))
207216
rows = convert(raw)
208217
if not rows:
209218
return None

‎tests/unit/test_fetches.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,17 @@ def test_fetchone_loop_converts_in_batches(self):
446446
self.assertEqual(rows, list(range(100)))
447447
self.assertLessEqual(m.call_count, 100 // 10 + 2)
448448

449+
def test_fetchone_refill_is_capped_for_large_arraysize(self):
450+
from databricks.sql.result_set import _FETCHONE_BUFFER_MAX_ROWS
451+
452+
n_rows = _FETCHONE_BUFFER_MAX_ROWS * 3
453+
rs = self.make_columnar_result_set(n_rows, arraysize=100000)
454+
self.assertEqual(rs.fetchone()[0], 0)
455+
self.assertEqual(rs._row_buffer_count(), _FETCHONE_BUFFER_MAX_ROWS - 1)
456+
self.assertEqual(rs.rownumber, 1)
457+
self.assertEqual([r[0] for r in rs.fetchall()], list(range(1, n_rows)))
458+
self.assertEqual(rs.rownumber, n_rows)
459+
449460
def test_mixed_fetches_keep_order_and_rownumber(self):
450461
rs = self.make_columnar_result_set(10, arraysize=4)
451462
self.assertEqual(rs.fetchone()[0], 0)

0 commit comments

Comments
 (0)