Skip to content

Avoid multiple converter calls when parsing string aggregation stages. #4717

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>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4712-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>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4712-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 @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4712-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 @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4712-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ protected List<AggregationOperation> parseAggregationPipeline(String[] sourcePip
}

private AggregationOperation computePipelineStage(String source, ConvertingParameterAccessor accessor) {
return ctx -> ctx.getMappedObject(bindParameters(source, accessor), getQueryMethod().getDomainClass());
return new StringAggregationOperation(source, getQueryMethod().getDomainClass(),
(it) -> bindParameters(it, accessor));
}

protected Document decode(String source, ParameterBindingContext bindingContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,8 @@ protected Mono<List<AggregationOperation>> parseAggregationPipeline(String[] pip
private Mono<AggregationOperation> computePipelineStage(String source, MongoParameterAccessor accessor,
ParameterBindingDocumentCodec codec) {

return expressionEvaluator(source, accessor, codec).map(it -> {
return ctx -> ctx.getMappedObject(decode(it.getT1(), source, accessor, it.getT2()),
getQueryMethod().getDomainClass());
});
return expressionEvaluator(source, accessor, codec).map(
it -> new StringAggregationOperation(source, AbstractReactiveMongoQuery.this.getQueryMethod().getDomainClass(), bsonString -> AbstractReactiveMongoQuery.this.decode(it.getT1(), bsonString, accessor, it.getT2())));
}

private Mono<Tuple2<SpELExpressionEvaluator, ParameterBindingDocumentCodec>> expressionEvaluator(String source,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository.query;

import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;

/**
* @author Christoph Strobl
*/
class StringAggregationOperation implements AggregationOperation {

private static final Pattern OPERATOR_PATTERN = Pattern.compile("\\$\\w+");

private final String source;
private final Class<?> domainType;
private final Function<String, Document> bindFunction;

StringAggregationOperation(String source, Class<?> domainType, Function<String, Document> bindFunction) {

this.source = source;
this.domainType = domainType;
this.bindFunction = bindFunction;
}

@Override
public Document toDocument(AggregationOperationContext context) {
return context.getMappedObject(bindFunction.apply(source), domainType);
}

@Override
public String getOperator() {

Matcher matcher = OPERATOR_PATTERN.matcher(source);
if (matcher.find()) {
return matcher.group();
}
return AggregationOperation.super.getOperator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,9 @@ private BindableValue bindableValueFor(JsonToken token) {
if (matcher.find()) {

int index = computeParameterIndex(matcher.group());
bindableValue.setValue(getBindableValueForIndex(index));
bindableValue.setType(bsonTypeForValue(getBindableValueForIndex(index)));
Object bindableValueForIndex = getBindableValueForIndex(index);
bindableValue.setValue(bindableValueForIndex);
bindableValue.setType(bsonTypeForValue(bindableValueForIndex));
return bindableValue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository.query;

import static org.assertj.core.api.Assertions.assertThat;

import org.assertj.core.api.Assertions;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* @author Christoph Strobl
*/
public class StringBasedAggregationOperationUnitTests {

@ParameterizedTest // GH-4712
@ValueSource(strings = { "$project", "'$project'", "\"$project\"" })
void extractsAggregationOperatorFromAggregationStringWithoutBindingParameters(String operator) {

StringAggregationOperation agg = new StringAggregationOperation("{ %s : { 'fn' : 1 } }".formatted(operator),
Object.class, (it) -> Assertions.fail("o_O Parameter binding"));

assertThat(agg.getOperator()).isEqualTo("$project");
}

@Test
// GH-4712
void fallbackToParameterBindingIfAggregationOperatorCannotBeExtractedFromAggregationStringWithoutBindingParameters() {

StringAggregationOperation agg = new StringAggregationOperation("{ happy-madison : { 'fn' : 1 } }", Object.class,
(it) -> new Document("$project", ""));

assertThat(agg.getOperator()).isEqualTo("$project");
}
}
Loading