Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,121 +1,124 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Linq
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Spatial;
using Microsoft.Azure.Cosmos.SqlObjects;
using Microsoft.Azure.Documents;

internal abstract class BuiltinFunctionVisitor
{
public SqlScalarExpression Visit(MethodCallExpression methodCallExpression, TranslationContext context)
{
SqlScalarExpression result = this.VisitExplicit(methodCallExpression, context);
if (result != null)
{
return result;
}

result = this.VisitImplicit(methodCallExpression, context);
if (result != null)
{
return result;
}

throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name));
}

public static SqlScalarExpression VisitBuiltinFunctionCall(MethodCallExpression methodCallExpression, TranslationContext context)
{
Type declaringType;

// Method could be an extension method
if (methodCallExpression.Method.IsStatic && methodCallExpression.Method.IsExtensionMethod())
{
if (methodCallExpression.Arguments.Count < 1)
{
// Extension methods should has at least 1 argument, this should never happen
// Throwing ArgumentException instead of assert
throw new ArgumentException();
}

declaringType = methodCallExpression.Arguments[0].Type;

if (methodCallExpression.Method.DeclaringType.GeUnderlyingSystemType() == typeof(CosmosLinqExtensions))
{
// CosmosLinq Extensions can be RegexMatch, DocumentId or Type check functions (IsString, IsBool, etc.)
if ((methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.RegexMatch)) ||
(methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.FullTextContains)) ||
(methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.FullTextContainsAll)) ||
(methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.FullTextContainsAny)))
{
return StringBuiltinFunctions.Visit(methodCallExpression, context);
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Linq
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Spatial;
using Microsoft.Azure.Cosmos.SqlObjects;
using Microsoft.Azure.Documents;

internal abstract class BuiltinFunctionVisitor
{
public SqlScalarExpression Visit(MethodCallExpression methodCallExpression, TranslationContext context)
{
SqlScalarExpression result = this.VisitExplicit(methodCallExpression, context);
if (result != null)
{
return result;
}

result = this.VisitImplicit(methodCallExpression, context);
if (result != null)
{
return result;
}

throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name));
}

public static SqlScalarExpression VisitBuiltinFunctionCall(MethodCallExpression methodCallExpression, TranslationContext context)
{
Type declaringType;
bool isExtensionMethod = methodCallExpression.Method.IsExtensionMethod();
// Method could be an extension method
// RRF doesn't have "this" qualifier, so it's not considered an extension method by the compiler, and so needed to be checked separately
if (methodCallExpression.Method.IsStatic &&
(methodCallExpression.Method.IsExtensionMethod()
|| methodCallExpression.Method.Name.Equals(nameof(CosmosLinqExtensions.RRF))))
{
if (methodCallExpression.Arguments.Count < 1)
{
// Extension methods should has at least 1 argument, this should never happen
// Throwing ArgumentException instead of assert
throw new ArgumentException();
}

declaringType = methodCallExpression.Arguments[0].Type;

if (methodCallExpression.Method.DeclaringType.GeUnderlyingSystemType() == typeof(CosmosLinqExtensions))
{
// CosmosLinq Extensions can be RegexMatch, DocumentId or Type check functions (IsString, IsBool, etc.)
switch (methodCallExpression.Method.Name)
{
case nameof(CosmosLinqExtensions.RegexMatch):
case nameof(CosmosLinqExtensions.FullTextContains):
case nameof(CosmosLinqExtensions.FullTextContainsAll):
case nameof(CosmosLinqExtensions.FullTextContainsAny):
return StringBuiltinFunctions.Visit(methodCallExpression, context);
case nameof(CosmosLinqExtensions.DocumentId):
case nameof(CosmosLinqExtensions.RRF):
case nameof(CosmosLinqExtensions.FullTextScore):
return OtherBuiltinSystemFunctions.Visit(methodCallExpression, context);
default:
return TypeCheckFunctions.Visit(methodCallExpression, context);
}

if (methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.DocumentId))
{
return OtherBuiltinSystemFunctions.Visit(methodCallExpression, context);
}

return TypeCheckFunctions.Visit(methodCallExpression, context);
}
}
else
{
declaringType = methodCallExpression.Method.DeclaringType;
}

// Check order matters, some extension methods work for both strings and arrays

// Math functions
if (declaringType == typeof(Math))
{
return MathBuiltinFunctions.Visit(methodCallExpression, context);
}

// ToString with String and Guid only becomes passthrough
if (methodCallExpression.Method.Name == "ToString" &&
methodCallExpression.Arguments.Count == 0 &&
methodCallExpression.Object != null &&
((methodCallExpression.Object.Type == typeof(string)) ||
(methodCallExpression.Object.Type == typeof(Guid))))
{
return ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Object, context);
}

// String functions or ToString with Objects that are not strings and guids
if ((declaringType == typeof(string)) ||
(methodCallExpression.Method.Name == "ToString" &&
methodCallExpression.Arguments.Count == 0 &&
methodCallExpression.Object != null))
{
return StringBuiltinFunctions.Visit(methodCallExpression, context);
}

// Array functions
if (declaringType.IsEnumerable())
{
return ArrayBuiltinFunctions.Visit(methodCallExpression, context);
}

// Spatial functions
if (typeof(Geometry).IsAssignableFrom(declaringType))
{
return SpatialBuiltinFunctions.Visit(methodCallExpression, context);
}

throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name));
}

protected abstract SqlScalarExpression VisitExplicit(MethodCallExpression methodCallExpression, TranslationContext context);

protected abstract SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context);
}
}
}
}
else
{
declaringType = methodCallExpression.Method.DeclaringType;
}

// Check order matters, some extension methods work for both strings and arrays

// Math functions
if (declaringType == typeof(Math))
{
return MathBuiltinFunctions.Visit(methodCallExpression, context);
}

// ToString with String and Guid only becomes passthrough
if (methodCallExpression.Method.Name == "ToString" &&
methodCallExpression.Arguments.Count == 0 &&
methodCallExpression.Object != null &&
((methodCallExpression.Object.Type == typeof(string)) ||
(methodCallExpression.Object.Type == typeof(Guid))))
{
return ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Object, context);
}

// String functions or ToString with Objects that are not strings and guids
if ((declaringType == typeof(string)) ||
(methodCallExpression.Method.Name == "ToString" &&
methodCallExpression.Arguments.Count == 0 &&
methodCallExpression.Object != null))
{
return StringBuiltinFunctions.Visit(methodCallExpression, context);
}

// Array functions
if (declaringType.IsEnumerable())
{
return ArrayBuiltinFunctions.Visit(methodCallExpression, context);
}

// Spatial functions
if (typeof(Geometry).IsAssignableFrom(declaringType))
{
return SpatialBuiltinFunctions.Visit(methodCallExpression, context);
}

throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name));
}

protected abstract SqlScalarExpression VisitExplicit(MethodCallExpression methodCallExpression, TranslationContext context);

protected abstract SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,88 @@ namespace Microsoft.Azure.Cosmos.Linq
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq.Expressions;
using Microsoft.Azure.Cosmos.SqlObjects;

internal static class OtherBuiltinSystemFunctions
{
private class RRFVisit : SqlBuiltinFunctionVisitor
{
public RRFVisit()
: base("RRF",
true,
new List<Type[]>()
{
new Type[]{typeof(Func<object, object>[])}
})
{
}

protected override SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
if (methodCallExpression.Arguments.Count == 1
&& methodCallExpression.Arguments[0] is NewArrayExpression argumentsExpressions)
{
// For RRF, We don't need to care about the first argument, it is the object itself and have no relevance to the computation
ReadOnlyCollection<Expression> functionListExpression = argumentsExpressions.Expressions;
List<SqlScalarExpression> arguments = new List<SqlScalarExpression>();
foreach (Expression argument in functionListExpression)
{
arguments.Add(ExpressionToSql.VisitScalarExpression(argument, context));
}

return SqlFunctionCallScalarExpression.CreateBuiltin(SqlFunctionCallScalarExpression.Names.RRF, arguments.ToImmutableArray());
}

return null;
}

protected override SqlScalarExpression VisitExplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
return null;
}
}

private class FullTextScoreVisit : SqlBuiltinFunctionVisitor
{
public FullTextScoreVisit()
: base("FullTextScore",
true,
new List<Type[]>()
{
new Type[]{typeof(object), typeof(string[])}
})
{
}

protected override SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
if (methodCallExpression.Arguments.Count == 2
&& methodCallExpression.Arguments[1] is ConstantExpression stringListArgumentExpression
&& ExpressionToSql.VisitConstant(stringListArgumentExpression, context) is SqlArrayCreateScalarExpression arrayScalarExpressions)
{
List<SqlScalarExpression> arguments = new List<SqlScalarExpression>
{
ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Arguments[0], context)
};

arguments.AddRange(arrayScalarExpressions.Items);

return SqlFunctionCallScalarExpression.CreateBuiltin(SqlFunctionCallScalarExpression.Names.FullTextScore, arguments.ToImmutableArray());
}

return null;
}

protected override SqlScalarExpression VisitExplicit(MethodCallExpression methodCallExpression, TranslationContext context)
{
return null;
}
}

private static Dictionary<string, BuiltinFunctionVisitor> FunctionsDefinitions { get; set; }

static OtherBuiltinSystemFunctions()
Expand All @@ -24,7 +100,9 @@ static OtherBuiltinSystemFunctions()
argumentLists: new List<Type[]>()
{
new Type[]{typeof(object)},
})
}),
[nameof(CosmosLinqExtensions.RRF)] = new RRFVisit(),
[nameof(CosmosLinqExtensions.FullTextScore)] = new FullTextScoreVisit(),
};
}

Expand Down
Loading
Loading