Snowflake
Snowflake steps and tasks for Koheesio
Every class in this module is a subclass of Step
or BaseModel
and is used to perform operations on Snowflake.
Notes
Every Step in this module is based on SnowflakeBaseModel. The following parameters are available for every Step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
Hostname for the Snowflake account, e.g. |
required |
user
|
str
|
Login name for the Snowflake user.
Alias for |
required |
password
|
SecretStr
|
Password for the Snowflake user.
Alias for |
required |
database
|
str
|
The database to use for the session after connecting.
Alias for |
required |
sfSchema
|
str
|
The schema to use for the session after connecting.
Alias for |
required |
role
|
str
|
The default security role to use for the session after connecting.
Alias for |
required |
warehouse
|
str
|
The default virtual warehouse to use for the session after connecting.
Alias for |
required |
authenticator
|
Optional[str]
|
Authenticator for the Snowflake user. Example: "okta.com". |
None
|
options
|
Optional[Dict[str, Any]]
|
Extra options to pass to the Snowflake connector. |
{"sfCompress": "on", "continue_on_error": "off"}
|
format
|
str
|
The default |
"snowflake"
|
koheesio.integrations.snowflake.GrantPrivilegesOnFullyQualifiedObject #
Grant Snowflake privileges to a set of roles on a fully qualified object, i.e. database.schema.object_name
This class is a subclass of GrantPrivilegesOnObject
and is used to grant privileges on a fully qualified object.
The advantage of using this class is that it sets the object name to be fully qualified, i.e.
database.schema.object_name
.
Meaning, you can set the database
, schema
and object
separately and the object name will be set to be fully
qualified, i.e. database.schema.object_name
.
Example
GrantPrivilegesOnFullyQualifiedObject(
database="MY_DB",
schema="MY_SCHEMA",
warehouse="MY_WH",
...
object="MY_TABLE",
type="TABLE",
...
)
In this example, the object name will be set to be fully qualified, i.e. MY_DB.MY_SCHEMA.MY_TABLE
.
If you were to use GrantPrivilegesOnObject
instead, you would have to set the object name to be fully qualified
yourself.
set_object_name #
Set the object name to be fully qualified, i.e. database.schema.object_name
Source code in src/koheesio/integrations/snowflake/__init__.py
koheesio.integrations.snowflake.GrantPrivilegesOnObject #
A wrapper on Snowflake GRANT privileges
With this Step, you can grant Snowflake privileges to a set of roles on a table, a view, or an object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
account
|
str
|
Snowflake Account Name. |
required |
warehouse
|
str
|
The name of the warehouse. Alias for |
required |
user
|
str
|
The username. Alias for |
required |
password
|
SecretStr
|
The password. Alias for |
required |
role
|
str
|
The role name |
required |
object
|
str
|
The name of the object to grant privileges on |
required |
type
|
str
|
The type of object to grant privileges on, e.g. TABLE, VIEW |
required |
privileges
|
Union[conlist(str, min_length=1), str]
|
The Privilege/Permission or list of Privileges/Permissions to grant on the given object. |
required |
roles
|
Union[conlist(str, min_length=1), str]
|
The Role or list of Roles to grant the privileges to |
required |
Example
GrantPermissionsOnTable(
object="MY_TABLE",
type="TABLE",
warehouse="MY_WH",
user="gid.account@abc.com",
password=Secret("super-secret-password"),
role="APPLICATION.SNOWFLAKE.ADMIN",
permissions=["SELECT", "INSERT"],
).execute()
In this example, the APPLICATION.SNOWFLAKE.ADMIN
role will be granted SELECT
and INSERT
privileges on
the MY_TABLE
table using the MY_WH
warehouse.
object
class-attribute
instance-attribute
#
object: str = Field(
default=...,
description="The name of the object to grant privileges on",
)
privileges
class-attribute
instance-attribute
#
privileges: Union[conlist(str, min_length=1), str] = Field(
default=...,
alias="permissions",
description="The Privilege/Permission or list of Privileges/Permissions to grant on the given object. See https://docs.snowflake.com/en/sql-reference/sql/grant-privilege.html",
)
query
class-attribute
instance-attribute
#
query: str = (
"GRANT {privileges} ON {type} {object} TO ROLE {role}"
)
roles
class-attribute
instance-attribute
#
roles: Union[conlist(str, min_length=1), str] = Field(
default=...,
alias="role",
validation_alias="roles",
description="The Role or list of Roles to grant the privileges to",
)
type
class-attribute
instance-attribute
#
type: str = Field(
default=...,
description="The type of object to grant privileges on, e.g. TABLE, VIEW",
)
Output #
execute #
Source code in src/koheesio/integrations/snowflake/__init__.py
get_query #
Build the GRANT query
Parameters:
Name | Type | Description | Default |
---|---|---|---|
role
|
str
|
The role name |
required |
Returns:
Name | Type | Description |
---|---|---|
query |
str
|
The Query that performs the grant |
Source code in src/koheesio/integrations/snowflake/__init__.py
set_roles_privileges #
Coerce roles and privileges to be lists if they are not already.
Source code in src/koheesio/integrations/snowflake/__init__.py
validate_object_and_object_type #
Validate that the object and type are set.
Source code in src/koheesio/integrations/snowflake/__init__.py
koheesio.integrations.snowflake.GrantPrivilegesOnTable #
Grant Snowflake privileges to a set of roles on a table
koheesio.integrations.snowflake.GrantPrivilegesOnView #
Grant Snowflake privileges to a set of roles on a view
koheesio.integrations.snowflake.SnowflakeBaseModel #
BaseModel for setting up Snowflake Driver options.
Notes
- Snowflake is supported natively in Databricks 4.2 and newer: https://docs.snowflake.com/en/user-guide/spark-connector-databricks
- Refer to Snowflake docs for the installation instructions for non-Databricks environments: https://docs.snowflake.com/en/user-guide/spark-connector-install
- Refer to Snowflake docs for connection options: https://docs.snowflake.com/en/user-guide/spark-connector-use#setting-configuration-options-for-the-connector
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
Hostname for the Snowflake account, e.g. |
required |
user
|
str
|
Login name for the Snowflake user.
Alias for |
required |
password
|
SecretStr
|
Password for the Snowflake user.
Alias for |
required |
role
|
str
|
The default security role to use for the session after connecting.
Alias for |
required |
warehouse
|
str
|
The default virtual warehouse to use for the session after connecting.
Alias for |
required |
authenticator
|
Optional[str]
|
Authenticator for the Snowflake user. Example: "okta.com". |
None
|
database
|
Optional[str]
|
The database to use for the session after connecting.
Alias for |
None
|
sfSchema
|
Optional[str]
|
The schema to use for the session after connecting.
Alias for |
None
|
options
|
Optional[Dict[str, Any]]
|
Extra options to pass to the Snowflake connector. |
{"sfCompress": "on", "continue_on_error": "off"}
|
authenticator
class-attribute
instance-attribute
#
authenticator: Optional[str] = Field(
default=None,
description="Authenticator for the Snowflake user",
examples=["okta.com"],
)
database
class-attribute
instance-attribute
#
database: Optional[str] = Field(
default=None,
alias="sfDatabase",
description="The database to use for the session after connecting",
)
options
class-attribute
instance-attribute
#
options: Optional[Dict[str, Any]] = Field(
default={
"sfCompress": "on",
"continue_on_error": "off",
},
description="Extra options to pass to the Snowflake connector",
)
password
class-attribute
instance-attribute
#
password: SecretStr = Field(
default=...,
alias="sfPassword",
description="Password for the Snowflake user",
)
role
class-attribute
instance-attribute
#
role: str = Field(
default=...,
alias="sfRole",
description="The default security role to use for the session after connecting",
)
sfSchema
class-attribute
instance-attribute
#
sfSchema: Optional[str] = Field(
default=...,
alias="schema",
description="The schema to use for the session after connecting",
)
url
class-attribute
instance-attribute
#
url: str = Field(
default=...,
alias="sfURL",
description="Hostname for the Snowflake account, e.g. <account>.snowflakecomputing.com",
examples=["example.snowflakecomputing.com"],
)
user
class-attribute
instance-attribute
#
user: str = Field(
default=...,
alias="sfUser",
description="Login name for the Snowflake user",
)
warehouse
class-attribute
instance-attribute
#
warehouse: str = Field(
default=...,
alias="sfWarehouse",
description="The default virtual warehouse to use for the session after connecting",
)
get_options #
Get the sfOptions as a dictionary.
Note
- Any parameters that are
None
are excluded from the output dictionary. sfSchema
andpassword
are handled separately.- The values from both 'options' and 'params' (kwargs / extra params) are included as is.
- Koheesio specific fields are excluded by default (i.e.
name
,description
,format
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
by_alias
|
bool
|
Whether to use the alias names or not. E.g. |
True
|
include
|
Optional[Set[str]]
|
Set of keys to include in the output dictionary. When None is provided, all fields will be returned. Note: be sure to include all the keys you need. |
None
|
Source code in src/koheesio/integrations/snowflake/__init__.py
koheesio.integrations.snowflake.SnowflakeRunQueryPython #
Run a query on Snowflake using the Python connector
Example
account
class-attribute
instance-attribute
#
account: Optional[str] = Field(
default=None,
description="Snowflake Account Name",
alias="account",
)
query
class-attribute
instance-attribute
#
query: str = Field(
default=...,
description="The query to run",
alias="sql",
serialization_alias="query",
)
Output #
execute #
Execute the query
Source code in src/koheesio/integrations/snowflake/__init__.py
get_options #
Source code in src/koheesio/integrations/snowflake/__init__.py
validate_query #
Replace escape characters, strip whitespace, ensure it is not empty
Source code in src/koheesio/integrations/snowflake/__init__.py
koheesio.integrations.snowflake.SnowflakeStep #
Expands the SnowflakeBaseModel so that it can be used as a Step
koheesio.integrations.snowflake.SnowflakeTableStep #
koheesio.integrations.snowflake.safe_import_snowflake_connector #
safe_import_snowflake_connector() -> Optional[ModuleType]
Validate that the Snowflake connector is installed
Returns:
Type | Description |
---|---|
Optional[ModuleType]
|
The Snowflake connector module if it is installed, otherwise None |