-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf3cf5d
commit 40a065f
Showing
6 changed files
with
129 additions
and
5 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
96 changes: 96 additions & 0 deletions
96
partiql-spi/src/main/kotlin/org/partiql/spi/connector/sql/builtins/FnCollAgg.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,96 @@ | ||
// ktlint-disable filename | ||
@file:Suppress("ClassName") | ||
|
||
package org.partiql.spi.connector.sql.builtins | ||
|
||
import org.partiql.spi.connector.sql.builtins.internal.Accumulator | ||
import org.partiql.spi.connector.sql.builtins.internal.AccumulatorAnySome | ||
import org.partiql.spi.connector.sql.builtins.internal.AccumulatorAvg | ||
import org.partiql.spi.connector.sql.builtins.internal.AccumulatorCount | ||
import org.partiql.spi.connector.sql.builtins.internal.AccumulatorEvery | ||
import org.partiql.spi.connector.sql.builtins.internal.AccumulatorGroupAs | ||
import org.partiql.spi.connector.sql.builtins.internal.AccumulatorMax | ||
import org.partiql.spi.connector.sql.builtins.internal.AccumulatorMin | ||
import org.partiql.spi.connector.sql.builtins.internal.AccumulatorSum | ||
import org.partiql.spi.fn.Agg | ||
import org.partiql.spi.fn.Fn | ||
import org.partiql.spi.fn.FnExperimental | ||
import org.partiql.spi.fn.FnParameter | ||
import org.partiql.spi.fn.FnSignature | ||
import org.partiql.value.BagValue | ||
import org.partiql.value.PartiQLValue | ||
import org.partiql.value.PartiQLValueExperimental | ||
import org.partiql.value.PartiQLValueType | ||
import org.partiql.value.check | ||
|
||
@OptIn(PartiQLValueExperimental::class, FnExperimental::class) | ||
internal abstract class Fn_COLL_AGG__BAG__ANY : Fn { | ||
|
||
abstract fun getAccumulator(): Agg.Accumulator | ||
|
||
companion object { | ||
@JvmStatic | ||
internal fun createSignature(name: String) = FnSignature( | ||
name = name, | ||
returns = PartiQLValueType.ANY, | ||
parameters = listOf( | ||
FnParameter("value", PartiQLValueType.BAG), | ||
), | ||
isNullCall = true, | ||
isNullable = true | ||
) | ||
} | ||
|
||
override fun invoke(args: Array<PartiQLValue>): PartiQLValue { | ||
val bag = args[0].check<BagValue<*>>() | ||
val accumulator = getAccumulator() | ||
bag.forEach { element -> accumulator.next(arrayOf(element)) } | ||
return accumulator.value() | ||
} | ||
|
||
object SUM : Fn_COLL_AGG__BAG__ANY() { | ||
override val signature = createSignature("coll_sum") | ||
override fun getAccumulator(): Accumulator = AccumulatorSum() | ||
} | ||
|
||
object AVG : Fn_COLL_AGG__BAG__ANY() { | ||
override val signature = createSignature("coll_avg") | ||
override fun getAccumulator(): Accumulator = AccumulatorAvg() | ||
} | ||
|
||
object MIN : Fn_COLL_AGG__BAG__ANY() { | ||
override val signature = createSignature("coll_min") | ||
override fun getAccumulator(): Accumulator = AccumulatorMin() | ||
} | ||
|
||
object MAX : Fn_COLL_AGG__BAG__ANY() { | ||
override val signature = createSignature("coll_max") | ||
override fun getAccumulator(): Accumulator = AccumulatorMax() | ||
} | ||
|
||
object COUNT : Fn_COLL_AGG__BAG__ANY() { | ||
override val signature = createSignature("coll_count") | ||
override fun getAccumulator(): Accumulator = AccumulatorCount() | ||
} | ||
|
||
object EVERY : Fn_COLL_AGG__BAG__ANY() { | ||
override val signature = createSignature("coll_every") | ||
override fun getAccumulator(): Accumulator = AccumulatorEvery() | ||
} | ||
|
||
// TODO: Should we allow this? | ||
object GROUP_AS : Fn_COLL_AGG__BAG__ANY() { | ||
override val signature = createSignature("coll_group_as") | ||
override fun getAccumulator(): Accumulator = AccumulatorGroupAs() | ||
} | ||
|
||
object ANY : Fn_COLL_AGG__BAG__ANY() { | ||
override val signature = createSignature("coll_any") | ||
override fun getAccumulator(): Accumulator = AccumulatorAnySome() | ||
} | ||
|
||
object SOME : Fn_COLL_AGG__BAG__ANY() { | ||
override val signature = createSignature("coll_some") | ||
override fun getAccumulator(): Accumulator = AccumulatorAnySome() | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
...l-spi/src/main/kotlin/org/partiql/spi/connector/sql/builtins/internal/AccumulatorCount.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,17 @@ | ||
package org.partiql.spi.connector.sql.builtins.internal | ||
|
||
import org.partiql.value.PartiQLValue | ||
import org.partiql.value.PartiQLValueExperimental | ||
import org.partiql.value.int64Value | ||
|
||
@OptIn(PartiQLValueExperimental::class) | ||
internal class AccumulatorCount : Accumulator() { | ||
|
||
var count: Long = 0L | ||
|
||
override fun nextValue(value: PartiQLValue) { | ||
this.count += 1L | ||
} | ||
|
||
override fun value(): PartiQLValue = int64Value(count) | ||
} |
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