Skip to content

Add support for $derivative aggregation operator. #3742

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-3716-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-3716-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-3716-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-3716-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.StdDevPop;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.StdDevSamp;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Sum;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Gateway to {@literal Arithmetic} aggregation operations that perform math operations on numbers.
Expand Down Expand Up @@ -532,6 +534,31 @@ public Round roundToPlace(int place) {
return round().place(place);
}

/**
* Creates new {@link AggregationExpression} that calculates the mathematical derivative value.
*
* @return new instance of {@link Derivative}.
* @since 3.3
*/
public Derivative derivative() {
return derivative(null);
}

/**
* Creates new {@link AggregationExpression} that calculates the mathematical derivative value.
*
* @param unit The time unit ({@literal week, day, hour, minute, second, millisecond}) to apply can be
* {@literal null}.
* @return new instance of {@link Derivative}.
* @since 3.3
*/
public Derivative derivative(@Nullable String unit) {

Derivative derivative = usesFieldRef() ? Derivative.derivativeOf(fieldReference)
: Derivative.derivativeOf(expression);
return StringUtils.hasText(unit) ? derivative.unit(unit) : derivative;
}

private boolean usesFieldRef() {
return fieldReference != null;
}
Expand Down Expand Up @@ -1665,4 +1692,32 @@ protected String getMongoMethod() {
return "$round";
}
}

public static class Derivative extends AbstractAggregationExpression {

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

public static Derivative derivativeOf(String fieldReference) {
return new Derivative(Collections.singletonMap("input", Fields.field(fieldReference)));
}

public static Derivative derivativeOf(AggregationExpression expression) {
return new Derivative(Collections.singletonMap("input", expression));
}

public static Derivative derivativeOfValue(Number value) {
return new Derivative(Collections.singletonMap("input", value));
}

public Derivative unit(String unit) {
return new Derivative(append("unit", unit));
}

@Override
protected String getMongoMethod() {
return "$derivative";
}
}
}
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("derivative", mapArgRef().forOperator("$derivative").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,11 @@ void roundShouldWithPlaceFromExpression() {
.toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(new Document("$round", Arrays.asList("$field", new Document("$first", "$source"))));
}

@Test // GH-3716
void rendersDerivativeCorrectly() {

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

@Test // GH-3716
void shouldRenderDerivative() {
assertThat(transform("derivative(miles, 'hour')")).isEqualTo(Document.parse("{ \"$derivative\" : { input : '$miles', 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