Back to changelog
Improvement
•2 minute read

Snowflake: Data Metric Functions, Storage Lifecycle Policies, and More for Tables

The Snowflake table block takes new attributes for error logging, row timestamps, the default collation, the data metric schedule, and search optimization, and attaches storage lifecycle policies and data metric functions.

New Table Attributes

Atlas writes each new table attribute as the Snowflake table parameter of the same name, except search_optimization, which is a separate ALTER TABLE ... ADD SEARCH OPTIMIZATION:

AttributeValue
error_loggingtrue or false
row_timestamptrue or false
default_ddl_collationA collation specification, such as "en-ci"
data_metric_scheduleThe schedule of the table's data metric functions, such as "60 MINUTE"
search_optimizationtrue or false

Storage Lifecycle Policies

A policy block attaches a storage lifecycle policy to the table. The block takes:

Data Metric Functions

A data_metric_function block sets a data metric function on the table. Each block sets one function on one list of columns, and takes:

  • ref, a data_metric_function reference or a string
  • columns, the columns the function reads
  • expectation, a repeatable, named block whose expression checks the function's result, read by VALUE

System functions (SNOWFLAKE.CORE) are not managed.

Example

Policy attachments need policies = true:

atlas.hcl
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" {      policies = true      funcs    = true    }  }}

The desired state declares a data metric function that counts null IDs, a storage lifecycle policy that selects timestamps more than 3650 days old, and an EVENTS table with the new attributes. The table attaches the policy to METADATA$ROW_LAST_COMMIT_TIME, the commit time that row_timestamp = true records, and sets the data metric function on ID with an expectation that no ID is null:

schema.hcl
schema "PUBLIC" {}
data_metric_function "NULL_IDS" {  schema = schema.PUBLIC  arg "ARG_T" {    column "ARG_ID" {      type = NUMBER(38)    }  }  as = "SELECT COUNT_IF(ARG_ID IS NULL) FROM ARG_T"}
storage_lifecycle_policy "OLD_COMMITS" {  schema = schema.PUBLIC  arg "TS" {    type = TIMESTAMP_LTZ  }  as = "TS < DATEADD(DAY, -3650, CURRENT_TIMESTAMP())"}
table "EVENTS" {  schema                = schema.PUBLIC  error_logging         = true  row_timestamp         = true  default_ddl_collation = "en-ci"  data_metric_schedule  = "60 MINUTE"  search_optimization   = true  column "ID" {    type = NUMBER(38)  }  policy "storage_lifecycle" {    ref     = storage_lifecycle_policy.OLD_COMMITS    columns = ["METADATA$ROW_LAST_COMMIT_TIME"]  }  data_metric_function {    ref     = data_metric_function.NULL_IDS    columns = ["ID"]    expectation "NO_NULLS" {      expression = "VALUE = 0"    }  }}

Run the diff:

atlas migrate diff create_events --env local

Atlas creates the function and the policy, then the table with its new parameters and the policy attachment, and adds search optimization and the data metric function with ALTER TABLE:

migrations/20261202075819_create_events.sql
-- Create data metric function "NULL_IDS"CREATE DATA METRIC FUNCTION "PUBLIC"."NULL_IDS" ("ARG_T" TABLE ("ARG_ID" NUMBER)) RETURNS NUMBER AS $$SELECT COUNT_IF(ARG_ID IS NULL) FROM ARG_T$$;-- Create storage lifecycle policy "OLD_COMMITS"CREATE STORAGE LIFECYCLE POLICY "PUBLIC"."OLD_COMMITS" AS ("TS" TIMESTAMP_LTZ) RETURNS BOOLEAN -> TS < DATEADD(DAY, -3650, CURRENT_TIMESTAMP());-- Create "EVENTS" tableCREATE TABLE "PUBLIC"."EVENTS" (  "ID" NUMBER NOT NULL) DATA_RETENTION_TIME_IN_DAYS = 1 ERROR_LOGGING = TRUE ROW_TIMESTAMP = TRUE DEFAULT_DDL_COLLATION = 'en-ci' DATA_METRIC_SCHEDULE = '60 MINUTE' WITH STORAGE LIFECYCLE POLICY "PUBLIC"."OLD_COMMITS" ON (METADATA$ROW_LAST_COMMIT_TIME);-- Modify "EVENTS" table to add search optimizationALTER TABLE "PUBLIC"."EVENTS" ADD SEARCH OPTIMIZATION;-- Modify "EVENTS" table to add data metric function "NULL_IDS"ALTER TABLE "PUBLIC"."EVENTS" ADD DATA METRIC FUNCTION "PUBLIC"."NULL_IDS" ON ("ID") EXPECTATION "NO_NULLS" (VALUE = 0);
Notes
  • Before the first schema apply after upgrading, run atlas schema inspect and add to the desired state what was set outside Atlas: search_optimization, data_metric_schedule, default_ddl_collation, data metric functions on tables, and storage lifecycle policies on tables. Otherwise atlas schema apply plans to remove them.
  • Leaving out error_logging or row_timestamp keeps the current value. Setting one to false deletes the error table or the row commit times; atlas migrate lint reports it as SF101 or SF102.

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.

improvementsnowflaketablesstorage lifecycle policydata metric functionatlas pro