While writing the Snowpark code in a Snowsight Python worksheet, how can I select a specific schema? I know I can manually select it using the UI, but I want to select it programmatically.
import snowflake.snowpark as snowpark
from snowflake.snowpark.functions import col
def main(session: snowpark.Session):
df_sql = session.sql("SELECT CURRENT_ACCOUNT(), CURRENT_USER(), CURRENT_ROLE(), CURRENT_WAREHOUSE(), CURRENT_DATABASE(), CURRENT_SCHEMA()")
return df_sql
I have been writing Snowpark code in a Snowsight Python worksheet. While I know that I can manually select a specific schema using the UI, I want to do this programmatically within my code. I have looked through the Snowpark documentation but couldn't find a clear example or method to set the schema programmatically. I was expecting to find a way to set the schema directly in my Snowpark code, similar to how it can be done in SQL with a USE SCHEMA statement.
============ Update-1 ============
I tested with the below code, but it's failing with the error GENERAL_INVALID_OBJECT_NAME for schema 'MY_SCHEMA', but this is a valid schema under the current active Database.
import snowflake.snowpark as snowpark
from snowflake.snowpark.functions import col
def main(session: snowpark.Session):
session.use_schema('MY_SCHEMA')
df_sql = session.sql("SELECT CURRENT_ACCOUNT(), CURRENT_USER(), CURRENT_ROLE(), CURRENT_WAREHOUSE(), CURRENT_DATABASE(), CURRENT_SCHEMA()")
return df_sql
Session.use_schema should allow you to update the current schema as seen below:
>>> session = Session.builder.configs(connection_parameters).create()
>>> session.use_schema('FINANCE')
>>> session.sql("SELECT CURRENT_SCHEMA()")
<snowflake.snowpark.dataframe.DataFrame object at 0x00000286F5726890>
>>> session.sql("SELECT CURRENT_SCHEMA()").show()
"CURRENT_SCHEMA()" |
---|
FINANCE |
Do you have special characters in the schema name?