"use strict";(self.webpackChunkdocs_website=self.webpackChunkdocs_website||[]).push([[34538],{15680:(e,t,n)=>{n.d(t,{xA:()=>c,yg:()=>m});var a=n(96540);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function s(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function o(e){for(var t=1;t=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);for(a=0;a=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var i=a.createContext({}),u=function(e){var t=a.useContext(i),n=t;return e&&(n="function"==typeof e?e(t):o(o({},t),e)),n},c=function(e){var t=u(e.components);return a.createElement(i.Provider,{value:t},e.children)},g="mdxType",p={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},d=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,s=e.originalType,i=e.parentName,c=l(e,["components","mdxType","originalType","parentName"]),g=u(n),d=r,m=g["".concat(i,".").concat(d)]||g[d]||p[d]||s;return n?a.createElement(m,o(o({ref:t},c),{},{components:n})):a.createElement(m,o({ref:t},c))}));function m(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var s=n.length,o=new Array(s);o[0]=d;var l={};for(var i in t)hasOwnProperty.call(t,i)&&(l[i]=t[i]);l.originalType=e,l[g]="string"==typeof e?e:r,o[1]=l;for(var u=2;u{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>i,default:()=>m,frontMatter:()=>l,metadata:()=>u,toc:()=>g});n(96540);var a=n(15680);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function s(e,t){return t=null!=t?t:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):function(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))})),e}function o(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},s=Object.keys(e);for(a=0;a=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);for(a=0;a=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}const l={title:"Snowflake DMF Assertions [BETA]",slug:"/assertions/snowflake/snowflake_dmfs",custom_edit_url:"https://github.com/datahub-project/datahub/blob/master/docs/assertions/snowflake/snowflake_dmfs.md"},i="Snowflake DMF Assertions [BETA]",u={unversionedId:"docs/assertions/snowflake/snowflake_dmfs",id:"version-1.1.0/docs/assertions/snowflake/snowflake_dmfs",title:"Snowflake DMF Assertions [BETA]",description:"The DataHub Open Assertion Compiler allows you to define your Data Quality assertions in a simple YAML format, and then compile them to be executed by Snowflake Data Metric Functions.",source:"@site/versioned_docs/version-1.1.0/docs/assertions/snowflake/snowflake_dmfs.md",sourceDirName:"docs/assertions/snowflake",slug:"/assertions/snowflake/snowflake_dmfs",permalink:"/docs/1.1.0/assertions/snowflake/snowflake_dmfs",draft:!1,editUrl:"https://github.com/datahub-project/datahub/blob/master/docs/assertions/snowflake/snowflake_dmfs.md",tags:[],version:"1.1.0",frontMatter:{title:"Snowflake DMF Assertions [BETA]",slug:"/assertions/snowflake/snowflake_dmfs",custom_edit_url:"https://github.com/datahub-project/datahub/blob/master/docs/assertions/snowflake/snowflake_dmfs.md"},sidebar:"overviewSidebar",previous:{title:"Open Data Quality Assertions Specification",permalink:"/docs/1.1.0/assertions/open-assertions-spec"},next:{title:"Access Management",permalink:"/docs/1.1.0/features/feature-guides/access-management"}},c={},g=[{value:"Prerequisites",id:"prerequisites",level:2},{value:"Permissions",id:"permissions",level:3},{value:"Supported Assertion Types",id:"supported-assertion-types",level:2},{value:"Creating Snowflake DMF Assertions",id:"creating-snowflake-dmf-assertions",level:2},{value:"Step 1. Define your Data Quality assertions using Assertion YAML files",id:"step-1-define-your-data-quality-assertions-using-assertion-yaml-files",level:3},{value:"Step 2. Register your assertions with DataHub",id:"step-2-register-your-assertions-with-datahub",level:3},{value:"Step 3. Compile the assertions into Snowflake DMFs using the DataHub CLI",id:"step-3-compile-the-assertions-into-snowflake-dmfs-using-the-datahub-cli",level:3},{value:"Step 4. Register the compiled DMFs in your Snowflake environment",id:"step-4-register-the-compiled-dmfs-in-your-snowflake-environment",level:3},{value:"Step 5. Run ingestion to report the results back into DataHub",id:"step-5-run-ingestion-to-report-the-results-back-into-datahub",level:3},{value:"Caveats",id:"caveats",level:2},{value:"FAQ",id:"faq",level:2}],p={toc:g},d="wrapper";function m(e){var{components:t}=e,n=o(e,["components"]);return(0,a.yg)(d,s(function(e){for(var t=1;t to create DMFs and associate DMFs with table\ngrant usage on database "" to role ""\ngrant usage on schema "." to role ""\ngrant create function on schema "." to role ""\n-- grant ownership + rest of permissions to \ngrant role "" to role ""\n\n-- setup permissions for to run DMFs on schedule\ngrant usage on database "" to role ""\ngrant usage on schema "." to role ""\ngrant usage on all functions in "." to role ""\ngrant usage on future functions in "." to role ""\ngrant database role SNOWFLAKE.DATA_METRIC_USER to role ""\ngrant execute data metric function on account to role ""\n\n-- setup permissions for to query DMF results\ngrant application role SNOWFLAKE.DATA_QUALITY_MONITORING_VIEWER to role ""\n')),(0,a.yg)("h2",{id:"supported-assertion-types"},"Supported Assertion Types"),(0,a.yg)("p",null,"The following assertion types are currently supported by the DataHub Snowflake DMF Assertion Compiler:"),(0,a.yg)("ul",null,(0,a.yg)("li",{parentName:"ul"},(0,a.yg)("a",{parentName:"li",href:"/docs/1.1.0/managed-datahub/observe/freshness-assertions"},"Freshness")),(0,a.yg)("li",{parentName:"ul"},(0,a.yg)("a",{parentName:"li",href:"/docs/1.1.0/managed-datahub/observe/volume-assertions"},"Volume")),(0,a.yg)("li",{parentName:"ul"},(0,a.yg)("a",{parentName:"li",href:"/docs/1.1.0/managed-datahub/observe/column-assertions"},"Column")),(0,a.yg)("li",{parentName:"ul"},(0,a.yg)("a",{parentName:"li",href:"/docs/1.1.0/managed-datahub/observe/custom-sql-assertions"},"Custom SQL"))),(0,a.yg)("p",null,"Note that Schema Assertions are not currently supported."),(0,a.yg)("h2",{id:"creating-snowflake-dmf-assertions"},"Creating Snowflake DMF Assertions"),(0,a.yg)("p",null,"The process for declaring and running assertions backend by Snowflake DMFs consists of a few steps, which will be outlined\nin the following sections."),(0,a.yg)("h3",{id:"step-1-define-your-data-quality-assertions-using-assertion-yaml-files"},"Step 1. Define your Data Quality assertions using Assertion YAML files"),(0,a.yg)("p",null,"See the section ",(0,a.yg)("strong",{parentName:"p"},"Declaring Assertions in YAML")," below for examples of how to define assertions in YAML."),(0,a.yg)("h3",{id:"step-2-register-your-assertions-with-datahub"},"Step 2. Register your assertions with DataHub"),(0,a.yg)("p",null,"Use the DataHub CLI to register your assertions with DataHub, so they become visible in the DataHub UI:"),(0,a.yg)("pre",null,(0,a.yg)("code",{parentName:"pre",className:"language-bash"},"datahub assertions upsert -f examples/library/assertions_configuration.yml\n")),(0,a.yg)("h3",{id:"step-3-compile-the-assertions-into-snowflake-dmfs-using-the-datahub-cli"},"Step 3. Compile the assertions into Snowflake DMFs using the DataHub CLI"),(0,a.yg)("p",null,"Next, we'll use the ",(0,a.yg)("inlineCode",{parentName:"p"},"assertions compile")," command to generate the SQL code for the Snowflake DMFs,\nwhich can then be registered in Snowflake."),(0,a.yg)("pre",null,(0,a.yg)("code",{parentName:"pre",className:"language-bash"},"datahub assertions compile -f examples/library/assertions_configuration.yml -p snowflake -x DMF_SCHEMA=.\n")),(0,a.yg)("p",null,"Two files will be generated as output of running this command:"),(0,a.yg)("ul",null,(0,a.yg)("li",{parentName:"ul"},(0,a.yg)("inlineCode",{parentName:"li"},"dmf_definitions.sql"),": This file contains the SQL code for the DMFs that will be registered in Snowflake."),(0,a.yg)("li",{parentName:"ul"},(0,a.yg)("inlineCode",{parentName:"li"},"dmf_associations.sql"),": This file contains the SQL code for associating the DMFs with the target tables in Snowflake.")),(0,a.yg)("p",null,"By default in a folder called ",(0,a.yg)("inlineCode",{parentName:"p"},"target"),". You can use config option ",(0,a.yg)("inlineCode",{parentName:"p"},"-o ")," in ",(0,a.yg)("inlineCode",{parentName:"p"},"compile")," command to write these compile artifacts in another folder."),(0,a.yg)("p",null,"Each of these artifacts will be important for the next steps in the process."),(0,a.yg)("p",null,(0,a.yg)("em",{parentName:"p"},"dmf_definitions.sql")),(0,a.yg)("p",null,"This file stores the SQL code for the DMFs that will be registered in Snowflake, generated\nfrom your YAML assertion definitions during the compile step."),(0,a.yg)("pre",null,(0,a.yg)("code",{parentName:"pre",className:"language-sql"},"-- Example dmf_definitions.sql\n\n-- Start of Assertion 5c32eef47bd763fece7d21c7cbf6c659\n\n CREATE or REPLACE DATA METRIC FUNCTION\n test_db.datahub_dmfs.datahub__5c32eef47bd763fece7d21c7cbf6c659 (ARGT TABLE(col_date DATE))\n RETURNS NUMBER\n COMMENT = 'Created via DataHub for assertion urn:li:assertion:5c32eef47bd763fece7d21c7cbf6c659 of type volume'\n AS\n $$\n select case when metric <= 1000 then 1 else 0 end from (select count(*) as metric from TEST_DB.PUBLIC.TEST_ASSERTIONS_ALL_TIMES )\n $$;\n\n-- End of Assertion 5c32eef47bd763fece7d21c7cbf6c659\n....\n")),(0,a.yg)("p",null,(0,a.yg)("em",{parentName:"p"},"dmf_associations.sql")),(0,a.yg)("p",null,"This file stores the SQL code for associating with the target table,\nalong with scheduling the generated DMFs to run on at particular times."),(0,a.yg)("pre",null,(0,a.yg)("code",{parentName:"pre",className:"language-sql"},"-- Example dmf_associations.sql\n\n-- Start of Assertion 5c32eef47bd763fece7d21c7cbf6c659\n\n ALTER TABLE TEST_DB.PUBLIC.TEST_ASSERTIONS_ALL_TIMES SET DATA_METRIC_SCHEDULE = 'TRIGGER_ON_CHANGES';\n ALTER TABLE TEST_DB.PUBLIC.TEST_ASSERTIONS_ALL_TIMES ADD DATA METRIC FUNCTION test_db.datahub_dmfs.datahub__5c32eef47bd763fece7d21c7cbf6c659 ON (col_date);\n\n-- End of Assertion 5c32eef47bd763fece7d21c7cbf6c659\n....\n")),(0,a.yg)("h3",{id:"step-4-register-the-compiled-dmfs-in-your-snowflake-environment"},"Step 4. Register the compiled DMFs in your Snowflake environment"),(0,a.yg)("p",null,"Next, you'll need to run the generated SQL from the files output in Step 3 in Snowflake."),(0,a.yg)("p",null,"You can achieve this either by running the SQL files directly in the Snowflake UI, or by using the SnowSQL CLI tool:"),(0,a.yg)("pre",null,(0,a.yg)("code",{parentName:"pre",className:"language-bash"},"snowsql -f dmf_definitions.sql\nsnowsql -f dmf_associations.sql\n")),(0,a.yg)("p",null,":::NOTE\nScheduling Data Metric Function on table incurs Serverless Credit Usage in Snowflake. Refer ",(0,a.yg)("a",{parentName:"p",href:"https://docs.snowflake.com/en/user-guide/data-quality-intro#billing-and-pricing"},"Billing and Pricing")," for more details.\nPlease ensure you DROP Data Metric Function created via dmf_associations.sql if the assertion is no longer in use.\n:::"),(0,a.yg)("h3",{id:"step-5-run-ingestion-to-report-the-results-back-into-datahub"},"Step 5. Run ingestion to report the results back into DataHub"),(0,a.yg)("p",null,"Once you've registered the DMFs, they will be automatically executed, either when the target table is updated or on a fixed\nschedule."),(0,a.yg)("p",null,"To report the results of the generated Data Quality assertions back into DataHub, you'll need to run the DataHub ingestion process with a special configuration\nflag: ",(0,a.yg)("inlineCode",{parentName:"p"},"include_assertion_results: true"),":"),(0,a.yg)("pre",null,(0,a.yg)("code",{parentName:"pre",className:"language-yaml"},"# Your DataHub Snowflake Recipe\nsource:\n type: snowflake\n config:\n # ...\n include_assertion_results: True\n # ...\n")),(0,a.yg)("p",null,"During ingestion we will query for the latest DMF results stored in Snowflake, convert them into DataHub Assertion Results, and report the results back into DataHub during your ingestion process\neither via CLI or the UI visible as normal assertions."),(0,a.yg)("p",null,(0,a.yg)("inlineCode",{parentName:"p"},"datahub ingest -c snowflake.yml")),(0,a.yg)("h2",{id:"caveats"},"Caveats"),(0,a.yg)("ul",null,(0,a.yg)("li",{parentName:"ul"},"Currently, Snowflake supports at most 1000 DMF-table associations at the moment so you can not define more than 1000 assertions for snowflake."),(0,a.yg)("li",{parentName:"ul"},"Currently, Snowflake does not allow JOIN queries or non-deterministic functions in DMF definition so you can not use these in SQL for SQL assertion or in filters section."),(0,a.yg)("li",{parentName:"ul"},"Currently, all DMFs scheduled on a table must follow same exact schedule, so you can not set assertions on same table to run on different schedules."),(0,a.yg)("li",{parentName:"ul"},"Currently, DMFs are only supported for regular tables and not dynamic or external tables.")),(0,a.yg)("h2",{id:"faq"},"FAQ"),(0,a.yg)("p",null,"Coming soon!"))}m.isMDXComponent=!0}}]);