Skip to content

Commit

Permalink
Merge c205372 into d45e1d5
Browse files Browse the repository at this point in the history
  • Loading branch information
dowinter committed Nov 2, 2020
2 parents d45e1d5 + c205372 commit 0b5e90d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ public FirstAccumulator(String field, Object expression) {

@Override
public void aggregate(Object value) {
if (first && !(value instanceof Missing)) {
firstValue = value;
if (first) {
if (!(value instanceof Missing)) {
firstValue = value;
} else {
firstValue = null;
}
first = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public LastAccumulator(String field, Object expression) {
public void aggregate(Object value) {
if (!(value instanceof Missing)) {
lastValue = value;
} else {
lastValue = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,36 @@ void testAggregateWithFirstAndLast_Missing() throws Exception {
);
}

@Test
void testAggregateWithGroupingAndFirstMissing() throws Exception {
Document sort = json("$sort: {index: 1}");
Document group = json("$group: {_id: '$index', first: { $first: '$field1' }}");
List<Document> pipeline = Arrays.asList(sort, group);

collection.insertOne(json("_id: 1, index: 1"));
collection.insertOne(json("_id: 2, index: 1, field1: 'abc'"));

assertThat(collection.aggregate(pipeline))
.containsExactly(
json("_id: 1, first: null")
);
}

@Test
void testAggregateWithGroupingAndLastMissing() throws Exception {
Document sort = json("$sort: {index: 1}");
Document group = json("$group: {_id: '$index', last: { $last: '$field1' }}");
List<Document> pipeline = Arrays.asList(sort, group);

collection.insertOne(json("_id: 1, index: 1, field1: 'abc'"));
collection.insertOne(json("_id: 2, index: 1"));

assertThat(collection.aggregate(pipeline))
.containsExactly(
json("_id: 1, last: null")
);
}

@Test
void testAggregateWithPush() throws Exception {
List<Document> pipeline = jsonList("$group: {_id: null, a: {$push: '$a'}, b: {$push: {v: '$b'}}, c: {$push: '$c'}}");
Expand Down

0 comments on commit 0b5e90d

Please sign in to comment.