Skip to content

Commit

Permalink
Expose output of SetWindowFieldsOperation correctly to next aggrega…
Browse files Browse the repository at this point in the history
…tion stage.

This commit makes sure to expose calculated output fields correctly.

Original pull request #4751
Closes #4745
  • Loading branch information
christophstrobl authored and mp911de committed Aug 29, 2024
1 parent f8aa915 commit f17c125
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static SetWindowFieldsOperationBuilder builder() {

@Override
public ExposedFields getFields() {
return ExposedFields.nonSynthetic(Fields.from(output.fields.toArray(new Field[0])));
return ExposedFields.synthetic(Fields.from(output.fields.toArray(new Field[0])));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ void executesSetWindowFieldsOperationCorrectly() {
238, 378);
}

@Test // GH-4745
void exposesFieldsToNextStageCorrectly() {

initCakeSales();

SetWindowFieldsOperation setWindowFieldsOperation = SetWindowFieldsOperation.builder() //
.partitionByField("state") // resolves to field ref "$state"
.sortBy(Sort.by(Direction.ASC, "date")) // resolves to "orderDate"
.output(AccumulatorOperators.valueOf("qty").sum()) // resolves to "$quantity"
.within(Windows.documents().fromUnbounded().toCurrent().build()) //
.as("cumulativeQuantityForState") //
.build(); //

AggregationResults<Document> results = mongoTemplate.aggregateAndReturn(Document.class)
.by(Aggregation.newAggregation(CakeSale.class, setWindowFieldsOperation,
/* and now project on the field to see it can be referenced */
Aggregation.project("cumulativeQuantityForState")))
.all();

assertThat(results.getMappedResults()).map(it -> it.get("cumulativeQuantityForState")).contains(162, 282, 427, 134,
238, 378);
}

@Test // GH-3711
void executesSetWindowFieldsOperationWithPartitionExpressionCorrectly() {

Expand Down

0 comments on commit f17c125

Please sign in to comment.