Skip to content

Commit

Permalink
Add Translation for MathF (dotnet#18842)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntovas committed May 8, 2021
1 parent 67a5d16 commit 7a098e1
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 4 deletions.
26 changes: 23 additions & 3 deletions src/EFCore.SqlServer/Query/Internal/SqlServerMathTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,41 @@ public class SqlServerMathTranslator : IMethodCallTranslator
{ 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.Sign), new[] { typeof(short) }), "SIGN" },
{ 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" }
};

private static readonly IEnumerable<MethodInfo> _truncateMethodInfos = new[]
{
typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Truncate), new[] { typeof(decimal) }),
typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Truncate), new[] { typeof(double) })
typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Truncate), new[] { typeof(double) }),
typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Truncate), new[] { typeof(float) })
};

private static readonly IEnumerable<MethodInfo> _roundMethodInfos = new[]
{
typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Round), new[] { typeof(decimal) }),
typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Round), new[] { typeof(double) }),
typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Round), new[] { typeof(decimal), typeof(int) }),
typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Round), new[] { typeof(double), typeof(int) })
typeof(Math).GetRequiredRuntimeMethod(nameof(Math.Round), new[] { typeof(double), typeof(int) }),
typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Round), new[] { typeof(float) }),
typeof(MathF).GetRequiredRuntimeMethod(nameof(MathF.Round), new[] { typeof(float), typeof(int) })
};

private readonly ISqlExpressionFactory _sqlExpressionFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public class SqliteMathTranslator : IMethodCallTranslator
{ typeof(Math).GetRequiredMethod(nameof(Math.Min), new[] { typeof(uint), typeof(uint) }), "min" },
{ typeof(Math).GetRequiredMethod(nameof(Math.Min), new[] { typeof(ushort), typeof(ushort) }), "min" },
{ typeof(Math).GetRequiredMethod(nameof(Math.Round), new[] { typeof(double) }), "round" },
{ typeof(Math).GetRequiredMethod(nameof(Math.Round), new[] { typeof(double), typeof(int) }), "round" }
{ typeof(Math).GetRequiredMethod(nameof(Math.Round), new[] { typeof(double), typeof(int) }), "round" },
{ typeof(MathF).GetRequiredMethod(nameof(MathF.Abs), new[] { typeof(float) }), "abs" },
{ typeof(MathF).GetRequiredMethod(nameof(MathF.Max), new[] { typeof(float), typeof(float) }), "max" },
{ typeof(MathF).GetRequiredMethod(nameof(MathF.Min), new[] { typeof(float), typeof(float) }), "min" },
{ typeof(MathF).GetRequiredMethod(nameof(MathF.Round), new[] { typeof(float) }), "round" },
{ typeof(MathF).GetRequiredMethod(nameof(MathF.Round), new[] { typeof(float), typeof(int) }), "round" }
};

private readonly ISqlExpressionFactory _sqlExpressionFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,247 @@ public virtual Task Where_math_min(bool async)
entryCount: 25);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_abs1(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Product>()
.Where(od => od.ProductID > MathF.Abs(-10.0F)),
entryCount: 67);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_ceiling1(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>()
.Where(od => od.UnitPrice < 7)
.Where(od => MathF.Ceiling(od.Discount) > 0),
entryCount: 51);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_floor(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>()
.Where(od => MathF.Floor(od.Discount) > 0),
entryCount: 137);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_power(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => MathF.Pow(od.Discount, 2) > 0.05f),
entryCount: 154);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_mathf_round_int(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Order>()
.Where(o => o.OrderID < 10250)
.Select(o => new { A = MathF.Round(o.OrderID) }),
e => e.A);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Select_mathf_truncate_int(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Order>()
.Where(o => o.OrderID < 10250)
.Select(o => new { A = MathF.Truncate(o.OrderID) }),
e => e.A);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_round2(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => MathF.Round((float)od.UnitPrice, 2) > 100),
entryCount: 46);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_truncate(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>()
.Where(od => od.Quantity < 5)
.Where(od => MathF.Truncate((float)od.UnitPrice) > 10),
entryCount: 137);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_exp(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Exp(od.Discount) > 1),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_log10(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077 && od.Discount > 0).Where(od => MathF.Log10(od.Discount) < 0),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_log(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077 && od.Discount > 0).Where(od => MathF.Log(od.Discount) < 0),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_log_new_base(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077 && od.Discount > 0).Where(od => MathF.Log(od.Discount, 7) < 0),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_sqrt(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Sqrt(od.Discount) > 0),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_acos(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Acos(od.Discount) > 1),
entryCount: 25);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_asin(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Asin(od.Discount) > 0),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_atan(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Atan(od.Discount) > 0),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_atan2(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Atan2(od.Discount, 1) > 0),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_cos(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Cos(od.Discount) > 0),
entryCount: 25);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_sin(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Sin(od.Discount) > 0),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_tan(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Tan(od.Discount) > 0),
entryCount: 13);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_sign(bool async)
{
return AssertQuery(
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_max(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077).Where(od => MathF.Max(od.OrderID, od.ProductID) == od.OrderID),
entryCount: 25);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_mathf_min(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OrderDetail>().Where(od => od.OrderID == 11077)
.Where(od => MathF.Min(od.OrderID, od.ProductID) == od.ProductID),
entryCount: 25);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_guid_newguid(bool async)
Expand Down

0 comments on commit 7a098e1

Please sign in to comment.