Skip to content

GH-35515: [C++][Python] Add non decomposable aggregation UDF - #35514

Merged
icexelloss merged 19 commits into
apache:mainfrom
icexelloss:acero-group-agg-UDF-2
Jun 8, 2023
Merged

icexelloss merged 19 commits into
apache:mainfrom
icexelloss:acero-group-agg-UDF-2

Conversation

@icexelloss

@icexelloss icexelloss commented May 9, 2023 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Non decomposable aggregation is aggregation that cannot be split into consume/merge/finalize. This is often when the logic rewritten with external python libraries (numpy, pandas, statmodels, etc) and those either cannot be decomposed or not worthy the effect (these are often one-off function instead of reusable one). This PR implements the support for non decomposable aggregation UDFs.

The major issue with non decomposable UDF is that the UDF needs to see all data at once, unlike scalar UDF where UDF only needs to see a batch at a time. This makes non decomposable not so useful as it is same as collect all the data to a pd.DataFrame and apply the UDF on it. However, one very application of non decomposable UDF is with segmented aggregation. To refresh, segmented aggregation works on ordered data and passed one logic chunk at a time (e.g., all data with the same date). With segmented aggregation and non decomposable aggregation UDF, the user can apply any custom aggregation logic over large stream of ordered data, with the memory overhead of a single segment.

What changes are included in this PR?

This PR is currently WIP and not ready for review.

So far I have implemented the minimal amount of code to make a basic test working but needs clean up, error handling etc.

  • First round of self review
  • Second round of self review
  • Implement and test unary
  • Implement and test varargs
  • Implement and test Acero support with segmented aggregation

Are these changes tested?

Added new test calling with compute and acero.

The compute tests calls the aggregation on the full array. The acero test callings the aggregation with segmented aggregation.

Are there any user-facing changes?

@icexelloss
icexelloss requested a review from AlenkaF as a code owner May 9, 2023 15:16
@github-actions

github-actions Bot commented May 9, 2023

Copy link
Copy Markdown

Thanks for opening a pull request!

If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose

Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project.

Then could you also rename the pull request title in the following format?

GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

In the case of PARQUET issues on JIRA the title also supports:

PARQUET-${JIRA_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

See also:

@icexelloss icexelloss changed the title WIP: [C++][Python] Add non decomposable aggregation UDF GH-35515: [WIP][C++][Python] Add non decomposable aggregation UDF May 9, 2023
@github-actions

github-actions Bot commented May 9, 2023

Copy link
Copy Markdown

@github-actions

github-actions Bot commented May 9, 2023

Copy link
Copy Markdown

⚠️ GitHub issue #35515 has been automatically assigned in GitHub to PR creator.

@icexelloss
icexelloss force-pushed the acero-group-agg-UDF-2 branch 2 times, most recently from 0dd8e25 to 2b38a2f Compare May 9, 2023 15:31
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.

Comment thread python/pyarrow/tests/test_udf.py Resolved
Comment thread python/pyarrow/src/arrow/python/udf.cc Resolved
Comment thread python/pyarrow/src/arrow/python/udf.cc Resolved
@icexelloss
icexelloss force-pushed the acero-group-agg-UDF-2 branch from 7ba9cc8 to 1203346 Compare June 6, 2023 15:39
@icexelloss
icexelloss force-pushed the acero-group-agg-UDF-2 branch from 6044f20 to 17ff274 Compare June 6, 2023 21:53
@icexelloss

Copy link
Copy Markdown
Contributor Author

@westonpace I believe this PR is good to go. The failed CI seems unrelated. I have checked the Py refcount and it seems OK (I will add details in the comment thread above)

@westonpace

Copy link
Copy Markdown
Member

@icexelloss I'll take another look through today.

@icexelloss

Copy link
Copy Markdown
Contributor Author

@icexelloss I'll take another look through today.

Thank you!

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

A few more very minor suggestions but, overall, I think this is fine.

Comment thread python/pyarrow/_compute.pyx Resolved
Comment thread python/pyarrow/src/arrow/python/udf.cc Resolved
Comment thread python/pyarrow/src/arrow/python/udf.cc Resolved
Comment thread python/pyarrow/conftest.py Outdated
Comment on lines 331 to 332

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.

Ok, so the test case is verifying that the python function can take in *args if needed (even though it still lists the args when registering)?

std::vector<std::shared_ptr<DataType>> input_types,
std::shared_ptr<DataType> output_type)
: agg_cb(agg_cb), agg_function(agg_function), output_type(output_type) {
Py_INCREF(agg_function->obj());

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.

This increment seems redundant given you already have one here.

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.

Admitted there could be some redundancy here. I created an follow up to take a closer look:

#36000

Comment thread python/pyarrow/src/arrow/python/udf.cc Resolved
Comment thread python/pyarrow/src/arrow/python/udf.cc Resolved
Comment thread python/pyarrow/src/arrow/python/udf.cc Resolved
Comment thread python/pyarrow/src/arrow/python/udf.cc Resolved
Comment thread python/pyarrow/src/arrow/python/udf.cc Resolved
@icexelloss

Copy link
Copy Markdown
Contributor Author

I checked failed CI jobs and those seem unrelated.

@ursabot

ursabot commented Jun 10, 2023

Copy link
Copy Markdown

Benchmark runs are scheduled for baseline = e920bed and contender = 8b5919d. 8b5919d is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Finished ⬇️0.0% ⬆️0.0%] ec2-t3-xlarge-us-east-2
[Finished ⬇️1.15% ⬆️0.06%] test-mac-arm
[Finished ⬇️10.46% ⬆️6.21%] ursa-i9-9960x
[Finished ⬇️0.3% ⬆️0.0%] ursa-thinkcentre-m75q
Buildkite builds:
[Finished] 8b5919d8 ec2-t3-xlarge-us-east-2
[Finished] 8b5919d8 test-mac-arm
[Finished] 8b5919d8 ursa-i9-9960x
[Finished] 8b5919d8 ursa-thinkcentre-m75q
[Finished] e920bed4 ec2-t3-xlarge-us-east-2
[Finished] e920bed4 test-mac-arm
[Finished] e920bed4 ursa-i9-9960x
[Finished] e920bed4 ursa-thinkcentre-m75q
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

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] Non decomposable aggregation UDF

4 participants