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

SNOW-900244-Expose DataFrame._session and Session._session_id to users #1030

Merged
merged 6 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Added support for VOLATILE/IMMUTABLE keyword when registering UDFs.
- Added support for specifying clustering keys when saving dataframes using `DataFrame.save_as_table`.
- Accept `Iterable` objects input for `schema` when creating dataframes using `Session.create_dataframe`.
- Added the property `DataFrame.session` to return a `Session` object.
- Added the property `Session.session_id` to return an integer that represents session ID.
- Added the property `Session.connection` to return a `SnowflakeConnection` object .

- Added support for creating a Snowpark session from a configuration file or environment variables.

### Dependency updates
Expand Down
1 change: 1 addition & 0 deletions docs/source/dataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,4 @@ DataFrame
DataFrame.stat
DataFrame.write
DataFrame.is_cached
DataFrame.session
4 changes: 3 additions & 1 deletion docs/source/session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ Snowpark Session
Session.sql_simplifier_enabled
Session.telemetry_enabled
Session.udf
Session.udtf
Session.udtf
Session.session_id
Session.connection
7 changes: 7 additions & 0 deletions src/snowflake/snowpark/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3320,6 +3320,13 @@ def na(self) -> DataFrameNaFunctions:
"""
return self._na

@property
def session(self) -> "snowflake.snowpark.Session":
"""
Returns a :class:`snowflake.snowpark.Session` object that provides access to the session the current DataFrame is relying on.
"""
return self._session

def describe(self, *cols: Union[str, List[str]]) -> "DataFrame":
"""
Computes basic statistics for numeric columns, which includes
Expand Down
12 changes: 12 additions & 0 deletions src/snowflake/snowpark/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,18 @@ def read(self) -> "DataFrameReader":
supported sources (e.g. a file in a stage) as a DataFrame."""
return DataFrameReader(self)

@property
def session_id(self) -> int:
"""Returns an integer that represents the session ID of this session."""
return self._session_id

@property
def connection(self) -> "SnowflakeConnection":
"""Returns a :class:`SnowflakeConnection` object that allows you to access the connection between the current session
and Snowflake server."""
return self._conn._conn


def _run_query(
self,
query: str,
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

import snowflake.snowpark.session
from snowflake.snowpark.session import Session
from snowflake.snowpark import (
DataFrame,
DataFrameNaFunctions,
Expand Down Expand Up @@ -292,3 +293,14 @@ def test_dataFrame_printSchema(capfd):
out
== "root\n |-- A: IntegerType() (nullable = False)\n |-- B: StringType() (nullable = True)\n"
)


def test_session():
sfc-gh-yuwang marked this conversation as resolved.
Show resolved Hide resolved
fake_session = mock.create_autospec(Session, _session_id=123456)
fake_session._analyzer = mock.Mock()
df = DataFrame(fake_session)

assert(df.session == fake_session)
assert(df.session._session_id == fake_session._session_id)


24 changes: 23 additions & 1 deletion tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
import json
import os
from typing import Optional
from typing import Optional, Dict, Union
from unittest import mock
from unittest.mock import MagicMock

Expand Down Expand Up @@ -368,3 +368,25 @@ def test_parse_table_name():
assert parse_table_name('*&^."abc".abc') # unsupported chars in unquoted ids
with pytest.raises(SnowparkInvalidObjectNameException):
assert parse_table_name('."abc".') # unsupported semantic


def test_session_id():
fake_server_connection = mock.create_autospec(ServerConnection)
fake_server_connection.get_session_id = mock.Mock(return_value=123456)
session = Session(fake_server_connection)

assert(session.session_id == 123456)


def test_connection():
fake_snowflake_connection = mock.create_autospec(SnowflakeConnection)
fake_snowflake_connection._telemetry = mock.Mock()
fake_snowflake_connection._session_parameters = mock.Mock()
fake_snowflake_connection.is_closed = mock.Mock(return_value=False)
fake_options = {"": ""}
server_connection = ServerConnection(fake_options, fake_snowflake_connection)
session = Session(server_connection)

assert(session.connection == session._conn._conn)
assert(session.connection == fake_snowflake_connection)

Loading