Skip to content

A scenario for a data engineer · Junior and up

The cleaning step takes most of the run

Tensor · Data science
Thursday, 14:00

A Databricks job spends forty minutes of every hour in one step: a Python function, run row by row as a Spark UDF, that trims names and parses dates.

What do you do?

Tap the one you would do. The answer opens below.

Show the best call

Replace the function with built-in functions such as trim and to_date.

The work runs inside the engine, with no trip to Python for each row. The step drops to a few minutes.

Why

Built-in functions run inside the query engine and are optimized with the rest of the query. A Python UDF sends rows to a Python process and back. Use a UDF only when no built-in function does the job, and prefer a pandas UDF then.

Read it in the Databricks docs (opens in a new tab)

The other two

Works, at a price. Rewrite it as a pandas UDF, which handles rows in batches.
Several times faster than row by row. Still slower than built-in functions, and still code you maintain.
A poor call. Add workers until the step is fast enough.
It gets faster, and each row costs as much as before. You are paying more machines to do slow work.

The words in this scenario

UDF.
A user-defined function: your own code, called from a query for the rows it works on. In the docs (opens in a new tab)

Go deeper on brickster.ai

More scenarios

Had one like this at work? Send it to us: the best tickets come from real incidents.

Checked against the Databricks documentation on . brickster.ai is independent and is not affiliated with Databricks.