Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Math translators for Cosmos DB provider #24463

Merged
merged 5 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -39,7 +39,8 @@ public CosmosMethodCallTranslatorProvider(
new EqualsTranslator(sqlExpressionFactory),
new StringMethodTranslator(sqlExpressionFactory),
new ContainsTranslator(sqlExpressionFactory),
new RandomTranslator(sqlExpressionFactory)
new RandomTranslator(sqlExpressionFactory),
new MathTranslator(sqlExpressionFactory)
//new LikeTranslator(sqlExpressionFactory),
//new EnumHasFlagTranslator(sqlExpressionFactory),
//new GetValueOrDefaultTranslator(sqlExpressionFactory),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,32 @@ private static Expression TryRemoveImplicitConvert(Expression expression)
}
}

/* TODO
if (expression is MethodCallExpression methodCallExpression)
Marusyk marked this conversation as resolved.
Show resolved Hide resolved
{
var innerType = methodCallExpression.Arguments[0].Type.UnwrapNullableType();
if (innerType.IsEnum)
{
innerType = Enum.GetUnderlyingType(innerType);
}

var convertedType = methodCallExpression.Type.UnwrapNullableType();

if (innerType == convertedType
|| (convertedType == typeof(int)
&& (innerType == typeof(byte)
|| innerType == typeof(sbyte)
|| innerType == typeof(char)
|| innerType == typeof(short)
|| innerType == typeof(ushort)))
|| (convertedType == typeof(double)
&& (innerType == typeof(float))))
{
return TryRemoveImplicitConvert(methodCallExpression.Arguments[0]);
}
}
*/

return expression;
}

Expand Down
132 changes: 132 additions & 0 deletions src/EFCore.Cosmos/Query/Internal/MathTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class MathTranslator : IMethodCallTranslator
{
private static readonly Dictionary<MethodInfo, string> _supportedMethodTranslations = new()
{
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Abs), new[] { typeof(decimal) }), "ABS" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Abs), new[] { typeof(double) }), "ABS" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Abs), new[] { typeof(float) }), "ABS" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Abs), new[] { typeof(int) }), "ABS" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Abs), new[] { typeof(long) }), "ABS" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Abs), new[] { typeof(sbyte) }), "ABS" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Abs), new[] { typeof(short) }), "ABS" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Ceiling), new[] { typeof(decimal) }), "CEILING" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Ceiling), new[] { typeof(double) }), "CEILING" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Floor), new[] { typeof(decimal) }), "FLOOR" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Floor), new[] { typeof(double) }), "FLOOR" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Pow), new[] { typeof(double), typeof(double) }), "POWER" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Exp), new[] { typeof(double) }), "EXP" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Log10), new[] { typeof(double) }), "LOG10" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Log), new[] { typeof(double) }), "LOG" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Log), new[] { typeof(double), typeof(double) }), "LOG" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Sqrt), new[] { typeof(double) }), "SQRT" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Acos), new[] { typeof(double) }), "ACOS" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Asin), new[] { typeof(double) }), "ASIN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Atan), new[] { typeof(double) }), "ATAN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Atan2), new[] { typeof(double), typeof(double) }), "ATN2" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Cos), new[] { typeof(double) }), "COS" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Sin), new[] { typeof(double) }), "SIN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Tan), new[] { typeof(double) }), "TAN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Sign), new[] { typeof(decimal) }), "SIGN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Sign), new[] { typeof(double) }), "SIGN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Sign), new[] { typeof(float) }), "SIGN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Sign), new[] { typeof(int) }), "SIGN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Sign), new[] { typeof(long) }), "SIGN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Sign), new[] { typeof(sbyte) }), "SIGN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Sign), new[] { typeof(short) }), "SIGN" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Truncate), new[] { typeof(decimal) }), "TRUNC" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Truncate), new[] { typeof(double) }), "TRUNC" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Round), new[] { typeof(decimal) }), "ROUND" },
{ typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Round), new[] { typeof(double) }), "ROUND" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Abs), new[] { typeof(float) }), "ABS" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Ceiling), new[] { typeof(float) }), "CEILING" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Floor), new[] { typeof(float) }), "FLOOR" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Pow), new[] { typeof(float), typeof(float) }), "POWER" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Exp), new[] { typeof(float) }), "EXP" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Log10), new[] { typeof(float) }), "LOG10" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Log), new[] { typeof(float) }), "LOG" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Log), new[] { typeof(float), typeof(float) }), "LOG" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Sqrt), new[] { typeof(float) }), "SQRT" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Acos), new[] { typeof(float) }), "ACOS" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Asin), new[] { typeof(float) }), "ASIN" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Atan), new[] { typeof(float) }), "ATAN" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Atan2), new[] { typeof(float), typeof(float) }), "ATN2" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Cos), new[] { typeof(float) }), "COS" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Sin), new[] { typeof(float) }), "SIN" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Tan), new[] { typeof(float) }), "TAN" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Sign), new[] { typeof(float) }), "SIGN" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Truncate), new[] { typeof(float) }), "TRUNC" },
{ typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Round), new[] { typeof(float) }), "ROUND" }
};

private readonly ISqlExpressionFactory _sqlExpressionFactory;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public MathTranslator(ISqlExpressionFactory sqlExpressionFactory)
{
_sqlExpressionFactory = sqlExpressionFactory;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Check.NotNull(method, nameof(method));
Check.NotNull(arguments, nameof(arguments));
Check.NotNull(logger, nameof(logger));

if (_supportedMethodTranslations.TryGetValue(method, out var sqlFunctionName))
{
var typeMapping = arguments.Count == 1
? ExpressionExtensions.InferTypeMapping(arguments[0])
: ExpressionExtensions.InferTypeMapping(arguments[0], arguments[1]);

var newArguments = new SqlExpression[arguments.Count];
Marusyk marked this conversation as resolved.
Show resolved Hide resolved
newArguments[0] = _sqlExpressionFactory.ApplyTypeMapping(arguments[0], typeMapping!);

if (arguments.Count == 2)
{
newArguments[1] = _sqlExpressionFactory.ApplyTypeMapping(arguments[1], typeMapping!);
}

return _sqlExpressionFactory.Function(
sqlFunctionName,
newArguments,
method.ReturnType,
typeMapping);
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,37 +290,34 @@ FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_abs1(bool async)
{
await base.Where_math_abs1(async);

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""OrderDetail"")");
WHERE ((c[""Discriminator""] = ""Product"") AND (ABS(c[""ProductID""]) > 10))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_abs2(bool async)
{
await base.Where_math_abs2(async);

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""OrderDetail"")");
WHERE (((c[""Discriminator""] = ""OrderDetail"") AND (c[""UnitPrice""] < 7.0)) AND (ABS(c[""Quantity""]) > 10))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_abs3(bool async)
{
await base.Where_math_abs3(async);

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""OrderDetail"")");
WHERE (((c[""Discriminator""] = ""OrderDetail"") AND (c[""Quantity""] < 5)) AND (ABS(c[""UnitPrice""]) > 10.0))");
}

public override async Task Where_math_abs_uncorrelated(bool async)
Expand All @@ -333,7 +330,6 @@ FROM root c
WHERE (((c[""Discriminator""] = ""OrderDetail"") AND (c[""UnitPrice""] < 7.0)) AND (10 < c[""ProductID""]))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_ceiling1(bool async)
{
await base.Where_math_ceiling1(async);
Expand All @@ -355,18 +351,16 @@ FROM root c
WHERE (c[""Discriminator""] = ""OrderDetail"")");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_floor(bool async)
{
await base.Where_math_floor(async);

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""OrderDetail"")");
WHERE (((c[""Discriminator""] = ""OrderDetail"") AND (c[""Quantity""] < 5)) AND (FLOOR(c[""UnitPrice""]) > 10.0))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_power(bool async)
{
await base.Where_math_power(async);
Expand All @@ -377,15 +371,14 @@ FROM root c
WHERE (c[""Discriminator""] = ""OrderDetail"")");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_round(bool async)
{
await base.Where_math_round(async);

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""OrderDetail"")");
WHERE (((c[""Discriminator""] = ""OrderDetail"") AND (c[""Quantity""] < 5)) AND (ROUND(c[""UnitPrice""]) > 10.0))");
}

public override async Task Select_math_round_int(bool async)
Expand Down Expand Up @@ -416,21 +409,19 @@ public override async Task Where_math_round2(bool async)
AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""OrderDetail"")");
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (ROUND(c[""UnitPrice""]) > 100.0))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_truncate(bool async)
{
await base.Where_math_truncate(async);

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] = ""OrderDetail"")");
WHERE (((c[""Discriminator""] = ""OrderDetail"") AND (c[""Quantity""] < 5)) AND (TRUNC(c[""UnitPrice""]) > 10.0))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_exp(bool async)
{
await base.Where_math_exp(async);
Expand All @@ -441,7 +432,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_log10(bool async)
{
await base.Where_math_log10(async);
Expand All @@ -452,7 +442,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND ((c[""OrderID""] = 11077) AND (c[""Discount""] > 0.0)))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_log(bool async)
{
await base.Where_math_log(async);
Expand All @@ -463,7 +452,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND ((c[""OrderID""] = 11077) AND (c[""Discount""] > 0.0)))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_log_new_base(bool async)
{
await base.Where_math_log_new_base(async);
Expand All @@ -474,7 +462,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND ((c[""OrderID""] = 11077) AND (c[""Discount""] > 0.0)))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_sqrt(bool async)
{
await base.Where_math_sqrt(async);
Expand All @@ -485,7 +472,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_acos(bool async)
{
await base.Where_math_acos(async);
Expand All @@ -496,7 +482,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_asin(bool async)
{
await base.Where_math_asin(async);
Expand All @@ -507,7 +492,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_atan(bool async)
{
await base.Where_math_atan(async);
Expand All @@ -518,7 +502,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_atan2(bool async)
{
await base.Where_math_atan2(async);
Expand All @@ -529,7 +512,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_cos(bool async)
{
await base.Where_math_cos(async);
Expand All @@ -540,7 +522,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_sin(bool async)
{
await base.Where_math_sin(async);
Expand All @@ -551,7 +532,6 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_tan(bool async)
{
await base.Where_math_tan(async);
Expand All @@ -562,15 +542,14 @@ FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
}

[ConditionalTheory(Skip = "Issue #17246")]
public override async Task Where_math_sign(bool async)
{
await base.Where_math_sign(async);

AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077))");
WHERE (((c[""Discriminator""] = ""OrderDetail"") AND (c[""OrderID""] = 11077)) AND (SIGN(c[""Discount""]) > 0))");
}

[ConditionalTheory(Skip = "Issue #17246")]
Expand Down