Skip to content

Commit 3a80e9a

Browse files
SNOW-647539 Closing the cursor erases the last rowcount (#1229)
1 parent e145e62 commit 3a80e9a

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/snowflake/connector/cursor.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def close(self) -> bool | None:
399399
return False
400400

401401
with self._lock_canceling:
402-
self.reset()
402+
self.reset(closing=True)
403403
self._connection = None
404404
del self.messages[:]
405405
return True
@@ -1175,15 +1175,18 @@ def scroll(self, value, mode="relative"):
11751175
},
11761176
)
11771177

1178-
def reset(self):
1178+
def reset(self, closing=False):
11791179
"""Resets the result set."""
1180-
self._total_rowcount = -1 # reset the rowcount
1180+
# SNOW-647539: Do not erase the rowcount
1181+
# information when closing the cursor
1182+
if not closing:
1183+
self._total_rowcount = -1
11811184
if self._result_state != ResultState.DEFAULT:
11821185
self._result_state = ResultState.RESET
11831186
if self._result is not None:
11841187
self._result = None
11851188
if self._inner_cursor is not None:
1186-
self._inner_cursor.reset()
1189+
self._inner_cursor.reset(closing=closing)
11871190
self._result = None
11881191
self._inner_cursor = None
11891192
self._prefetch_hook = None

test/integ/test_cursor.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,9 @@ def test_closed_cursor(conn, db_parameters):
747747
with pytest.raises(InterfaceError, match="Cursor is closed in execute") as err:
748748
c.execute(f"select aa from {table_name}")
749749
assert err.value.errno == errorcode.ER_CURSOR_IS_CLOSED
750+
assert (
751+
c.rowcount == 5
752+
), "SNOW-647539: rowcount should remain available after cursor is closed"
750753

751754

752755
def test_fetchmany(conn, db_parameters):
@@ -1039,11 +1042,16 @@ def test_empty_execution(conn, sql):
10391042
"reuse_results", (False, pytest.param(True, marks=pytest.mark.skipolddriver))
10401043
)
10411044
def test_reset_fetch(conn, reuse_results):
1042-
"""Tests behavior after resetting the cursor."""
1045+
"""Tests behavior after resetting an open cursor."""
10431046
with conn(reuse_results=reuse_results) as cnx:
10441047
with cnx.cursor() as cur:
10451048
cur.execute("select 1")
1049+
assert cur.rowcount == 1
10461050
cur.reset()
1051+
assert (
1052+
cur.rowcount is None
1053+
), "calling reset on an open cursor should unset rowcount"
1054+
assert not cur.is_closed(), "calling reset should not close the cursor"
10471055
if reuse_results:
10481056
assert cur.fetchone() == (1,)
10491057
else:

0 commit comments

Comments
 (0)