Skip to content

Commit 4c2d0bb

Browse files
committed
update some mql based on robert's suggestions
1 parent 205eb56 commit 4c2d0bb

File tree

4 files changed

+87
-98
lines changed

4 files changed

+87
-98
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/ContainsKeyMethodToAggregationExpressionTranslator.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,24 @@ public static TranslatedExpression TranslateContainsKey(TranslationContext conte
6161
case DictionaryRepresentation.ArrayOfDocuments:
6262
{
6363
var keyFieldName = GetKeyFieldName(context, expression, keyExpression, dictionarySerializer.KeySerializer);
64-
var (valueBinding, valueAst) = AstExpression.UseVarIfNotSimple("value", keyFieldName);
65-
ast = AstExpression.Let(
66-
var: valueBinding,
67-
@in: AstExpression.Reduce(
68-
input: dictionaryTranslation.Ast,
69-
initialValue: false,
70-
@in: AstExpression.Cond(
71-
@if: AstExpression.Var("value"),
72-
@then: true,
73-
@else: AstExpression.Eq(AstExpression.GetField(AstExpression.Var("this"), "k"), valueAst))));
64+
var kvpVar = AstExpression.Var("kvp");
65+
var keysArray = AstExpression.Map(
66+
input: dictionaryTranslation.Ast,
67+
@as: kvpVar,
68+
@in: AstExpression.GetField(kvpVar, "k"));
69+
ast = AstExpression.In(keyFieldName, keysArray);
7470
break;
7571
}
7672

7773
case DictionaryRepresentation.ArrayOfArrays:
7874
{
7975
var keyFieldName = GetKeyFieldName(context, expression, keyExpression, dictionarySerializer.KeySerializer);
80-
var (valueBinding, valueAst) = AstExpression.UseVarIfNotSimple("value", keyFieldName);
81-
ast = AstExpression.Let(
82-
var: valueBinding,
83-
@in: AstExpression.Reduce(
84-
input: dictionaryTranslation.Ast,
85-
initialValue: false,
86-
@in: AstExpression.Cond(
87-
@if: AstExpression.Var("value"),
88-
@then: true,
89-
@else: AstExpression.Eq(AstExpression.ArrayElemAt(AstExpression.Var("this"), 0), valueAst))));
76+
var kvpVar = AstExpression.Var("kvp");
77+
var keysArray = AstExpression.Map(
78+
input: dictionaryTranslation.Ast,
79+
@as: kvpVar,
80+
@in: AstExpression.ArrayElemAt(kvpVar, 0));
81+
ast = AstExpression.In(keyFieldName, keysArray);
9082
break;
9183
}
9284

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/ContainsValueMethodToAggregationExpressionTranslator.cs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,54 +42,39 @@ public static TranslatedExpression Translate(TranslationContext context, MethodC
4242

4343
public static TranslatedExpression TranslateContainsValue(TranslationContext context, Expression expression, Expression dictionaryExpression, Expression valueExpression)
4444
{
45-
var dictionaryTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, dictionaryExpression);
45+
var dictionaryTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, dictionaryExpression);
4646
var dictionarySerializer = GetDictionarySerializer(expression, dictionaryTranslation);
4747
var dictionaryRepresentation = dictionarySerializer.DictionaryRepresentation;
4848

4949
var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression);
50-
var (valueBinding, valueAst) = AstExpression.UseVarIfNotSimple("value", valueTranslation.Ast);
50+
var valueAst = valueTranslation.Ast;
5151

52+
var kvpVar = AstExpression.Var("kvp");
5253
AstExpression ast;
5354
switch (dictionaryRepresentation)
5455
{
55-
case DictionaryRepresentation.Document:
56-
ast = AstExpression.Let(
57-
var: valueBinding,
58-
@in: AstExpression.Reduce(
59-
input: AstExpression.ObjectToArray(dictionaryTranslation.Ast),
60-
initialValue: false,
61-
@in: AstExpression.Cond(
62-
@if: AstExpression.Var("value"),
63-
@then: true,
64-
@else: AstExpression.Eq(AstExpression.GetField(AstExpression.Var("this"), "v"), valueAst))));
65-
break;
66-
6756
case DictionaryRepresentation.ArrayOfArrays:
68-
ast = AstExpression.Let(
69-
var: valueBinding,
70-
@in: AstExpression.Reduce(
57+
{
58+
var valuesArray = AstExpression.Map(
7159
input: dictionaryTranslation.Ast,
72-
initialValue: false,
73-
@in: AstExpression.Cond(
74-
@if: AstExpression.Var("value"),
75-
@then: true,
76-
@else: AstExpression.Eq(AstExpression.ArrayElemAt(AstExpression.Var("this"), 1), valueAst))));
77-
break;
60+
@as: kvpVar,
61+
@in: AstExpression.ArrayElemAt(kvpVar, 1));
62+
ast = AstExpression.In(valueAst, valuesArray);
63+
break;
64+
}
7865

7966
case DictionaryRepresentation.ArrayOfDocuments:
80-
ast = AstExpression.Let(
81-
var: valueBinding,
82-
@in: AstExpression.Reduce(
67+
{
68+
var valuesArray = AstExpression.Map(
8369
input: dictionaryTranslation.Ast,
84-
initialValue: false,
85-
@in: AstExpression.Cond(
86-
@if: AstExpression.Var("value"),
87-
@then: true,
88-
@else: AstExpression.Eq(AstExpression.GetField(AstExpression.Var("this"), "v"), valueAst))));
89-
break;
70+
@as: kvpVar,
71+
@in: AstExpression.GetField(kvpVar, "v"));
72+
ast = AstExpression.In(valueAst, valuesArray);
73+
break;
74+
}
9075

9176
default:
92-
throw new ExpressionNotSupportedException(expression, because: $"DictionaryRepresentation: {dictionaryRepresentation} is not supported.");
77+
throw new ExpressionNotSupportedException(expression, because: $"Unexpected dictionary representation: {dictionaryRepresentation}");
9378
}
9479

9580
return new TranslatedExpression(expression, ast, BooleanSerializer.Instance);

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/GetItemMethodToAggregationExpressionTranslator.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,26 @@ private static TranslatedExpression TranslateIDictionaryGetItemWithKey(Translati
164164
break;
165165

166166
case DictionaryRepresentation.ArrayOfArrays:
167-
ast = AstExpression.GetField(AstExpression.ArrayToObject(dictionaryTranslation.Ast), keyFieldNameAst);
168-
break;
167+
{
168+
var filter = AstExpression.Filter(
169+
dictionaryTranslation.Ast,
170+
AstExpression.Eq(AstExpression.ArrayElemAt(AstExpression.Var("kvp"), 0), keyFieldNameAst),
171+
"kvp",
172+
limit: 1);
173+
ast = AstExpression.ArrayElemAt(AstExpression.ArrayElemAt(filter, 0), 1);
174+
break;
175+
}
169176

170177
case DictionaryRepresentation.ArrayOfDocuments:
171-
var filter = AstExpression.Filter(dictionaryTranslation.Ast,
172-
AstExpression.Eq(AstExpression.GetField(AstExpression.Var("kvp"), "k"), keyFieldNameAst), "kvp");
173-
ast = AstExpression.GetField(AstExpression.ArrayElemAt(filter, 0), "v");
174-
break;
178+
{
179+
var filter = AstExpression.Filter(
180+
dictionaryTranslation.Ast,
181+
AstExpression.Eq(AstExpression.GetField(AstExpression.Var("kvp"), "k"), keyFieldNameAst),
182+
"kvp",
183+
limit: 1);
184+
ast = AstExpression.GetField(AstExpression.ArrayElemAt(filter, 0), "v");
185+
break;
186+
}
175187
default:
176188
throw new ExpressionNotSupportedException(expression, because: $"Indexer access is not supported when DictionaryRepresentation is: {dictionaryRepresentation}");
177189
}

0 commit comments

Comments
 (0)