Skip to content

DATAMONGO-1538 - Add support for $let to $project aggregation. #417

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 3 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>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1538-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1538-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1538-SNAPSHOT</version>
</dependency>

<dependency>
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 @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1538-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1538-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>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1538-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -25,8 +26,8 @@
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Cond.OtherwiseBuilder;
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Cond.ThenBuilder;
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Filter.AsBuilder;
import org.springframework.data.mongodb.core.aggregation.AggregationExpressions.Let.ExpressionVariable;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -1986,6 +1987,28 @@ public static Map.AsBuilder mapItemsOf(String fieldReference) {
public static Map.AsBuilder mapItemsOf(AggregationExpression expression) {
return Map.itemsOf(expression);
}

/**
* Start creating new {@link Let} that allows definition of {@link ExpressionVariable} that can be used within a
* nested {@link AggregationExpression}.
*
* @param variables must not be {@literal null}.
* @return
*/
public static Let.LetBuilder define(ExpressionVariable... variables) {
return Let.define(variables);
}

/**
* Start creating new {@link Let} that allows definition of {@link ExpressionVariable} that can be used within a
* nested {@link AggregationExpression}.
*
* @param variables must not be {@literal null}.
* @return
*/
public static Let.LetBuilder define(Collection<ExpressionVariable> variables) {
return Let.define(variables);
}
}

/**
Expand Down Expand Up @@ -3881,31 +3904,19 @@ public static AsBuilder filter(List<?> values) {
*/
@Override
public DBObject toDbObject(final AggregationOperationContext context) {

return toFilter(new ExposedFieldsAggregationOperationContext(ExposedFields.from(as), context) {

@Override
public FieldReference getReference(Field field) {

FieldReference ref = null;
try {
ref = context.getReference(field);
} catch (Exception e) {
// just ignore that one.
}
return ref != null ? ref : super.getReference(field);
}
});
return toFilter(ExposedFields.from(as), context);
}

private DBObject toFilter(AggregationOperationContext context) {
private DBObject toFilter(ExposedFields exposedFields, AggregationOperationContext context) {

DBObject filterExpression = new BasicDBObject();
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
exposedFields, context);

filterExpression.putAll(context.getMappedObject(new BasicDBObject("input", getMappedInput(context))));
filterExpression.put("as", as.getTarget());

filterExpression.putAll(context.getMappedObject(new BasicDBObject("cond", getMappedCondition(context))));
filterExpression.putAll(context.getMappedObject(new BasicDBObject("cond", getMappedCondition(operationContext))));

return new BasicDBObject("$filter", filterExpression);
}
Expand Down Expand Up @@ -5995,27 +6006,14 @@ public Map andApply(final AggregationExpression expression) {

@Override
public DBObject toDbObject(final AggregationOperationContext context) {

return toMap(new ExposedFieldsAggregationOperationContext(
ExposedFields.synthetic(Fields.fields(itemVariableName)), context) {

@Override
public FieldReference getReference(Field field) {

FieldReference ref = null;
try {
ref = context.getReference(field);
} catch (Exception e) {
// just ignore that one.
}
return ref != null ? ref : super.getReference(field);
}
});
return toMap(ExposedFields.synthetic(Fields.fields(itemVariableName)), context);
}

private DBObject toMap(AggregationOperationContext context) {
private DBObject toMap(ExposedFields exposedFields, AggregationOperationContext context) {

BasicDBObject map = new BasicDBObject();
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
exposedFields, context);

BasicDBObject input;
if (sourceArray instanceof Field) {
Expand All @@ -6026,7 +6024,8 @@ private DBObject toMap(AggregationOperationContext context) {

map.putAll(context.getMappedObject(input));
map.put("as", itemVariableName);
map.put("in", functionToApply.toDbObject(new NestedDelegatingExpressionAggregationOperationContext(context)));
map.put("in",
functionToApply.toDbObject(new NestedDelegatingExpressionAggregationOperationContext(operationContext)));

return new BasicDBObject("$map", map);
}
Expand Down Expand Up @@ -6696,4 +6695,173 @@ public Cond otherwiseValueOf(AggregationExpression expression) {
}
}
}

/**
* {@link AggregationExpression} for {@code $let} that binds {@link AggregationExpression} to variables for use in the
* specified {@code in} expression, and returns the result of the expression.
*
* @author Christoph Strobl
* @since 1.10
*/
class Let implements AggregationExpression {

private final List<ExpressionVariable> vars;
private final AggregationExpression expression;

private Let(List<ExpressionVariable> vars, AggregationExpression expression) {

this.vars = vars;
this.expression = expression;
}

/**
* Start creating new {@link Let} by defining the variables for {@code $vars}.
*
* @param variables must not be {@literal null}.
* @return
*/
public static LetBuilder define(final Collection<ExpressionVariable> variables) {

Assert.notNull(variables, "Variables must not be null!");

return new LetBuilder() {
@Override
public Let andApply(final AggregationExpression expression) {

Assert.notNull(expression, "Expression must not be null!");
return new Let(new ArrayList<ExpressionVariable>(variables), expression);
}
};
}

/**
* Start creating new {@link Let} by defining the variables for {@code $vars}.
*
* @param variables must not be {@literal null}.
* @return
*/
public static LetBuilder define(final ExpressionVariable... variables) {

Assert.notNull(variables, "Variables must not be null!");

return new LetBuilder() {
@Override
public Let andApply(final AggregationExpression expression) {

Assert.notNull(expression, "Expression must not be null!");
return new Let(Arrays.asList(variables), expression);
}
};
}

public interface LetBuilder {

/**
* Define the {@link AggregationExpression} to evaluate.
*
* @param expression must not be {@literal null}.
* @return
*/
Let andApply(AggregationExpression expression);
}

@Override
public DBObject toDbObject(final AggregationOperationContext context) {
return toLet(ExposedFields.synthetic(Fields.fields(getVariableNames())), context);
}

private String[] getVariableNames() {

String[] varNames = new String[this.vars.size()];
for (int i = 0; i < this.vars.size(); i++) {
varNames[i] = this.vars.get(i).variableName;
}

return varNames;
}

private DBObject toLet(ExposedFields exposedFields, AggregationOperationContext context) {

DBObject letExpression = new BasicDBObject();
DBObject mappedVars = new BasicDBObject();
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
exposedFields, context);

for (ExpressionVariable var : this.vars) {
mappedVars.putAll(getMappedVariable(var, context));
}

letExpression.put("vars", mappedVars);
letExpression.put("in", getMappedIn(operationContext));

return new BasicDBObject("$let", letExpression);
}

private DBObject getMappedVariable(ExpressionVariable var, AggregationOperationContext context) {

return new BasicDBObject(var.variableName, var.expression instanceof AggregationExpression
? ((AggregationExpression) var.expression).toDbObject(context) : var.expression);
}

private Object getMappedIn(AggregationOperationContext context) {
return expression.toDbObject(new NestedDelegatingExpressionAggregationOperationContext(context));
}

/**
* @author Christoph Strobl
*/
public static class ExpressionVariable {

private final String variableName;
private final Object expression;

/**
* Creates new {@link ExpressionVariable}.
*
* @param variableName can be {@literal null}.
* @param expression can be {@literal null}.
*/
private ExpressionVariable(String variableName, Object expression) {

this.variableName = variableName;
this.expression = expression;
}

/**
* Create a new {@link ExpressionVariable} with given name.
*
* @param variableName must not be {@literal null}.
* @return never {@literal null}.
*/
public static ExpressionVariable newVariable(String variableName) {

Assert.notNull(variableName, "VariableName must not be null!");
return new ExpressionVariable(variableName, null);
}

/**
* Create a new {@link ExpressionVariable} with current name and given {@literal expression}.
*
* @param expression must not be {@literal null}.
* @return never {@literal null}.
*/
public ExpressionVariable forExpression(AggregationExpression expression) {

Assert.notNull(expression, "Expression must not be null!");
return new ExpressionVariable(variableName, expression);
}

/**
* Create a new {@link ExpressionVariable} with current name and given {@literal expressionObject}.
*
* @param expressionObject must not be {@literal null}.
* @return never {@literal null}.
*/
public ExpressionVariable forExpression(DBObject expressionObject) {

Assert.notNull(expressionObject, "Expression must not be null!");
return new ExpressionVariable(variableName, expressionObject);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@Deprecated
public enum AggregationFunctionExpressions {

SIZE, CMP, EQ, GT, GTE, LT, LTE, NE, SUBTRACT, ADD;
SIZE, CMP, EQ, GT, GTE, LT, LTE, NE, SUBTRACT, ADD, MULTIPLY;

/**
* Returns an {@link AggregationExpression} build from the current {@link Enum} name and the given parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.data.mongodb.core.aggregation;

import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.util.Assert;

/**
* {@link ExposedFieldsAggregationOperationContext} that inherits fields from its parent
* {@link AggregationOperationContext}.
*
* @author Mark Paluch
* @since 1.9
*/
class InheritingExposedFieldsAggregationOperationContext extends ExposedFieldsAggregationOperationContext {

Expand All @@ -40,7 +39,7 @@ public InheritingExposedFieldsAggregationOperationContext(ExposedFields exposedF
AggregationOperationContext previousContext) {

super(exposedFields, previousContext);
Assert.notNull(previousContext, "PreviousContext must not be null!");

this.previousContext = previousContext;
}

Expand Down
Loading