Skip to content

Commit c983103

Browse files
committed
SQLite: Translate Math.Sign
Part of #18843
1 parent 63dde72 commit c983103

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/EFCore.Sqlite.Core/Query/Internal/SqliteMathTranslator.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ public class SqliteMathTranslator : IMethodCallTranslator
4242
{ typeof(Math).GetMethod(nameof(Math.Min), new[] { typeof(ushort), typeof(ushort) })!, "min" },
4343
{ typeof(Math).GetMethod(nameof(Math.Round), new[] { typeof(double) })!, "round" },
4444
{ typeof(Math).GetMethod(nameof(Math.Round), new[] { typeof(double), typeof(int) })!, "round" },
45+
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(double) })!, "sign" },
46+
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(float) })!, "sign" },
47+
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(long) })!, "sign" },
48+
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(sbyte) })!, "sign" },
49+
{ typeof(Math).GetMethod(nameof(Math.Sign), new[] { typeof(short) })!, "sign" },
4550
{ typeof(MathF).GetMethod(nameof(MathF.Abs), new[] { typeof(float) })!, "abs" },
4651
{ typeof(MathF).GetMethod(nameof(MathF.Max), new[] { typeof(float), typeof(float) })!, "max" },
4752
{ typeof(MathF).GetMethod(nameof(MathF.Min), new[] { typeof(float), typeof(float) })!, "min" },
4853
{ typeof(MathF).GetMethod(nameof(MathF.Round), new[] { typeof(float) })!, "round" },
49-
{ typeof(MathF).GetMethod(nameof(MathF.Round), new[] { typeof(float), typeof(int) })!, "round" }
54+
{ typeof(MathF).GetMethod(nameof(MathF.Round), new[] { typeof(float), typeof(int) })!, "round" },
55+
{ typeof(MathF).GetMethod(nameof(MathF.Sign), new[] { typeof(float) })!, "sign" }
5056
};
5157

5258
private readonly ISqlExpressionFactory _sqlExpressionFactory;
@@ -78,7 +84,7 @@ public SqliteMathTranslator(ISqlExpressionFactory sqlExpressionFactory)
7884
{
7985
RelationalTypeMapping? typeMapping;
8086
List<SqlExpression>? newArguments = null;
81-
if (sqlFunctionName == "max" || sqlFunctionName == "max")
87+
if (sqlFunctionName == "max" || sqlFunctionName == "min")
8288
{
8389
typeMapping = ExpressionExtensions.InferTypeMapping(arguments[0], arguments[1]);
8490
newArguments = new List<SqlExpression>

test/EFCore.Sqlite.FunctionalTests/Query/NorthwindFunctionsQuerySqliteTest.cs

+22-4
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,17 @@ public override Task Sum_over_truncate_works_correctly_in_projection_2(bool asyn
120120
public override Task Where_math_round2(bool async)
121121
=> AssertTranslationFailed(() => base.Where_math_round2(async));
122122

123-
public override Task Where_math_sign(bool async)
124-
=> AssertTranslationFailed(() => base.Where_math_sign(async));
123+
public override async Task Where_math_sign(bool async)
124+
{
125+
await base.Where_math_sign(async);
126+
127+
AssertSql(
128+
"""
129+
SELECT "o"."OrderID", "o"."ProductID", "o"."Discount", "o"."Quantity", "o"."UnitPrice"
130+
FROM "Order Details" AS "o"
131+
WHERE "o"."OrderID" = 11077 AND sign("o"."Discount") > 0
132+
""");
133+
}
125134

126135
public override Task Where_math_sin(bool async)
127136
=> AssertTranslationFailed(() => base.Where_math_sin(async));
@@ -174,8 +183,17 @@ public override Task Where_mathf_power(bool async)
174183
public override Task Where_mathf_square(bool async)
175184
=> AssertTranslationFailed(() => base.Where_mathf_square(async));
176185

177-
public override Task Where_mathf_sign(bool async)
178-
=> AssertTranslationFailed(() => base.Where_mathf_sign(async));
186+
public override async Task Where_mathf_sign(bool async)
187+
{
188+
await base.Where_mathf_sign(async);
189+
190+
AssertSql(
191+
"""
192+
SELECT "o"."OrderID", "o"."ProductID", "o"."Discount", "o"."Quantity", "o"."UnitPrice"
193+
FROM "Order Details" AS "o"
194+
WHERE "o"."OrderID" = 11077 AND sign("o"."Discount") > 0
195+
""");
196+
}
179197

180198
public override Task Where_mathf_sin(bool async)
181199
=> AssertTranslationFailed(() => base.Where_mathf_sin(async));

0 commit comments

Comments
 (0)