0
0
Snowflakecloud~7 mins

User-defined functions with Snowpark in Snowflake - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to create your own small programs inside your database to reuse logic easily. User-defined functions with Snowpark let you write these programs in Python and run them close to your data in Snowflake.
When you want to reuse a calculation or transformation on your data multiple times without rewriting it.
When you need to run custom Python code on your data inside Snowflake for better performance.
When you want to keep your data logic centralized and easy to update.
When you want to avoid moving large data sets outside Snowflake for processing.
When you want to combine SQL and Python logic seamlessly in your queries.
Commands
Connect to your Snowflake account using snowsql with your account, user, role, warehouse, database, and schema specified.
Terminal
snowsql -a myaccount -u myuser -r myrole -w mywarehouse -d mydatabase -s public
Expected OutputExpected
Welcome to Snowflake You are now connected to Snowflake as user 'myuser'.
-a - Specifies the Snowflake account name
-u - Specifies the username
-r - Specifies the role to use
-w - Specifies the warehouse to use
Create a user-defined function named add_one that takes an integer and returns the integer plus one using Python code inside Snowflake.
Terminal
CREATE OR REPLACE FUNCTION add_one(x INT) RETURNS INT LANGUAGE PYTHON RUNTIME_VERSION = '3.8' HANDLER = 'add_one' AS $$ def add_one(x):
    return x + 1
$$;
Expected OutputExpected
Function ADD_ONE successfully created.
OR REPLACE - Replaces the function if it already exists
LANGUAGE PYTHON - Specifies the function is written in Python
RUNTIME_VERSION = '3.8' - Specifies the Python version to use
Run the user-defined function add_one with input 5 to test that it returns 6.
Terminal
SELECT add_one(5);
Expected OutputExpected
ADD_ONE(5) 6
Check that the user-defined function add_one exists in your current schema.
Terminal
SHOW USER FUNCTIONS LIKE 'ADD_ONE';
Expected OutputExpected
created_on name argument_signature return_type language owner 2024-06-01 12:00:00.000 +0000 ADD_ONE (X INT) INT PYTHON MYUSER
Key Concept

If you remember nothing else from this pattern, remember: user-defined functions with Snowpark let you run your own Python code inside Snowflake close to your data for reusable and efficient processing.

Common Mistakes
Forgetting to specify the HANDLER function name inside the Python code.
Snowflake needs to know which Python function to run; without HANDLER, it cannot execute your code.
Always include HANDLER = 'function_name' matching the Python function defined inside the $$ code block.
Using unsupported Python versions or syntax not compatible with Snowflake's Python runtime.
Snowflake only supports specific Python versions and features; unsupported code will cause errors.
Use supported Python versions like 3.8 and test your code locally before deploying.
Not testing the function after creation with a SELECT statement.
You might miss syntax or runtime errors until you try to use the function.
Always run a simple SELECT with your function to verify it works as expected.
Summary
Connect to Snowflake using snowsql with your account details.
Create a Python user-defined function with CREATE FUNCTION specifying language, handler, and runtime.
Test the function by running a SELECT query calling it with sample input.
Verify the function exists using SHOW USER FUNCTIONS.