-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-6125: Reject non-collection inner sequences in LINQ Join/LeftJoin #2075
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
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -16,8 +16,6 @@ | |
| using System; | ||
| using System.Linq; | ||
| using FluentAssertions; | ||
| using MongoDB.Driver.Core.Misc; | ||
| using MongoDB.Driver.Core.TestHelpers.XunitExtensions; | ||
| using MongoDB.Driver.Linq; | ||
| using MongoDB.Driver.TestHelpers; | ||
| using Xunit; | ||
|
|
@@ -209,32 +207,75 @@ public void LeftJoin_should_preserve_outer_when_no_matching_inner() | |
| noMatch.CustomerName.Should().BeNull(); | ||
| } | ||
|
|
||
| // A filter chained onto the inner queryable must be honored: a row whose only candidate | ||
| // inner match is filtered out gets a null inner, preserving left-join semantics. | ||
| // A non-collection inner sequence (filtered, ordered, or limited) is not supported: translating it | ||
| // into a correlated $lookup pipeline would apply the operation per outer document rather than once | ||
| // globally, producing wrong results (CSHARP-6125). Such subqueries must be rejected until proper | ||
| // support is added (CSHARP-6118). | ||
| [Fact] | ||
| public void LeftJoin_with_filtered_inner_queryable_should_apply_filter() | ||
| public void LeftJoin_with_filtered_inner_queryable_should_throw() | ||
|
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. Do we need similar tests for
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. Now added. |
||
| { | ||
| // A filtered inner sequence is translated to a $lookup that combines localField/foreignField | ||
| // with a pipeline, which requires the concise $lookup syntax introduced in MongoDB 5.0. | ||
| RequireServer.Check().Supports(Feature.LookupConciseSyntax); | ||
| var orders = Fixture.OrdersCollection; | ||
|
|
||
| var queryable = orders.AsQueryable() | ||
| .LeftJoin( | ||
| Fixture.CustomersCollection.AsQueryable().Where(c => c.Name == "Alice"), | ||
| o => o.CustomerId, | ||
| c => c.Id, | ||
| (o, c) => new { OrderId = o.Id, CustomerName = c.Name }); | ||
|
|
||
| var exception = Record.Exception(() => Translate(orders, queryable)); | ||
| exception.Should().BeOfType<ExpressionNotSupportedException>(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LeftJoin_with_ordered_and_limited_inner_queryable_should_throw() | ||
| { | ||
| var orders = Fixture.OrdersCollection; | ||
|
|
||
| // Only Alice (id=10) should participate as an inner match. | ||
| var queryable = orders.AsQueryable() | ||
| .LeftJoin( | ||
| Fixture.CustomersCollection.AsQueryable().OrderBy(c => c.Name).Take(4), | ||
| o => o.CustomerId, | ||
| c => c.Id, | ||
| (o, c) => new { OrderId = o.Id, CustomerName = c.Name }); | ||
|
|
||
| var exception = Record.Exception(() => Translate(orders, queryable)); | ||
| exception.Should().BeOfType<ExpressionNotSupportedException>(); | ||
| } | ||
|
|
||
| // The same restriction applies to an inner join (Queryable.Join). The MongoDB LINQ Join overload | ||
| // only accepts an IMongoCollection, so a non-collection inner can only arrive via the BCL | ||
| // Queryable.Join; it must be rejected for the same reason (CSHARP-6125). | ||
| [Fact] | ||
| public void Join_with_filtered_inner_queryable_should_throw() | ||
| { | ||
| var orders = Fixture.OrdersCollection; | ||
|
|
||
| var queryable = orders.AsQueryable() | ||
| .Join( | ||
| Fixture.CustomersCollection.AsQueryable().Where(c => c.Name == "Alice"), | ||
| o => o.CustomerId, | ||
| c => c.Id, | ||
| (o, c) => new { OrderId = o.Id, CustomerName = c.Name }); | ||
|
|
||
| var results = queryable.ToList(); | ||
| results.Should().HaveCount(3); | ||
| var exception = Record.Exception(() => Translate(orders, queryable)); | ||
| exception.Should().BeOfType<ExpressionNotSupportedException>(); | ||
| } | ||
|
|
||
| // Order 2 (CustomerId=20) only matches Bob, who is filtered out of the inner source, | ||
| // so its inner match is null. | ||
| var order2 = results.Single(r => r.OrderId == 2); | ||
| order2.CustomerName.Should().BeNull(); | ||
| [Fact] | ||
| public void Join_with_ordered_and_limited_inner_queryable_should_throw() | ||
| { | ||
| var orders = Fixture.OrdersCollection; | ||
|
|
||
| var queryable = orders.AsQueryable() | ||
| .Join( | ||
| Fixture.CustomersCollection.AsQueryable().OrderBy(c => c.Name).Take(4), | ||
| o => o.CustomerId, | ||
| c => c.Id, | ||
| (o, c) => new { OrderId = o.Id, CustomerName = c.Name }); | ||
|
|
||
| var exception = Record.Exception(() => Translate(orders, queryable)); | ||
| exception.Should().BeOfType<ExpressionNotSupportedException>(); | ||
| } | ||
|
|
||
| #if NET10_0_OR_GREATER | ||
|
|
||
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.
This sounds reasonable.
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.
Fixed.