Skip to main content

Use Snowflake User Defined Functions (UDF)

Simple Example

Here is an example of a simple function in Snowflake that returns pi to 9-digits and doesn't accept any argument.

create function mydb.myschema.pi_udf()
returns float
as '3.141592654::FLOAT';

To use the above UDF as a transform in a Node, take the following steps:

  1. Run the above statement in Snowflake and make note of the database and schema it's in.

  2. Go to Build Settings >vStorage Locations.

  3. Create a Storage Location - named FUNC in the following examples, but feel free to choose any name.

  4. Map that Storage Location to mydb.myschema where the UDF was created in Step 1.

  5. Use ref_no_link to specify the storage location from Step 1, with name of function as 2nd parameter. It is very important to use ref_no_link as we do not want to attempt to create a relationship to another Coalesce metadata object. In this example, {{red_no_link('FUNC', 'PI_UDF')}}().

    General format - {{ref_no_link('storage_loc', 'func_name')}} (udf_argument1, udf_argument2, ...)

    The image shows the Mapping tab within the STG_SUPPLIER view of a data processing tool. It lists several columns such as S_SUPPKEY, S_NAME, S_ADDRESS, S_NATIONKEY, S_PHONE, S_ACCTBAL, and S_COMMENT along with their corresponding data types, sources, and nullability. The S_ACCTBAL column is highlighted and has a transformation applied using the function ref_no_link(FUNC, PI_UDF)().
  6. Run the node and you'll notice that all the entries for the S_ACCTBAL column will be 3.14.

Example With UDF Arguments

For this example, we'll use a function that accepts one argument and carries out a simple calculation.

CREATE FUNCTION mydb.myschema.area_of_circle(radius FLOAT)
RETURNS FLOAT
AS
$$
pi() * radius * radius
$$;

To use the above UDF as a transform in a Node, take the following steps:

  1. Run the above statement in Snowflake and make note of the database and schema it's in.

  2. If you went through the previous example, skip to the next step. Otherwise, follow Steps 2 to 4.

  3. Use ref_no_link to specify the storage location from Step 1, with the name of the function as the 2nd parameter, and the UDF argument will be in parenthesis () afterward.

    General format - {{ref_no_link('storage_loc', 'func_name')}} (udf_argument1, udf_argument2, ...)

    The image shows the Mapping tab within the STG_SUPPLIER view of a data processing tool. It lists columns such as S_SUPPKEY, S_NAME, S_ADDRESS, S_NATIONKEY, S_PHONE, S_ACCTBAL, and S_COMMENT along with their data types and sources. The S_ACCTBAL column is highlighted and has a transformation applied using the function ref_no_link(FUNC, AREA_OF_CIRCLE)(SUPPLIER.S_ACCTBAL).
  4. Run the node and you'll notice that all the entries for the S_ACCTBAL column will be much larger numbers, as they were multiplied times themselves and pi.