User-Defined Functions#
User-Defined Functions (UDFs) are a mechanism to run Python code on the data that lives in a DataFrame. A UDF can be used just like Expressions, allowing users to express computation that should be executed by Daft lazily.
To write a UDF, you should use the @udf
decorator, which can decorate either a Python function or a Python class, producing a UDF.
Learn more about UDFs in Daft User Guide.
Creating UDFs#
udf #
udf(
*,
return_dtype: DataTypeLike,
num_cpus: float | None = None,
num_gpus: float | None = None,
memory_bytes: int | None = None,
batch_size: int | None = None,
concurrency: int | None = None,
) -> Callable[[UserDefinedPyFuncLike], UDF]
@udf
Decorator to convert a Python function/class into a UDF
.
UDFs allow users to run arbitrary Python code on the outputs of Expressions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
return_dtype | DataType | Returned type of the UDF | required |
num_cpus | float | None | Number of CPUs to allocate each running instance of your UDF. Note that this is purely used for placement (e.g. if your machine has 8 CPUs and you specify num_cpus=4, then Daft can run at most 2 instances of your UDF at a time). The default | None |
num_gpus | float | None | Number of GPUs to allocate each running instance of your UDF. This is used for placement and also for allocating the appropriate GPU to each UDF using | None |
memory_bytes | int | None | Amount of memory to allocate each running instance of your UDF in bytes. If your UDF is experiencing out-of-memory errors, this parameter can help hint Daft that each UDF requires a certain amount of heap memory for execution. | None |
batch_size | int | None | Enables batching of the input into batches of at most this size. Results between batches are concatenated. | None |
concurrency | int | None | Spin up | None |
Returns:
Type | Description |
---|---|
Callable[[UserDefinedPyFuncLike], UDF] | Callable[[UserDefinedPyFuncLike], UDF]: UDF decorator - converts a user-provided Python function as a UDF that can be called on Expressions |
Note
In most cases, UDFs will be slower than a native kernel/expression because of the required Rust and Python overheads. If your computation can be expressed using Daft expressions, you should do so instead of writing a UDF. If your UDF expresses a common use-case that isn't already covered by Daft, you should file a ticket or contribute this functionality back to Daft as a kernel!
Examples:
In the example below, we create a UDF that:
- Receives data under the argument name
x
- Iterates over the
x
Daft Series - Adds a Python constant value
c
to every element inx
- Returns a new list of Python values which will be coerced to the specified return type:
return_dtype=DataType.int64()
. - We can call our UDF on a dataframe using any of the dataframe projection operations (df.with_column(), df.select(), etc.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Resource Requests:
You can also hint Daft about the resources that your UDF will require to run. For example, the following UDF requires 2 CPUs to run. On a machine/cluster with 8 CPUs, Daft will be able to run up to 4 instances of this UDF at once!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Your UDF's resources can also be overridden before you call it like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Source code in daft/udf.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
|
Using UDFs#
UDF #
UDF(
inner: UserDefinedPyFuncLike,
name: str,
return_dtype: DataType,
init_args: InitArgsType = None,
concurrency: int | None = None,
resource_request: ResourceRequest | None = None,
batch_size: int | None = None,
)
A class produced by applying the @daft.udf
decorator over a Python function or class.
Calling this class produces a daft.Expression
that can be used in a DataFrame function.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Methods:
Name | Description |
---|---|
__call__ | |
override_options | Replace the resource requests for running each instance of your UDF. |
with_concurrency | Override the concurrency of this UDF, which tells Daft how many instances of your UDF to run concurrently. |
with_init_args | Replace initialization arguments for a class UDF when calling |
Attributes:
Name | Type | Description |
---|---|---|
batch_size | int | None | |
concurrency | int | None | |
init_args | InitArgsType | |
inner | UserDefinedPyFuncLike | |
name | str | |
resource_request | ResourceRequest | None | |
return_dtype | DataType | |
__call__ #
__call__(*args: Any, **kwargs: Any) -> Expression
Source code in daft/udf.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
override_options #
override_options(
*,
num_cpus: float | None = _UnsetMarker,
num_gpus: float | None = _UnsetMarker,
memory_bytes: int | None = _UnsetMarker,
batch_size: int | None = _UnsetMarker,
) -> UDF
Replace the resource requests for running each instance of your UDF.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_cpus | float | None | Number of CPUs to allocate each running instance of your UDF. Note that this is purely used for placement (e.g. if your machine has 8 CPUs and you specify num_cpus=4, then Daft can run at most 2 instances of your UDF at a time). | _UnsetMarker |
num_gpus | float | None | Number of GPUs to allocate each running instance of your UDF. This is used for placement and also for allocating the appropriate GPU to each UDF using | _UnsetMarker |
memory_bytes | int | None | Amount of memory to allocate each running instance of your UDF in bytes. If your UDF is experiencing out-of-memory errors, this parameter can help hint Daft that each UDF requires a certain amount of heap memory for execution. | _UnsetMarker |
batch_size | int | None | Enables batching of the input into batches of at most this size. Results between batches are concatenated. | _UnsetMarker |
Examples:
For instance, if your UDF requires 4 CPUs to run, you can configure it like so:
1 2 3 4 5 6 7 8 9 |
|
Source code in daft/udf.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
|
with_concurrency #
with_concurrency(concurrency: int) -> UDF
Override the concurrency of this UDF, which tells Daft how many instances of your UDF to run concurrently.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Source code in daft/udf.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
with_init_args #
with_init_args(*args: Any, **kwargs: Any) -> UDF
Replace initialization arguments for a class UDF when calling __init__
at runtime on each instance of the UDF.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Source code in daft/udf.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|