-
Notifications
You must be signed in to change notification settings - Fork 117
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
Add new API connection for SiS/SPCS #1236
Conversation
src/snowflake/snowpark/session.py
Outdated
else: | ||
try: | ||
# 2) check if we are running in SPCS | ||
with open("/snowflake/session/token") as file: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could replace this block by adding a helper method like _get_default_SPSC_connection_params
in connector (similar to _get_default_connection_params
) to get SPCS default params and move the whole thing in _create_internal
. Please lemme know which one is a better option/pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no preference, either will work!
src/snowflake/snowpark/session.py
Outdated
): # (will be removed before merging) no need for `"streamlit" in sys.modules` check since is_in_stored_procedure() must be true for SiS as well | ||
# Basically noop and return the existing session | ||
return _get_active_session() | ||
else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the else-clause since you return in the then-clause. Just get rid of this line and de-dent the rest
src/snowflake/snowpark/session.py
Outdated
except Exception: | ||
# a general error happened, re-raise it | ||
raise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to catch an exception just to throw it again, let it bubble up
except Exception: | |
# a general error happened, re-raise it | |
raise |
src/snowflake/snowpark/session.py
Outdated
else: | ||
try: | ||
# 2) check if we are running in SPCS | ||
with open("/snowflake/session/token") as file: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no preference, either will work!
return _get_active_session() | ||
try: | ||
# 2) check if we are running in SPCS | ||
with open("/snowflake/session/token") as file: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggestion/idea on how I can test this for SPCS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably even more challenging than stored proc. In stored proc, we have a test job that creates Anaconda package, and does some repodata hack to use the new package (test job: https://ci-dev-142.int.snowflakecomputing.com/job/PythonStoredProcBuildPrecommitTest/).
Do you know how SPCS does its regression test today?
@@ -586,7 +585,10 @@ def test_create_session_from_default_config_file(monkeypatch, db_parameters): | |||
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: | |||
if not IS_IN_STORED_PROC: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm now I'm not sure if it works (haven't tested this yet)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some tests failed. Looks like a rebase with main is needed before we do another run
src/snowflake/snowpark/session.py
Outdated
@@ -2749,4 +2749,32 @@ def _explain_query(self, query: str) -> Optional[str]: | |||
_logger.warning("query `%s` cannot be explained", query) | |||
return None | |||
|
|||
@staticmethod | |||
def create() -> "Session": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is create
the right action name for "gets existing session if one exists", or should this be fetch
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is already a method called getOrCreate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So basically this comes from a request from field/PM: https://docs.google.com/document/d/12FGgc9NeH-xa31FMZa_lO2N5Xq71Cm0aquTLI2-G0IE/edit#heading=h.hvtde6rw3c3p
They want a unified connect
method in session (which I mistakenly named the method created
) which works across different use-cases (i.e., when Snowpark is used as is or in SiS, SP, SPCS)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. @sfc-gh-mkeller @sfc-gh-yixie : Do you have concerns for introducing a connect
method in session?
return _get_active_session() | ||
try: | ||
# 2) check if we are running in SPCS | ||
with open("/snowflake/session/token") as file: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably even more challenging than stored proc. In stored proc, we have a test job that creates Anaconda package, and does some repodata hack to use the new package (test job: https://ci-dev-142.int.snowflakecomputing.com/job/PythonStoredProcBuildPrecommitTest/).
Do you know how SPCS does its regression test today?
): # (will be removed before merging) no need for `"streamlit" in sys.modules` check since is_in_stored_procedure() must be true for SiS as well | ||
# Basically noop and return the existing session |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be removed before merging to main? I don't understand why have it then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already Session.builder.getOrCreate()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getOrCreate
as is, doesn't work (since we expect to have token in the toml file and not as a separate file). But I had a conversion with Srikanth, he is tasked with adding connect
for SPCS for all connectors. He will write a design doc based on the existing connect
API in Python connector where we take into account a new connect argument (like token_file) to be added to the toml file. I'll hold off working on this for now since his general solution is better/cleaner than what I have here.
@@ -501,6 +501,10 @@ def is_in_stored_procedure(): | |||
return PLATFORM == "XP" | |||
|
|||
|
|||
def is_in_spcs(): | |||
return os.path.exists("/snowflake/session/token") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there another way?
): # (will be removed before merging) no need for `"streamlit" in sys.modules` check since is_in_stored_procedure() must be true for SiS as well | ||
# Basically noop and return the existing session |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already Session.builder.getOrCreate()
"protocol": os.getenv("SNOWFLAKE_PROTOCOL"), | ||
"authenticator": "oauth", | ||
"token": token, | ||
"warehouse": os.getenv("SNOWFLAKE_WAREHOUSE"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SNOWFLAKE_WAREHOUSE may or may not be set. Setting the QUERY_WAREHOUSE on the SERVICE is optional.
Please answer these questions before submitting your pull requests. Thanks!
What GitHub issue is this PR addressing? Make sure that there is an accompanying issue to your PR.
Fixes #NNNN
Fill out the following pre-review checklist:
Please describe how your code solves the related issue.
The goal is to add a new API
connect
for SiS and SPCS where we can create a session using the default connection params.