Skip to content

Commit

Permalink
Add support for $tsIncrement aggregation operator.
Browse files Browse the repository at this point in the history
See #4139
Original pull request: #4182.
  • Loading branch information
christophstrobl authored and mp911de committed Oct 12, 2022
1 parent 714b23e commit 6a973b2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
Expand Up @@ -818,6 +818,21 @@ public DateFromString fromString() {
return applyTimezone(DateFromString.fromString(dateReference()), timezone);
}

/**
* Creates new {@link AggregationExpression} that returns the incrementing ordinal from a timestamp.
*
* @return new instance of {@link TsIncrement}.
* @since 4.0
*/
public TsIncrement tsIncrement() {

if(timezone != null && !Timezone.none().equals(timezone)) {
throw new IllegalArgumentException("$tsIncrement does not support timezones");
}

return TsIncrement.tsIncrement(dateReference());
}

private Object dateReference() {

if (usesFieldRef()) {
Expand Down Expand Up @@ -3182,6 +3197,63 @@ protected String getMongoMethod() {
}
}

/**
* {@link AggregationExpression} for {@code $tsIncrement}.
*
* @author Christoph Strobl
* @since 4.0
*/
public static class TsIncrement extends AbstractAggregationExpression {

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

/**
* Creates new {@link TsIncrement} that returns the incrementing ordinal from a timestamp.
*
* @param value must not be {@literal null}.
* @return new instance of {@link TsIncrement}.
* @throws IllegalArgumentException if given {@literal value} is {@literal null}.
*/
public static TsIncrement tsIncrement(Object value) {

Assert.notNull(value, "Value must not be null");
return new TsIncrement(value);
}

/**
* Creates new {@link TsIncrement} that returns the incrementing ordinal from a timestamp.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link TsIncrement}.
* @throws IllegalArgumentException if given {@literal fieldReference} is {@literal null}.
*/
public static TsIncrement tsIncrementValueOf(String fieldReference) {

Assert.notNull(fieldReference, "FieldReference must not be null");
return tsIncrement(Fields.field(fieldReference));
}

/**
* Creates new {@link TsIncrement}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link TsIncrement}.
* @throws IllegalArgumentException if given {@literal expression} is {@literal null}.
*/
public static TsIncrement tsIncrementValueOf(AggregationExpression expression) {

Assert.notNull(expression, "Expression must not be null");
return tsIncrement(expression);
}

@Override
protected String getMongoMethod() {
return "$tsIncrement";
}
}

/**
* Interface defining a temporal unit for date operators.
*
Expand Down
Expand Up @@ -191,6 +191,7 @@ public class MethodReferenceNode extends ExpressionNode {
map.put("isoDayOfWeek", singleArgRef().forOperator("$isoDayOfWeek"));
map.put("isoWeek", singleArgRef().forOperator("$isoWeek"));
map.put("isoWeekYear", singleArgRef().forOperator("$isoWeekYear"));
map.put("tsIncrement", singleArgRef().forOperator("$tsIncrement"));

// CONDITIONAL OPERATORS
map.put("cond", mapArgRef().forOperator("$cond") //
Expand Down
Expand Up @@ -24,8 +24,6 @@
import java.util.TimeZone;

import org.junit.jupiter.api.Test;

import org.springframework.data.mongodb.core.aggregation.DateOperators.TemporalUnit;
import org.springframework.data.mongodb.core.aggregation.DateOperators.Timezone;

/**
Expand Down Expand Up @@ -118,4 +116,18 @@ void rendersDateTruncWithTimezone() {
assertThat(DateOperators.zonedDateOf("purchaseDate", Timezone.valueOf("America/Chicago")).truncate("week").binSize(2).startOfWeek(DayOfWeek.MONDAY).toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $dateTrunc: { date: \"$purchaseDate\", unit: \"week\", binSize: 2, startOfWeek : \"monday\", timezone : \"America/Chicago\" } }");
}

@Test // GH-4139
void rendersTsIncrement() {

assertThat(DateOperators.dateOf("saleTimestamp").tsIncrement().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(
"{ $tsIncrement: \"$saleTimestamp\" }");
}

@Test // GH-4139
void tsIncrementErrorsOnTimezone() {

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> DateOperators.zonedDateOf("purchaseDate", Timezone.valueOf("America/Chicago")).tsIncrement());
}
}
Expand Up @@ -1240,6 +1240,11 @@ void shouldRenderSortArray() {
assertThat(transform(
"sortArray(team, new org.bson.Document(\"name\" , 1))")).isEqualTo("{ $sortArray : { input : \"$team\", sortBy : {\"name\" : 1 } }}");
}

@Test // GH-4139
void shouldTsIncrement() {
assertThat(transform("tsIncrement(saleTimestamp)")).isEqualTo("{ $tsIncrement: \"$saleTimestamp\" }");
}

private Document transform(String expression, Object... params) {
return (Document) transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
Expand Down

0 comments on commit 6a973b2

Please sign in to comment.