From cc5efcdd9008ddfa5fddbf7912663c98757c6017 Mon Sep 17 00:00:00 2001 From: Jianzhun Du <68252326+sfc-gh-jdu@users.noreply.github.com> Date: Thu, 7 Sep 2023 16:17:54 -0700 Subject: [PATCH] SNOW-910685: Support creating session from default configuration file (#1041) --- CHANGELOG.md | 5 +++++ setup.py | 2 +- src/snowflake/snowpark/session.py | 6 ++++++ tests/integ/test_session.py | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee89c4820c2..a4a56cee91b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ - 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 support for creating a Snowpark session from a configuration file or environment variables. + +### Dependency updates + +- Updated ``snowflake-connector-python`` to 3.2.0. ### Bug Fixes diff --git a/setup.py b/setup.py index cebb7e4121f..17abe322001 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ THIS_DIR = os.path.dirname(os.path.realpath(__file__)) SRC_DIR = os.path.join(THIS_DIR, "src") SNOWPARK_SRC_DIR = os.path.join(SRC_DIR, "snowflake", "snowpark") -CONNECTOR_DEPENDENCY_VERSION = ">=3.0.4, <4.0.0" +CONNECTOR_DEPENDENCY_VERSION = ">=3.2.0, <4.0.0" REQUIRED_PYTHON_VERSION = ">=3.8, <3.11" if os.getenv("SNOWFLAKE_IS_PYTHON_RUNTIME_TEST", False): REQUIRED_PYTHON_VERSION = ">=3.8" diff --git a/src/snowflake/snowpark/session.py b/src/snowflake/snowpark/session.py index da312ec8a99..ebaaa1540cb 100644 --- a/src/snowflake/snowpark/session.py +++ b/src/snowflake/snowpark/session.py @@ -21,6 +21,7 @@ import pkg_resources from snowflake.connector import ProgrammingError, SnowflakeConnection +from snowflake.connector.config_manager import _get_default_connection_params from snowflake.connector.options import installed_pandas, pandas from snowflake.connector.pandas_tools import write_pandas from snowflake.snowpark._internal.analyzer.analyzer import Analyzer @@ -328,6 +329,11 @@ def getOrCreate(self) -> "Session": def _create_internal( self, conn: Optional[SnowflakeConnection] = None ) -> "Session": + # If no connection object and no connection parameter is provided, + # we read from the default config file + if not conn and not self._options: + self._options = _get_default_connection_params() + # Set paramstyle to qmark by default to be consistent with previous behavior if "paramstyle" not in self._options: self._options["paramstyle"] = "qmark" diff --git a/tests/integ/test_session.py b/tests/integ/test_session.py index c064f54eca1..2b384a9b7b5 100644 --- a/tests/integ/test_session.py +++ b/tests/integ/test_session.py @@ -550,3 +550,26 @@ def test_sql_simplifier_disabled_on_session(db_parameters): } with Session.builder.configs(parameters).create() as new_session2: assert new_session2.sql_simplifier_enabled is False + + +@pytest.mark.skipif(IS_IN_STORED_PROC, reason="Cannot create session in SP") +def test_create_session_from_default_config_file(monkeypatch, db_parameters): + import tomlkit + + doc = tomlkit.document() + default_con = tomlkit.table() + try: + # If anything unexpected fails here, don't want to expose password + for k, v in db_parameters.items(): + default_con[k] = v + doc["default"] = default_con + with monkeypatch.context() as m: + m.setenv("SNOWFLAKE_CONNECTIONS", tomlkit.dumps(doc)) + m.setenv("SNOWFLAKE_DEFAULT_CONNECTION_NAME", "default") + with Session.builder.create() as new_session: + _ = new_session.sql("select 1").collect()[0][0] + except Exception: + # This is my way of guaranteeing that we'll not expose the + # sensitive information that this test needs to handle. + # db_parameter contains passwords. + pytest.fail("something failed", pytrace=False)