Skip to content

GH-36672: [Python][C++] Add support for vector function UDF - #36673

Merged
icexelloss merged 8 commits into
apache:mainfrom
icexelloss:acero-compute-vector-UDF
Aug 8, 2023
Merged

icexelloss merged 8 commits into
apache:mainfrom
icexelloss:acero-compute-vector-UDF

Conversation

@icexelloss

@icexelloss icexelloss commented Jul 13, 2023 •

Copy link
Copy Markdown
Contributor

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.

Comment thread python/pyarrow/src/arrow/python/udf.cc Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orthogonal to this PR, but got rid of some compiler warnings

@icexelloss

Copy link
Copy Markdown
Contributor Author

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 finanlize in the VectorKernel is relevant in vector UDF here because vector UDF shouldn't need to accumulate state.

@icexelloss
icexelloss requested a review from westonpace July 13, 2023 18:54
@icexelloss icexelloss changed the title GH-36672: [Python][C++] Initial implementation vector function UDF GH-36672: [Python][C++] Add support for vector function UDF Jul 13, 2023
@icexelloss

Copy link
Copy Markdown
Contributor Author

@westonpace CI should be passing now - The failed Python MacOS seems unrelated (failed to build flight). Can you take a look?

@icexelloss

Copy link
Copy Markdown
Contributor Author

gentle ping @westonpace

@westonpace westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ...

Comment thread python/pyarrow/_compute.pyx Resolved
Comment thread python/pyarrow/_compute.pyx Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is "grouping semantics"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this.

Comment thread python/pyarrow/_compute.pyx Outdated
Comment on lines 2807 to 2808

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this.

@icexelloss

Copy link
Copy Markdown
Contributor Author

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 ...

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?

@icexelloss
icexelloss force-pushed the acero-compute-vector-UDF branch from a6bda52 to f1fd112 Compare August 8, 2023 12:17
@icexelloss

Copy link
Copy Markdown
Contributor Author
The following tests FAILED:
	 69 - arrow-s3fs-test (Failed)
Errors while running CTest

Unrelated CI error

@icexelloss
icexelloss merged commit b1e85a6 into apache:main Aug 8, 2023
@icexelloss

Copy link
Copy Markdown
Contributor Author

All comments addressed. Merging.

@conbench-apache-arrow

Copy link
Copy Markdown

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.

loicalleyne pushed a commit to loicalleyne/arrow that referenced this pull request Nov 13, 2023
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++][Python] Support vector UDF

2 participants