-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for aggregations (GROUP BY)
Adds support for COLL_AGGs
- Loading branch information
1 parent
f1aeb6f
commit 45315e4
Showing
67 changed files
with
1,698 additions
and
447 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
137 changes: 137 additions & 0 deletions
137
partiql-eval/src/main/kotlin/org/partiql/eval/internal/operator/rel/RelAggregate.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,137 @@ | ||
package org.partiql.eval.internal.operator.rel | ||
|
||
import org.partiql.eval.internal.Record | ||
import org.partiql.eval.internal.operator.Operator | ||
import org.partiql.spi.fn.Agg | ||
import org.partiql.spi.fn.FnExperimental | ||
import org.partiql.value.ListValue | ||
import org.partiql.value.PartiQLValue | ||
import org.partiql.value.PartiQLValueExperimental | ||
import org.partiql.value.PartiQLValueType | ||
import org.partiql.value.listValue | ||
import org.partiql.value.nullValue | ||
import java.util.TreeMap | ||
import java.util.TreeSet | ||
|
||
internal class RelAggregate( | ||
val input: Operator.Relation, | ||
val keys: List<Operator.Expr>, | ||
val functions: List<Operator.Accumulator> | ||
) : Operator.Relation { | ||
|
||
lateinit var records: Iterator<Record> | ||
|
||
@OptIn(PartiQLValueExperimental::class) | ||
val aggregationMap = TreeMap<PartiQLValue, List<AccumulatorWrapper>>(PartiQLValue.comparator(nullsFirst = false)) | ||
|
||
@OptIn(PartiQLValueExperimental::class) | ||
object PartiQLValueListComparator : Comparator<List<PartiQLValue>> { | ||
private val delegate = PartiQLValue.comparator(nullsFirst = false) | ||
override fun compare(o1: List<PartiQLValue>, o2: List<PartiQLValue>): Int { | ||
if (o1.size < o2.size) { | ||
return -1 | ||
} | ||
if (o1.size > o2.size) { | ||
return 1 | ||
} | ||
for (index in 0..o2.lastIndex) { | ||
val element1 = o1[index] | ||
val element2 = o2[index] | ||
val compared = delegate.compare(element1, element2) | ||
if (compared != 0) { | ||
return compared | ||
} | ||
} | ||
return 0 | ||
} | ||
} | ||
|
||
/** | ||
* Wraps an [Operator.Accumulator.Instance] to help with filtering distinct values. | ||
* | ||
* @property seen maintains which values have already been seen. If null, we accumulate all values coming through. | ||
*/ | ||
class AccumulatorWrapper @OptIn(PartiQLValueExperimental::class, FnExperimental::class) constructor( | ||
val delegate: Agg.Accumulator, | ||
val args: List<Operator.Expr>, | ||
val seen: TreeSet<List<PartiQLValue>>? | ||
) | ||
|
||
@OptIn(PartiQLValueExperimental::class, FnExperimental::class) | ||
override fun open() { | ||
input.open() | ||
var inputRecord = input.next() | ||
while (inputRecord != null) { | ||
// Initialize the AggregationMap | ||
val evaluatedGroupByKeys = listValue( | ||
keys.map { | ||
val key = it.eval(inputRecord!!) | ||
when (key.type == PartiQLValueType.MISSING) { | ||
true -> nullValue() | ||
false -> key | ||
} | ||
} | ||
) | ||
val accumulators = aggregationMap.getOrPut(evaluatedGroupByKeys) { | ||
functions.map { | ||
AccumulatorWrapper( | ||
delegate = it.delegate.accumulator(), | ||
args = it.args, | ||
seen = when (it.setQuantifier) { | ||
Operator.Accumulator.SetQuantifier.DISTINCT -> TreeSet(PartiQLValueListComparator) | ||
Operator.Accumulator.SetQuantifier.ALL -> null | ||
} | ||
) | ||
} | ||
} | ||
|
||
// Aggregate Values in Aggregation State | ||
accumulators.forEachIndexed { index, function -> | ||
val valueToAggregate = function.args.map { it.eval(inputRecord!!) } | ||
// Skip over aggregation if NULL/MISSING | ||
if (valueToAggregate.any { it.type == PartiQLValueType.MISSING || it.isNull }) { | ||
return@forEachIndexed | ||
} | ||
// Skip over aggregation if DISTINCT and SEEN | ||
if (function.seen != null && (function.seen.add(valueToAggregate).not())) { | ||
return@forEachIndexed | ||
} | ||
accumulators[index].delegate.next(valueToAggregate.toTypedArray()) | ||
} | ||
inputRecord = input.next() | ||
} | ||
|
||
// No Aggregations Created // TODO: How would this be possible? | ||
if (keys.isEmpty() && aggregationMap.isEmpty()) { | ||
val record = mutableListOf<PartiQLValue>() | ||
functions.forEach { function -> | ||
val accumulator = function.delegate.accumulator() | ||
record.add(accumulator.value()) | ||
} | ||
records = iterator { yield(Record.of(*record.toTypedArray())) } | ||
return | ||
} | ||
|
||
records = iterator { | ||
aggregationMap.forEach { (pValue, accumulators) -> | ||
val keysEvaluated = pValue as ListValue<*> | ||
val recordValues = accumulators.map { acc -> acc.delegate.value() } + keysEvaluated.map { value -> value } | ||
yield(Record.of(*recordValues.toTypedArray())) | ||
} | ||
} | ||
} | ||
|
||
override fun next(): Record? { | ||
return if (records.hasNext()) { | ||
records.next() | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
@OptIn(PartiQLValueExperimental::class) | ||
override fun close() { | ||
aggregationMap.clear() | ||
input.close() | ||
} | ||
} |
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
Oops, something went wrong.