Skip to content

SNOW-3264599: Python - implement cursor reset method#723

Merged
sfc-gh-asolarski merged 1 commit intomainfrom
03-25-snow-3264599_python_-_implement_cursor_reset_method
Mar 27, 2026
Merged

SNOW-3264599: Python - implement cursor reset method#723
sfc-gh-asolarski merged 1 commit intomainfrom
03-25-snow-3264599_python_-_implement_cursor_reset_method

Conversation

@sfc-gh-asolarski
Copy link
Copy Markdown
Contributor

@sfc-gh-asolarski sfc-gh-asolarski commented Mar 25, 2026

TL;DR

Implemented proper cursor state management by adding a reset() method and updating close() to properly clean up resources and return status indicators.

What changed?

  • Added reset() method: Clears all result-related state including execute_result, _iterator, _description, _sqlstate, _fetch_mode, _binding_data, and _rownumber. The closing parameter preserves _rowcount when set to True.
  • Enhanced close() method: Now calls reset(closing=True), clears the messages list, and returns a boolean indicating success (True), already closed (False), or exception (None).
  • Updated execute() method: Added _do_reset parameter (defaults to True) and moved the reset logic to call reset() at the beginning instead of manually clearing state afterward.
  • Modified executemany() method: Now calls reset() once before the loop for client-side binding scenarios and passes _do_reset=False to individual execute() calls to avoid redundant resets.

Why make this change?

This change provides proper cursor lifecycle management by centralizing state cleanup logic in the reset() method. It ensures consistent behavior across different execution patterns, prevents state leakage between queries, and provides better error handling and status reporting for cursor operations. The close() method now properly indicates its success status, and executemany() avoids unnecessary resets between individual executions while maintaining clean state.

@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-20-snow-3243341_python_wrapper_-_support_pandas_and_pyarrow_fetching branch from b2a1f1b to 3fc02a0 Compare March 25, 2026 14:35
@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch from 0f7a79e to 0dcaa5e Compare March 25, 2026 14:35
@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-20-snow-3243341_python_wrapper_-_support_pandas_and_pyarrow_fetching branch from 3fc02a0 to d1c0313 Compare March 26, 2026 11:33
@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch from 0dcaa5e to 9d678e8 Compare March 26, 2026 11:33
@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch from 9d678e8 to 6ed1405 Compare March 26, 2026 11:35
# Then it should be None (per PEP 249)
assert result is None
# Then it should be -1 (per PEP 249)
assert result == -1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it pass with reference driver?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed (it seems that None incorrect per pep249 but backward compatible)
I covered this additionally with more integration tests.

@sfc-gh-asolarski sfc-gh-asolarski changed the base branch from 03-20-snow-3243341_python_wrapper_-_support_pandas_and_pyarrow_fetching to graphite-base/723 March 26, 2026 13:46
@sfc-gh-asolarski sfc-gh-asolarski changed the base branch from graphite-base/723 to 03-20-snow-3243341_python_wrapper_-_support_pandas_and_pyarrow_fetching March 26, 2026 14:28
@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch from 6ed1405 to 6739317 Compare March 26, 2026 14:28
Base automatically changed from 03-20-snow-3243341_python_wrapper_-_support_pandas_and_pyarrow_fetching to main March 26, 2026 15:11
@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch from 6739317 to 84c72e0 Compare March 26, 2026 15:31


@contextmanager
def create_statement(connection: Connection, query: str) -> Generator[StatementHandle]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def create_statement(connection: Connection, query: str) -> Generator[StatementHandle]:
Query = str
def create_statement(connection: Connection, query: Query) -> Generator[StatementHandle]:

Nit, we can introduce Query = str it would be aligned with Row = tuple[Any, ...] DictRow = dict[str, Any]. Possibly having snowflake.connector.types can be reasonable place to keep all custom types. Not mandatory in this PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a good idea indeed.
I will take a look into that in one of the following PRs.

@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch from 84c72e0 to b717b89 Compare March 27, 2026 08:17
@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch 3 times, most recently from d3f0ab1 to 3f48dcf Compare March 27, 2026 09:34
@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch from 3f48dcf to 06aa4bd Compare March 27, 2026 10:10
# Conflicts:
#	python/src/snowflake/connector/cursor.py
@sfc-gh-asolarski sfc-gh-asolarski force-pushed the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch from 06aa4bd to bb36ffa Compare March 27, 2026 10:59
@sfc-gh-asolarski sfc-gh-asolarski added this pull request to the merge queue Mar 27, 2026
Merged via the queue into main with commit 2a67349 Mar 27, 2026
63 of 64 checks passed
@sfc-gh-asolarski sfc-gh-asolarski deleted the 03-25-snow-3264599_python_-_implement_cursor_reset_method branch March 27, 2026 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants