Skip to content

Commit f1d98e4

Browse files
committed
add a new translator for dictionary indexer comparison cases
1 parent 4c2d0bb commit f1d98e4

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionTranslators/ComparisonExpressionToFilterTranslator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public static AstFilter Translate(TranslationContext context, BinaryExpression e
5656
return CountComparisonExpressionToFilterTranslator.Translate(context, expression, comparisonOperator, countExpression, sizeExpression);
5757
}
5858

59+
if (DictionaryIndexerComparisonExpressionToFilterTranslator.CanTranslate(leftExpression, rightExpression))
60+
{
61+
return DictionaryIndexerComparisonExpressionToFilterTranslator.Translate(context, expression, comparisonOperator, (MethodCallExpression)leftExpression, (ConstantExpression)rightExpression);
62+
}
63+
5964
if (GetTypeComparisonExpressionToFilterTranslator.CanTranslate(leftExpression, comparisonOperator, rightExpression))
6065
{
6166
return GetTypeComparisonExpressionToFilterTranslator.Translate(context, expression, (MethodCallExpression)leftExpression, comparisonOperator, (ConstantExpression)rightExpression);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Linq.Expressions;
17+
using MongoDB.Bson;
18+
using MongoDB.Bson.Serialization;
19+
using MongoDB.Bson.Serialization.Options;
20+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters;
21+
using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods;
22+
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
23+
using MongoDB.Driver.Linq.Linq3Implementation.Reflection;
24+
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators;
25+
26+
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators
27+
{
28+
internal static class DictionaryIndexerComparisonExpressionToFilterTranslator
29+
{
30+
public static bool CanTranslate(Expression leftExpression, Expression rightExpression)
31+
{
32+
return leftExpression is MethodCallExpression methodCallExpression &&
33+
rightExpression is ConstantExpression &&
34+
DictionaryMethod.IsGetItemWithKeyMethod(methodCallExpression.Method);
35+
}
36+
37+
public static AstFilter Translate(TranslationContext context, Expression containingExpression, AstComparisonFilterOperator comparisonOperator, MethodCallExpression indexerExpression, ConstantExpression valueExpression)
38+
{
39+
var dictionaryExpression = indexerExpression.Object;
40+
var keyExpression = indexerExpression.Arguments[0];
41+
42+
var fieldTranslation = ExpressionToFilterFieldTranslator.Translate(context, dictionaryExpression);
43+
44+
if (fieldTranslation.Serializer is not IBsonDictionarySerializer dictionarySerializer)
45+
{
46+
throw new ExpressionNotSupportedException(containingExpression, because: $"class {fieldTranslation.Serializer.GetType().FullName} does not implement the IBsonDictionarySerializer interface");
47+
}
48+
49+
var dictionaryRepresentation = dictionarySerializer.DictionaryRepresentation;
50+
var key = GetKeyStringConstant(containingExpression, keyExpression, dictionarySerializer.KeySerializer);
51+
var serializedValue = SerializationHelper.SerializeValue(dictionarySerializer.ValueSerializer, valueExpression, containingExpression);
52+
53+
switch (dictionaryRepresentation)
54+
{
55+
case DictionaryRepresentation.Document:
56+
{
57+
var subField = fieldTranslation.SubField(key, dictionarySerializer.ValueSerializer);
58+
return AstFilter.Compare(subField.Ast, comparisonOperator, serializedValue);
59+
}
60+
61+
case DictionaryRepresentation.ArrayOfArrays:
62+
case DictionaryRepresentation.ArrayOfDocuments:
63+
{
64+
var keyFieldName = dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays ? "0" : "k";
65+
var valueFieldName = dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays ? "1" : "v";
66+
67+
var keyField = AstFilter.Field(keyFieldName);
68+
var valueField = AstFilter.Field(valueFieldName);
69+
var keyMatchFilter = AstFilter.Eq(keyField, key);
70+
var valueMatchFilter = AstFilter.Compare(valueField, comparisonOperator, serializedValue);
71+
var combinedFilter = AstFilter.And(keyMatchFilter, valueMatchFilter);
72+
73+
return AstFilter.ElemMatch(fieldTranslation.Ast, combinedFilter);
74+
}
75+
76+
default:
77+
throw new ExpressionNotSupportedException(containingExpression, because: $"Unexpected dictionary representation: {dictionaryRepresentation}");
78+
}
79+
}
80+
81+
private static string GetKeyStringConstant(Expression expression, Expression keyExpression, IBsonSerializer keySerializer)
82+
{
83+
var key = keyExpression.GetConstantValue<object>(containingExpression: expression);
84+
var serializedKey = SerializationHelper.SerializeValue(keySerializer, key);
85+
if (serializedKey is not BsonString)
86+
{
87+
throw new ExpressionNotSupportedException(expression, because: "key did not serialize as a string");
88+
}
89+
90+
return serializedKey.AsString;
91+
}
92+
}
93+
}

tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4443Tests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ public void Where_DictionaryAsArrayOfArrays_IndexerAccess_should_work()
15561556
.Where(x => x.Dictionary["life"] == 42);
15571557

15581558
var stages = Translate(collection, queryable);
1559-
AssertStages(stages, "{ $match : { $expr : { $eq : [{ $arrayElemAt : [{ $arrayElemAt : [{ $filter : { input : '$Dictionary', as : 'kvp', cond : { $eq : [{ $arrayElemAt : ['$$kvp', 0] }, 'life'] }, limit : 1 } }, 0] }, 1] }, 42] } } }");
1559+
AssertStages(stages, "{ $match : { Dictionary : { $elemMatch : { '0' : 'life', '1' : 42 } } } }");
15601560

15611561
var result = queryable.ToList();
15621562
result.Should().ContainSingle();
@@ -1755,7 +1755,7 @@ public void Where_DictionaryAsArrayOfDocuments_IndexerAccess_should_work()
17551755
.Where(x => x.Dictionary["life"] == 42);
17561756

17571757
var stages = Translate(collection, queryable);
1758-
AssertStages(stages, "{ $match : { $expr : { $eq : [{ $let : { vars : { this : { $arrayElemAt : [{ $filter : { input : '$Dictionary', as : 'kvp', cond : { $eq : ['$$kvp.k', 'life'] }, limit : 1 } }, 0] } }, in : '$$this.v' } }, 42] } } }");
1758+
AssertStages(stages, "{ $match : { Dictionary : { $elemMatch : { k : 'life', v : 42 } } } }");
17591759

17601760
var result = queryable.ToList();
17611761
result.Should().ContainSingle();
@@ -2131,7 +2131,7 @@ public void Where_IDictionaryAsArrayOfArrays_IndexerAccess_should_work()
21312131
.Where(x => x.DictionaryInterface["life"] == 42);
21322132

21332133
var stages = Translate(collection, queryable);
2134-
AssertStages(stages, "{ $match : { $expr : { $eq : [{ $arrayElemAt : [{ $arrayElemAt : [{ $filter : { input : '$DictionaryInterface', as : 'kvp', cond : { $eq : [{ $arrayElemAt : ['$$kvp', 0] }, 'life'] }, limit : 1 } }, 0] }, 1] }, 42] } } }");
2134+
AssertStages(stages, "{ $match : { DictionaryInterface : { $elemMatch : { '0' : 'life', '1' : 42 } } } }");
21352135

21362136
var result = queryable.ToList();
21372137
result.Should().ContainSingle();
@@ -2312,7 +2312,7 @@ public void Where_IDictionaryAsArrayOfDocuments_IndexerAccess_should_work()
23122312
.Where(x => x.DictionaryInterface["life"] == 42);
23132313

23142314
var stages = Translate(collection, queryable);
2315-
AssertStages(stages, "{ $match : { $expr : { $eq : [{ $let : { vars : { this : { $arrayElemAt : [{ $filter : { input : '$DictionaryInterface', as : 'kvp', cond : { $eq : ['$$kvp.k', 'life'] }, limit : 1 } }, 0] } }, in : '$$this.v' } }, 42] } } }");
2315+
AssertStages(stages, "{ $match : { DictionaryInterface : { $elemMatch : { k : 'life', v : 42 } } } }");
23162316

23172317
var result = queryable.ToList();
23182318
result.Should().ContainSingle();

0 commit comments

Comments
 (0)