Skip to content

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

Merged
smitpatel merged 3 commits into
dotnet:mainfrom
yesmey:orderby-before-distinct
Mar 15, 2021
Merged

#23267 Warn when Orderby is silently ignored when using Distinct#24160
smitpatel merged 3 commits into
dotnet:mainfrom
yesmey:orderby-before-distinct

Conversation

@yesmey

@yesmey yesmey commented Feb 15, 2021

Copy link
Copy Markdown
Contributor

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

Comment thread test/EFCore.Specification.Tests/Query/InheritanceQueryTestBase.cs Outdated
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
Copy Markdown
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
Copy Markdown
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?

Comment thread test/EFCore.Cosmos.FunctionalTests/Query/InheritanceQueryCosmosTest.cs Outdated
@smitpatel

Copy link
Copy Markdown
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
Copy Markdown
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
Comment thread src/EFCore/Properties/CoreStrings.resx Outdated
<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
Copy Markdown
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

Comment thread src/EFCore/Diagnostics/CoreEventId.cs Outdated
/// This event is in the <see cref="DbLoggerCategory.Query" /> category.
/// </para>
/// </summary>
public static readonly EventId DistinctAfterOrderByWarning

@smitpatel smitpatel Mar 12, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

DistinctAfterOrderByWithoutRowLimitingOperatorWarning

Comment thread src/EFCore/Properties/CoreStrings.resx Outdated
<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
Copy Markdown
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>

Comment thread src/EFCore/Properties/CoreStrings.resx Outdated
</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
Copy Markdown
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>

yesmey added 3 commits March 13, 2021 00:11
Revert subquery_oftype test modifications. Only warn when Limit/Offset is not specified
Update text in LogRowLimitingOperationWithoutOrderBy
@yesmey
yesmey force-pushed the orderby-before-distinct branch from e2f012d to 6e92cda Compare March 14, 2021 11:39
@yesmey

yesmey commented Mar 14, 2021

Copy link
Copy Markdown
Contributor Author

@smitpatel please review the changes in 6e92cdaf779e809954efb43c5de085d1d9bab841

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

Copy link
Copy Markdown
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