Skip to content

Commit

Permalink
Query: Translate DEGREES and RADIANS
Browse files Browse the repository at this point in the history
Resolves dotnet#30926
  • Loading branch information
bricelam committed Jul 18, 2023
1 parent 41cc37c commit 0c447b6
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/EFCore.Cosmos/Query/Internal/CosmosMathTranslator.cs
Expand Up @@ -48,6 +48,8 @@ public class CosmosMathTranslator : IMethodCallTranslator
{ typeof(Math).GetRuntimeMethod(nameof(Math.Truncate), new[] { typeof(double) })!, "TRUNC" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Round), new[] { typeof(decimal) })!, "ROUND" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Round), new[] { typeof(double) })!, "ROUND" },
{ typeof(double).GetRuntimeMethod(nameof(double.DegreesToRadians), new[] { typeof(double) })!, "RADIANS" },
{ typeof(double).GetRuntimeMethod(nameof(double.RadiansToDegrees), new[] { typeof(double) })!, "DEGREES" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Abs), new[] { typeof(float) })!, "ABS" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Ceiling), new[] { typeof(float) })!, "CEILING" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Floor), new[] { typeof(float) })!, "FLOOR" },
Expand All @@ -66,7 +68,9 @@ public class CosmosMathTranslator : IMethodCallTranslator
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Tan), new[] { typeof(float) })!, "TAN" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Sign), new[] { typeof(float) })!, "SIGN" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Truncate), new[] { typeof(float) })!, "TRUNC" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Round), new[] { typeof(float) })!, "ROUND" }
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Round), new[] { typeof(float) })!, "ROUND" },
{ typeof(float).GetRuntimeMethod(nameof(float.DegreesToRadians), new[] { typeof(float) })!, "RADIANS" },
{ typeof(float).GetRuntimeMethod(nameof(float.RadiansToDegrees), new[] { typeof(float) })!, "DEGREES" },
};

private readonly ISqlExpressionFactory _sqlExpressionFactory;
Expand Down
Expand Up @@ -47,6 +47,8 @@ public class SqlServerMathTranslator : IMethodCallTranslator
{ typeof(Math).GetRuntimeMethod(nameof(Math.Sign), new[] { typeof(long) })!, "SIGN" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Sign), new[] { typeof(sbyte) })!, "SIGN" },
{ typeof(Math).GetRuntimeMethod(nameof(Math.Sign), new[] { typeof(short) })!, "SIGN" },
{ typeof(double).GetRuntimeMethod(nameof(double.DegreesToRadians), new[] { typeof(double) })!, "RADIANS" },
{ typeof(double).GetRuntimeMethod(nameof(double.RadiansToDegrees), new[] { typeof(double) })!, "DEGREES" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Abs), new[] { typeof(float) })!, "ABS" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Ceiling), new[] { typeof(float) })!, "CEILING" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Floor), new[] { typeof(float) })!, "FLOOR" },
Expand All @@ -63,7 +65,9 @@ public class SqlServerMathTranslator : IMethodCallTranslator
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Cos), new[] { typeof(float) })!, "COS" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Sin), new[] { typeof(float) })!, "SIN" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Tan), new[] { typeof(float) })!, "TAN" },
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Sign), new[] { typeof(float) })!, "SIGN" }
{ typeof(MathF).GetRuntimeMethod(nameof(MathF.Sign), new[] { typeof(float) })!, "SIGN" },
{ typeof(float).GetRuntimeMethod(nameof(float.DegreesToRadians), new[] { typeof(float) })!, "RADIANS" },
{ typeof(float).GetRuntimeMethod(nameof(float.RadiansToDegrees), new[] { typeof(float) })!, "DEGREES" }
};

private static readonly IEnumerable<MethodInfo> TruncateMethodInfos = new[]
Expand Down
Expand Up @@ -573,6 +573,22 @@ public override async Task Where_math_max(bool async)
AssertSql();
}

public override async Task Where_math_degrees(bool async)
{
// Cosmos client evaluation. Issue #17246.
await AssertTranslationFailed(() => base.Where_math_degrees(async));

AssertSql();
}

public override async Task Where_math_radians(bool async)
{
// Cosmos client evaluation. Issue #17246.
await AssertTranslationFailed(() => base.Where_math_radians(async));

AssertSql();
}

public override async Task Where_mathf_abs1(bool async)
{
// Cosmos client evaluation. Issue #17246.
Expand Down Expand Up @@ -797,6 +813,30 @@ SELECT c
""");
}

public override async Task Where_mathf_degrees(bool async)
{
await base.Where_mathf_degrees(async);

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

public override async Task Where_mathf_radians(bool async)
{
await base.Where_mathf_radians(async);

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

public override async Task Where_guid_newguid(bool async)
{
// Cosmos client evaluation. Issue #17246.
Expand Down
Expand Up @@ -1113,6 +1113,22 @@ public virtual Task Where_math_min(bool async)
.Where(od => Math.Min(od.OrderID, od.ProductID) == od.ProductID),
entryCount: 25);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_math_degrees(bool async)
=> AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => double.RadiansToDegrees(od.Discount) > 0),
entryCount: 13);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_math_radians(bool async)
=> AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => double.DegreesToRadians(od.Discount) > 0),
entryCount: 13);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_abs1(bool async)
Expand Down Expand Up @@ -1307,6 +1323,22 @@ public virtual Task Where_mathf_sign(bool async)
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Sign(od.Discount) > 0),
entryCount: 13);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_degrees(bool async)
=> AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => float.RadiansToDegrees(od.Discount) > 0),
entryCount: 13);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_radians(bool async)
=> AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => float.DegreesToRadians(od.Discount) > 0),
entryCount: 13);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_guid_newguid(bool async)
Expand Down
Expand Up @@ -1323,6 +1323,30 @@ public override async Task Where_math_max(bool async)
AssertSql();
}

public override async Task Where_math_degrees(bool async)
{
await base.Where_math_degrees(async);

AssertSql(
"""
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
FROM [Order Details] AS [o]
WHERE [o].[OrderID] = 11077 AND DEGREES(CAST([o].[Discount] AS float)) > 0.0E0
""");
}

public override async Task Where_math_radians(bool async)
{
await base.Where_math_radians(async);

AssertSql(
"""
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
FROM [Order Details] AS [o]
WHERE [o].[OrderID] = 11077 AND RADIANS(CAST([o].[Discount] AS float)) > 0.0E0
""");
}

public override async Task Where_mathf_abs1(bool async)
{
await base.Where_mathf_abs1(async);
Expand Down Expand Up @@ -1599,6 +1623,30 @@ public override async Task Where_mathf_sign(bool async)
""");
}

public override async Task Where_mathf_degrees(bool async)
{
await base.Where_mathf_degrees(async);

AssertSql(
"""
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
FROM [Order Details] AS [o]
WHERE [o].[OrderID] = 11077 AND DEGREES([o].[Discount]) > CAST(0 AS real)
""");
}

public override async Task Where_mathf_radians(bool async)
{
await base.Where_mathf_radians(async);

AssertSql(
"""
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
FROM [Order Details] AS [o]
WHERE [o].[OrderID] = 11077 AND RADIANS([o].[Discount]) > CAST(0 AS real)
""");
}

public override async Task Where_guid_newguid(bool async)
{
await base.Where_guid_newguid(async);
Expand Down
Expand Up @@ -144,6 +144,12 @@ public override Task Where_math_tan(bool async)
public override Task Where_math_truncate(bool async)
=> AssertTranslationFailed(() => base.Where_math_truncate(async));

public override Task Where_math_degrees(bool async)
=> AssertTranslationFailed(() => base.Where_math_degrees(async));

public override Task Where_math_radians(bool async)
=> AssertTranslationFailed(() => base.Where_math_radians(async));

public override Task Where_mathf_acos(bool async)
=> AssertTranslationFailed(() => base.Where_mathf_acos(async));

Expand Down Expand Up @@ -207,6 +213,12 @@ public override Task Where_mathf_tan(bool async)
public override Task Where_mathf_truncate(bool async)
=> AssertTranslationFailed(() => base.Where_mathf_truncate(async));

public override Task Where_mathf_degrees(bool async)
=> AssertTranslationFailed(() => base.Where_mathf_degrees(async));

public override Task Where_mathf_radians(bool async)
=> AssertTranslationFailed(() => base.Where_mathf_radians(async));

public override async Task String_StartsWith_Literal(bool async)
{
await base.String_StartsWith_Literal(async);
Expand Down

0 comments on commit 0c447b6

Please sign in to comment.