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

[2.0.1] Query: Fix for #9892 GroupJoin to parent with no child throws… #9982

Merged
merged 1 commit into from
Oct 5, 2017
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
41 changes: 41 additions & 0 deletions src/EFCore.Relational/Query/RelationalQueryModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,19 @@ var innerShapedQuery
return false;
}

if (!(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue9892", out var enabled) && enabled))
{
if (!IsFlattenableGroupJoinDefaultIfEmpty(groupJoinClause, queryModel, index))
Copy link
Contributor

Choose a reason for hiding this comment

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

Combine conditions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kept it separate intentionally.
In past @anpete raised point not to combine conditions of quirks with code so that it becomes easy to understand exact condition.

Copy link
Contributor

Choose a reason for hiding this comment

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

fair enough

{
var shaperType = innerShapedQuery?.Arguments.Last().Type;
if (shaperType == null
|| !typeof(EntityShaper).IsAssignableFrom(shaperType))
{
return false;
}
}
}

var joinClause = groupJoinClause.JoinClause;

var outerQuerySource = FindPreviousQuerySource(queryModel, index);
Expand Down Expand Up @@ -1630,6 +1643,34 @@ var newShapedQueryMethod
return true;
}

private bool IsFlattenableGroupJoinDefaultIfEmpty(
[NotNull] GroupJoinClause groupJoinClause,
QueryModel queryModel,
int index)
{
var additionalFromClause
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This can be DRY with method below but for the patch I kept it separate.

= queryModel.BodyClauses.ElementAtOrDefault(index + 1)
as AdditionalFromClause;

var subQueryModel
= (additionalFromClause?.FromExpression as SubQueryExpression)
?.QueryModel;

var referencedQuerySource
= subQueryModel?.MainFromClause.FromExpression.TryGetReferencedQuerySource();

if (referencedQuerySource != groupJoinClause
|| queryModel.CountQuerySourceReferences(groupJoinClause) != 1
|| subQueryModel.BodyClauses.Count != 0
|| subQueryModel.ResultOperators.Count != 1
|| !(subQueryModel.ResultOperators[0] is DefaultIfEmptyResultOperator))
{
return false;
}

return true;
}

private bool TryFlattenGroupJoinDefaultIfEmpty(
[NotNull] GroupJoinClause groupJoinClause,
QueryModel queryModel,
Expand Down
92 changes: 92 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,98 @@ public class EntityWithLocalVariableAccessInFilter9825

#endregion

#region Bug9892

[Fact]
public virtual void GroupJoin_to_parent_with_no_child_works_9892()
{
using (CreateDatabase9892())
{
using (var context = new MyContext9892(_options))
{
var results = (
from p in context.Parents
join c in (
from x in context.Children
select new
{
ParentId = x.ParentId,
OtherParent = x.OtherParent.Name
})
on p.Id equals c.ParentId into child
select new
{
ParentId = p.Id,
ParentName = p.Name,
Children = child.Select(c => c.OtherParent)
}).ToList();

Assert.Equal(3, results.Count);
Assert.Single(results.Where(t => !t.Children.Any()));
}
}
}

private SqlServerTestStore CreateDatabase9892()
=> CreateTestStore(
() => new MyContext9892(_options),
context =>
{
context.Parents.Add(new Parent9892 { Name = "Parent1" });
context.Parents.Add(new Parent9892 { Name = "Parent2" });
context.Parents.Add(new Parent9892 { Name = "Parent3" });

context.OtherParents.Add(new OtherParent9892 { Name = "OtherParent1" });
context.OtherParents.Add(new OtherParent9892 { Name = "OtherParent2" });

context.SaveChanges();

context.Children.Add(new Child9892 { ParentId = 1, OtherParentId = 1 });
context.Children.Add(new Child9892 { ParentId = 1, OtherParentId = 2 });
context.Children.Add(new Child9892 { ParentId = 2, OtherParentId = 1 });
context.Children.Add(new Child9892 { ParentId = 2, OtherParentId = 2 });

context.SaveChanges();

ClearLog();
});

public class MyContext9892 : DbContext
{
public MyContext9892(DbContextOptions options)
: base(options)
{
}

public DbSet<Parent9892> Parents { get; set; }
public DbSet<Child9892> Children { get; set; }
public DbSet<OtherParent9892> OtherParents { get; set; }
}

public class Parent9892
{
public int Id { get; set; }
public string Name { get; set; }
public List<Child9892> Children { get; set; }
}

public class OtherParent9892
{
public int Id { get; set; }
public string Name { get; set; }
}

public class Child9892
{
public int Id { get; set; }
public int ParentId { get; set; }
public Parent9892 Parent { get; set; }
public int OtherParentId { get; set; }
public OtherParent9892 OtherParent { get; set; }
}

#endregion

private DbContextOptions _options;

private SqlServerTestStore CreateTestStore<TContext>(
Expand Down