0
0
Snowflakecloud~30 mins

User-defined functions with Snowpark in Snowflake - Mini Project: Build & Apply

Choose your learning style9 modes available
User-defined functions with Snowpark
📖 Scenario: You are working with Snowflake's Snowpark to create a user-defined function (UDF) that processes data inside the database. This helps you run custom logic close to your data for faster and scalable processing.
🎯 Goal: Build a simple user-defined function (UDF) using Snowpark that takes a string input and returns the string in uppercase.
📋 What You'll Learn
Create a Snowpark session variable called session.
Define a Python function called to_upper that takes one string argument.
Register the to_upper function as a UDF named uppercase_udf in Snowflake.
Use the UDF in a Snowpark DataFrame to transform a column of strings to uppercase.
💡 Why This Matters
🌍 Real World
User-defined functions let you run custom code close to your data in Snowflake, improving performance and enabling complex logic inside the database.
💼 Career
Knowing how to create and use UDFs with Snowpark is valuable for data engineers and analysts who want to extend Snowflake's capabilities with custom processing.
Progress0 / 4 steps
1
Create a Snowpark session
Create a Snowpark session variable called session using Session.builder.configs() with an empty dictionary {} and then call .create().
Snowflake
Need a hint?

Use Session.builder.configs({}).create() to create the session.

2
Define the Python function to convert strings to uppercase
Define a Python function called to_upper that takes one argument input_str and returns input_str.upper().
Snowflake
Need a hint?

Use the upper() method on the string argument.

3
Register the Python function as a Snowflake UDF
Register the function to_upper as a Snowflake UDF named uppercase_udf using session.udf.register() with to_upper and name='uppercase_udf'.
Snowflake
Need a hint?

Use session.udf.register() with the function and the name.

4
Use the UDF in a Snowpark DataFrame
Create a Snowpark DataFrame called df from a list of strings [('hello',), ('world',)] with column name 'word'. Then create a new DataFrame called df_upper by selecting the column 'word' and applying the UDF uppercase_udf on it as uppercase_word.
Snowflake
Need a hint?

Use session.create_dataframe() with the list and schema, then use select() with the UDF and alias().