Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2010-present MongoDB Inc.
/* 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.
Expand Down Expand Up @@ -95,16 +95,20 @@ public static TranslatedExpression Translate(TranslationContext context, MethodC
var sourceAst = sourceTranslation.Ast;
var itemSerializer = ArraySerializerHelper.GetItemSerializer(sourceTranslation.Serializer);

var isFirstMethod = method.Name.StartsWith("First");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general we prefer to test which method it is like this:

var isFirstMethod = method.IsOneOf(__firstMethods);

It is less likely to have false matches than string matching (partial or otherwise).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it this way since I reasoned that if we passed the initial if statement:
if (method.IsOneOf(__firstOrLastMethods)) then we already know that the method has matched one of the first methods so no need to test the methods again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The statement you make is true, but even so we prefer to stick to method.IsOneOf instead of string matching.

The only time we don't use method.IsOneOf is when we are translating a method that might be implemented by classes we don't even know about (e.g. CompareTo).


if (method.IsOneOf(__withPredicateMethods))
{
var predicateLambda = ExpressionHelper.UnquoteLambdaIfQueryableMethod(method, arguments[1]);
var parameterExpression = predicateLambda.Parameters.Single();
var parameterSymbol = context.CreateSymbol(parameterExpression, itemSerializer, isCurrent: false);
var predicateTranslation = ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(context, predicateLambda, parameterSymbol);
var limit = isFirstMethod ? AstExpression.Constant(1) : null;
sourceAst = AstExpression.Filter(
input: sourceAst,
cond: predicateTranslation.Ast,
@as: parameterSymbol.Var.Name);
@as: parameterSymbol.Var.Name,
limit: limit);
}

AstExpression ast;
Expand All @@ -119,11 +123,11 @@ public static TranslatedExpression Translate(TranslationContext context, MethodC
@in: AstExpression.Cond(
@if: AstExpression.Eq(AstExpression.Size(valuesAst), 0),
then: serializedDefaultValue,
@else: method.IsOneOf(__firstMethods) ? AstExpression.First(valuesAst) : AstExpression.Last(valuesAst)));
@else: isFirstMethod ? AstExpression.First(valuesAst) : AstExpression.Last(valuesAst)));
}
else
{
ast = method.Name == "First" ? AstExpression.First(sourceAst) : AstExpression.Last(sourceAst);
ast = isFirstMethod ? AstExpression.First(sourceAst) : AstExpression.Last(sourceAst);
}

return new TranslatedExpression(expression, ast, itemSerializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void SelectMany_should_translate_correctly()
Meta : '$$meta',
Property : {
$arrayElemAt : [
{ $filter : { input : '$Properties', as : 'p', cond : { $eq : ['$$p._id', '$$meta.PropertyId'] } } },
{ $filter : { input : '$Properties', as : 'p', cond : { $eq : ['$$p._id', '$$meta.PropertyId'] }, limit : 1 } },
0
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void List_get_Item_of_scalar_should_work()
var expectedStages = new[]
{
"{ $group : { _id : '$_id', _elements : { $push: '$X' } } }",
"{ $project : { _id : '$_id' Result : { $arrayElemAt : ['$_elements', 0] } } }",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How was this test not failing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea. I just happened to catch it.

"{ $project : { _id : '$_id', Result : { $arrayElemAt : ['$_elements', 0] } } }",
"{ $sort : { _id : 1 } }"
};
AssertStages(stages, expectedStages);
Expand Down Expand Up @@ -1043,7 +1043,7 @@ public void IGrouping_First_with_predicate_of_root_should_work()
var expectedStages = new[]
{
"{ $group : { _id : '$_id', _elements : { $push : '$$ROOT' } } }",
"{ $project : { _id : '$_id', Result : { $arrayElemAt : [{ $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e.X', 1] } } }, 0] } } }",
"{ $project : { _id : '$_id', Result : { $arrayElemAt : [{ $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e.X', 1] }, limit : 1 } }, 0] } } }",
"{ $sort : { _id : 1 } }"
};
AssertStages(stages, expectedStages);
Expand All @@ -1068,7 +1068,7 @@ public void IGrouping_First_with_predicate_of_scalar_should_work()
var expectedStages = new[]
{
"{ $group : { _id : '$_id', _elements : { $push : '$X' } } }",
"{ $project : { _id : '$_id', Result : { $arrayElemAt : [{ $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e', 1] } } }, 0] } } }",
"{ $project : { _id : '$_id', Result : { $arrayElemAt : [{ $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e', 1] }, limit : 1 } }, 0] } } }",
"{ $sort : { _id : 1 } }"
};
AssertStages(stages, expectedStages);
Expand Down Expand Up @@ -1143,7 +1143,7 @@ public void IGrouping_FirstOrDefault_with_predicate_of_root_should_work()
var expectedStages = new[]
{
"{ $group : { _id : '$_id', _elements : { $push : '$$ROOT' } } }",
"{ $project : { _id : '$_id', Result : { $let : { vars : { values : { $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e.X', 1] } } } }, in : { $cond : { if : { $eq : [{ $size : '$$values' }, 0] }, then : null, else : { $arrayElemAt : ['$$values', 0] } } } } } } }",
"{ $project : { _id : '$_id', Result : { $let : { vars : { values : { $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e.X', 1] }, limit : 1 } } }, in : { $cond : { if : { $eq : [{ $size : '$$values' }, 0] }, then : null, else : { $arrayElemAt : ['$$values', 0] } } } } } } }",
"{ $sort : { _id : 1 } }"
};
AssertStages(stages, expectedStages);
Expand All @@ -1169,7 +1169,7 @@ public void IGrouping_FirstOrDefault_with_predicate_of_scalar_should_work()
var expectedStages = new[]
{
"{ $group : { _id : '$_id', _elements : { $push : '$X' } } }",
"{ $project : { _id : '$_id', Result : { $let : { vars : { values : { $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e', 1] } } } }, in : { $cond : { if : { $eq : [{ $size : '$$values' }, 0] }, then : 0, else : { $arrayElemAt : ['$$values', 0] } } } } } } }",
"{ $project : { _id : '$_id', Result : { $let : { vars : { values : { $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e', 1] }, limit : 1 } } }, in : { $cond : { if : { $eq : [{ $size : '$$values' }, 0] }, then : 0, else : { $arrayElemAt : ['$$values', 0] } } } } } } }",
"{ $sort : { _id : 1 } }"
};
AssertStages(stages, expectedStages);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void Select_First_with_predicate_should_work()
.Select(_x => _x.List.First(_y => _y > 2));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : '$List', as : 'v__0', cond : { $gt : ['$$v__0', 2] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : '$List', as : 'v__0', cond : { $gt : ['$$v__0', 2] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Should().Equal(3, 4);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2010-present MongoDB Inc.
/* 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.
Expand Down Expand Up @@ -73,7 +73,7 @@ public void DictionaryAsArrayOfArrays_Keys_First_with_predicate_should_work()
.Select(x => x.DictionaryAsArrayOfArrays.Keys.First(k => k == "b"));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : '$DictionaryAsArrayOfArrays', as : 'kvp', in : { $arrayElemAt : ['$$kvp', 0] } } }, as : 'k', cond : { $eq : ['$$k', 'b'] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : '$DictionaryAsArrayOfArrays', as : 'kvp', in : { $arrayElemAt : ['$$kvp', 0] } } }, as : 'k', cond : { $eq : ['$$k', 'b'] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand All @@ -92,7 +92,7 @@ public void DictionaryAsArrayOfArrays_Values_should_work()
.Select(x => x.DictionaryAsArrayOfArrays.Values);

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $map : { input : '$DictionaryAsArrayOfArrays', as : 'kvp' in : { k : { $arrayElemAt : ['$$kvp', 0] }, v : { $arrayElemAt : ['$$kvp', 1] } } } } , _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $map : { input : '$DictionaryAsArrayOfArrays', as : 'kvp', in : { k : { $arrayElemAt : ['$$kvp', 0] }, v : { $arrayElemAt : ['$$kvp', 1] } } } }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand All @@ -111,7 +111,7 @@ public void DictionaryAsArrayOfArrays_Values_First_with_predicate_should_work()
.Select(x => x.DictionaryAsArrayOfArrays.Values.First(v => v == 2));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $let : { vars : { this : { $arrayElemAt : [{ $filter : { input : { $map : { input : '$DictionaryAsArrayOfArrays', as : 'kvp', in : { k : { $arrayElemAt : ['$$kvp', 0] }, v : { $arrayElemAt : ['$$kvp', 1] } } } }, as : 'v', cond : { $eq : ['$$v.v', 2] } } }, 0] } }, in : '$$this.v' } }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $let : { vars : { this : { $arrayElemAt : [{ $filter : { input : { $map : { input : '$DictionaryAsArrayOfArrays', as : 'kvp', in : { k : { $arrayElemAt : ['$$kvp', 0] }, v : { $arrayElemAt : ['$$kvp', 1] } } } }, as : 'v', cond : { $eq : ['$$v.v', 2] }, limit : 1 } }, 0] } }, in : '$$this.v' } }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -149,7 +149,7 @@ public void DictionaryAsArrayOfDocuments_Keys_First_with_predicate_should_work()
.Select(x => x.DictionaryAsArrayOfDocuments.Keys.First(k => k == "b"));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : '$DictionaryAsArrayOfDocuments.k', as : 'k', cond : { $eq : ['$$k', 'b'] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : '$DictionaryAsArrayOfDocuments.k', as : 'k', cond : { $eq : ['$$k', 'b'] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -187,7 +187,7 @@ public void DictionaryAsArrayOfDocuments_Values_First_with_predicate_should_work
.Select(x => x.DictionaryAsArrayOfDocuments.Values.First(v => v == 2));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $let : { vars : { this : { $arrayElemAt : [{ $filter : { input : '$DictionaryAsArrayOfDocuments', as : 'v', cond : { $eq : ['$$v.v', 2] } } }, 0] } }, in : '$$this.v' } }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $let : { vars : { this : { $arrayElemAt : [{ $filter : { input : '$DictionaryAsArrayOfDocuments', as : 'v', cond : { $eq : ['$$v.v', 2] }, limit : 1 } }, 0] } }, in : '$$this.v' } }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -225,7 +225,7 @@ public void DictionaryAsDocument_Keys_First_with_predicate_should_work()
.Select(x => x.DictionaryAsDocument.Keys.First(k => k == "b"));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : { $objectToArray : '$DictionaryAsDocument' }, as : 'kvp', in : '$$kvp.k' } }, as : 'k', cond : { $eq : ['$$k', 'b'] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : { $objectToArray : '$DictionaryAsDocument' }, as : 'kvp', in : '$$kvp.k' } }, as : 'k', cond : { $eq : ['$$k', 'b'] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -263,7 +263,7 @@ public void DictionaryAsDocument_Values_First_with_predicate_should_work()
.Select(x => x.DictionaryAsDocument.Values.First(v => v == 2));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $let : { vars : { this : { $arrayElemAt : [{ $filter : { input : { $objectToArray : '$DictionaryAsDocument' }, as : 'v', cond : { $eq : ['$$v.v', 2] } } }, 0] } }, in : '$$this.v' } }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $let : { vars : { this : { $arrayElemAt : [{ $filter : { input : { $objectToArray : '$DictionaryAsDocument' }, as : 'v', cond : { $eq : ['$$v.v', 2] }, limit : 1 } }, 0] } }, in : '$$this.v' } }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -301,7 +301,7 @@ public void IDictionaryAsArrayOfArrays_Keys_First_with_predicate_should_work()
.Select(x => x.IDictionaryAsArrayOfArrays.Keys.First(k => k == "b"));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : '$IDictionaryAsArrayOfArrays', as : 'kvp', in : { $arrayElemAt : ['$$kvp', 0] } } }, as : 'k', cond : { $eq : ['$$k', 'b'] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : '$IDictionaryAsArrayOfArrays', as : 'kvp', in : { $arrayElemAt : ['$$kvp', 0] } } }, as : 'k', cond : { $eq : ['$$k', 'b'] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -339,7 +339,7 @@ public void IDictionaryAsArrayOfArrays_Values_First_with_predicate_should_work()
.Select(x => x.IDictionaryAsArrayOfArrays.Values.First(v => v == 2));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : '$IDictionaryAsArrayOfArrays', as : 'kvp', in : { $arrayElemAt : ['$$kvp', 1] } } }, as : 'v', cond : { $eq : ['$$v', 2] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : '$IDictionaryAsArrayOfArrays', as : 'kvp', in : { $arrayElemAt : ['$$kvp', 1] } } }, as : 'v', cond : { $eq : ['$$v', 2] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -377,7 +377,7 @@ public void IDictionaryAsArrayOfDocuments_Keys_First_with_predicate_should_work(
.Select(x => x.IDictionaryAsArrayOfDocuments.Keys.First(k => k == "b"));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : '$IDictionaryAsArrayOfDocuments.k', as : 'k', cond : { $eq : ['$$k', 'b'] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : '$IDictionaryAsArrayOfDocuments.k', as : 'k', cond : { $eq : ['$$k', 'b'] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -415,7 +415,7 @@ public void IDictionaryAsArrayOfDocuments_Values_First_with_predicate_should_wor
.Select(x => x.IDictionaryAsArrayOfDocuments.Values.First(v => v == 2));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : '$IDictionaryAsArrayOfDocuments.v', as : 'v', cond : { $eq : ['$$v', 2] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : '$IDictionaryAsArrayOfDocuments.v', as : 'v', cond : { $eq : ['$$v', 2] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -453,7 +453,7 @@ public void IDictionaryAsDocument_Keys_First_with_predicate_should_work()
.Select(x => x.IDictionaryAsDocument.Keys.First(k => k == "b"));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : { $objectToArray : '$IDictionaryAsDocument' }, as : 'kvp', in : '$$kvp.k' } }, as : 'k', cond : { $eq : ['$$k', 'b'] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : { $objectToArray : '$IDictionaryAsDocument' }, as : 'kvp', in : '$$kvp.k' } }, as : 'k', cond : { $eq : ['$$k', 'b'] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down Expand Up @@ -491,7 +491,7 @@ public void IDictionaryAsDocument_Values_First_with_predicate_should_work()
.Select(x => x.IDictionaryAsDocument.Values.First(v => v == 2));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : { $objectToArray : '$IDictionaryAsDocument' }, as : 'kvp', in : '$$kvp.v' } }, as : 'v', cond : { $eq : ['$$v', 2] } } }, 0] }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $arrayElemAt : [{ $filter : { input : { $map : { input : { $objectToArray : '$IDictionaryAsDocument' }, as : 'kvp', in : '$$kvp.v' } }, as : 'v', cond : { $eq : ['$$v', 2] }, limit : 1 } }, 0] }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand All @@ -510,7 +510,7 @@ public void DictionaryAsDocumentOfNestedDictionaryAsArrayOfArrays_Values_SelectM
.Select(x => x.DictionaryAsDocumentOfNestedDictionaryAsArrayOfArrays.Values.SelectMany(n => n.Keys));

var stages = Translate(collection, queryable);
AssertStages(stages, "{ $project : { _v : { $reduce : { input : { $map : { input : { $objectToArray : '$DictionaryAsDocumentOfNestedDictionaryAsArrayOfArrays' }, as : 'n', in : { $map : { input : '$$n.v', as : 'kvp', in : { $arrayElemAt : ['$$kvp', 0] } } } } }, initialValue : [], in : { $concatArrays : ['$$value', '$$this'] } } }, _id : 0 } }");
AssertStages(stages, "{ $project : { _v : { $reduce : { input : { $map : { input : { $objectToArray : '$DictionaryAsDocumentOfNestedDictionaryAsArrayOfArrays' }, as : 'n', in : { $map : { input : '$$n.v', as : 'kvp', in : { $arrayElemAt : ['$$kvp', 0] } } } } }, initialValue : [], in : { $concatArrays : ['$$value', '$$this'] } } }, _id : 0 } }");

var results = queryable.ToList();
results.Count.Should().Be(4);
Expand Down
Loading
Loading