Skip to content
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
7 changes: 4 additions & 3 deletions src/EFCore.Relational/Query/EnumerableExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public virtual EnumerableExpression ApplySelector(Expression expression)
=> new(expression, IsDistinct, Predicate, Orderings);

/// <summary>
/// Applies DISTINCT operator to the selector of the <see cref="EnumerableExpression" />.
/// Sets whether the DISTINCT operator should be applied to the selector
/// of the <see cref="EnumerableExpression" />.
/// </summary>
/// <returns>The new expression with specified component updated.</returns>
public virtual EnumerableExpression ApplyDistinct()
=> new(Selector, distinct: true, Predicate, Orderings);
public virtual EnumerableExpression SetDistinct(bool value)
=> new(Selector, distinct: value, Predicate, Orderings);

/// <summary>
/// Applies filter predicate to the <see cref="EnumerableExpression" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,9 @@ private SqlExpression CreateJoinPredicate(Expression outerKey, Expression innerK
/// <inheritdoc />
protected override ShapedQueryExpression? TranslateMax(ShapedQueryExpression source, LambdaExpression? selector, Type resultType)
{
var selectExpression = (SelectExpression)source.QueryExpression;
selectExpression.IsDistinct = false;

// For Max() over an inline array, translate to GREATEST() if possible; otherwise use the default translation of aggregate SQL
// MAX().
// Note that some providers propagate NULL arguments (SQLite, MySQL), while others only return NULL if all arguments evaluate to
Expand All @@ -974,6 +977,9 @@ private SqlExpression CreateJoinPredicate(Expression outerKey, Expression innerK
/// <inheritdoc />
protected override ShapedQueryExpression? TranslateMin(ShapedQueryExpression source, LambdaExpression? selector, Type resultType)
{
var selectExpression = (SelectExpression)source.QueryExpression;
selectExpression.IsDistinct = false;

// See comments above in TranslateMax()
if (TryExtractBareInlineCollectionValues(source, out var values)
&& _sqlTranslator.GenerateLeast(values, resultType.UnwrapNullableType()) is SqlFunctionExpression leastExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1362,12 +1362,15 @@ protected virtual bool TryTranslateAggregateMethodCall(
{
switch (genericMethod.Name)
{
case nameof(Queryable.Average)
when QueryableMethods.IsAverageWithoutSelector(genericMethod):
case nameof(Queryable.Max)
when genericMethod == QueryableMethods.MaxWithoutSelector:
case nameof(Queryable.Min)
when genericMethod == QueryableMethods.MinWithoutSelector:
enumerableExpression = enumerableExpression.SetDistinct(false);
break;

case nameof(Queryable.Average)
when QueryableMethods.IsAverageWithoutSelector(genericMethod):
case nameof(Queryable.Sum)
when QueryableMethods.IsSumWithoutSelector(genericMethod):
case nameof(Queryable.Count)
Expand All @@ -1376,12 +1379,16 @@ when QueryableMethods.IsSumWithoutSelector(genericMethod):
when genericMethod == QueryableMethods.LongCountWithoutPredicate:
break;

case nameof(Queryable.Average)
when QueryableMethods.IsAverageWithSelector(genericMethod):
case nameof(Queryable.Max)
when genericMethod == QueryableMethods.MaxWithSelector:
case nameof(Queryable.Min)
when genericMethod == QueryableMethods.MinWithSelector:
enumerableExpression = enumerableExpression.SetDistinct(false);
enumerableExpression = ProcessSelector(enumerableExpression, arguments[1].UnwrapLambdaFromQuote());
break;

case nameof(Queryable.Average)
when QueryableMethods.IsAverageWithSelector(genericMethod):
case nameof(Queryable.Sum)
when QueryableMethods.IsSumWithSelector(genericMethod):
enumerableExpression = ProcessSelector(enumerableExpression, arguments[1].UnwrapLambdaFromQuote());
Expand Down Expand Up @@ -1478,7 +1485,7 @@ private bool TryTranslateAsEnumerableExpression(

if (!enumerableSource.IsDistinct)
{
enumerableExpression = enumerableSource.ApplyDistinct();
enumerableExpression = enumerableSource.SetDistinct(true);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2475,7 +2475,7 @@ public override async Task GroupBy_Select_Distinct_aggregate(bool async)

AssertSql(
"""
SELECT [o].[CustomerID] AS [Key], AVG(DISTINCT (CAST([o].[OrderID] AS float))) AS [Average], COUNT(DISTINCT ([o].[EmployeeID])) AS [Count], COUNT_BIG(DISTINCT ([o].[EmployeeID])) AS [LongCount], MAX(DISTINCT ([o].[OrderDate])) AS [Max], MIN(DISTINCT ([o].[OrderDate])) AS [Min], COALESCE(SUM(DISTINCT ([o].[OrderID])), 0) AS [Sum]
SELECT [o].[CustomerID] AS [Key], AVG(DISTINCT (CAST([o].[OrderID] AS float))) AS [Average], COUNT(DISTINCT ([o].[EmployeeID])) AS [Count], COUNT_BIG(DISTINCT ([o].[EmployeeID])) AS [LongCount], MAX([o].[OrderDate]) AS [Max], MIN([o].[OrderDate]) AS [Min], COALESCE(SUM(DISTINCT ([o].[OrderID])), 0) AS [Sum]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
""");
Expand All @@ -2487,7 +2487,7 @@ public override async Task GroupBy_group_Distinct_Select_Distinct_aggregate(bool

AssertSql(
"""
SELECT [o].[CustomerID] AS [Key], MAX(DISTINCT ([o].[OrderDate])) AS [Max]
SELECT [o].[CustomerID] AS [Key], MAX([o].[OrderDate]) AS [Max]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
""");
Expand All @@ -2499,9 +2499,9 @@ public override async Task GroupBy_group_Where_Select_Distinct_aggregate(bool as

AssertSql(
"""
SELECT [o].[CustomerID] AS [Key], MAX(DISTINCT (CASE
SELECT [o].[CustomerID] AS [Key], MAX(CASE
WHEN [o].[OrderDate] IS NOT NULL THEN [o].[OrderDate]
END)) AS [Max]
END) AS [Max]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4744,11 +4744,8 @@ public override async Task Select_distinct_max(bool async)

AssertSql(
"""
SELECT MAX([o0].[OrderID])
FROM (
SELECT DISTINCT [o].[OrderID]
FROM [Orders] AS [o]
) AS [o0]
SELECT MAX([o].[OrderID])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these tests seem to be aimed at checking exactly this behavior; should I add something similar or are they adequate?

Copy link
Member

Choose a reason for hiding this comment

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

They look good to me...

FROM [Orders] AS [o]
""");
}

Expand All @@ -4758,11 +4755,8 @@ public override async Task Select_distinct_min(bool async)

AssertSql(
"""
SELECT MIN([o0].[OrderID])
FROM (
SELECT DISTINCT [o].[OrderID]
FROM [Orders] AS [o]
) AS [o0]
SELECT MIN([o].[OrderID])
FROM [Orders] AS [o]
""");
}

Expand Down