Skip to content

Commit a24cbf0

Browse files
SNOW-900244-Expose DataFrame._session and Session._session_id to users (#1030)
Exposed DataFrame._session, Session._session_id, Session._conn._conn to users Added mock unit test of exposed properties
1 parent c7dc5ab commit a24cbf0

File tree

7 files changed

+62
-2
lines changed

7 files changed

+62
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Added support for VOLATILE/IMMUTABLE keyword when registering UDFs.
88
- Added support for specifying clustering keys when saving dataframes using `DataFrame.save_as_table`.
99
- Accept `Iterable` objects input for `schema` when creating dataframes using `Session.create_dataframe`.
10+
- Added the property `DataFrame.session` to return a `Session` object.
11+
- Added the property `Session.session_id` to return an integer that represents session ID.
12+
- Added the property `Session.connection` to return a `SnowflakeConnection` object .
13+
1014
- Added support for creating a Snowpark session from a configuration file or environment variables.
1115

1216
### Dependency updates

docs/source/dataframe.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,4 @@ DataFrame
125125
DataFrame.stat
126126
DataFrame.write
127127
DataFrame.is_cached
128+
DataFrame.session

docs/source/session.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@ Snowpark Session
7070
Session.sql_simplifier_enabled
7171
Session.telemetry_enabled
7272
Session.udf
73-
Session.udtf
73+
Session.udtf
74+
Session.session_id
75+
Session.connection

src/snowflake/snowpark/dataframe.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,6 +3320,13 @@ def na(self) -> DataFrameNaFunctions:
33203320
"""
33213321
return self._na
33223322

3323+
@property
3324+
def session(self) -> "snowflake.snowpark.Session":
3325+
"""
3326+
Returns a :class:`snowflake.snowpark.Session` object that provides access to the session the current DataFrame is relying on.
3327+
"""
3328+
return self._session
3329+
33233330
def describe(self, *cols: Union[str, List[str]]) -> "DataFrame":
33243331
"""
33253332
Computes basic statistics for numeric columns, which includes

src/snowflake/snowpark/session.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,18 @@ def read(self) -> "DataFrameReader":
16961696
supported sources (e.g. a file in a stage) as a DataFrame."""
16971697
return DataFrameReader(self)
16981698

1699+
@property
1700+
def session_id(self) -> int:
1701+
"""Returns an integer that represents the session ID of this session."""
1702+
return self._session_id
1703+
1704+
@property
1705+
def connection(self) -> "SnowflakeConnection":
1706+
"""Returns a :class:`SnowflakeConnection` object that allows you to access the connection between the current session
1707+
and Snowflake server."""
1708+
return self._conn._conn
1709+
1710+
16991711
def _run_query(
17001712
self,
17011713
query: str,

tests/unit/test_dataframe.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
import snowflake.snowpark.session
11+
from snowflake.snowpark.session import Session
1112
from snowflake.snowpark import (
1213
DataFrame,
1314
DataFrameNaFunctions,
@@ -292,3 +293,14 @@ def test_dataFrame_printSchema(capfd):
292293
out
293294
== "root\n |-- A: IntegerType() (nullable = False)\n |-- B: StringType() (nullable = True)\n"
294295
)
296+
297+
298+
def test_session():
299+
fake_session = mock.create_autospec(Session, _session_id=123456)
300+
fake_session._analyzer = mock.Mock()
301+
df = DataFrame(fake_session)
302+
303+
assert(df.session == fake_session)
304+
assert(df.session._session_id == fake_session._session_id)
305+
306+

tests/unit/test_session.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
import json
55
import os
6-
from typing import Optional
6+
from typing import Optional, Dict, Union
77
from unittest import mock
88
from unittest.mock import MagicMock
99

@@ -368,3 +368,25 @@ def test_parse_table_name():
368368
assert parse_table_name('*&^."abc".abc') # unsupported chars in unquoted ids
369369
with pytest.raises(SnowparkInvalidObjectNameException):
370370
assert parse_table_name('."abc".') # unsupported semantic
371+
372+
373+
def test_session_id():
374+
fake_server_connection = mock.create_autospec(ServerConnection)
375+
fake_server_connection.get_session_id = mock.Mock(return_value=123456)
376+
session = Session(fake_server_connection)
377+
378+
assert(session.session_id == 123456)
379+
380+
381+
def test_connection():
382+
fake_snowflake_connection = mock.create_autospec(SnowflakeConnection)
383+
fake_snowflake_connection._telemetry = mock.Mock()
384+
fake_snowflake_connection._session_parameters = mock.Mock()
385+
fake_snowflake_connection.is_closed = mock.Mock(return_value=False)
386+
fake_options = {"": ""}
387+
server_connection = ServerConnection(fake_options, fake_snowflake_connection)
388+
session = Session(server_connection)
389+
390+
assert(session.connection == session._conn._conn)
391+
assert(session.connection == fake_snowflake_connection)
392+

0 commit comments

Comments
 (0)