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-2520] add UDF/UDAF in BeamSql.query/simpleQuery #3491
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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
137 changes: 137 additions & 0 deletions
137
dsls/sql/src/test/java/org/apache/beam/dsls/sql/BeamSqlDslUdfUdafTest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.beam.dsls.sql; | ||
|
||
import java.sql.Types; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import org.apache.beam.dsls.sql.schema.BeamSqlRecordType; | ||
import org.apache.beam.dsls.sql.schema.BeamSqlRow; | ||
import org.apache.beam.dsls.sql.schema.BeamSqlUdaf; | ||
import org.apache.beam.sdk.testing.PAssert; | ||
import org.apache.beam.sdk.values.PCollection; | ||
import org.apache.beam.sdk.values.PCollectionTuple; | ||
import org.apache.beam.sdk.values.TupleTag; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for UDF/UDAF. | ||
*/ | ||
public class BeamSqlDslUdfUdafTest extends BeamSqlDslBase { | ||
/** | ||
* GROUP-BY with UDAF. | ||
*/ | ||
@Test | ||
public void testUdaf() throws Exception { | ||
BeamSqlRecordType resultType = BeamSqlRecordType.create(Arrays.asList("f_int2", "squaresum"), | ||
Arrays.asList(Types.INTEGER, Types.INTEGER)); | ||
|
||
BeamSqlRow record = new BeamSqlRow(resultType); | ||
record.addField("f_int2", 0); | ||
record.addField("squaresum", 30); | ||
|
||
String sql1 = "SELECT f_int2, squaresum1(f_int) AS `squaresum`" | ||
+ " FROM PCOLLECTION GROUP BY f_int2"; | ||
PCollection<BeamSqlRow> result1 = | ||
boundedInput1.apply("testUdaf1", | ||
BeamSql.simpleQuery(sql1).withUdaf("squaresum1", SquareSum.class)); | ||
PAssert.that(result1).containsInAnyOrder(record); | ||
|
||
String sql2 = "SELECT f_int2, squaresum2(f_int) AS `squaresum`" | ||
+ " FROM PCOLLECTION GROUP BY f_int2"; | ||
PCollection<BeamSqlRow> result2 = | ||
PCollectionTuple.of(new TupleTag<BeamSqlRow>("PCOLLECTION"), boundedInput1) | ||
.apply("testUdaf2", | ||
BeamSql.query(sql2).withUdaf("squaresum2", SquareSum.class)); | ||
PAssert.that(result2).containsInAnyOrder(record); | ||
|
||
pipeline.run().waitUntilFinish(); | ||
} | ||
|
||
/** | ||
* test UDF. | ||
*/ | ||
@Test | ||
public void testUdf() throws Exception{ | ||
BeamSqlRecordType resultType = BeamSqlRecordType.create(Arrays.asList("f_int", "cubicvalue"), | ||
Arrays.asList(Types.INTEGER, Types.INTEGER)); | ||
|
||
BeamSqlRow record = new BeamSqlRow(resultType); | ||
record.addField("f_int", 2); | ||
record.addField("cubicvalue", 8); | ||
|
||
String sql1 = "SELECT f_int, cubic1(f_int) as cubicvalue FROM PCOLLECTION WHERE f_int = 2"; | ||
PCollection<BeamSqlRow> result1 = | ||
boundedInput1.apply("testUdf1", | ||
BeamSql.simpleQuery(sql1).withUdf("cubic1", CubicInteger.class, "cubic")); | ||
PAssert.that(result1).containsInAnyOrder(record); | ||
|
||
String sql2 = "SELECT f_int, cubic2(f_int) as cubicvalue FROM PCOLLECTION WHERE f_int = 2"; | ||
PCollection<BeamSqlRow> result2 = | ||
PCollectionTuple.of(new TupleTag<BeamSqlRow>("PCOLLECTION"), boundedInput1) | ||
.apply("testUdf2", | ||
BeamSql.query(sql2).withUdf("cubic2", CubicInteger.class, "cubic")); | ||
PAssert.that(result2).containsInAnyOrder(record); | ||
|
||
pipeline.run().waitUntilFinish(); | ||
} | ||
|
||
/** | ||
* UDAF for test, which returns the sum of square. | ||
*/ | ||
public static class SquareSum extends BeamSqlUdaf<Integer, Integer, Integer> { | ||
|
||
public SquareSum() { | ||
} | ||
|
||
// @Override | ||
public Integer init() { | ||
return 0; | ||
} | ||
|
||
// @Override | ||
public Integer add(Integer accumulator, Integer input) { | ||
return accumulator + input * input; | ||
} | ||
|
||
// @Override | ||
public Integer merge(Iterable<Integer> accumulators) { | ||
int v = 0; | ||
Iterator<Integer> ite = accumulators.iterator(); | ||
while (ite.hasNext()) { | ||
v += ite.next(); | ||
} | ||
return v; | ||
} | ||
|
||
// @Override | ||
public Integer result(Integer accumulator) { | ||
return accumulator; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* A example UDF for test. | ||
*/ | ||
public static class CubicInteger{ | ||
public static Integer cubic(Integer input){ | ||
return input * input * input; | ||
} | ||
} | ||
} |
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.
Is it necessary to support having arbitrarily named (and potentially multiple) UDF methods on a single class? Why not just have a simply BeamSqlUdf interface you must implement? And why not accept a lambda?
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.
Is it necessary to support having arbitrarily named (and potentially multiple) UDF methods on a single class?
[Mingmin]: I would like to keep the capacity to put similar or related methods together.
Why not just have a simply BeamSqlUdf interface you must implement?
[Mingmin]: That's how we do with UDAF, UDF is a little different as it may have variable, even optional parameters. Annotation (like
DoFn
) sounds a solution but the existing definition is more simple IMO.And why not accept a lambda?
[Mingmin]: I remember Beam is still on JDK 7, --sorry that I may miss something.
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.