Atlas now manages Snowflake data metric functions with the new data_metric_function block.
The new data_metric_function block declares a Snowflake data metric function in a schema. Each arg is a table argument whose column blocks declare its columns.
Data Metric Functions
- Like other functions, data metric functions are managed by the funcs mode, which is on by default.
- A function takes the required schema and as attributes and arg blocks, the optional return, secure, comment, and depends_on attributes, and tag blocks. return can also be a block with type and null.
- With permissions = true, permission blocks grant privileges on data metric functions.
- With tags = true, tag blocks set tags on data metric functions.
Example
The env manages functions with the funcs mode, which is on by default, grants with permissions = true, and tags with tags = true:
env "local" { url = "snowflake://user:password@account_identifier/DEMO_DB?warehouse=DEMO_WH" dev = "snowflake://user:password@account_identifier/DEMO_DEV_DB?warehouse=DEMO_WH" schema { src = "file://schema.hcl" mode "snowflake" { funcs = true permissions = true tags = true } }}
The desired schema declares a secure data metric function that counts the rows where three columns are positive, tags it, and grants USAGE on it to the DQ_ANALYST database role, which exists outside Atlas:
schema "PUBLIC" {} tag "DATA_QUALITY_TIER" { schema = schema.PUBLIC allowed_values = ["critical", "high", "low"] comment = "How urgent a failing data quality check is"} data_metric_function "COUNT_POSITIVE_NUMBERS" { schema = schema.PUBLIC arg "ARG_T" { column "ARG_C1" { type = NUMBER } column "ARG_C2" { type = NUMBER } column "ARG_C3" { type = NUMBER } } as = <<-SQL SELECT COUNT(*) FROM ARG_T WHERE ARG_C1 > 0 AND ARG_C2 > 0 AND ARG_C3 > 0 SQL secure = true comment = "count positive numbers" tag { ref = tag.DATA_QUALITY_TIER value = "high" }} # DQ_ANALYST is a database role created outside Atlas: ref() only grants to it.permission { to = ref("database_role.DQ_ANALYST") for = data_metric_function.COUNT_POSITIVE_NUMBERS privileges = [USAGE]}
Run the diff:
atlas migrate diff --env localAtlas writes the migration:
-- Create tag "DATA_QUALITY_TIER"CREATE TAG "PUBLIC"."DATA_QUALITY_TIER" ALLOWED_VALUES 'critical', 'high', 'low' COMMENT = 'How urgent a failing data quality check is';-- Create data metric function "COUNT_POSITIVE_NUMBERS"CREATE SECURE DATA METRIC FUNCTION "PUBLIC"."COUNT_POSITIVE_NUMBERS" ("ARG_T" TABLE ("ARG_C1" NUMBER, "ARG_C2" NUMBER, "ARG_C3" NUMBER)) RETURNS NUMBER COMMENT = 'count positive numbers' AS $$SELECT COUNT(*)FROM ARG_TWHERE ARG_C1 > 0 AND ARG_C2 > 0 AND ARG_C3 > 0$$;-- Modify data metric function "COUNT_POSITIVE_NUMBERS" to set tagsALTER FUNCTION "PUBLIC"."COUNT_POSITIVE_NUMBERS" (TABLE(NUMBER, NUMBER, NUMBER)) SET TAG PUBLIC.DATA_QUALITY_TIER = 'high';-- Grant on data metric function "COUNT_POSITIVE_NUMBERS" to "DQ_ANALYST"GRANT USAGE ON FUNCTION "PUBLIC"."COUNT_POSITIVE_NUMBERS" (TABLE(NUMBER, NUMBER, NUMBER)) TO DATABASE ROLE "DQ_ANALYST";
Getting Started
Snowflake support is part of Atlas Pro. Run atlas login to get started.
See the Snowflake HCL reference for every attribute and block.