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

#23267 Warn when Orderby is silently ignored when using Distinct #24160

Merged
merged 3 commits into from
Mar 15, 2021

Conversation

yesmey
Copy link
Contributor

@yesmey yesmey commented Feb 15, 2021

Implement a warning for when using OrderBy before Distinct. The OrderBy will silently get ignored or cause unintentional behavior.

The reason and design behind this change can be read in #23267

In simple terms, the definition of Enumerable.Distinct() is that it returns an unordered result...

The Distinct(IEnumerable) method returns an unordered sequence that contains no duplicate values

... so when using a Distinct after a OrderBy - the previously specified order will get ignored

@@ -4410,7 +4410,7 @@ public virtual Task Correlated_collections_with_Distinct(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Squad>().OrderBy(s => s.Name).Select(s => s.Members.OrderBy(m => m.Nickname).Distinct()),
ss => ss.Set<Squad>().OrderBy(s => s.Name).Select(s => s.Members.Distinct().OrderBy(m => m.Nickname).ToList()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert this change and apply Skip(0) after OrderBy to continue generating LeftJoin.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It still generates a OUTER APPLY after the change. Any idea how to solve it it?

@smitpatel
Copy link
Contributor

@yesmey - Please hold on making any changes to PR. This doesn't feel exactly the warning we want to show. We will discuss with team what exactly we want to implement. Please monitor the tracking issue for any updates.

@smitpatel
Copy link
Contributor

@yesmey - Please refer to design meeting notes on original issue and update the PR accordingly.

@yesmey yesmey force-pushed the orderby-before-distinct branch 2 times, most recently from 555d137 to e19947a Compare March 4, 2021 15:57
@@ -765,6 +765,10 @@
<value>The query uses the 'First'/'FirstOrDefault' operator without 'OrderBy' and filter operators. This may lead to unpredictable results.</value>
<comment>Warning CoreEventId.FirstWithoutOrderByAndFilterWarning</comment>
</data>
<data name="LogDistinctAfterOrderBy" xml:space="preserve">
Copy link
Contributor

Choose a reason for hiding this comment

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

@dotnet/efteam - For exception message review

/// This event is in the <see cref="DbLoggerCategory.Query" /> category.
/// </para>
/// </summary>
public static readonly EventId DistinctAfterOrderByWarning
Copy link
Contributor

@smitpatel smitpatel Mar 12, 2021

Choose a reason for hiding this comment

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

DistinctAfterOrderByWithoutRowLimitingOperatorWarning

@@ -765,6 +765,10 @@
<value>The query uses the 'First'/'FirstOrDefault' operator without 'OrderBy' and filter operators. This may lead to unpredictable results.</value>
<comment>Warning CoreEventId.FirstWithoutOrderByAndFilterWarning</comment>
</data>
<data name="LogDistinctAfterOrderBy" xml:space="preserve">
<value>The query uses the 'Distinct' operator after a 'OrderBy' operator. This may lead to unpredictable results.</value>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
<value>The query uses the 'Distinct' operator after a 'OrderBy' operator. This may lead to unpredictable results.</value>
<value>The query uses the 'Distinct' operator after applying an ordering. If there are any row limiting operation used before `Distinct` and after ordering then ordering will be used for it. Ordering(s) will be erased after `Distinct` and results afterwards would be unordered.</value>

@@ -910,7 +914,7 @@
<comment>Debug CoreEventId.RequiredAttributeOnSkipNavigation string string</comment>
</data>
<data name="LogRowLimitingOperationWithoutOrderBy" xml:space="preserve">
<value>The query uses a row limiting operator ('Skip'/'Take') without an 'OrderBy' operator. This may lead to unpredictable results.</value>
<value>The query uses a row limiting operator ('Skip'/'Take') without an 'OrderBy' operator. This may lead to unpredictable results. If the 'Distinct' operator is used, then make sure to use the 'OrderBy' operator after 'Distinct' as the order would otherwise get erased.</value>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
<value>The query uses a row limiting operator ('Skip'/'Take') without an 'OrderBy' operator. This may lead to unpredictable results. If the 'Distinct' operator is used, then make sure to use the 'OrderBy' operator after 'Distinct' as the order would otherwise get erased.</value>
<value>The query uses a row limiting operator ('Skip'/'Take') without an 'OrderBy' operator. This may lead to unpredictable results. If the 'Distinct' operator is used after `OrderBy`, then make sure to use the 'OrderBy' operator after 'Distinct' as the ordering would otherwise get erased.</value>

Revert subquery_oftype test modifications. Only warn when Limit/Offset is not specified
Update text in LogRowLimitingOperationWithoutOrderBy
@yesmey
Copy link
Contributor Author

yesmey commented Mar 14, 2021

@smitpatel please review the changes in 6e92cdaf779e809954efb43c5de085d1d9bab841

@smitpatel smitpatel merged commit 4e66f15 into dotnet:main Mar 15, 2021
@smitpatel
Copy link
Contributor

@yesmey - Thank you for contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants