Function
Snowflake Docs - CREATE FUNCTION
Snowflake Docs - ALTER FUNCTION
Usage
- A Function is made up of 2 parts
- YAML Configuration
- Code with the policy itself (see next sections for supported languages and associated file extensiosn)
- All masking policies must live inside a [DATABASE]/[SCHEMA]/FUNCTIONS folder and contain both a .yml file and code file with the same name.
- Object name = name of the yml config file within [DATABASE]/[SCHEMA]/FUNCTIONS folder
- In Snowflake, multiple functions can share the same name with different signatures. Therefore, the signature with associated data types (no input names, just data types) must be include in the file name to make the file unique. See example sections below.
Supported Languages
Language |
File Extension |
|---|---|
SQL |
.sql |
JAVASCRIPT |
.js |
PYTHON |
.py |
Snowflake Attributes
Parameter |
Description |
|---|---|
INPUT_ARGS |
(List[{name: datatype}]) - Optional |
IS_SECURE |
(Bool) - Optional |
RETURNS |
(String) - Optional |
LANGUAGE |
(String) - Optional
|
NULL_HANDLING |
(String) - Optional
|
COMMENT |
(String) - Optional |
OWNER |
(String) - Optional
|
TAGS |
({KEY:VALUE}) - Optional |
GRANTS |
({KEY:VALUE}) - Optional |
Python Only
| IMPORTS | (List[String]) - Optional |
| HANDLER | (String) - Optional |
| RUNTIME_VERSION | (String) - Optional |
| PACKAGES | (List[String]) - Optional |
Optional Parameter Defaults - if omitted, Snowflake defaults for parameters are used, just like creating an object manually.
Deployment attributes
Parameter |
Description |
|---|---|
DEPLOY_ENV |
(List) - Optional
|
DEPLOY_LOCK |
(Bool) - Optional
|
Folder Structure
Configuration:
snowflake/data/[database name]/[schema name]/FUNCTIONS/[function name].yml
Code
snowflake/data/[database name]/[schema name]/FUNCTIONS/[function name].[extension]
Example Structure
snowflake/data/CONTROL/CODE/FUNCTIONS/GET_FISCAL_YEARS().yml
snowflake/data/CONTROL/CODE/FUNCTIONS/GET_FISCAL_YEARS.sql
snowflake/data/CONTROL/CODE/FUNCTIONS/PY_PI(varchar,int).yml
snowflake/data/CONTROL/CODE/FUNCTIONS/PY_PI(varchar,int).py
This specifies the metadata for a sql function named "GET_FISCAL_YEARS" with an empty signature and a python function "PY_PI" with a signature of (varchar,int). Both within the CONTROL.CODE schema, each with their own yml config file & code based on the function name.
Samples
Config
INPUT_ARGS:
- TABLENAME: VARCHAR
- ROLE: VARCHAR
RETURNS: TABLE (ID NUMBER, NAME VARCHAR, ROLE VARCHAR)
LANGUAGE: PYTHON
OWNER: INSTANCEADMIN
COMMENT:
IS_SECURE: false
IMPORTS:
HANDLER: filter_by_role
RUNTIME_VERSION: 3.8
PACKAGES:
- snowflake-snowpark-python
TAGS:
- {{ref('CONTROL__GOVERNANCE__ENV')}}: {{env}}
GRANTS:
- {{role('SOME_ROLE')}}: USAGE
Code file (.py in this example)
from snowflake.snowpark.functions import col
def filter_by_role(session, table_name, role):
df = session.table(table_name)
return df.filter(col("role") == role)