This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sum/max/min reducers in Scala (#3133)
- Loading branch information
Showing
4 changed files
with
109 additions
and
6 deletions.
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
48 changes: 48 additions & 0 deletions
48
heron/api/src/scala/org/apache/heron/streamlet/scala/StreamletReducers.scala
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,48 @@ | ||
/** | ||
* 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.heron.streamlet.scala | ||
|
||
/** | ||
* This class contains a few standard reduces that can be used with | ||
* Streamlet reduce functions such as reduceByKeyAndWindow. | ||
* Example, assuming s is a Stringlet<T> object and each tuple has these functions: | ||
* - Integer getKey() and | ||
* - Double getValue() | ||
* To get streams of sum, min and max of all values upto the current one: | ||
* s.reduceByKey(T::getKey, T::getValue, StreamletReducers::sum); | ||
* s.reduceByKey(T::getKey, T::getValue, StreamletReducers::min); | ||
* s.reduceByKey(T::getKey, T::getValue, StreamletReducers::max); | ||
*/ | ||
object StreamletReducers { | ||
|
||
def sum(a: Int, b: Int): Int = a + b | ||
def sum(a: Long, b: Long): Long = a + b | ||
def sum(a: Float, b: Float): Float = a + b | ||
def sum(a: Double, b: Double): Double = a + b | ||
|
||
def max(a: Int, b: Int): Int = math.max(a, b) | ||
def max(a: Long, b: Long): Long = math.max(a, b) | ||
def max(a: Float, b: Float): Float = math.max(a, b) | ||
def max(a: Double, b: Double): Double = math.max(a, b) | ||
|
||
def min(a: Int, b: Int): Int = math.min(a, b) | ||
def min(a: Long, b: Long): Long = math.min(a, b) | ||
def min(a: Float, b: Float): Float = math.min(a, b) | ||
def min(a: Double, b: Double): Double = math.min(a, b) | ||
} |
55 changes: 55 additions & 0 deletions
55
heron/api/tests/scala/org/apache/heron/streamlet/scala/StreamletReducersTest.scala
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,55 @@ | ||
/** | ||
* 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.heron.streamlet.scala | ||
|
||
import org.junit.Assert.assertEquals | ||
|
||
import org.apache.heron.streamlet.scala.common.BaseFunSuite | ||
|
||
class StreamletReducersTest extends BaseFunSuite { | ||
|
||
test("Sum should work correctly") { | ||
assertEquals(StreamletReducers.sum(1, 2), 3) | ||
assertEquals(StreamletReducers.sum(1L, 2L), 3L) | ||
assertEquals(StreamletReducers.sum(1.0f, 2.0f), 3.0f, 0.01f) | ||
assertEquals(StreamletReducers.sum(1.0, 2.0), 3.0, 0.01) | ||
} | ||
|
||
test("Max should work correctly") { | ||
assertEquals(StreamletReducers.max(1, 2), 2) | ||
assertEquals(StreamletReducers.max(2, 1), 2) | ||
assertEquals(StreamletReducers.max(1L, 2L), 2L) | ||
assertEquals(StreamletReducers.max(2L, 1L), 2L) | ||
assertEquals(StreamletReducers.max(1.0f, 2.0f), 2.0f, 0.01f) | ||
assertEquals(StreamletReducers.max(2.0f, 1.0f), 2.0f, 0.01f) | ||
assertEquals(StreamletReducers.max(1.0, 2.0), 2.0, 0.01) | ||
assertEquals(StreamletReducers.max(2.0, 1.0), 2.0, 0.01) | ||
} | ||
|
||
test("Min should work correctly") { | ||
assertEquals(StreamletReducers.min(1, 2), 1) | ||
assertEquals(StreamletReducers.min(2, 1), 1) | ||
assertEquals(StreamletReducers.min(1L, 2L), 1L) | ||
assertEquals(StreamletReducers.min(2L, 1L), 1L) | ||
assertEquals(StreamletReducers.min(1.0f, 2.0f), 1.0f, 0.01f) | ||
assertEquals(StreamletReducers.min(2.0f, 1.0f), 1.0f, 0.01f) | ||
assertEquals(StreamletReducers.min(1.0, 2.0), 1.0, 0.01) | ||
assertEquals(StreamletReducers.min(2.0, 1.0), 1.0, 0.01) | ||
} | ||
} |
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