-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5785: Optimize LINQ translation for First() and FirstOrDefault() methods with predicates #1817
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
Merged
Merged
CSHARP-5785: Optimize LINQ translation for First() and FirstOrDefault() methods with predicates #1817
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
984874b
CSHARP-5785: Optimize LINQ translation for First() and FirstOrDefault…
adelinowona d5f7846
fix some more tests
adelinowona e13fd75
gate $filter limit optimization based on server version
adelinowona 8cc0d37
Restore UTF-8 BOM to edited files
adelinowona fed12d1
adress review comments
adelinowona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,7 +118,7 @@ public void List_get_Item_of_scalar_should_work() | |
| var expectedStages = new[] | ||
| { | ||
| "{ $group : { _id : '$_id', _elements : { $push: '$X' } } }", | ||
| "{ $project : { _id : '$_id' Result : { $arrayElemAt : ['$_elements', 0] } } }", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How was this test not failing?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea. I just happened to catch it. |
||
| "{ $project : { _id : '$_id', Result : { $arrayElemAt : ['$_elements', 0] } } }", | ||
| "{ $sort : { _id : 1 } }" | ||
| }; | ||
| AssertStages(stages, expectedStages); | ||
|
|
@@ -1043,7 +1043,7 @@ public void IGrouping_First_with_predicate_of_root_should_work() | |
| var expectedStages = new[] | ||
| { | ||
| "{ $group : { _id : '$_id', _elements : { $push : '$$ROOT' } } }", | ||
| "{ $project : { _id : '$_id', Result : { $arrayElemAt : [{ $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e.X', 1] } } }, 0] } } }", | ||
| "{ $project : { _id : '$_id', Result : { $arrayElemAt : [{ $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e.X', 1] }, limit : 1 } }, 0] } } }", | ||
| "{ $sort : { _id : 1 } }" | ||
| }; | ||
| AssertStages(stages, expectedStages); | ||
|
|
@@ -1068,7 +1068,7 @@ public void IGrouping_First_with_predicate_of_scalar_should_work() | |
| var expectedStages = new[] | ||
| { | ||
| "{ $group : { _id : '$_id', _elements : { $push : '$X' } } }", | ||
| "{ $project : { _id : '$_id', Result : { $arrayElemAt : [{ $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e', 1] } } }, 0] } } }", | ||
| "{ $project : { _id : '$_id', Result : { $arrayElemAt : [{ $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e', 1] }, limit : 1 } }, 0] } } }", | ||
| "{ $sort : { _id : 1 } }" | ||
| }; | ||
| AssertStages(stages, expectedStages); | ||
|
|
@@ -1143,7 +1143,7 @@ public void IGrouping_FirstOrDefault_with_predicate_of_root_should_work() | |
| var expectedStages = new[] | ||
| { | ||
| "{ $group : { _id : '$_id', _elements : { $push : '$$ROOT' } } }", | ||
| "{ $project : { _id : '$_id', Result : { $let : { vars : { values : { $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e.X', 1] } } } }, in : { $cond : { if : { $eq : [{ $size : '$$values' }, 0] }, then : null, else : { $arrayElemAt : ['$$values', 0] } } } } } } }", | ||
| "{ $project : { _id : '$_id', Result : { $let : { vars : { values : { $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e.X', 1] }, limit : 1 } } }, in : { $cond : { if : { $eq : [{ $size : '$$values' }, 0] }, then : null, else : { $arrayElemAt : ['$$values', 0] } } } } } } }", | ||
| "{ $sort : { _id : 1 } }" | ||
| }; | ||
| AssertStages(stages, expectedStages); | ||
|
|
@@ -1169,7 +1169,7 @@ public void IGrouping_FirstOrDefault_with_predicate_of_scalar_should_work() | |
| var expectedStages = new[] | ||
| { | ||
| "{ $group : { _id : '$_id', _elements : { $push : '$X' } } }", | ||
| "{ $project : { _id : '$_id', Result : { $let : { vars : { values : { $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e', 1] } } } }, in : { $cond : { if : { $eq : [{ $size : '$$values' }, 0] }, then : 0, else : { $arrayElemAt : ['$$values', 0] } } } } } } }", | ||
| "{ $project : { _id : '$_id', Result : { $let : { vars : { values : { $filter : { input : '$_elements', as : 'e', cond : { $ne : ['$$e', 1] }, limit : 1 } } }, in : { $cond : { if : { $eq : [{ $size : '$$values' }, 0] }, then : 0, else : { $arrayElemAt : ['$$values', 0] } } } } } } }", | ||
| "{ $sort : { _id : 1 } }" | ||
| }; | ||
| AssertStages(stages, expectedStages); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general we prefer to test which method it is like this:
It is less likely to have false matches than string matching (partial or otherwise).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it this way since I reasoned that if we passed the initial if statement:
if (method.IsOneOf(__firstOrLastMethods))then we already know that the method has matched one of the first methods so no need to test the methods again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The statement you make is true, but even so we prefer to stick to
method.IsOneOfinstead of string matching.The only time we don't use
method.IsOneOfis when we are translating a method that might be implemented by classes we don't even know about (e.g.CompareTo).