Skip to content

Commit

Permalink
core: reducers: reduce records provided by data source
Browse files Browse the repository at this point in the history
  • Loading branch information
Peva Blanchard committed Feb 23, 2024
1 parent d46a2e9 commit bf2ff76
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ open class DataSourceOperationsBase<Q>(
val description = DataSourceDescription(source.location, source.schema)
val records = load(description)
val filter = source.filter
val reducer = DataExpressionReducer(
dataRegister = Prelude.units(),
dataSourceRegister = DataSourceRegister.empty(),
ops = ops,
sourceOps = this,
)
return records
.filter { record ->
filter.entries.all {
Expand All @@ -40,10 +34,6 @@ open class DataSourceOperationsBase<Q>(
actual == expected
} else throw EvaluatorException("invalid matching condition $it")
}
}.map { record ->
ERecord(
record.entries.mapValues { reducer.reduce(it.value) }
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DataExpressionReducer<Q>(

private fun reduceFirstRecordOf(expression: EFirstRecordOf<Q>): DataExpression<Q> {
val dataSource = evalDataSource(expression.dataSource)
return sourceOps.getFirst(dataSource)
return reduceMap(sourceOps.getFirst(dataSource))
}

fun reduceDataSource(expression: DataSourceExpression<Q>, filter: Map<String, DataExpression<Q>> = emptyMap()): EDataSource<Q> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class LcaExpressionReducer<Q>(
is EBlockForEach -> {
val ds = dataExpressionReducer.evalDataSource(expression.dataSource)
sourceOps.readAll(ds)
.map { dataExpressionReducer.reduce(it) }
.flatMap { record ->
val reducer = push(mapOf(
DataKey(expression.rowRef) to record
Expand All @@ -110,6 +111,7 @@ class LcaExpressionReducer<Q>(
is EBlockForEach -> {
val ds = dataExpressionReducer.evalDataSource(expression.dataSource)
sourceOps.readAll(ds)
.map { dataExpressionReducer.reduce(it) }
.flatMap { record ->
val reducer = push(mapOf(
DataKey(expression.rowRef) to record
Expand All @@ -135,6 +137,7 @@ class LcaExpressionReducer<Q>(
is EBlockForEach -> {
val ds = dataExpressionReducer.evalDataSource(expression.dataSource)
sourceOps.readAll(ds)
.map { dataExpressionReducer.reduce(it) }
.flatMap { record ->
val reducer = push(mapOf(
DataKey(expression.rowRef) to record
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class DataExpressionReducerTest {
"mass" to QuantityFixture.oneKilogram
)
)
val record = EFirstRecordOf<BasicNumber>(EDataSourceRef("source"))
val firstRecordOf = EFirstRecordOf<BasicNumber>(EDataSourceRef("source"))
val reducer = DataExpressionReducer(
DataRegister.empty(),
DataSourceRegister.from(mapOf(
Expand All @@ -264,12 +264,16 @@ class DataExpressionReducerTest {
ops,
sourceOps,
)
val expected = mockk<ERecord<BasicNumber>>()
val expected = ERecord(
mapOf(
"mass" to QuantityFixture.twoKilograms
)
)
val dataSourceValue = with(ToValue(ops)) { dataSource.toValue() }
every { sourceOps.getFirst(dataSourceValue) } returns expected

// when
val actual = reducer.reduce(record)
val actual = reducer.reduce(firstRecordOf)

// then
assertEquals(expected, actual)
Expand Down

0 comments on commit bf2ff76

Please sign in to comment.