Skip to content

Add support for $integral aggregation operator. #3746

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.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3721-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.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3721-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.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3721-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.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3721-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ public Floor floor() {
return usesFieldRef() ? Floor.floorValueOf(fieldReference) : Floor.floorValueOf(expression);
}

/**
* Creates new {@link AggregationExpression} that calculates the approximation for the mathematical integral value.
*
* @return new instance of {@link Integral}.
* @since 3.3
*/
public Integral integral() {
return usesFieldRef() ? Integral.integralOf(fieldReference) : Integral.integralOf(expression);
}

/**
* Creates new {@link AggregationExpression} that calculates the approximation for the mathematical integral value.
*
* @param unit the unit of measure.
* @return new instance of {@link Integral}.
* @since 3.3
*/
public Integral integral(String unit) {
return integral().unit(unit);
}

/**
* Creates new {@link AggregationExpression} that calculates the natural logarithm ln (i.e loge) of the assoicated
* number.
Expand Down Expand Up @@ -1665,4 +1686,52 @@ protected String getMongoMethod() {
return "$round";
}
}

/**
* Value object to represent an {@link AggregationExpression expression} that calculates the approximation for the mathematical integral value.
*
* @author Christoph Strobl
* @since 3.3
*/
public static class Integral extends AbstractAggregationExpression {

private Integral(Object value) {
super(value);
}

/**
* Create a new instance of {@link Integral} for the value stored at the given field holding a numeric value.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link Integral}.
*/
public static Integral integralOf(String fieldReference) {
return new Integral(Collections.singletonMap("input", Fields.field(fieldReference)));
}

/**
* Create a new instance of {@link Integral} for the value provided by the given expression that resolves to a numeric value.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Integral}.
*/
public static Integral integralOf(AggregationExpression expression) {
return new Integral(Collections.singletonMap("input", expression));
}

/**
* Set the unit of measure.
*
* @param unit the unit of measure.
* @return new instance of {@link Integral}.
*/
public Integral unit(String unit) {
return new Integral(append("unit", unit));
}

@Override
protected String getMongoMethod() {
return "$integral";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class MethodReferenceNode extends ExpressionNode {
map.put("subtract", arrayArgRef().forOperator("$subtract"));
map.put("trunc", singleArgRef().forOperator("$trunc"));
map.put("round", arrayArgRef().forOperator("$round"));
map.put("integral", mapArgRef().forOperator("$integral").mappingParametersTo("input", "unit"));

// STRING OPERATORS
map.put("concat", arrayArgRef().forOperator("$concat"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ void roundShouldWithPlaceFromExpression() {
.toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(new Document("$round", Arrays.asList("$field", new Document("$first", "$source"))));
}

@Test // GH-3721
void rendersIntegral() {
assertThat(valueOf("kilowatts").integral().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(Document.parse("{ $integral : { input : \"$kilowatts\" } }"));
}

@Test // GH-3721
void rendersIntegralWithUnit() {
assertThat(valueOf("kilowatts").integral("hour").toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(Document.parse("{ $integral : { input : \"$kilowatts\", unit : \"hour\" } }"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,16 @@ public void shouldRenderRoundWithPlace() {
assertThat(transform("round(field, 2)")).isEqualTo(Document.parse("{ \"$round\" : [\"$field\", 2]}"));
}

@Test // GH-3721
public void shouldIntegral() {
assertThat(transform("integral(field)")).isEqualTo(Document.parse("{ \"$integral\" : { \"input\" : \"$field\" }}"));
}

@Test // GH-3721
public void shouldIntegralWithUnit() {
assertThat(transform("integral(field, 'hour')")).isEqualTo(Document.parse("{ \"$integral\" : { \"input\" : \"$field\", \"unit\" : \"hour\" }}"));
}

private Object transform(String expression, Object... params) {
Object result = transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
return result == null ? null : (!(result instanceof org.bson.Document) ? result.toString() : result);
Expand Down