Skip to content

Commit

Permalink
feat(udf): support batch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li committed Sep 1, 2024
1 parent db70b6a commit ae390cd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
23 changes: 19 additions & 4 deletions python/databend_udf/udf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ class ScalarFunction(UserDefinedFunction):
_io_threads: Optional[int]
_executor: Optional[ThreadPoolExecutor]
_skip_null: bool
_batch_mode: bool

def __init__(
self, func, input_types, result_type, name=None, io_threads=None, skip_null=None
self, func, input_types, result_type, name=None, io_threads=None, skip_null=None, batch_mode=False
):
self._func = func
self._input_schema = pa.schema(
Expand All @@ -78,6 +79,7 @@ def __init__(
func.__name__ if hasattr(func, "__name__") else func.__class__.__name__
)
self._io_threads = io_threads
self._batch_mode = batch_mode
self._executor = (
ThreadPoolExecutor(max_workers=self._io_threads)
if self._io_threads is not None
Expand All @@ -98,7 +100,11 @@ def eval_batch(self, batch: pa.RecordBatch) -> Iterator[pa.RecordBatch]:
_input_process_func(_list_field(field))(array)
for array, field in zip(inputs, self._input_schema)
]
if self._executor is not None:

# evaluate the function for each row
if self._batch_mode:
column = self._func(*inputs)
elif self._executor is not None:
# concurrently evaluate the function for each row
if self._skip_null:
tasks = []
Expand All @@ -113,7 +119,6 @@ def eval_batch(self, batch: pa.RecordBatch) -> Iterator[pa.RecordBatch]:
]
column = [future.result() for future in tasks]
else:
# evaluate the function for each row
if self._skip_null:
column = []
for row in range(batch.num_rows):
Expand All @@ -140,6 +145,7 @@ def udf(
name: Optional[str] = None,
io_threads: Optional[int] = None,
skip_null: Optional[bool] = False,
batch_mode: Optional[bool] = False,
) -> Callable:
"""
Annotation for creating a user-defined scalar function.
Expand All @@ -153,6 +159,7 @@ def udf(
- skip_null: A boolean value specifying whether to skip NULL value. If it is set to True,
NULL values will not be passed to the function,
and the corresponding return value is set to NULL. Default to False.
- batch_mode: A boolean value specifying whether to use batch mode. Default to False.
Example:
```
Expand All @@ -170,6 +177,13 @@ def external_api(x):
response = requests.get(my_endpoint + '?param=' + x)
return response["data"]
```
Batch mode example:
```
@udf(input_types=['INT', 'INT'], result_type='INT', batch_mode=True)
def gcd(x, y):
return [x_i if y_i == 0 else gcd(y_i, x_i % y_i) for x_i, y_i in zip(x, y)]
```
"""

if io_threads is not None and io_threads > 1:
Expand All @@ -180,10 +194,11 @@ def external_api(x):
name,
io_threads=io_threads,
skip_null=skip_null,
batch_mode=batch_mode
)
else:
return lambda f: ScalarFunction(
f, input_types, result_type, name, skip_null=skip_null
f, input_types, result_type, name, skip_null=skip_null, batch_mode=batch_mode
)


Expand Down
15 changes: 15 additions & 0 deletions python/example/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ def gcd(x: int, y: int) -> int:
(x, y) = (y, x % y)
return x

@udf(
name="gcd_batch",
input_types=["INT", "INT"],
result_type="INT",
batch_mode=True,
)
def gcd_batch(x: list[int], y: list[int]) -> list[int]:
def gcd_single(x_i, y_i):
if x_i == None or y_i == None:
return None
while y_i != 0:
(x_i, y_i) = (y_i, x_i % y_i)
return x_i
return [gcd_single(x_i, y_i) for x_i, y_i in zip(x, y)]

@udf(input_types=["VARCHAR", "VARCHAR", "VARCHAR"], result_type="VARCHAR")
def split_and_join(s: str, split_s: str, join_s: str) -> str:
Expand Down Expand Up @@ -303,6 +317,7 @@ def wait_concurrent(x):
udf_server.add_function(binary_reverse)
udf_server.add_function(bool_select)
udf_server.add_function(gcd)
udf_server.add_function(gcd_batch)
udf_server.add_function(split_and_join)
udf_server.add_function(decimal_div)
udf_server.add_function(hex_to_dec)
Expand Down

0 comments on commit ae390cd

Please sign in to comment.