Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
GH-32916: [C++] [Python] User-defined tabular functions#14682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0cb4b5a
ARROW-17676: [C++] [Python] User-defined tabular functions
rtpsw d0cc3f1
lint
rtpsw 2b2986c
add docs
rtpsw 3e8b0ad
lint
rtpsw 8f38e95
fix tabular next-function
rtpsw 345e961
requested changes
rtpsw 2fcc553
more requested fixes
rtpsw 3f35ccd
lint
rtpsw 3c30eee
lint
rtpsw feaa957
requested changes
rtpsw 37451c7
RecordBatchReader API
rtpsw d691c86
Merge branch 'master' into ARROW-17676
rtpsw d0c8f5f
requested fixes
rtpsw 4236e37
fix copy elision
rtpsw 8a4d820
revert to scalar-UDF naming
rtpsw fb7049f
Merge branch 'master' into ARROW-17676
rtpsw 5076b97
fix merge
rtpsw dc61c55
add todo
rtpsw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -36,6 +36,18 @@ import inspect | ||
| import numpy as np | ||
| def _forbid_instantiation(klass, subclasses_instead=True): | ||
| msg = '{} is an abstract class thus cannot be initialized.'.format( | ||
| klass.__name__ | ||
| ) | ||
| if subclasses_instead: | ||
| subclasses = [cls.__name__ for cls in klass.__subclasses__] | ||
| msg += ' Use one of the subclasses instead: {}'.format( | ||
| ', '.join(subclasses) | ||
| ) | ||
| raise TypeError(msg) | ||
| cdef wrap_scalar_function(const shared_ptr[CFunction]& sp_func): | ||
| """ | ||
| Wrap a C++ scalar Function in a ScalarFunction object. | ||
| @@ -2574,7 +2586,7 @@ cdef object box_scalar_udf_context(const CScalarUdfContext& c_context): | ||
| return context | ||
| cdef _scalar_udf_callback(user_function, const CScalarUdfContext& c_context, inputs): | ||
| cdef _udf_callback(user_function, const CScalarUdfContext& c_context, inputs): | ||
| """ | ||
| Helper callback function used to wrap the ScalarUdfContext from Python to C++ | ||
| execution. | ||
| @@ -2591,8 +2603,30 @@ def _get_scalar_udf_context(memory_pool, batch_length): | ||
| return context | ||
| def register_scalar_function(func, function_name, function_doc, in_types, | ||
| out_type): | ||
| ctypedef CStatus (*CRegisterUdf)(PyObject* function, function[CallbackUdf] wrapper, | ||
| const CUdfOptions& options, CFunctionRegistry* registry) | ||
| cdef class RegisterUdf(_Weakrefable): | ||
| cdef CRegisterUdf register_func | ||
| cdef void init(self, const CRegisterUdf register_func): | ||
| self.register_func = register_func | ||
| cdef get_register_scalar_function(): | ||
| cdef RegisterUdf reg = RegisterUdf.__new__(RegisterUdf) | ||
| reg.register_func = RegisterScalarFunction | ||
| return reg | ||
| cdef get_register_tabular_function(): | ||
| cdef RegisterUdf reg = RegisterUdf.__new__(RegisterUdf) | ||
| reg.register_func = RegisterTabularFunction | ||
| return reg | ||
| def register_scalar_function(func, function_name, function_doc, in_types, out_type, | ||
| func_registry=None): | ||
| """ | ||
| Register a user-defined scalar function. | ||
| @@ -2633,6 +2667,8 @@ def register_scalar_function(func, function_name, function_doc, in_types, | ||
| arity. | ||
| out_type : DataType | ||
| Output type of the function. | ||
| func_registry : FunctionRegistry | ||
| Optional function registry to use instead of the default global one. | ||
| Examples | ||
| -------- | ||
| @@ -2662,14 +2698,106 @@ def register_scalar_function(func, function_name, function_doc, in_types, | ||
| 21 | ||
| ] | ||
| """ | ||
| return _register_scalar_like_function(get_register_scalar_function(), | ||
| func, function_name, function_doc, in_types, | ||
| out_type, func_registry) | ||
| def register_tabular_function(func, function_name, function_doc, in_types, out_type, | ||
rtpsw marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| func_registry=None): | ||
| """ | ||
| Register a user-defined tabular function. | ||
| A tabular function is one accepting a context argument of type | ||
| ScalarUdfContext and returning a generator of struct arrays. | ||
| The in_types argument must be empty and the out_type argument | ||
| specifies a schema. Each struct array must have field types | ||
| correspoding to the schema. | ||
| Parameters | ||
| ---------- | ||
| func : callable | ||
| A callable implementing the user-defined function. | ||
| The only argument is the context argument of type | ||
| ScalarUdfContext. It must return a callable that | ||
| returns on each invocation a StructArray matching | ||
| the out_type, where an empty array indicates end. | ||
| function_name : str | ||
| Name of the function. This name must be globally unique. | ||
| function_doc : dict | ||
| A dictionary object with keys "summary" (str), | ||
| and "description" (str). | ||
| in_types : Dict[str, DataType] | ||
| Must be an empty dictionary (reserved for future use). | ||
| out_type : Union[Schema, DataType] | ||
| Schema of the function's output, or a corresponding flat struct type. | ||
| func_registry : FunctionRegistry | ||
| Optional function registry to use instead of the default global one. | ||
| """ | ||
| cdef: | ||
| shared_ptr[CSchema] c_schema | ||
| shared_ptr[CDataType] c_type | ||
| if isinstance(out_type, Schema): | ||
| c_schema = pyarrow_unwrap_schema(out_type) | ||
| with nogil: | ||
| c_type = <shared_ptr[CDataType]>make_shared[CStructType](deref(c_schema).fields()) | ||
| out_type = pyarrow_wrap_data_type(c_type) | ||
| return _register_scalar_like_function(get_register_tabular_function(), | ||
| func, function_name, function_doc, in_types, | ||
| out_type, func_registry) | ||
| def _register_scalar_like_function(register_func, func, function_name, function_doc, in_types, | ||
| out_type, func_registry=None): | ||
| """ | ||
| Register a user-defined scalar-like function. | ||
| A scalar-like function is a callable accepting a first | ||
| context argument of type ScalarUdfContext as well as | ||
| possibly additional Arrow arguments, and returning a | ||
| an Arrow result appropriate for the kind of function. | ||
| A scalar function and a tabular function are examples | ||
| for scalar-like functions. | ||
| This function is normally not called directly but via | ||
| register_scalar_function or register_tabular_function. | ||
rtpsw marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Parameters | ||
| ---------- | ||
| register_func: object | ||
| An object holding a CRegisterUdf in a "register_func" attribute. Use | ||
| get_register_scalar_function() for a scalar function and | ||
| get_register_tabular_function() for a tabular function. | ||
| func : callable | ||
| A callable implementing the user-defined function. | ||
| See register_scalar_function and | ||
| register_tabular_function for details. | ||
| function_name : str | ||
| Name of the function. This name must be globally unique. | ||
| function_doc : dict | ||
| A dictionary object with keys "summary" (str), | ||
| and "description" (str). | ||
| in_types : Dict[str, DataType] | ||
| A dictionary mapping function argument names to | ||
| their respective DataType. | ||
| See register_scalar_function and | ||
| register_tabular_function for details. | ||
| out_type : DataType | ||
| Output type of the function. | ||
| func_registry : FunctionRegistry | ||
| Optional function registry to use instead of the default global one. | ||
| """ | ||
| cdef: | ||
| CRegisterUdf c_register_func | ||
| c_string c_func_name | ||
| CArity c_arity | ||
| CFunctionDoc c_func_doc | ||
| vector[shared_ptr[CDataType]] c_in_types | ||
| PyObject* c_function | ||
| shared_ptr[CDataType] c_out_type | ||
| CScalarUdfOptions c_options | ||
| CUdfOptions c_options | ||
| CFunctionRegistry* c_func_registry | ||
| if callable(func): | ||
| c_function = <PyObject*>func | ||
| @@ -2711,5 +2839,51 @@ def register_scalar_function(func, function_name, function_doc, in_types, | ||
| c_options.input_types = c_in_types | ||
| c_options.output_type = c_out_type | ||
| check_status(RegisterScalarFunction(c_function, | ||
| <function[CallbackUdf]> &_scalar_udf_callback, c_options)) | ||
| if func_registry is None: | ||
| c_func_registry = NULL | ||
| else: | ||
| c_func_registry = (<FunctionRegistry>func_registry).registry | ||
| c_register_func = (<RegisterUdf>register_func).register_func | ||
| check_status(c_register_func(c_function, | ||
| <function[CallbackUdf]> &_udf_callback, | ||
| c_options, c_func_registry)) | ||
| def call_tabular_function(function_name, args=None, func_registry=None): | ||
| """ | ||
| Get a record batch iterator from a tabular function. | ||
| Parameters | ||
| ---------- | ||
| function_name : str | ||
| Name of the function. | ||
| args : iterable | ||
| The arguments to pass to the function. Accepted types depend | ||
| on the specific function. Currently, only an empty args is supported. | ||
| func_registry : FunctionRegistry | ||
| Optional function registry to use instead of the default global one. | ||
| """ | ||
| cdef: | ||
| c_string c_func_name | ||
| vector[CDatum] c_args | ||
| CFunctionRegistry* c_func_registry | ||
| shared_ptr[CRecordBatchReader] c_reader | ||
| RecordBatchReader reader | ||
| c_func_name = tobytes(function_name) | ||
| if func_registry is None: | ||
| c_func_registry = NULL | ||
| else: | ||
| c_func_registry = (<FunctionRegistry>func_registry).registry | ||
| if args is None: | ||
| args = [] | ||
| _pack_compute_args(args, &c_args) | ||
| with nogil: | ||
| c_reader = GetResultValue(CallTabularFunction( | ||
| c_func_name, c_args, c_func_registry)) | ||
| reader = RecordBatchReader.__new__(RecordBatchReader) | ||
| reader.reader = c_reader | ||
| return RecordBatchReader.from_batches(pyarrow_wrap_schema(deref(c_reader).schema()), reader) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
appreciate your effort to make this more generic, but with the other UDFs, we have a complex strucutre for the callbacks. In that case this generalization won't be that useful as far as I feel. For the moment, shall we keep them separate? There would be a sort of
DRYviolated here, but once we generalized this API after our experimental version, we should be able to streamline these.cc @westonpace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean revert to the original
register_scalar_udfcode, then duplicate it and adapt toregister_tabular_udf? The disadvantage of code repetition is clear, but what is the advantage? The remaining PR would still need to resolve the conflict, since you say the callback structure changed in that PR, only this time the conflict would not be caught by source control tools, because theregister_scalar_udfcode would not have changed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maybe I didn't clearly state it in the description, do we need this interface
RegisterUdf? Is it a must? I was referring to this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proposed common design for scalar and tabular function registration requires a parameter to distinguish between the two. I considered 3 options to do so:
is_tabular. This seemed the least elegant to me, and is definitely the least extensible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My preference would be to keep this and, if we have something better when we add aggregate udfs, we can switch to that then. That being said, I'm not 100% certain I follow the arguments here.