Skip to main content

User-Defined Functions allow us to write functions inside our SQL code. In Snowflake you can use SQL, Java, JavaScript and Python to write UDFs. While you are creating a UDF, the main thing that you should consider is to be sure that the function has a return value. Spoiler!! This is the main difference between User-Defined Functions and the Stored Procedures that we are going to talk about in the next blog. There are two types of them; User-Defined Function (UDF) which is a scalar function that returns one output and User-Defined Table Function (UDTF) which is a tabular function that returns tabular value.

Once you have created these functions; you can call them from any part of your code. So it gives you the ability to write your whole code in a more determine and in a cleaner way.

With UDFs you can’t take actions to modify the database. You can’t use temporary tables inside the UDFs.

Let’s have some examples to understand better how we can use them. We will create 2 differet User-Defined Functions where one will be executed in Python and will have UDF and the other one will execute SQL and will have UDTF. For simplicity let’s do the exact same operation for two of them; just multiply the inserte value with 3:

User-Defined Function Example (in Python)

CREATE OR REPLACE FUNCTION multiply_three(num integer)
RETURNS int
LANGUAGE python
RUNTIME_VERSION = '3.8'
HANDLER = 'multiply_three_py'
--PACKAGES = ('math')
AS
$$
def multiply_three_py(i):
    return i*3
$$;
SELECT multiply_three(3);
UDF Example using Python
UDF Example using Python

The most important part that you have to pay attention to is naming your HANDLER with the same name of your function declaration of your code. Since HANDLER will be your function name; your system will require that handler from your SQL code; while a user want to use our function, they will call it with the SQL name as in the example with multiply_three(x).

While using Python we could insert the python package that will help us in the code. To do so we should add the package parameter in the function creation and give the packages that we want to use in our function. For example if we would like to use Numpy in our function, we could add:

packages = ('numpy', 'pandas')

we could use multiple packages in the same function.

User-Defined Table Function Example (in SQL)

The return value will be the biggest difference between UTDF and UDF. Since we are expecting a table to return; the return value will be table and we should specify the column types of that table at the same time. Let me show you with the example that we mentioned before.

UDTF initial table

As you can see here inside of our udtf_exercise table we have 6 rows of integer values.

And we expect from our function to return us a table that has these values in a column and to add one more column that stores the triplicated ones. (Without making changes on table)

CREATE OR REPLACE FUNCTION mult_columns_per_three()
  returns TABLE(column_1 integer, mult_three integer)
  AS
  $$
    SELECT col_1, col_1*3 FROM udtf_exercise
  $$
  ;
SELECT * FROM TABLE(mult_columns_per_three());

after the function creation and function call we will have a returned value as below:

UDTF Returned Result
UDTF Returned Result

with this operation we could have some extra data that will not be stored in any storage place in our Snowflake account. If we would like to store these values in a new table we could simply create a new table or view using this function returned values.

CREATE TABLE udtf_result_table AS
    (
    SELECT * FROM TABLE(mult_columns_per_three())
    );

SELECT * FROM udtf_result_table;

and now we have our results stored in the table udtf_result_table.

Conclusion

User-Defined Functions and User-Defined Table Functions are features that simplify our code complexity. We could easily create a function for the repeating process and use that for the rest of the code.

There are also Stored Procedures that have specific areas of use and they are very similar to UDFs. You can check the blog in here for the details of Stored Procedures and check here for the detailed difference between UDFs. Thanks for reading.

Credits

https://docs.snowflake.com/

https://medium.com/

Auteur

  • Can Hobek

    Hello, my name is Can Höbek. I am originally from Turkey, and I currently live in Milan. In my second year of high school, I did a year of exchange in Turin to prepare myself for university and improve my Italian language. I am currently in my final year of bachelor degree in Politecnico di Torino in Management Engineering. During my time there, I was involved in various extracurricular activities, such as volunteer organizations that make science laboratories for primary school students with specific topics that point towards the 2030 goals , which helped me to develop my ability to work in a team and developed my soft-skills. From primary school up to last year of high school, for 10 years I went to conservatory school specializing in classic guitar. I always loved to play different instruments even though my professionality is on classic guitar. Another part that creates me is sports; I love all types of sports but I played professionally in some teams as a football, volleyball player. Before my last year of university I had started to think about my interests and did some research on how to improve myself on that. I found myself wanting to improve on the data and programming part that I started to learn in my first years of university. When I was doing these researches, I found Nimbus that offers first a formation on specific topics that was my area of interest and then gives us an opportunity to work on some projects that you have had formation. The most important part for me was to feel myself learning new things in my area of interest and then working on it.

Can Hobek

Hello, my name is Can Höbek. I am originally from Turkey, and I currently live in Milan. In my second year of high school, I did a year of exchange in Turin to prepare myself for university and improve my Italian language. I am currently in my final year of bachelor degree in Politecnico di Torino in Management Engineering. During my time there, I was involved in various extracurricular activities, such as volunteer organizations that make science laboratories for primary school students with specific topics that point towards the 2030 goals , which helped me to develop my ability to work in a team and developed my soft-skills. From primary school up to last year of high school, for 10 years I went to conservatory school specializing in classic guitar. I always loved to play different instruments even though my professionality is on classic guitar. Another part that creates me is sports; I love all types of sports but I played professionally in some teams as a football, volleyball player. Before my last year of university I had started to think about my interests and did some research on how to improve myself on that. I found myself wanting to improve on the data and programming part that I started to learn in my first years of university. When I was doing these researches, I found Nimbus that offers first a formation on specific topics that was my area of interest and then gives us an opportunity to work on some projects that you have had formation. The most important part for me was to feel myself learning new things in my area of interest and then working on it.