-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-4872: Add support for Append in aggregate expressions.
- Loading branch information
Showing
6 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...nExpressionTranslators/MethodTranslators/AppendMethodToAggregationExpressionTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
using System.Linq.Expressions; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators | ||
{ | ||
internal static class AppendMethodToAggregationExpressionTranslator | ||
{ | ||
public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression) | ||
{ | ||
if (EnumerableAppendMethodToAggregationExpressionTranslator.CanTranslate(expression)) | ||
{ | ||
return EnumerableAppendMethodToAggregationExpressionTranslator.Translate(context, expression); | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...nTranslators/MethodTranslators/EnumerableAppendMethodToAggregationExpressionTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Reflection; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Serializers; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators | ||
{ | ||
internal static class EnumerableAppendMethodToAggregationExpressionTranslator | ||
{ | ||
private static readonly MethodInfo[] __appendMethods = | ||
{ | ||
EnumerableMethod.Append, | ||
QueryableMethod.Append | ||
}; | ||
|
||
public static bool CanTranslate(MethodCallExpression expression) | ||
=> expression.Method.IsOneOf(__appendMethods); | ||
|
||
public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression) | ||
{ | ||
var method = expression.Method; | ||
var arguments = expression.Arguments; | ||
|
||
if (method.IsOneOf(__appendMethods)) | ||
{ | ||
var firstExpression = arguments[0]; | ||
var secondExpression = arguments[1]; | ||
|
||
var firstTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, firstExpression); | ||
NestedAsQueryableHelper.EnsureQueryableMethodHasNestedAsQueryableSource(expression, firstTranslation); | ||
var itemSerializer = ArraySerializerHelper.GetItemSerializer(firstTranslation.Serializer); | ||
|
||
AggregationExpression secondTranslation; | ||
if (secondExpression is ConstantExpression secondConstantExpression) | ||
{ | ||
var value = secondConstantExpression.Value; | ||
var serializedValue = SerializationHelper.SerializeValue(itemSerializer, value); | ||
secondTranslation = new AggregationExpression(secondExpression, AstExpression.Constant(serializedValue), itemSerializer); | ||
} | ||
else | ||
{ | ||
secondTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, secondExpression); | ||
if (!secondTranslation.Serializer.Equals(itemSerializer)) | ||
{ | ||
throw new ExpressionNotSupportedException(expression, because: "argument serializers are not compatible"); | ||
} | ||
} | ||
|
||
var ast = AstExpression.ConcatArrays(firstTranslation.Ast, AstExpression.ComputedArray(secondTranslation.Ast)); | ||
var serializer = NestedAsQueryableSerializer.CreateIEnumerableOrNestedAsQueryableSerializer(expression.Type, itemSerializer); | ||
|
||
return new AggregationExpression(expression, ast, serializer); | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4872Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
using System.Linq; | ||
using FluentAssertions; | ||
using MongoDB.TestHelpers.XunitExtensions; | ||
using Xunit; | ||
|
||
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira | ||
{ | ||
public class CSharp4872Tests : Linq3IntegrationTest | ||
{ | ||
[Theory] | ||
[ParameterAttributeData] | ||
public void Append_constant_should_work( | ||
[Values(false, true)] bool withNestedAsQueryable) | ||
{ | ||
var collection = GetCollection(); | ||
|
||
var queryable = withNestedAsQueryable ? | ||
collection.AsQueryable().Select(x => x.A.AsQueryable().Append(4).ToList()) : | ||
collection.AsQueryable().Select(x => x.A.Append(4).ToList()); | ||
|
||
var stages = Translate(collection, queryable); | ||
AssertStages(stages, "{ $project : { _v : { $concatArrays : ['$A', [4]] }, _id : 0 } }"); | ||
|
||
var result = queryable.Single(); | ||
result.Should().Equal(1, 2, 3, 4); | ||
} | ||
|
||
[Theory] | ||
[ParameterAttributeData] | ||
public void Append_expression_should_work( | ||
[Values(false, true)] bool withNestedAsQueryable) | ||
{ | ||
var collection = GetCollection(); | ||
|
||
var queryable = withNestedAsQueryable ? | ||
collection.AsQueryable().Select(x => x.A.AsQueryable().Append(x.B).ToList()) : | ||
collection.AsQueryable().Select(x => x.A.Append(x.B).ToList()); | ||
|
||
var stages = Translate(collection, queryable); | ||
AssertStages(stages, "{ $project : { _v : { $concatArrays : ['$A', ['$B']] }, _id : 0 } }"); | ||
|
||
var result = queryable.Single(); | ||
result.Should().Equal(1, 2, 3, 4); | ||
} | ||
|
||
private IMongoCollection<C> GetCollection() | ||
{ | ||
var collection = GetCollection<C>("test"); | ||
CreateCollection( | ||
collection, | ||
new C { Id = 1, A = [1, 2, 3], B = 4 }); | ||
return collection; | ||
} | ||
|
||
private class C | ||
{ | ||
public int Id { get; set; } | ||
public int[] A { get; set; } | ||
public int B { get; set; } | ||
} | ||
} | ||
} |