-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
CSHARP-4872: Add support for Append in aggregate expressions. #1569
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* 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 AppendOrPrependMethodToAggregationExpressionTranslator | ||
{ | ||
private static readonly MethodInfo[] __appendOrPrependMethods = | ||
{ | ||
EnumerableMethod.Append, | ||
EnumerableMethod.Prepend, | ||
QueryableMethod.Append, | ||
QueryableMethod.Prepend | ||
}; | ||
|
||
private static readonly MethodInfo[] __appendMethods = | ||
{ | ||
EnumerableMethod.Append, | ||
QueryableMethod.Append | ||
}; | ||
|
||
public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression) | ||
{ | ||
var method = expression.Method; | ||
var arguments = expression.Arguments; | ||
|
||
if (method.IsOneOf(__appendOrPrependMethods)) | ||
{ | ||
var sourceExpression = arguments[0]; | ||
var elementExpression = arguments[1]; | ||
|
||
var sourceTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, sourceExpression); | ||
NestedAsQueryableHelper.EnsureQueryableMethodHasNestedAsQueryableSource(expression, sourceTranslation); | ||
var itemSerializer = ArraySerializerHelper.GetItemSerializer(sourceTranslation.Serializer); | ||
|
||
AggregationExpression elementTranslation; | ||
if (elementExpression is ConstantExpression elementConstantExpression) | ||
{ | ||
var value = elementConstantExpression.Value; | ||
var serializedValue = SerializationHelper.SerializeValue(itemSerializer, value); | ||
elementTranslation = new AggregationExpression(elementExpression, AstExpression.Constant(serializedValue), itemSerializer); | ||
} | ||
else | ||
{ | ||
elementTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, elementExpression); | ||
if (!elementTranslation.Serializer.Equals(itemSerializer)) | ||
{ | ||
throw new ExpressionNotSupportedException(expression, because: "argument serializers are not compatible"); | ||
} | ||
} | ||
|
||
var ast = method.IsOneOf(__appendMethods) ? | ||
AstExpression.ConcatArrays(sourceTranslation.Ast, AstExpression.ComputedArray(elementTranslation.Ast)) : | ||
AstExpression.ConcatArrays(AstExpression.ComputedArray(elementTranslation.Ast), sourceTranslation.Ast); | ||
var serializer = NestedAsQueryableSerializer.CreateIEnumerableOrNestedAsQueryableSerializer(expression.Type, itemSerializer); | ||
|
||
return new AggregationExpression(expression, ast, serializer); | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I be able to Append There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but... The compiler will insert a |
||
{ | ||
[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); | ||
} | ||
|
||
[Theory] | ||
[ParameterAttributeData] | ||
public void Prepend_constant_should_work( | ||
[Values(false, true)] bool withNestedAsQueryable) | ||
{ | ||
var collection = GetCollection(); | ||
|
||
var queryable = withNestedAsQueryable ? | ||
collection.AsQueryable().Select(x => x.A.AsQueryable().Prepend(4).ToList()) : | ||
collection.AsQueryable().Select(x => x.A.Prepend(4).ToList()); | ||
|
||
var stages = Translate(collection, queryable); | ||
AssertStages(stages, "{ $project : { _v : { $concatArrays : [[4], '$A'] }, _id : 0 } }"); | ||
|
||
var result = queryable.Single(); | ||
result.Should().Equal(4, 1, 2, 3); | ||
} | ||
|
||
[Theory] | ||
[ParameterAttributeData] | ||
public void Prepend_expression_should_work( | ||
[Values(false, true)] bool withNestedAsQueryable) | ||
{ | ||
var collection = GetCollection(); | ||
|
||
var queryable = withNestedAsQueryable ? | ||
collection.AsQueryable().Select(x => x.A.AsQueryable().Prepend(x.B).ToList()) : | ||
collection.AsQueryable().Select(x => x.A.Prepend(x.B).ToList()); | ||
|
||
var stages = Translate(collection, queryable); | ||
AssertStages(stages, "{ $project : { _v : { $concatArrays : [['$B'], '$A'] }, _id : 0 } }"); | ||
|
||
var result = queryable.Single(); | ||
result.Should().Equal(4, 1, 2, 3); | ||
} | ||
|
||
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; } | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check for compatible serializers instead? For example if collection is of
long
and appended item isint
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should never happen. By the time we get here both serializers will always have the exact same
ValueType
.The only question is whether they are serialized the same way.