Skip to content

Commit

Permalink
CSHARP-2509: Support Dictionary.ContainsValue in LINQ queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed Jun 11, 2024
1 parent fbcf93a commit 61d2c77
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,11 @@ public static AstExpression NullaryWindowExpression(AstNullaryWindowOperator @op
return new AstNullaryWindowExpression(@operator, window);
}

public static AstExpression ObjectToArray(AstExpression arg)
{
return new AstUnaryExpression(AstUnaryOperator.ObjectToArray, arg);
}

public static AstExpression Or(params AstExpression[] args)
{
Ensure.IsNotNull(args, nameof(args));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static AggregationExpression Translate(TranslationContext context, Method
case "Concat": return ConcatMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Contains": return ContainsMethodToAggregationExpressionTranslator.Translate(context, expression);
case "ContainsKey": return ContainsKeyMethodToAggregationExpressionTranslator.Translate(context, expression);
case "ContainsValue": return ContainsValueMethodToAggregationExpressionTranslator.Translate(context, expression);
case "CovariancePopulation": return CovariancePopulationMethodToAggregationExpressionTranslator.Translate(context, expression);
case "CovarianceSample": return CovarianceSampleMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Create": return CreateMethodToAggregationExpressionTranslator.Translate(context, expression);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* 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.Bson.Serialization;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;

namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators
{
internal static class ContainsValueMethodToAggregationExpressionTranslator
{
// public methods
public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression)
{
var method = expression.Method;
var arguments = expression.Arguments;

if (IsContainsValueMethod(method))
{
var dictionaryExpression = expression.Object;
var valueExpression = arguments[0];

var dictionaryTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, dictionaryExpression);
var dictionarySerializer = GetDictionarySerializer(expression, dictionaryTranslation);
var dictionaryRepresentation = dictionarySerializer.DictionaryRepresentation;

var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression);
var (valueBinding, valueAst) = AstExpression.UseVarIfNotSimple("value", valueTranslation.Ast);

AstExpression ast;
switch (dictionaryRepresentation)
{
case DictionaryRepresentation.Document:
ast = AstExpression.Let(
var: valueBinding,
@in: AstExpression.Reduce(
input: AstExpression.ObjectToArray(dictionaryTranslation.Ast),
initialValue: false,
@in: AstExpression.Cond(
@if: AstExpression.Var("value"),
@then: true,
@else: AstExpression.Eq(AstExpression.GetField(AstExpression.Var("this"), "v"), valueAst))));
break;

case DictionaryRepresentation.ArrayOfArrays:
ast = AstExpression.Let(
var: valueBinding,
@in: AstExpression.Reduce(
input: dictionaryTranslation.Ast,
initialValue: false,
@in: AstExpression.Cond(
@if: AstExpression.Var("value"),
@then: true,
@else: AstExpression.Eq(AstExpression.ArrayElemAt(AstExpression.Var("this"), 1), valueAst))));
break;

case DictionaryRepresentation.ArrayOfDocuments:
ast = AstExpression.Let(
var: valueBinding,
@in: AstExpression.Reduce(
input: dictionaryTranslation.Ast,
initialValue: false,
@in: AstExpression.Cond(
@if: AstExpression.Var("value"),
@then: true,
@else: AstExpression.Eq(AstExpression.GetField(AstExpression.Var("this"), "v"), valueAst))));
break;

default:
throw new ExpressionNotSupportedException(expression, because: $"ContainsValue is not supported when DictionaryRepresentation is: {dictionaryRepresentation}");
}

return new AggregationExpression(expression, ast, BooleanSerializer.Instance);
}

throw new ExpressionNotSupportedException(expression);
}

private static IBsonDictionarySerializer GetDictionarySerializer(Expression expression, AggregationExpression dictionaryTranslation)
{
if (dictionaryTranslation.Serializer is IBsonDictionarySerializer dictionarySerializer)
{
return dictionarySerializer;
}

throw new ExpressionNotSupportedException(expression, because: $"class {dictionaryTranslation.Serializer.GetType().FullName} does not implement the IBsonDictionarySerializer interface");
}

private static bool IsContainsValueMethod(MethodInfo method)
{
return
!method.IsStatic &&
method.IsPublic &&
method.ReturnType == typeof(bool) &&
method.Name == "ContainsValue" &&
method.GetParameters() is var parameters &&
parameters.Length == 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* 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.Bson.Serialization;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators;

namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.MethodTranslators
{
internal static class ContainsValueMethodToFilterTranslator
{
public static AstFilter Translate(TranslationContext context, MethodCallExpression expression)
{
var method = expression.Method;
var arguments = expression.Arguments;

if (IsContainsValueMethod(method))
{
var dictionaryExpression = expression.Object;
var valueExpression = arguments[0];

var dictionaryField = ExpressionToFilterFieldTranslator.Translate(context, dictionaryExpression);
var dictionarySerializer = GetDictionarySerializer(expression, dictionaryField);
var dictionaryRepresentation = dictionarySerializer.DictionaryRepresentation;
var valueSerializer = dictionarySerializer.ValueSerializer;

if (valueExpression is ConstantExpression constantValueExpression)
{
var valueField = AstFilter.Field("v", valueSerializer);
var value = constantValueExpression.Value;
var serializedValue = SerializationHelper.SerializeValue(valueSerializer, value);

switch (dictionaryRepresentation)
{
case DictionaryRepresentation.ArrayOfDocuments:
return AstFilter.ElemMatch(dictionaryField, AstFilter.Eq(valueField, serializedValue));

default:
throw new ExpressionNotSupportedException(expression, because: $"ContainsValue is not supported when DictionaryRepresentation is: {dictionaryRepresentation}");
}
}
}

throw new ExpressionNotSupportedException(expression);
}

private static IBsonDictionarySerializer GetDictionarySerializer(Expression expression, AstFilterField field)
{
if (field.Serializer is IBsonDictionarySerializer dictionarySerializer)
{
return dictionarySerializer;
}

throw new ExpressionNotSupportedException(expression, because: $"class {field.Serializer.GetType().FullName} does not implement the IBsonDictionarySerializer interface");
}

private static bool IsContainsValueMethod(MethodInfo method)
{
return
!method.IsStatic &&
method.IsPublic &&
method.ReturnType == typeof(bool) &&
method.Name == "ContainsValue" &&
method.GetParameters() is var parameters &&
parameters.Length == 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static AstFilter Translate(TranslationContext context, MethodCallExpressi
{
case "Contains": return ContainsMethodToFilterTranslator.Translate(context, expression);
case "ContainsKey": return ContainsKeyMethodToFilterTranslator.Translate(context, expression);
case "ContainsValue": return ContainsValueMethodToFilterTranslator.Translate(context, expression);
case "EndsWith": return EndsWithMethodToFilterTranslator.Translate(context, expression);
case "Equals": return EqualsMethodToFilterTranslator.Translate(context, expression);
case "Exists": return ExistsMethodToFilterTranslator.Translate(context, expression);
Expand Down
Loading

0 comments on commit 61d2c77

Please sign in to comment.