Skip to content
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

[BEAM-10925] Create interface for SQL Java aggregate function. #13306

Merged
merged 1 commit into from Dec 16, 2020

Conversation

ibzib
Copy link
Contributor

@ibzib ibzib commented Nov 11, 2020

R: @amaliujia @kennknowles


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Choose reviewer(s) and mention them in a comment (R: @username).
  • Format the pull request title like [BEAM-XXX] Fixes bug in ApproximateQuantiles, where you replace BEAM-XXX with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

Post-Commit Tests Status (on master branch)

Lang SDK Dataflow Flink Samza Spark Twister2
Go Build Status --- Build Status --- Build Status ---
Java Build Status Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status Build Status
Build Status
Build Status
Build Status
Python Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
--- Build Status ---
XLang Build Status --- Build Status --- Build Status ---

Pre-Commit Tests Status (on master branch)

--- Java Python Go Website Whitespace Typescript
Non-portable Build Status Build Status
Build Status
Build Status
Build Status
Build Status Build Status Build Status Build Status
Portable --- Build Status --- --- --- ---

See .test-infra/jenkins/README for trigger phrase, status and link of all Jenkins jobs.

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests

See CI.md for more information about GitHub Actions CI.

import org.apache.beam.sdk.transforms.Combine;

/** An aggregate function that can be executed as part of a SQL query. */
public class AggregateFn implements Serializable {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think whether we should start by adding @Experimental?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely. Done.

@kennknowles
Copy link
Member

Just checking: the interface for a SQL aggregation function is "can give you a CombineFn"? I understand this is the current status of UDAFs. Is that where we want to leave it? I think the main value of a separate interface (style nit: prefer to separate the interface from this particular implementation) would be a slight decoupling.

(non-blocking, just questions & comments & I'll leave it to others)

@amaliujia
Copy link
Contributor

amaliujia commented Nov 13, 2020

If we don't expose CombineFn in interface, I think what we will end is a similar interface design to current CombineFn: we will ask users to offer implementations init accumulator, add input, merge accumulator, extract, etc. then in implementation we will create CombineFn as a wrapper on top of those implementations.

The benefits for this decoupling seems little given CombineFn is a stable API in Beam.

@amaliujia
Copy link
Contributor

amaliujia commented Nov 13, 2020

Also from potential user's requetes, there are existing CombinFn implementation and they want use those as UDAF. This is a different case from Scalar Fn that rewrite CombinFn into our UDAF API (if we plans to decouple the API with CombineFn) may be non-trivial.

@kennknowles
Copy link
Member

CombineFn is stable, but UDAF is not. One example is that a UDAF has to have a SQL type. Right now this is not represented on the UDAF object but is implied. That might change. Personally, I would make the type part of the UDAF object. If you look at any other SQL product's definition of UDAFs you will see lots of extra metadata (postgresql, mssql).

A decoupled interface and trivial wrapper have obvious benefits and no real downside.

@amaliujia
Copy link
Contributor

amaliujia commented Nov 18, 2020

I am not sure if I have understand:

One example is that a UDAF has to have a SQL

This interface is the implementation interface of a UDAF, but not function definition. Function definition should provide SQL types, but definition in our case is the CREATE FUNCTION statement.

A decoupled interface and trivial wrapper have obvious benefits and no real downside.

I don't agree in this case that creating a wrapper is trivial. For UDAF/CombeFn, I think it is non-trival work to create the wrapper.

Copy link
Member

@apilloud apilloud left a comment

Choose a reason for hiding this comment

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

How do we determine the name of the UDAF? Should there be a name method? Is there another interface for discovering these?

This mostly matches the interface of registerUdaf, so LGTM. I agree with Kenn that we will eventually want to offer a version of this interface that doesn't have a dependency on Beam.

@ibzib
Copy link
Contributor Author

ibzib commented Nov 20, 2020

How do we determine the name of the UDAF? Should there be a name method? Is there another interface for discovering these?

I will add another interface to discover these after this PR and #13305 are merged. Something like this:

https://github.com/ibzib/beam/blob/0499c8f08839a9846ecb726b78eef738d21d0052/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/UdfProvider.java

@ibzib
Copy link
Contributor Author

ibzib commented Dec 16, 2020

As requested I rewrote this without a dependency on Beam Java core. Right now it is a shameless copy-paste of some stuff from CombineFn, since the implementation will still depend on CombineFn anyway. But now we're decoupled at the API level, so we have more flexibility to make whatever changes are needed later (such as Kenn's suggestion to add a SQL type).

I don't agree in this case that creating a wrapper is trivial. For UDAF/CombeFn, I think it is non-trival work to create the wrapper.

There already is a wrapper that's basically the same as what we're doing here: https://github.com/apache/beam/blob/release-2.25.0/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/transform/agg/AggregationCombineFnAdapter.java

Anyway, I will figure it out :)

* @param <AccumT> type of mutable accumulator values
* @param <OutputT> type of output values
*/
public interface AggregateFn<
Copy link
Contributor

Choose a reason for hiding this comment

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

Mark as @experimental?

Copy link
Contributor

@amaliujia amaliujia left a comment

Choose a reason for hiding this comment

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

I think there is a experimental annotation is missing. Otherwise LGTM

@ibzib
Copy link
Contributor Author

ibzib commented Dec 16, 2020

I think there is a experimental annotation is missing.

Experimental itself is a part of Beam java core: https://github.com/apache/beam/blob/486d23007dac513f9f90bb08c2a039cd72a7ec94/sdks/java/core/src/main/java/org/apache/beam/sdk/annotations/Experimental.java

I added a note to the javadoc instead.

Copy link
Member

@apilloud apilloud left a comment

Choose a reason for hiding this comment

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

LGTM. Make sure you get the changes to build.gradle from #13305...

@ibzib ibzib merged commit a7457c0 into apache:master Dec 16, 2020
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.

None yet

4 participants