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

Add translations for greatest/least,math.min/math.max #270

Merged
merged 3 commits into from
Oct 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,44 @@ private Expression TranslateByteArrayElementAccess(Expression array, Expression
: QueryCompilationContext.NotTranslatedExpression;
}

public override SqlExpression? GenerateGreatest(IReadOnlyList<SqlExpression> expressions, Type resultType)
{
if (expressions.Count == 0)
{
return null;
}

return expressions.Aggregate((current, next) =>
Dependencies.SqlExpressionFactory.Case(
new[]
{
new CaseWhenClause(
Dependencies.SqlExpressionFactory.GreaterThan(current, next),
current)
},
elseResult: next));
}

public override SqlExpression? GenerateLeast(IReadOnlyList<SqlExpression> expressions, Type resultType)
{
if (expressions.Count == 0)
{
return null;
}

return expressions.Aggregate((current, next) =>
Dependencies.SqlExpressionFactory.Case(
new[]
{
new CaseWhenClause(
Dependencies.SqlExpressionFactory.LessThan(current, next),
current)
},
elseResult: next));
}


private static string? GetProviderType(SqlExpression expression)
=> expression.TypeMapping?.StoreType;

}
Original file line number Diff line number Diff line change
Expand Up @@ -13238,8 +13238,16 @@ EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Whe
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log(isAsync: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log10(isAsync: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log10(isAsync: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested_twice(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested_twice(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested_twice(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested_twice(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_power(isAsync: False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14528,8 +14528,16 @@ EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Whe
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log(isAsync: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log10(isAsync: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_log10(isAsync: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested_twice(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested_twice(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max_nested(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_max(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested_twice(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested_twice(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min_nested(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min(async: False)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_min(async: True)
EntityFrameworkCore.Jet.FunctionalTests.Query.NorthwindFunctionsQueryJetTest.Where_math_power(isAsync: False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2948,10 +2948,12 @@ public override async Task Nav_rewrite_doesnt_apply_null_protection_for_function
await base.Nav_rewrite_doesnt_apply_null_protection_for_function_arguments(isAsync);

AssertSql(
$@"SELECT `l0`.`Level1_Required_Id`
"""
SELECT IIF(`l0`.`Level1_Required_Id` > 7, `l0`.`Level1_Required_Id`, 7)
FROM `LevelOne` AS `l`
LEFT JOIN `LevelTwo` AS `l0` ON `l`.`Id` = `l0`.`OneToOne_Optional_PK_Inverse2Id`
WHERE `l0`.`Id` IS NOT NULL");
WHERE `l0`.`Id` IS NOT NULL
""");
}

public override async Task Accessing_optional_property_inside_result_operator_subquery(bool isAsync)
Expand Down
52 changes: 44 additions & 8 deletions test/EFCore.Jet.FunctionalTests/Query/DbFunctionsJetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,53 @@ SELECT COUNT(*)
""");
}

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

AssertSql(
"""
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `Order Details` AS `o`
WHERE IIF(`o`.`OrderID` < 10251, `o`.`OrderID`, 10251) = 10251
""");
}

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

AssertSql(
"""
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `Order Details` AS `o`
WHERE IIF(`o`.`OrderID` > 10251, `o`.`OrderID`, 10251) = 10251
""");
}

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

public override Task Greatest(bool async)
=> AssertTranslationFailed(() => base.Greatest(async));
AssertSql(
"""
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `Order Details` AS `o`
WHERE IIF(`o`.`OrderID` < 10251, `o`.`OrderID`, 10251) = 10251
""");
}

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

public override Task Greatest_with_nullable_value_type(bool async)
=> AssertTranslationFailed(() => base.Greatest_with_nullable_value_type(async));
AssertSql(
"""
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `Order Details` AS `o`
WHERE IIF(`o`.`OrderID` > 10251, `o`.`OrderID`, 10251) = 10251
""");
}

public override async Task Least_with_parameter_array_is_not_supported(bool async)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1352,9 +1352,14 @@ public override async Task Where_math_sign(bool isAsync)
public override async Task Where_math_min(bool async)
{
// Translate Math.Min.
await AssertTranslationFailed(() => base.Where_math_min(async));
await base.Where_math_min(async);

AssertSql();
AssertSql(
"""
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `Order Details` AS `o`
WHERE `o`.`OrderID` = 11077 AND IIF(`o`.`OrderID` < `o`.`ProductID`, `o`.`OrderID`, `o`.`ProductID`) = `o`.`ProductID`
""");
}

public override async Task Where_math_min_nested(bool async)
Expand All @@ -1363,9 +1368,9 @@ public override async Task Where_math_min_nested(bool async)

AssertSql(
"""
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
FROM [Order Details] AS [o]
WHERE [o].[OrderID] = 11077 AND LEAST([o].[OrderID], [o].[ProductID], 99999) = [o].[ProductID]
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `Order Details` AS `o`
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`
""");
}

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

AssertSql(
"""
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
FROM [Order Details] AS [o]
WHERE [o].[OrderID] = 11077 AND LEAST(99999, [o].[OrderID], 99998, [o].[ProductID]) = [o].[ProductID]
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `Order Details` AS `o`
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`
""");
}

public override async Task Where_math_max(bool async)
{
// Translate Math.Max.
await AssertTranslationFailed(() => base.Where_math_max(async));
await base.Where_math_max(async);

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

public override async Task Where_math_max_nested(bool async)
Expand All @@ -1395,9 +1405,9 @@ public override async Task Where_math_max_nested(bool async)

AssertSql(
"""
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
FROM [Order Details] AS [o]
WHERE [o].[OrderID] = 11077 AND GREATEST([o].[OrderID], [o].[ProductID], 1) = [o].[OrderID]
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `Order Details` AS `o`
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`
""");
}

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

AssertSql(
"""
SELECT [o].[OrderID], [o].[ProductID], [o].[Discount], [o].[Quantity], [o].[UnitPrice]
FROM [Order Details] AS [o]
WHERE [o].[OrderID] = 11077 AND GREATEST(1, [o].[OrderID], 2, [o].[ProductID]) = [o].[OrderID]
SELECT `o`.`OrderID`, `o`.`ProductID`, `o`.`Discount`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `Order Details` AS `o`
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`
""");
}

Expand Down