User-defined functions
Databricks Data Engineer Professional · Lesson 2 of 21 · 10:48
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Data Engineer Professional exam. In this lesson: when a user-defined function is worth it compared with built-in Spark functions, Python UDFs and pandas UDFs (batches of rows with Apache Arrow), Python UDTFs that return several rows for each input row, Unity Catalog functions in SQL and Python, and how to choose between them. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Logic Spark does not have
Quayside Eats needs a custom delivery fee rule, a distance between two GPS points, and a fraud score from a trained model, applied to millions of orders. Built-in Spark functions cannot express all of these, so the team writes user defined functions, or UDFs. This lesson covers when to write a UDF, Python and pandas UDFs, functions in Unity Catalog, and how to choose between them.
When to write a UDF
A UDF extends Spark with your own logic, like a calculation, a validation rule, or a call to a Python library. Built-in Spark functions are optimized for distributed processing, so they perform better at scale. Databricks recommends built-in functions for large datasets and for workloads that run regularly or continuously, like ETL jobs and streaming. UDFs fit logic that is hard to express with built-in functions, and ad hoc work on small to medium datasets. So before you write a UDF, check the built-in functions: string, date, JSON, and hashing functions already cover most needs.
Python and pandas UDFs
A Python UDF, declared with the udf decorator and a return type, processes one row at a time, and each value travels between the Spark engine and Python. Without a return type, it returns a string, and spark.udf.register makes it callable from SQL in the same session. A pandas UDF, declared with the pandas_udf decorator, is vectorized: Apache Arrow moves whole batches of rows, and your function works on pandas Series, which can be up to a hundred times faster. A Series to Series UDF transforms columns, and a Series to scalar UDF aggregates a group into one value. An Iterator of Series UDF runs an expensive setup only once, like loading a model, and then processes every batch. And a user defined table function, or UDTF, returns several rows for each input row.
Unity Catalog functions
A session UDF disappears when the session ends, and only that notebook or job can call it. A Unity Catalog function is created once with create function and a three level name, in SQL or in Python, and it is shared and governed like a table. Callers need the execute privilege on the function, and use catalog and use schema on its parents. A SQL function returns a SQL expression, so the engine can optimize it like built-in SQL. A Python function in Unity Catalog runs in an isolated environment, can list its own dependencies in an environment clause, and can process batches with the pandas parameter style and a handler.
How to choose
To choose, start with the constraint. If built-in functions can do it, use them. If the logic fits in a SQL expression, and analysts must reuse it from SQL warehouses and dashboards, create a SQL function in Unity Catalog. If it needs Python on large data, a pandas UDF beats a Python UDF, and a Python function in Unity Catalog adds governance and sharing. Keep a session UDF for logic that only one notebook or job needs. And Python functions in Unity Catalog need a pro or serverless SQL warehouse, not a classic one.
Recap
Let's recap. Built-in Spark functions come first, since they are optimized for large data, ETL, and streaming. A Python UDF works one row at a time, while a pandas UDF is vectorized with Arrow, and can be much faster. Series to Series transforms, Series to scalar aggregates, and Iterator of Series runs an expensive setup once. Unity Catalog functions, in SQL or Python, are created once, governed with the execute privilege, and shared across warehouses, notebooks, and jobs. And session UDFs only live in the notebook or job that registers them.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A Python UDF trims the spaces from restaurant names and turns them to lower case, in a streaming job that processes millions of rows each hour. The job is slow. What should the data engineer do?
- Rewrite it as a pandas UDF
- Register it in Unity Catalog
- Use the built-in trim and lower functions
- Turn on high memory for the job
Show the answer
Use the built-in trim and lower functions
Trim and lower are built-in functions, which Spark optimizes, so no UDF is needed at all. “Rewrite it as a pandas UDF” would be faster than a Python UDF, but still slower than built-in functions. “Register it in Unity Catalog” would govern the function, without making it any faster. And “Turn on high memory for the job” would add memory, which is not what slows down a UDF that works one row at a time.
A UDF must score each order with a fraud model that takes ten seconds to load. The team wants to load the model once, then score the orders batch by batch. Which kind of UDF fits best?
- An Iterator of Series pandas UDF
- A SQL function in Unity Catalog
- A Series to scalar pandas UDF
- A user-defined table function
Show the answer
An Iterator of Series pandas UDF
An Iterator of Series pandas UDF loads the model once, and then scores every batch of orders. “A SQL function in Unity Catalog” can only return a SQL expression, and cannot load a Python model. “A Series to scalar pandas UDF” would aggregate a group into one value, instead of scoring each order. And “A user-defined table function” would return several rows for each input row, which is not needed here.
Analysts must compute the same delivery fee from a SQL warehouse, in dashboards, and in several jobs, and the rule is a simple formula on two columns. What should the data engineer create?
- A Python UDF registered in each notebook
- A pandas UDF in a shared notebook
- A view over the orders table
- A SQL function in Unity Catalog
Show the answer
A SQL function in Unity Catalog
A SQL function in Unity Catalog is defined once, governed with the execute privilege, and callable from warehouses, dashboards, and jobs. “A Python UDF registered in each notebook” would only exist in each session, and would be copied everywhere. “A pandas UDF in a shared notebook” cannot be called from a SQL warehouse, and is still a session function. And “A view over the orders table” would only compute the fee for one table, and could not be reused in other queries.