Skip to content

Commit f392984

Browse files
christophstroblmp911de
authored andcommitted
Expose output of SetWindowFieldsOperation correctly to next aggregation stage.
This commit makes sure to expose calculated output fields correctly. Original pull request #4751 Closes #4745
1 parent ac0fd6c commit f392984

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SetWindowFieldsOperation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static SetWindowFieldsOperationBuilder builder() {
7070

7171
@Override
7272
public ExposedFields getFields() {
73-
return ExposedFields.nonSynthetic(Fields.from(output.fields.toArray(new Field[0])));
73+
return ExposedFields.synthetic(Fields.from(output.fields.toArray(new Field[0])));
7474
}
7575

7676
@Override

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SetWindowFieldsOperationTests.java

+23
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,29 @@ void executesSetWindowFieldsOperationCorrectly() {
7373
238, 378);
7474
}
7575

76+
@Test // GH-4745
77+
void exposesFieldsToNextStageCorrectly() {
78+
79+
initCakeSales();
80+
81+
SetWindowFieldsOperation setWindowFieldsOperation = SetWindowFieldsOperation.builder() //
82+
.partitionByField("state") // resolves to field ref "$state"
83+
.sortBy(Sort.by(Direction.ASC, "date")) // resolves to "orderDate"
84+
.output(AccumulatorOperators.valueOf("qty").sum()) // resolves to "$quantity"
85+
.within(Windows.documents().fromUnbounded().toCurrent().build()) //
86+
.as("cumulativeQuantityForState") //
87+
.build(); //
88+
89+
AggregationResults<Document> results = mongoTemplate.aggregateAndReturn(Document.class)
90+
.by(Aggregation.newAggregation(CakeSale.class, setWindowFieldsOperation,
91+
/* and now project on the field to see it can be referenced */
92+
Aggregation.project("cumulativeQuantityForState")))
93+
.all();
94+
95+
assertThat(results.getMappedResults()).map(it -> it.get("cumulativeQuantityForState")).contains(162, 282, 427, 134,
96+
238, 378);
97+
}
98+
7699
@Test // GH-3711
77100
void executesSetWindowFieldsOperationWithPartitionExpressionCorrectly() {
78101

0 commit comments

Comments
 (0)