Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ internal enum AstComparisonFilterOperator

internal static class AstComparisonFilterOperatorExtensions
{
public static AstComparisonFilterOperator GetComparisonOperatorForSwappedLeftAndRight(this AstComparisonFilterOperator @operator)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Put here so that it can be used from more than one place.

{
return @operator switch
{
AstComparisonFilterOperator.Eq => AstComparisonFilterOperator.Eq,
AstComparisonFilterOperator.Gt => AstComparisonFilterOperator.Lt,
AstComparisonFilterOperator.Gte => AstComparisonFilterOperator.Lte,
AstComparisonFilterOperator.Lt => AstComparisonFilterOperator.Gt,
AstComparisonFilterOperator.Lte => AstComparisonFilterOperator.Gte,
AstComparisonFilterOperator.Ne => AstComparisonFilterOperator.Ne,
_ => throw new InvalidOperationException($"Unexpected comparison filter operator: {@operator}.")
};
}

public static string Render(this AstComparisonFilterOperator @operator)
{
return @operator switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* limitations under the License.
*/

using System;
using System.Linq;
using System.Reflection;

namespace MongoDB.Driver.Linq.Linq3Implementation.Misc
Expand All @@ -38,6 +40,30 @@ public static bool Is(this MethodInfo method, MethodInfo comparand)
return false;
}

public static bool IsInstanceCompareToMethod(this MethodInfo method)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Put here so that it can be used from more than one place.

{
if (method.IsPublic &&
!method.IsStatic &&
method.ReturnType == typeof(int) &&
method.Name == "CompareTo" &&
method.GetParameters() is var parameters &&
parameters.Length == 1)
{
var declaringType = method.DeclaringType;
var comparandType = declaringType switch
{
_ when declaringType == typeof(IComparable) => typeof(object),
_ when declaringType.IsConstructedGenericType && declaringType.GetGenericTypeDefinition() == typeof(IComparable<>) => declaringType.GetGenericArguments().Single(),
_ => declaringType
};

var parameterType = parameters[0].ParameterType;
return parameterType == comparandType;
}

return false;
}

public static bool IsOneOf(this MethodInfo method, MethodInfo comparand1, MethodInfo comparand2)
{
return method.Is(comparand1) || method.Is(comparand2);
Expand Down Expand Up @@ -78,5 +104,18 @@ public static bool IsOneOf(this MethodInfo method, params MethodInfo[][] compara

return false;
}

public static bool IsStaticCompareMethod(this MethodInfo method)
{
return
method.IsPublic &&
method.IsStatic &&
method.ReturnType == typeof(int) &&
method.Name == "Compare" &&
method.GetParameters() is var parameters &&
parameters.Length == 2 &&
parameters[0].ParameterType == method.DeclaringType &&
parameters[1].ParameterType == method.DeclaringType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ internal static class StringMethod
private static readonly MethodInfo __startsWithWithString;
private static readonly MethodInfo __startsWithWithStringAndComparisonType;
private static readonly MethodInfo __startsWithWithStringAndIgnoreCaseAndCulture;
private static readonly MethodInfo __staticCompare;
private static readonly MethodInfo __staticCompareWithIgnoreCase;
private static readonly MethodInfo __stringInWithEnumerable;
private static readonly MethodInfo __stringInWithParams;
private static readonly MethodInfo __stringNinWithEnumerable;
Expand Down Expand Up @@ -151,6 +153,8 @@ static StringMethod()
__startsWithWithString = ReflectionInfo.Method((string s, string value) => s.StartsWith(value));
__startsWithWithStringAndComparisonType = ReflectionInfo.Method((string s, string value, StringComparison comparisonType) => s.StartsWith(value, comparisonType));
__startsWithWithStringAndIgnoreCaseAndCulture = ReflectionInfo.Method((string s, string value, bool ignoreCase, CultureInfo culture) => s.StartsWith(value, ignoreCase, culture));
__staticCompare = ReflectionInfo.Method((string strA, string strB) => String.Compare(strA, strB));
__staticCompareWithIgnoreCase = ReflectionInfo.Method((string strA, string strB, bool ignoreCase) => String.Compare(strA, strB, ignoreCase));
__stringInWithEnumerable = ReflectionInfo.Method((string s, IEnumerable<StringOrRegularExpression> values) => s.StringIn(values));
__stringInWithParams = ReflectionInfo.Method((string s, StringOrRegularExpression[] values) => s.StringIn(values));
__stringNinWithEnumerable = ReflectionInfo.Method((string s, IEnumerable<StringOrRegularExpression> values) => s.StringNin(values));
Expand Down Expand Up @@ -220,6 +224,8 @@ static StringMethod()
public static MethodInfo StartsWithWithString => __startsWithWithString;
public static MethodInfo StartsWithWithStringAndComparisonType => __startsWithWithStringAndComparisonType;
public static MethodInfo StartsWithWithStringAndIgnoreCaseAndCulture => __startsWithWithStringAndIgnoreCaseAndCulture;
public static MethodInfo StaticCompare => __staticCompare;
public static MethodInfo StaticCompareWithIgnoreCase => __staticCompareWithIgnoreCase;
public static MethodInfo StringInWithEnumerable => __stringInWithEnumerable;
public static MethodInfo StringInWithParams => __stringInWithParams;
public static MethodInfo StringNinWithEnumerable => __stringNinWithEnumerable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static TranslatedExpression Translate(TranslationContext context, MethodC
case "AsQueryable": return AsQueryableMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Average": return AverageMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Ceiling": return CeilingMethodToAggregationExpressionTranslator.Translate(context, expression);
case "CompareTo": return CompareToMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Concat": return ConcatMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Constant": return ConstantMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Contains": return ContainsMethodToAggregationExpressionTranslator.Translate(context, expression);
Expand Down Expand Up @@ -138,6 +137,10 @@ public static TranslatedExpression Translate(TranslationContext context, MethodC
case "TopN":
return PickMethodToAggregationExpressionTranslator.Translate(context, expression);

case "Compare":
case "CompareTo":
return CompareMethodToAggregationExpressionTranslator.Translate(context, expression);

case "Count":
case "LongCount":
return CountMethodToAggregationExpressionTranslator.Translate(context, expression);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* 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.Serializers;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;

namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators
{
internal static class CompareMethodToAggregationExpressionTranslator
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Compare and CompareTo handling is consolidated into a single class.

{
private static readonly MethodInfo[] __stringCompareMethods =
[
StringMethod.StaticCompare,
StringMethod.StaticCompareWithIgnoreCase
];

public static TranslatedExpression Translate(TranslationContext context, MethodCallExpression expression)
{
var method = expression.Method;
var arguments = expression.Arguments;

if (method.IsStaticCompareMethod() || method.IsInstanceCompareToMethod() || method.IsOneOf(__stringCompareMethods))
{
Expression value1Expression;
Expression value2Expression;
if (method.IsStatic)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main difference between Compare and CompareTo is whether it's a static method or not.

{
value1Expression = arguments[0];
value2Expression = arguments[1];
}
else
{
value1Expression = expression.Object;
value2Expression = arguments[0];
}

var value1Translation = ExpressionToAggregationExpressionTranslator.Translate(context, value1Expression);
var value2Translation = ExpressionToAggregationExpressionTranslator.Translate(context, value2Expression);

Copy link
Member

Choose a reason for hiding this comment

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

Should we check if the value1 and value2 has string representation?

AstExpression ast;
if (method.Is(StringMethod.StaticCompareWithIgnoreCase))
{
var ignoreCaseExpression = arguments[2];
var ignoreCase = ignoreCaseExpression.GetConstantValue<bool>(containingExpression: expression);
ast = ignoreCase
? AstExpression.StrCaseCmp(value1Translation.Ast, value2Translation.Ast)
: AstExpression.Cmp(value1Translation.Ast, value2Translation.Ast);
}
else
{
ast = AstExpression.Cmp(value1Translation.Ast, value2Translation.Ast);
}

return new TranslatedExpression(expression, ast, Int32Serializer.Instance);
}

throw new ExpressionNotSupportedException(expression);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* 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 MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters;
using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators;

namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators
{
internal static class CompareComparisonExpressionToFilterTranslator
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Compare and CompareTo handling is consolidated into a single class.

{
public static bool CanTranslate(Expression leftExpression)
{
return
leftExpression is MethodCallExpression leftMethodCallExpression &&
leftMethodCallExpression.Method is var method &&
(method.IsStaticCompareMethod() || method.IsInstanceCompareToMethod() || method.Is(StringMethod.StaticCompareWithIgnoreCase));
}

// caller is responsible for ensuring constant is on the right
public static AstFilter Translate(
TranslationContext context,
Expression expression,
Expression leftExpression,
AstComparisonFilterOperator outerComparisonOperator,
Expression rightExpression)
{
if (CanTranslate(leftExpression))
{
var compareMethodCallExpression = (MethodCallExpression)leftExpression;
var compareMethod = compareMethodCallExpression.Method;
var compareArguments = compareMethodCallExpression.Arguments;
var outerValue = rightExpression.GetConstantValue<int>(containingExpression: expression);

Expression fieldExpression;
Expression innerValueExpression;
if (compareMethod.IsStatic)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main difference between Compare and CompareTo is whether it's a static method or not.

{
fieldExpression = compareArguments[0];
innerValueExpression = compareArguments[1];
}
else
{
fieldExpression = compareMethodCallExpression.Object;
innerValueExpression = compareArguments[0];
}

if (compareMethod.Is(StringMethod.StaticCompareWithIgnoreCase))
{
var ignoreCaseExpression = compareArguments[2];
var ignoreCase = ignoreCaseExpression.GetConstantValue<bool>(containingExpression: compareMethodCallExpression);
if (ignoreCase)
{
throw new ExpressionNotSupportedException(compareMethodCallExpression, because: "ignoreCase must be false");
}
}

var fieldComparisonOperator = (outerComparisonOperator, outerValue) switch
{
(AstComparisonFilterOperator.Eq, -1) => AstComparisonFilterOperator.Lt,
(AstComparisonFilterOperator.Ne, -1) => AstComparisonFilterOperator.Gte,
(AstComparisonFilterOperator.Gt, -1) => AstComparisonFilterOperator.Gte,
(AstComparisonFilterOperator.Eq, 0) => AstComparisonFilterOperator.Eq,
(AstComparisonFilterOperator.Ne, 0) => AstComparisonFilterOperator.Ne,
(AstComparisonFilterOperator.Lt, 0) => AstComparisonFilterOperator.Lt,
(AstComparisonFilterOperator.Lte, 0) => AstComparisonFilterOperator.Lte,
(AstComparisonFilterOperator.Gt, 0) => AstComparisonFilterOperator.Gt,
(AstComparisonFilterOperator.Gte, 0) => AstComparisonFilterOperator.Gte,
(AstComparisonFilterOperator.Eq, 1) => AstComparisonFilterOperator.Gt,
(AstComparisonFilterOperator.Ne, 1) => AstComparisonFilterOperator.Lte,
(AstComparisonFilterOperator.Lt, 1) => AstComparisonFilterOperator.Lte,
_ => throw new ExpressionNotSupportedException(expression)
};

if (fieldExpression.NodeType == ExpressionType.Constant && innerValueExpression.NodeType != ExpressionType.Constant)
{
(fieldExpression, innerValueExpression) = (innerValueExpression, fieldExpression);
fieldComparisonOperator = fieldComparisonOperator.GetComparisonOperatorForSwappedLeftAndRight();
}

var fieldTranslation = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression);
var value = innerValueExpression.GetConstantValue<object>(containingExpression: expression);
var serializedValue = SerializationHelper.SerializeValue(fieldTranslation.Serializer, value);

return AstFilter.Compare(fieldTranslation.Ast, fieldComparisonOperator, serializedValue);
}

throw new ExpressionNotSupportedException(expression);
}
}
}
Loading