Skip to content

Commit 1de9eb4

Browse files
authored
Add translations for greatest/least,math.min/math.max (#270)
* Add translations for greatest/least,math.min/math.max using if statements
1 parent ec6c702 commit 1de9eb4

File tree

6 files changed

+128
-26
lines changed

6 files changed

+128
-26
lines changed

src/EFCore.Jet/Query/Internal/JetSqlTranslatingExpressionVisitor.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,44 @@ private Expression TranslateByteArrayElementAccess(Expression array, Expression
452452
: QueryCompilationContext.NotTranslatedExpression;
453453
}
454454

455+
public override SqlExpression? GenerateGreatest(IReadOnlyList<SqlExpression> expressions, Type resultType)
456+
{
457+
if (expressions.Count == 0)
458+
{
459+
return null;
460+
}
461+
462+
return expressions.Aggregate((current, next) =>
463+
Dependencies.SqlExpressionFactory.Case(
464+
new[]
465+
{
466+
new CaseWhenClause(
467+
Dependencies.SqlExpressionFactory.GreaterThan(current, next),
468+
current)
469+
},
470+
elseResult: next));
471+
}
472+
473+
public override SqlExpression? GenerateLeast(IReadOnlyList<SqlExpression> expressions, Type resultType)
474+
{
475+
if (expressions.Count == 0)
476+
{
477+
return null;
478+
}
479+
480+
return expressions.Aggregate((current, next) =>
481+
Dependencies.SqlExpressionFactory.Case(
482+
new[]
483+
{
484+
new CaseWhenClause(
485+
Dependencies.SqlExpressionFactory.LessThan(current, next),
486+
current)
487+
},
488+
elseResult: next));
489+
}
490+
491+
455492
private static string? GetProviderType(SqlExpression expression)
456493
=> expression.TypeMapping?.StoreType;
494+
457495
}

test/EFCore.Jet.FunctionalTests/GreenTests/ace_2010_odbc_x86.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13238,8 +13238,16 @@ EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Whe
1323813238
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log(isAsync: True)
1323913239
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log10(isAsync: False)
1324013240
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log10(isAsync: True)
13241+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested_twice(async: False)
13242+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested_twice(async: True)
13243+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested(async: False)
13244+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested(async: True)
1324113245
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max(async: False)
1324213246
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max(async: True)
13247+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested_twice(async: False)
13248+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested_twice(async: True)
13249+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested(async: False)
13250+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested(async: True)
1324313251
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min(async: False)
1324413252
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min(async: True)
1324513253
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_power(isAsync: False)

test/EFCore.Jet.FunctionalTests/GreenTests/ace_2010_oledb_x86.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14528,8 +14528,16 @@ EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Whe
1452814528
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log(isAsync: True)
1452914529
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log10(isAsync: False)
1453014530
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log10(isAsync: True)
14531+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested_twice(async: False)
14532+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested_twice(async: True)
14533+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested(async: False)
14534+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested(async: True)
1453114535
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max(async: False)
1453214536
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max(async: True)
14537+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested_twice(async: False)
14538+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested_twice(async: True)
14539+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested(async: False)
14540+
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested(async: True)
1453314541
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min(async: False)
1453414542
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min(async: True)
1453514543
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_power(isAsync: False)

test/EFCore.Jet.FunctionalTests/Query/ComplexNavigationsQueryJetTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,10 +2948,12 @@ public override async Task Nav_rewrite_doesnt_apply_null_protection_for_function
29482948
await base.Nav_rewrite_doesnt_apply_null_protection_for_function_arguments(isAsync);
29492949

29502950
AssertSql(
2951-
$@"SELECT `l0`.`Level1_Required_Id`
2951+
"""
2952+
SELECT IIF(`l0`.`Level1_Required_Id` > 7, `l0`.`Level1_Required_Id`, 7)
29522953
FROM `LevelOne` AS `l`
29532954
LEFT JOIN `LevelTwo` AS `l0` ON `l`.`Id` = `l0`.`OneToOne_Optional_PK_Inverse2Id`
2954-
WHERE `l0`.`Id` IS NOT NULL");
2955+
WHERE `l0`.`Id` IS NOT NULL
2956+
""");
29552957
}
29562958

29572959
public override async Task Accessing_optional_property_inside_result_operator_subquery(bool isAsync)

test/EFCore.Jet.FunctionalTests/Query/DbFunctionsJetTest.cs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,53 @@ SELECT COUNT(*)
138138
""");
139139
}
140140

141-
public override Task Least(bool async)
142-
=> AssertTranslationFailed(() => base.Least(async));
141+
public override async Task Least(bool async)
142+
{
143+
await base.Least(async);
144+
145+
AssertSql(
146+
"""
147+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
148+
FROM `Order Details` AS `o`
149+
WHERE IIF(`o`.`OrderID` < 10251, `o`.`OrderID`, 10251) = 10251
150+
""");
151+
}
152+
153+
public override async Task Greatest(bool async)
154+
{
155+
await base.Greatest(async);
156+
157+
AssertSql(
158+
"""
159+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
160+
FROM `Order Details` AS `o`
161+
WHERE IIF(`o`.`OrderID` > 10251, `o`.`OrderID`, 10251) = 10251
162+
""");
163+
}
164+
165+
public override async Task Least_with_nullable_value_type(bool async)
166+
{
167+
await base.Least_with_nullable_value_type(async);
143168

144-
public override Task Greatest(bool async)
145-
=> AssertTranslationFailed(() => base.Greatest(async));
169+
AssertSql(
170+
"""
171+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
172+
FROM `Order Details` AS `o`
173+
WHERE IIF(`o`.`OrderID` < 10251, `o`.`OrderID`, 10251) = 10251
174+
""");
175+
}
146176

147-
public override Task Least_with_nullable_value_type(bool async)
148-
=> AssertTranslationFailed(() => base.Least_with_nullable_value_type(async));
177+
public override async Task Greatest_with_nullable_value_type(bool async)
178+
{
179+
await base.Greatest_with_nullable_value_type(async);
149180

150-
public override Task Greatest_with_nullable_value_type(bool async)
151-
=> AssertTranslationFailed(() => base.Greatest_with_nullable_value_type(async));
181+
AssertSql(
182+
"""
183+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
184+
FROM `Order Details` AS `o`
185+
WHERE IIF(`o`.`OrderID` > 10251, `o`.`OrderID`, 10251) = 10251
186+
""");
187+
}
152188

153189
public override async Task Least_with_parameter_array_is_not_supported(bool async)
154190
{

test/EFCore.Jet.FunctionalTests/Query/NorthwindFunctionsQueryJetTest.Functions.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,9 +1352,14 @@ public override async Task Where_math_sign(bool isAsync)
13521352
public override async Task Where_math_min(bool async)
13531353
{
13541354
// Translate Math.Min.
1355-
await AssertTranslationFailed(() => base.Where_math_min(async));
1355+
await base.Where_math_min(async);
13561356

1357-
AssertSql();
1357+
AssertSql(
1358+
"""
1359+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
1360+
FROM `Order Details` AS `o`
1361+
WHERE `o`.`OrderID` = 11077 AND IIF(`o`.`OrderID` < `o`.`ProductID`, `o`.`OrderID`, `o`.`ProductID`) = `o`.`ProductID`
1362+
""");
13581363
}
13591364

13601365
public override async Task Where_math_min_nested(bool async)
@@ -1363,9 +1368,9 @@ public override async Task Where_math_min_nested(bool async)
13631368

13641369
AssertSql(
13651370
"""
1366-
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
1367-
FROM [Order Details] AS [o]
1368-
WHERE [o].[OrderID] = 11077 AND LEAST([o].[OrderID], [o].[ProductID], 99999) = [o].[ProductID]
1371+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
1372+
FROM `Order Details` AS `o`
1373+
WHERE `o`.`OrderID` = 11077 AND IIF(IIF(`o`.`OrderID` < `o`.`ProductID`, `o`.`OrderID`, `o`.`ProductID`) < 99999, IIF(`o`.`OrderID` < `o`.`ProductID`, `o`.`OrderID`, `o`.`ProductID`), 99999) = `o`.`ProductID`
13691374
""");
13701375
}
13711376

@@ -1375,18 +1380,23 @@ public override async Task Where_math_min_nested_twice(bool async)
13751380

13761381
AssertSql(
13771382
"""
1378-
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
1379-
FROM [Order Details] AS [o]
1380-
WHERE [o].[OrderID] = 11077 AND LEAST(99999, [o].[OrderID], 99998, [o].[ProductID]) = [o].[ProductID]
1383+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
1384+
FROM `Order Details` AS `o`
1385+
WHERE `o`.`OrderID` = 11077 AND IIF(IIF(IIF(99999 < `o`.`OrderID`, 99999, `o`.`OrderID`) < 99998, IIF(99999 < `o`.`OrderID`, 99999, `o`.`OrderID`), 99998) < `o`.`ProductID`, IIF(IIF(99999 < `o`.`OrderID`, 99999, `o`.`OrderID`) < 99998, IIF(99999 < `o`.`OrderID`, 99999, `o`.`OrderID`), 99998), `o`.`ProductID`) = `o`.`ProductID`
13811386
""");
13821387
}
13831388

13841389
public override async Task Where_math_max(bool async)
13851390
{
13861391
// Translate Math.Max.
1387-
await AssertTranslationFailed(() => base.Where_math_max(async));
1392+
await base.Where_math_max(async);
13881393

1389-
AssertSql();
1394+
AssertSql(
1395+
"""
1396+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
1397+
FROM `Order Details` AS `o`
1398+
WHERE `o`.`OrderID` = 11077 AND IIF(`o`.`OrderID` > `o`.`ProductID`, `o`.`OrderID`, `o`.`ProductID`) = `o`.`OrderID`
1399+
""");
13901400
}
13911401

13921402
public override async Task Where_math_max_nested(bool async)
@@ -1395,9 +1405,9 @@ public override async Task Where_math_max_nested(bool async)
13951405

13961406
AssertSql(
13971407
"""
1398-
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
1399-
FROM [Order Details] AS [o]
1400-
WHERE [o].[OrderID] = 11077 AND GREATEST([o].[OrderID], [o].[ProductID], 1) = [o].[OrderID]
1408+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
1409+
FROM `Order Details` AS `o`
1410+
WHERE `o`.`OrderID` = 11077 AND IIF(IIF(`o`.`OrderID` > `o`.`ProductID`, `o`.`OrderID`, `o`.`ProductID`) > 1, IIF(`o`.`OrderID` > `o`.`ProductID`, `o`.`OrderID`, `o`.`ProductID`), 1) = `o`.`OrderID`
14011411
""");
14021412
}
14031413

@@ -1407,9 +1417,9 @@ public override async Task Where_math_max_nested_twice(bool async)
14071417

14081418
AssertSql(
14091419
"""
1410-
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
1411-
FROM [Order Details] AS [o]
1412-
WHERE [o].[OrderID] = 11077 AND GREATEST(1, [o].[OrderID], 2, [o].[ProductID]) = [o].[OrderID]
1420+
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
1421+
FROM `Order Details` AS `o`
1422+
WHERE `o`.`OrderID` = 11077 AND IIF(IIF(IIF(1 > `o`.`OrderID`, 1, `o`.`OrderID`) > 2, IIF(1 > `o`.`OrderID`, 1, `o`.`OrderID`), 2) > `o`.`ProductID`, IIF(IIF(1 > `o`.`OrderID`, 1, `o`.`OrderID`) > 2, IIF(1 > `o`.`OrderID`, 1, `o`.`OrderID`), 2), `o`.`ProductID`) = `o`.`OrderID`
14131423
""");
14141424
}
14151425

0 commit comments

Comments
 (0)