Skip to content
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

Alter closing sessions inside a stored procedure to write a warning instead of throwing an error #1263

Merged
merged 18 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
3 changes: 2 additions & 1 deletion src/snowflake/snowpark/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ def _generate_new_action_id(self) -> int:
def close(self) -> None:
"""Close this session."""
if is_in_stored_procedure():
raise SnowparkClientExceptionMessages.DONT_CLOSE_SESSION_IN_SP()
_logger.warning("Closing a session in a stored procedure is a no-op.")
zaramzamzam marked this conversation as resolved.
Show resolved Hide resolved
return
try:
if self._conn.is_closed():
_logger.debug(
Expand Down
8 changes: 3 additions & 5 deletions tests/integ/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from functools import partial

import pytest

zaramzamzam marked this conversation as resolved.
Show resolved Hide resolved
import snowflake.connector
from snowflake.connector.errors import ProgrammingError
from snowflake.snowpark import Row, Session
Expand All @@ -23,6 +22,7 @@
_get_active_session,
_get_active_sessions,
)

zaramzamzam marked this conversation as resolved.
Show resolved Hide resolved
from tests.utils import IS_IN_STORED_PROC, IS_IN_STORED_PROC_LOCALFS, TestFiles, Utils


Expand Down Expand Up @@ -199,9 +199,8 @@ def test_close_session_in_sp(session):
original_platform = internal_utils.PLATFORM
internal_utils.PLATFORM = "XP"
try:
with pytest.raises(SnowparkSessionException) as exec_info:
session.close()
assert exec_info.value.error_code == "1411"
session.close()
assert not session.connection.is_closed()
finally:
internal_utils.PLATFORM = original_platform

Expand Down Expand Up @@ -570,7 +569,6 @@ def test_use_secondary_roles(session):
session.use_secondary_roles(current_role[1:-1])


@pytest.mark.skipif(IS_IN_STORED_PROC, reason="SP doesn't allow to close a session.")
def test_close_session_twice(db_parameters):
zaramzamzam marked this conversation as resolved.
Show resolved Hide resolved
new_session = Session.builder.configs(db_parameters).create()
new_session.close()
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
#
import json
import logging
import os
from typing import Optional
from unittest import mock
Expand Down Expand Up @@ -122,6 +123,47 @@ def test_close_exception():
session.close()


def test_close_session_in_stored_procedure_no_op():
fake_connection = mock.create_autospec(ServerConnection)
fake_connection._conn = mock.Mock()
fake_connection.is_closed = MagicMock(return_value=False)
session = Session(fake_connection)
with mock.patch.object(
snowflake.snowpark.session, "is_in_stored_procedure"
) as mock_fn, mock.patch.object(
session._conn, "close"
) as mock_close, mock.patch.object(
session, "cancel_all"
) as mock_cancel_all, mock.patch.object(
snowflake.snowpark.session, "_remove_session"
) as mock_remove:
mock_fn.return_value = True
session.close()
mock_cancel_all.assert_not_called()
mock_close.assert_not_called()
mock_remove.assert_not_called()


@pytest.mark.parametrize(
"warning_level, expected",
[(logging.WARNING, True), (logging.INFO, True), (logging.ERROR, False)],
)
def test_close_session_in_stored_procedure_log_level(caplog, warning_level, expected):
caplog.clear()
caplog.set_level(warning_level)
fake_connection = mock.create_autospec(ServerConnection)
fake_connection._conn = mock.Mock()
fake_connection.is_closed = MagicMock(return_value=False)
session = Session(fake_connection)
with mock.patch.object(
snowflake.snowpark.session, "is_in_stored_procedure"
) as mock_fn:
mock_fn.return_value = True
session.close()
result = "Closing a session in a stored procedure is a no-op." in caplog.text
assert result == expected


def test_resolve_import_path_ignore_import_path(tmp_path_factory):
fake_connection = mock.create_autospec(ServerConnection)
fake_connection._conn = mock.Mock()
Expand Down
Loading