Repository navigation
GH-36672: [Python][C++] Add support for vector function UDF - #36673
Conversation
There was a problem hiding this comment.
Orthogonal to this PR, but got rid of some compiler warnings
|
cc @westonpace would appreciate if you can take a look. The implementation ended up being pretty simple because most of the logic can be shared with Scalar functions. (Both takes a input batch and invokes the function, only the output shapes are different). I don't think the |
|
@westonpace CI should be passing now - The failed Python MacOS seems unrelated (failed to build flight). Can you take a look? |
|
gentle ping @westonpace |
westonpace
left a comment
There was a problem hiding this comment.
From a code perspective I don't see much to worry about here. Conceptually though I think we should think carefully about how we explain these concepts to users. I left a few comments about the wording. I wonder if we might also want something in the docs (can be a future PR).
Basically, as a user, I think I will encounter this code and my first question will be "why are there two different kinds of UDFs?" and then "which one should I use?"
Also, eventually I think there will be even more than two kinds of UDFs. So it would be nice, in the docs somewhere, to be able to say something like...
We have several different categories of functions. For example, scalar functions,
aggregate functions, window functions, and vector functions. These categories
describe how a function should behave and control where a function can be used.
The actual mechanics of a UDF are very similar regardless of the category of the
function.
Scalar Functions - These functions return 1 row for each input row. In addition,
the calculation for each row should not depend on any other rows (e.g. function
is stateless). As a result, the output for a row should be the same no matter
what order the rows arrive. Most arithmetic functions (add, power, log) can be
implemented as scalar functions. These functions can be used in project, filter,
and batch-in/batch-out relations.
Window Functions - These functions return 1 row for each input row. However,
the output for a row is allowed to depend on other rows. These functions rely
on a specific row order and are often executed in groups. For example, rank,
first, and cumulative_sum are all window functions. These functions can be
used in window and batch-in/batch-out relations.
Vector Functions - These functions are not required to return 1 output row for
each input row. For example, a "drop_nulls" function might return fewer rows
than the input. A "list_flatten" function might return more rows than the input.
These functions can be used in batch-in/batch-out relations.
Scalar Aggregate Functions ...
Hash Aggregate Functions ...
There was a problem hiding this comment.
What is "grouping semantics"?
There was a problem hiding this comment.
"grouping semantics" is another/short way of saying "output of a row depend on other rows" (as supposed to scalar functions). I can just remove "grouping semantics" and use "output of a row depend on other rows".
There was a problem hiding this comment.
I'm not sure if we want rank to be listed as the prototypical "vector function" since it is often thought of as the prototypical "window function".
list_flatten is maybe a better example of a function that is neither a scalar function or a window function.
I think I understand where this is coming from because I believe you are using the vector function type to do window function operations. However, I think we want to leave the door open for a future where all three exist. In other words:
ProjectNode - Can only run "scalar" functions
AggregateNode - Can only run "scalar aggregate" functions
GroupByNode - Can only run "hash aggregate" functions
WindowNode (does not exist) - Can only run "window" functions
NonDecomposableAggregateNode (e.g. batch in / batch out) - Can run any kind of function
So, today, I think you are working with some kind of batch-in/batch-out node that can run any function. As a result, you can use this to run window functions. So you need to add support for registering vector functions because you want to make sure these functions don't run in project/aggregate/groupby nodes.
This is all fine. I'm just wondering if we can describe vector functions not as "a function whose output depends on other rows" (since this is the definition of window functions) but as "a function whose output may depend on other rows and whose output length does not need to be the same as the input length".
I don't know if I'm explaining myself very well though.
Another option might be to call these "window functions" with the caveat that we don't actually have a window node yet. Then, if you have some kind of batch-in/batch-out node you can still run window functions with it (since a batch-in/batch-out node can run pretty much anything).
There was a problem hiding this comment.
I'm just wondering if we can describe vector functions not as "a function whose output depends on other rows" (since this is the definition of window functions) but as "a function whose output may depend on other rows and whose output length does not need to be the same as the input length".
I think I understand what you are saying. N -> N with grouping semantics has been traditional called "window functions" and I am fine with reserving the word "window function" so I agree we can make vector function description more specific. I will make the change to the doc.
I think this is useful - where do you suggest I put this? Also do you prefer to combine the doc for "different function categories" in this change or separately? |
1169dd2 to
a6bda52
Compare
a6bda52 to
f1fd112
Compare
Unrelated CI error |
|
All comments addressed. Merging. |
|
After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit b1e85a6. There were no benchmark performance regressions. 🎉 The full Conbench report has more details. It also includes information about possible false positives for unstable benchmarks that are known to sometimes produce them. |
…ache#36673) ### Rationale for this change In Arrow compute, there are four main types of functions: Scalar, Vector, ScalarAggregate and HashAggregate. Some of the previous work added support for Scalar, ScalarAggregate(apache#35515) and HashAggregate(apache#36252). I think it makes sense to add support for vector function as well to complete all non-decomposable UDF kernel support. Internally, we plan to extend Acero to implement a "SegmentVectorNode" which would use this API to invoke vector on a segment by segment basis, which will allow to use constant memory to compute things like "rank the value across all rows per segment using a python UDF". ### What changes are included in this PR? The change includes is very similar to the support for aggregate function, which includes code to register the vector UDF, and a kernel that invokes the vector UDF on given inputs. ### Are these changes tested? Yes. Added new test. ### Are there any user-facing changes? Yes. This adds an user-facing API to register the vector function. * Closes: apache#36672 Authored-by: Li Jin <[email protected]> Signed-off-by: Li Jin <[email protected]>
Rationale for this change
In Arrow compute, there are four main types of functions: Scalar, Vector, ScalarAggregate and HashAggregate.
Some of the previous work added support for Scalar, ScalarAggregate(#35515) and HashAggregate(#36252). I think it makes sense to add support for vector function as well to complete all non-decomposable UDF kernel support.
Internally, we plan to extend Acero to implement a "SegmentVectorNode" which would use this API to invoke vector on a segment by segment basis, which will allow to use constant memory to compute things like "rank the value across all rows per segment using a python UDF".
What changes are included in this PR?
The change includes is very similar to the support for aggregate function, which includes code to register the vector UDF, and a kernel that invokes the vector UDF on given inputs.
Are these changes tested?
Yes. Added new test.
Are there any user-facing changes?
Yes. This adds an user-facing API to register the vector function.