Skip to content

DATAMONGO-2651 - Support $accumulator in GroupOperationBuilder. #898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-DATAMONGO-2651-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-DATAMONGO-2651-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-DATAMONGO-2651-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-DATAMONGO-2651-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.data.mongodb.core.aggregation.ScriptOperators.Accumulator;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -375,6 +376,17 @@ public GroupOperationBuilder stdDevPop(AggregationExpression expr) {
return newBuilder(GroupOps.STD_DEV_POP, null, expr);
}

/**
* Generates an {@link GroupOperationBuilder} for an {@code $accumulator}-expression.
*
* @param accumulator must not be {@literal null}.
* @return never {@literal null}.
* @since 1.10
*/
public GroupOperationBuilder accumulate(Accumulator accumulator) {
return new GroupOperationBuilder(this, new Operation(accumulator));
}

private GroupOperationBuilder newBuilder(Keyword keyword, @Nullable String reference, @Nullable Object value) {
return new GroupOperationBuilder(this, new Operation(keyword, null, reference, value));
}
Expand Down Expand Up @@ -465,12 +477,16 @@ public String toString() {

static class Operation implements AggregationOperation {

private final Keyword op;
private final @Nullable Keyword op;
private final @Nullable String key;
private final @Nullable String reference;
private final @Nullable Object value;

public Operation(Keyword op, @Nullable String key, @Nullable String reference, @Nullable Object value) {
Operation(AggregationExpression expression) {
this(null, null, null, expression);
}

public Operation(@Nullable Keyword op, @Nullable String key, @Nullable String reference, @Nullable Object value) {

this.op = op;
this.key = key;
Expand All @@ -487,7 +503,12 @@ public ExposedField asField() {
}

public Document toDocument(AggregationOperationContext context) {
return new Document(key, new Document(op.toString(), getValue(context)));

Object value = getValue(context);
if(op == null && value instanceof Document) {
return new Document(key, value);
}
return new Document(key, new Document(op.toString(), value));
}

public Object getValue(AggregationOperationContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.bson.Document;
import org.junit.jupiter.api.Test;

import org.springframework.data.mongodb.core.DocumentTestUtils;
import org.springframework.data.mongodb.core.query.Criteria;

Expand Down Expand Up @@ -241,6 +240,20 @@ public void sumWithNullExpressionShouldThrowException() {
.isThrownBy(() -> Aggregation.group("username").sum((AggregationExpression) null));
}

@Test // DATAMONGO-2651
void accumulatorShouldBeAllowedOnGroupOperation() {

GroupOperation groupOperation = Aggregation.group("id")
.accumulate(
ScriptOperators.accumulatorBuilder().init("inti").accumulate("acc").merge("merge").finalize("finalize"))
.as("accumulated-value");

Document groupClause = extractDocumentFromGroupOperation(groupOperation);
Document accumulatedValue = DocumentTestUtils.getAsDocument(groupClause, "accumulated-value");

assertThat(accumulatedValue.get("$accumulator")).isNotNull();
}

private Document extractDocumentFromGroupOperation(GroupOperation groupOperation) {
Document document = groupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
Document groupClause = DocumentTestUtils.getAsDocument(document, "$group");
Expand Down