-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add accumulator, first impl and its test
- Loading branch information
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
physical-plan/src/main/kotlin/expressions/AggregateExpression.kt
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,11 @@ | ||
package io.hqew.kquery.physical.expressions | ||
|
||
interface AggregateExpression { | ||
fun inputExpression(): Expression | ||
fun createAccumulator(): Accumulator | ||
} | ||
|
||
interface Accumulator{ | ||
fun accumulate(value: Any?) | ||
fun finalValue() : Any? | ||
} |
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
42 changes: 42 additions & 0 deletions
42
physical-plan/src/main/kotlin/expressions/MaxExpression.kt
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,42 @@ | ||
package io.hqew.kquery.physical.expressions | ||
|
||
class MaxExpression(private val expr: Expression) : AggregateExpression { | ||
override fun inputExpression(): Expression { | ||
return expr | ||
} | ||
|
||
override fun createAccumulator(): Accumulator { | ||
return MaxAccumulator() | ||
} | ||
|
||
override fun toString(): String { | ||
return "MAX($expr)" | ||
} | ||
} | ||
|
||
class MaxAccumulator : Accumulator { | ||
var value: Any? = null | ||
override fun accumulate(value: Any?) { | ||
if (value != null) { | ||
if (this.value == null) { | ||
this.value = value | ||
} else { | ||
val isMax = when(value) { | ||
is Byte -> value > this.value as Byte | ||
is Short -> value > this.value as Short | ||
is Int -> value > this.value as Int | ||
is Long -> value > this.value as Long | ||
is Float -> value > this.value as Float | ||
is Double -> value > this.value as Double | ||
is String -> value > this.value as String | ||
else -> throw UnsupportedOperationException("MAX does not support type of ${value.javaClass.name} ") | ||
} | ||
if (isMax) this.value = value | ||
} | ||
} | ||
} | ||
|
||
override fun finalValue(): Any? { | ||
return this.value | ||
} | ||
} |
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,28 @@ | ||
import io.hqew.kquery.physical.expressions.MaxAccumulator | ||
import org.junit.Test | ||
import org.junit.jupiter.api.TestInstance | ||
import kotlin.test.assertEquals | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class AccumulatorTest { | ||
|
||
@Test | ||
fun `max accumulator should accumulate value`() { | ||
val numbers = listOf(1, 2, 3, 5, 4) | ||
val maxAcc = MaxAccumulator() | ||
|
||
numbers.forEach { maxAcc.accumulate(it) } | ||
|
||
assertEquals(5, maxAcc.finalValue()) | ||
} | ||
|
||
@Test | ||
fun `max accumulator should accumulate string value`() { | ||
val numbers = listOf("10", "2", "3", "5", "4", "a") | ||
val maxAcc = MaxAccumulator() | ||
|
||
numbers.forEach { maxAcc.accumulate(it) } | ||
|
||
assertEquals("a", maxAcc.finalValue()) | ||
} | ||
} |