-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NU-1921] Add median aggregator #7321
Co-authored-by: Pawel Czajka <[email protected]>
- Loading branch information
Showing
10 changed files
with
227 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
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
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
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
58 changes: 58 additions & 0 deletions
58
...ala/pl/touk/nussknacker/engine/flink/util/transformer/aggregate/median/MedianHelper.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,58 @@ | ||
package pl.touk.nussknacker.engine.flink.util.transformer.aggregate.median | ||
|
||
import pl.touk.nussknacker.engine.api.typed.supertype.NumberTypesPromotionStrategy.{ | ||
ForLargeFloatingNumbersOperation, | ||
} | ||
import pl.touk.nussknacker.engine.util.MathUtils | ||
|
||
import scala.annotation.tailrec | ||
import scala.util.Random | ||
|
||
object MedianHelper { | ||
private val rand = new Random(42) | ||
|
||
def calculateMedian(numbers: List[Number]): Option[Number] = { | ||
if (numbers.isEmpty) { | ||
None | ||
} else if (numbers.size % 2 == 1) { | ||
Some(MathUtils.convertToPromotedType(quickSelect(numbers, (numbers.size - 1) / 2))(ForLargeFloatingNumbersOperation)) | ||
} else { | ||
// it is possible to fetch both numbers with single recursion, but it would complicate code | ||
val firstNumber = quickSelect(numbers, numbers.size / 2 - 1) | ||
val secondNumber = quickSelect(numbers, numbers.size / 2) | ||
|
||
val sum = MathUtils.largeFloatingSum(firstNumber, secondNumber) | ||
Some(MathUtils.divideWithDefaultBigDecimalScale(sum, 2)) | ||
} | ||
} | ||
|
||
// https://en.wikipedia.org/wiki/Quickselect | ||
@tailrec | ||
private def quickSelect(numbers: List[Number], indexToTake: Int): Number = { | ||
require(numbers.nonEmpty) | ||
require(indexToTake >= 0) | ||
require(indexToTake < numbers.size) | ||
|
||
val randomElement = numbers(rand.nextInt(numbers.size)) | ||
val groupedBy = numbers.groupBy(e => { | ||
val cmp = MathUtils.compare(e, randomElement) | ||
if (cmp < 0) { | ||
-1 | ||
} else if (cmp == 0) { | ||
0 | ||
} else 1 | ||
}) | ||
val smallerNumbers = groupedBy.getOrElse(-1, Nil) | ||
val equalNumbers = groupedBy.getOrElse(0, Nil) | ||
val largerNumbers = groupedBy.getOrElse(1, Nil) | ||
|
||
if (indexToTake < smallerNumbers.size) { | ||
quickSelect(smallerNumbers, indexToTake) | ||
} else if (indexToTake < smallerNumbers.size + equalNumbers.size) { | ||
equalNumbers(indexToTake - smallerNumbers.size) | ||
} else { | ||
quickSelect(largerNumbers, indexToTake - smallerNumbers.size - equalNumbers.size) | ||
} | ||
} | ||
|
||
} |
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
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