snowflake.snowpark.Session¶

class snowflake.snowpark.Session(conn: Union[ServerConnection, MockServerConnection, NopConnection], options: Optional[Dict[str, Any]] = None)[source]¶

Bases: object

Establishes a connection with a Snowflake database and provides methods for creating DataFrames and accessing objects for working with files in stages.

When you create a Session object, you provide connection parameters to establish a connection with a Snowflake database (e.g. an account, a user name, etc.). You can specify these settings in a dict that associates connection parameters names with values. The Snowpark library uses the Snowflake Connector for Python to connect to Snowflake. Refer to Connecting to Snowflake using the Python Connector for the details of Connection Parameters.

To create a Session object from a dict of connection parameters:

>>> connection_parameters = {
...     "user": "<user_name>",
...     "password": "<password>",
...     "account": "<account_name>",
...     "role": "<role_name>",
...     "warehouse": "<warehouse_name>",
...     "database": "<database_name>",
...     "schema": "<schema_name>",
... }
>>> session = Session.builder.configs(connection_parameters).create() 
Copy

To create a Session object from an existing Python Connector connection:

>>> session = Session.builder.configs({"connection": <your python connector connection>}).create() 
Copy

Session contains functions to construct a DataFrame like table(), sql() and read, etc.

A Session object is not thread-safe.

Methods

add_import(path[, import_path, chunk_size, ...])

Registers a remote file in stage or a local file as an import of a user-defined function (UDF).

add_packages(*packages)

Adds third-party packages as dependencies of a user-defined function (UDF).

add_requirements(file_path)

Adds a requirement file that contains a list of packages as dependencies of a user-defined function (UDF).

append_query_tag(tag[, separator])

Appends a tag to the current query tag.

ast_listener([include_error])

Creates an instance of AstListener as a context manager to capture ast batches flushed.

call(sproc_name, *args[, statement_params, ...])

Calls a stored procedure by name.

cancel_all()

Cancel all action methods that are running currently.

clear_imports()

Clears all files in a stage or local files from the imports of a user-defined function (UDF).

clear_packages()

Clears all third-party packages of a user-defined function (UDF).

close()

Close this session.

createDataFrame(data[, schema])

Creates a new DataFrame containing the specified values from the local data.

create_async_job(query_id)

Creates an AsyncJob from a query ID.

create_dataframe(data[, schema])

Creates a new DataFrame containing the specified values from the local data.

flatten(input[, path, outer, recursive, mode])

Creates a new DataFrame by flattening compound values into multiple rows.

generator(*columns[, rowcount, timelimit])

Creates a new DataFrame using the Generator table function.

getActiveSession()

Gets the active session if one is created.

get_active_session()

Gets the active session if one is created.

get_current_account()

Returns the name of the current account for the Python connector session attached to this session.

get_current_database()

Returns the name of the current database for the Python connector session attached to this session.

get_current_role()

Returns the name of the primary role in use for the current session.

get_current_schema()

Returns the name of the current schema for the Python connector session attached to this session.

get_current_user()

Returns the name of the user in the connection to Snowflake attached to this session.

get_current_warehouse()

Returns the name of the warehouse in use for the current session.

get_fully_qualified_current_schema()

Returns the fully qualified name of the current schema for the session.

get_fully_qualified_name_if_possible(name)

Returns the fully qualified object name if current database/schema exists, otherwise returns the object name

get_imports()

Returns a list of imports added for user defined functions (UDFs).

get_packages()

Returns a dict of packages added for user-defined functions (UDFs).

get_session_stage([statement_params])

Returns the name of the temporary stage created by the Snowpark library for uploading and storing temporary artifacts for this session.

is_feature_enabled_for_version(parameter_name)

This method checks if a feature is enabled for the current session based on the server side parameter.

query_history([include_describe, ...])

Create an instance of QueryHistory as a context manager to record queries that are pushed down to the Snowflake database.

range(start[, end, step])

Creates a new DataFrame from a range of numbers.

remove_import(path)

Removes a file in stage or local file from the imports of a user-defined function (UDF).

remove_package(package)

Removes a third-party package from the dependency list of a user-defined function (UDF).

replicate_local_environment([...])

Adds all third-party packages in your local environment as dependencies of a user-defined function (UDF).

sql(query[, params])

Returns a new DataFrame representing the results of a SQL query.

table(name)

Returns a Table that points the specified table.

table_function(func_name, *func_arguments, ...)

Creates a new DataFrame from the given snowflake SQL table function.

update_query_tag(tag)

Updates a query tag that is a json encoded string.

use_database(database)

Specifies the active/current database for the session.

use_role(role)

Specifies the active/current primary role for the session.

use_schema(schema)

Specifies the active/current schema for the session.

use_secondary_roles(roles)

Specifies the active/current secondary roles for the session.

use_warehouse(warehouse)

Specifies the active/current warehouse for the session.

write_pandas(df, table_name, *[, database, ...])

Writes a pandas DataFrame to a table in Snowflake and returns a Snowpark DataFrame object referring to the table where the pandas DataFrame was written to.

Attributes

ast_enabled

auto_clean_up_temp_table_enabled

When setting this parameter to True, Snowpark will automatically clean up temporary tables created by DataFrame.cache_result() in the current session when the DataFrame is no longer referenced (i.e., gets garbage collected).

builder

Returns a builder you can use to set configuration properties and create a Session object.

conf

connection

Returns a SnowflakeConnection object that allows you to access the connection between the current session and Snowflake server.

cte_optimization_enabled

Set to True to enable the CTE optimization (defaults to False).

custom_package_usage_config

Get or set configuration parameters related to usage of custom Python packages in Snowflake.

eliminate_numeric_sql_value_cast_enabled

file

Returns a FileOperation object that you can use to perform file operations on stages.

large_query_breakdown_complexity_bounds

large_query_breakdown_enabled

lineage

Returns a Lineage object that you can use to explore lineage of snowflake entities.

query_tag

The query tag for this session.

read

Returns a DataFrameReader that you can use to read data from various supported sources (e.g.

reduce_describe_query_enabled

When setting this parameter to True, Snowpark will infer the schema of DataFrame locally if possible, instead of issuing an internal describe query to get the schema from the Snowflake server.

session_id

Returns an integer that represents the session ID of this session.

sproc

Returns a stored_procedure.StoredProcedureRegistration object that you can use to register stored procedures.

sql_simplifier_enabled

Set to True to use the SQL simplifier (defaults to True).

stored_procedure_profiler

Returns a stored_procedure_profiler.StoredProcedureProfiler object that you can use to profile stored procedures.

telemetry_enabled

Controls whether or not the Snowpark client sends usage telemetry to Snowflake.

udaf

Returns a udaf.UDAFRegistration object that you can use to register UDAFs.

udf

Returns a udf.UDFRegistration object that you can use to register UDFs.

udtf

Returns a udtf.UDTFRegistration object that you can use to register UDTFs.