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

SQLite: Translate Math.Sign #30925

Merged
merged 1 commit into from
May 19, 2023
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
10 changes: 8 additions & 2 deletions src/EFCore.Sqlite.Core/Query/Internal/SqliteMathTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ public class SqliteMathTranslator : IMethodCallTranslator
{ typeof(Math).GetMethod(nameof(Math.Min), new[] { typeof(ushort), typeof(ushort) })!, "min" },
{ typeof(Math).GetMethod(nameof(Math.Round), new[] { typeof(double) })!, "round" },
{ typeof(Math).GetMethod(nameof(Math.Round), new[] { typeof(double), typeof(int) })!, "round" },
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(double) })!, "sign" },
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(float) })!, "sign" },
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(long) })!, "sign" },
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(sbyte) })!, "sign" },
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(short) })!, "sign" },
{ typeof(MathF).GetMethod(nameof(MathF.Abs), new[] { typeof(float) })!, "abs" },
{ typeof(MathF).GetMethod(nameof(MathF.Max), new[] { typeof(float), typeof(float) })!, "max" },
{ typeof(MathF).GetMethod(nameof(MathF.Min), new[] { typeof(float), typeof(float) })!, "min" },
{ typeof(MathF).GetMethod(nameof(MathF.Round), new[] { typeof(float) })!, "round" },
{ typeof(MathF).GetMethod(nameof(MathF.Round), new[] { typeof(float), typeof(int) })!, "round" }
{ typeof(MathF).GetMethod(nameof(MathF.Round), new[] { typeof(float), typeof(int) })!, "round" },
{ typeof(MathF).GetMethod(nameof(MathF.Sign), new[] { typeof(float) })!, "sign" }
};

private readonly ISqlExpressionFactory _sqlExpressionFactory;
Expand Down Expand Up @@ -78,7 +84,7 @@ public SqliteMathTranslator(ISqlExpressionFactory sqlExpressionFactory)
{
RelationalTypeMapping? typeMapping;
List<SqlExpression>? newArguments = null;
if (sqlFunctionName == "max" || sqlFunctionName == "max")
if (sqlFunctionName == "max" || sqlFunctionName == "min")
Copy link
Member

Choose a reason for hiding this comment

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

Oops - so we weren't inferring correctly for min previous...

Suggested change
if (sqlFunctionName == "max" || sqlFunctionName == "min")
if (sqlFunctionName is "min" or "max")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lol, I reject your new age sorcery. (I'm going to change it in the big Math PR anyway.)

Oops - so we weren't inferring correctly for min previous...

Yeah, but we'd still infer it from the first argument which is most likely your column anyway. Also type-mappings are a lot less important on SQLite since you can just ask the ADO.NET provider for whatever type you want. So yeah, there was probably a corner-case involving value converters somewhere that this fixes.

Copy link
Member

Choose a reason for hiding this comment

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

lol, I reject your new age sorcery. (I'm going to change it in the big Math PR anyway.)

Oh that's nothing, take a look at what @maumar has to put up with 🤣

{
typeMapping = ExpressionExtensions.InferTypeMapping(arguments[0], arguments[1]);
newArguments = new List<SqlExpression>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,17 @@ public override Task Sum_over_truncate_works_correctly_in_projection_2(bool asyn
public override Task Where_math_round2(bool async)
=> AssertTranslationFailed(() => base.Where_math_round2(async));

public override Task Where_math_sign(bool async)
=> AssertTranslationFailed(() => base.Where_math_sign(async));
public override async Task Where_math_sign(bool async)
{
await base.Where_math_sign(async);

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

public override Task Where_math_sin(bool async)
=> AssertTranslationFailed(() => base.Where_math_sin(async));
Expand Down Expand Up @@ -174,8 +183,17 @@ public override Task Where_mathf_power(bool async)
public override Task Where_mathf_square(bool async)
=> AssertTranslationFailed(() => base.Where_mathf_square(async));

public override Task Where_mathf_sign(bool async)
=> AssertTranslationFailed(() => base.Where_mathf_sign(async));
public override async Task Where_mathf_sign(bool async)
{
await base.Where_mathf_sign(async);

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

public override Task Where_mathf_sin(bool async)
=> AssertTranslationFailed(() => base.Where_mathf_sin(async));
Expand Down