Skip to content

Commit

Permalink
SNOW-910685: Support creating session from default configuration file (
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jdu authored Sep 7, 2023
1 parent 55940b0 commit cc5efcd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions src/snowflake/snowpark/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions tests/integ/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit cc5efcd

Please sign in to comment.