You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add functionality for retrieving callable definitions and converting them to callables (unitycatalog#912)
**PR Checklist**
- [X] A description of the changes is added to the description of this
PR.
- [ ] If there is a related issue, make sure it is linked to this PR.
- [ ] If you've fixed a bug or added code that should be tested, add
tests!
- [ ] If you've added or modified a feature, documentation in `docs` is
updated
**Description of changes**
Extension of unitycatalog#911 , bringing a unified functionality across both
clients. Adds a utility for converting the string representation to an
actual callable for local execution.
---------
Signed-off-by: Ben Wilson <[email protected]>
The `get_function_source` API is used to retrieve a recreated python callable definition (as a string) from a registered Unity Catalog Python function.
304
+
In order to use this API, the function that you are fetching **must be** an `EXTERNAL` (python function) type function. When called, the function's metadata will
305
+
be retrieved and the structure of the original callable will be rebuilt and returned as a string.
The returned value from the `get_function_source` API will be the same as the original input with a few caveats:
333
+
334
+
- `tuple` types will be cast to `list` due to the inability to express a Python `tuple` within Unity Catalog
335
+
- The docstring of the original functionwill be stripped out. Unity Catalog persists the docstring information in the logged functionand it is available in the return of the `get_function` API call if needed.
336
+
- Collection types for open source Unity Catalog will only capture the outer type (i.e., `list` or `dict`) as inner collection type metadata is not preserved
337
+
within the `FunctionInfo` object. In Databricks, full typing is supported for collecitons.
338
+
339
+
The result of calling the `get_function_source` API on the `sample_python_func` registered functionwill be (when printed):
340
+
341
+
```text
342
+
def sample_python_func(a: int, b: int) -> int:
343
+
"""
344
+
Returns the sum of a and b.
345
+
346
+
Args:
347
+
a: an int
348
+
b: another int
349
+
350
+
Returns:
351
+
int
352
+
"""
353
+
return a + b
354
+
```
355
+
356
+
Note: If you want to convert the extracted string back into an actual Python callable, you can use the utility `load_function_from_string`in the module `unitycatalog.ai.core.utils.execution_utils`. See below for further details on this API.
357
+
358
+
This API is useful for extracting already-registered functions that will be used as additional in-line calls within another functionthrough the use of the `create_wrapped_python_function` API, saving the effort required to either hand-craft a functiondefinition or having to track down where the original implementation of a logged functionwas defined.
359
+
301
360
#### List UC functions
302
361
303
362
To get a list of functions stored in a catalog and schema, you can use list API with wildcards to do so.
@@ -315,6 +374,79 @@ result = client.execute_function(full_func_name, {"s": "some_string"})
315
374
assert result.value == "some_string"
316
375
```
317
376
377
+
#### Execute a UC Python function locally
378
+
379
+
A utility `load_function_from_string` is available in`unitycatalog.ai.core.utils.execution_utils.py`. This utility allows you to couple the functionality
380
+
in the `get_function_source` API to create a locally-available python callable that can be direclty accessed, precisely as if it were originally defined
381
+
within your current REPL.
382
+
383
+
```python
384
+
from unitycatalog.ai.core.utils.execution_utils import load_function_from_string
385
+
386
+
func_str = """
387
+
def multiply_numbers(a: int, b: int) -> int:
388
+
\"\"\"
389
+
Multiplies two numbers.
390
+
391
+
Args:
392
+
a: first number.
393
+
b: second number.
394
+
395
+
Returns:
396
+
int
397
+
\"\"\"
398
+
return a * b
399
+
"""
400
+
401
+
# If specifying `register_global=False`, the original function name cannot be called and must be used
0 commit comments