0
0
Snowflakecloud~10 mins

User-defined functions with Snowpark in Snowflake - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - User-defined functions with Snowpark
Define function in Snowpark
Register function in Snowflake
Call function in SQL or Snowpark
Function executes with input
Returns output to caller
This flow shows how you create a function in Snowpark, register it in Snowflake, then call it to get results.
Execution Sample
Snowflake
from snowflake.snowpark import Session
from snowflake.snowpark.functions import udf

@udf
def add_one(x: int) -> int:
    return x + 1

session.udf.register(add_one, name='add_one')
Defines a simple function that adds 1 to a number and registers it as a Snowflake UDF.
Process Table
StepActionInputFunction ExecutionOutput
1Define function add_oneN/AFunction code storedN/A
2Register function in SnowflakeN/AFunction linked to SnowflakeN/A
3Call add_one with input 55Compute 5 + 16
4Call add_one with input 1010Compute 10 + 111
5Call add_one with input -3-3Compute -3 + 1-2
6Call add_one with input 00Compute 0 + 11
7No more callsN/AN/AExecution ends
💡 All function calls completed, no more inputs to process.
Status Tracker
VariableStartAfter Call 1After Call 2After Call 3After Call 4Final
input_valueN/A510-30N/A
output_valueN/A611-21N/A
Key Moments - 3 Insights
Why do we register the function after defining it?
Registering links the Python function to Snowflake so it can be called inside SQL or Snowpark, as shown in step 2 of the execution_table.
What happens if we call the function with a negative number?
The function still adds 1 correctly, as seen in step 5 where input -3 returns output -2.
Can the function be called multiple times with different inputs?
Yes, each call runs the function with the new input and returns the output, demonstrated in steps 3 to 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when the input is 10?
A9
B10
C11
D12
💡 Hint
Check row 4 in the execution_table where input is 10.
At which step does the function get registered in Snowflake?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the Action column in the execution_table for registration.
If we call add_one with input 0, what will be the output?
A0
B1
C-1
D2
💡 Hint
Refer to step 6 in the execution_table for input 0.
Concept Snapshot
User-defined functions (UDFs) in Snowpark let you write Python code to run inside Snowflake.
Define the function in Python, then register it with session.udf.register.
Call the function in SQL or Snowpark with inputs.
The function runs inside Snowflake and returns outputs.
This allows custom logic close to your data.
Full Transcript
This visual execution shows how to create and use user-defined functions with Snowpark in Snowflake. First, you define a Python function that does some work, like adding one to a number. Then you register this function with Snowflake so it can be called inside SQL or Snowpark code. When you call the function with different inputs, Snowflake runs the function code and returns the results. The execution table traces each step: defining, registering, calling with inputs, and outputs returned. The variable tracker shows how inputs and outputs change with each call. Key moments clarify why registration is needed, how negative inputs work, and that multiple calls are possible. The quiz tests understanding of outputs and steps. This helps beginners see how Snowpark UDFs work step-by-step.