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

[release/6.0] Query: Match joined tables properly when lifting for group by aggregate #27170

Merged
merged 1 commit into from
Jan 20, 2022
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
122 changes: 108 additions & 14 deletions src/EFCore.Relational/Query/SqlExpressions/SelectExpression.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,8 @@ private sealed class CloningExpressionVisitor : ExpressionVisitor
_usedAliases = selectExpression._usedAliases.ToHashSet(),
_projectionMapping = newProjectionMappings,
_groupingCorrelationPredicate = groupingCorrelationPredicate,
_groupingParentSelectExpressionId = selectExpression._groupingParentSelectExpressionId
_groupingParentSelectExpressionId = selectExpression._groupingParentSelectExpressionId,
_groupingParentSelectExpressionTableCount = selectExpression._groupingParentSelectExpressionTableCount,
};

newSelectExpression._tptLeftJoinTables.AddRange(selectExpression._tptLeftJoinTables);
Expand Down Expand Up @@ -869,29 +870,122 @@ public GroupByAggregateLiftingExpressionVisitor(SelectExpression selectExpressio
&& subquery.Offset == null
&& subquery._groupBy.Count == 0
&& subquery.Predicate != null
&& ((AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27102", out var enabled) && enabled)
&& ((AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27102", out var enabled27102) && enabled27102)
|| subquery.Predicate.Equals(subquery._groupingCorrelationPredicate))
&& ((AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27094", out var enabled2) && enabled2)
&& ((AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27094", out var enabled27094) && enabled27094)
|| subquery._groupingParentSelectExpressionId == _selectExpression._groupingParentSelectExpressionId))
{
var initialTableCounts = 0;
var potentialTableCount = Math.Min(_selectExpression._tables.Count, subquery._tables.Count);
for (var i = 0; i < potentialTableCount; i++)
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27163", out var enabled27163) && enabled27163)
{
if (!string.Equals(
_selectExpression._tableReferences[i].Alias,
subquery._tableReferences[i].Alias, StringComparison.OrdinalIgnoreCase))
var potentialTableCount = Math.Min(_selectExpression._tables.Count, subquery._tables.Count);
for (var i = 0; i < potentialTableCount; i++)
{
break;
if (!string.Equals(
_selectExpression._tableReferences[i].Alias,
subquery._tableReferences[i].Alias, StringComparison.OrdinalIgnoreCase))
{
break;
}

if (_selectExpression._tables[i] is SelectExpression originalNestedSelectExpression
&& subquery._tables[i] is SelectExpression subqueryNestedSelectExpression)
{
CopyOverOwnedJoinInSameTable(originalNestedSelectExpression, subqueryNestedSelectExpression);
}

initialTableCounts++;
}
}
else
{
initialTableCounts = _selectExpression._groupingParentSelectExpressionTableCount!.Value;
var potentialTableCount = Math.Min(_selectExpression._tables.Count, subquery._tables.Count);
// First verify that subquery has same structure for initial tables,
// If not then subquery may have different root than grouping element.
for (var i = 0; i < initialTableCounts; i++)
{
if (!string.Equals(
_selectExpression._tableReferences[i].Alias,
subquery._tableReferences[i].Alias, StringComparison.OrdinalIgnoreCase))
{
initialTableCounts = 0;
break;
}
}

if (_selectExpression._tables[i] is SelectExpression originalNestedSelectExpression
&& subquery._tables[i] is SelectExpression subqueryNestedSelectExpression)
if (initialTableCounts > 0)
{
CopyOverOwnedJoinInSameTable(originalNestedSelectExpression, subqueryNestedSelectExpression);
// If initial table structure matches and
// Parent has additional joins lifted already one of them is a subquery join
// Then we abort lifting if any of the joins from the subquery to lift are a subquery join
if (_selectExpression._tables.Skip(initialTableCounts)
.Select(e => UnwrapJoinExpression(e))
.Any(e => e is SelectExpression))
{
for (var i = initialTableCounts; i < subquery._tables.Count; i++)
{
if (UnwrapJoinExpression(subquery._tables[i]) is SelectExpression)
{
// If any of the join is to subquery then we abort the lifting group by term altogether.
initialTableCounts = 0;
break;
}
}
}
}

initialTableCounts++;
if (initialTableCounts > 0)
{
// We need to copy over owned join which are coming from same initial tables.
for (var i = 0; i < initialTableCounts; i++)
{
if (_selectExpression._tables[i] is SelectExpression originalNestedSelectExpression
&& subquery._tables[i] is SelectExpression subqueryNestedSelectExpression)
{
CopyOverOwnedJoinInSameTable(originalNestedSelectExpression, subqueryNestedSelectExpression);
}
}


for (var i = initialTableCounts; i < potentialTableCount; i++)
{
// Try to match additional tables for the cases where we can match exact so we can avoid lifting
// same joins to parent
if (!string.Equals(
_selectExpression._tableReferences[i].Alias,
subquery._tableReferences[i].Alias, StringComparison.OrdinalIgnoreCase))
{
break;
}

var outerTableExpressionBase = _selectExpression._tables[i];
var innerTableExpressionBase = subquery._tables[i];

if (outerTableExpressionBase is InnerJoinExpression outerInnerJoin
&& innerTableExpressionBase is InnerJoinExpression innerInnerJoin)
{
outerTableExpressionBase = outerInnerJoin.Table as TableExpression;
innerTableExpressionBase = innerInnerJoin.Table as TableExpression;
}
else if (outerTableExpressionBase is LeftJoinExpression outerLeftJoin
&& innerTableExpressionBase is LeftJoinExpression innerLeftJoin)
{
outerTableExpressionBase = outerLeftJoin.Table as TableExpression;
innerTableExpressionBase = innerLeftJoin.Table as TableExpression;
}

if (outerTableExpressionBase is TableExpression outerTable
&& innerTableExpressionBase is TableExpression innerTable
&& !(string.Equals(outerTable.Name, innerTable.Name, StringComparison.OrdinalIgnoreCase)
&& string.Equals(outerTable.Schema, innerTable.Schema, StringComparison.OrdinalIgnoreCase)))
{
break;
}

initialTableCounts++;
}
}
}

if (initialTableCounts > 0)
Expand All @@ -900,7 +994,7 @@ public GroupByAggregateLiftingExpressionVisitor(SelectExpression selectExpressio
// We only replace columns from initial tables.
// Additional tables may have been added to outer from other terms which may end up matching on table alias
var columnExpressionReplacingExpressionVisitor =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27083", out var enabled3) && enabled3
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27083", out var enabled27083) && enabled27083
? new ColumnExpressionReplacingExpressionVisitor(
subquery, _selectExpression._tableReferences)
: new ColumnExpressionReplacingExpressionVisitor(
Expand Down
18 changes: 14 additions & 4 deletions src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public sealed partial class SelectExpression : TableExpressionBase

private SqlExpression? _groupingCorrelationPredicate;
private Guid? _groupingParentSelectExpressionId;
private int? _groupingParentSelectExpressionTableCount;
private CloningExpressionVisitor? _cloningExpressionVisitor;

private SelectExpression(
Expand Down Expand Up @@ -1256,7 +1257,7 @@ public GroupByShaperExpression ApplyGrouping(

// We generate the cloned expression before changing identifier for this SelectExpression
// because we are going to erase grouping for cloned expression.
if (!(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27094", out var enabled2) && enabled2))
if (!(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27094", out var enabled27094) && enabled27094))
{
_groupingParentSelectExpressionId = Guid.NewGuid();

Expand All @@ -1267,10 +1268,15 @@ public GroupByShaperExpression ApplyGrouping(
.Aggregate((l, r) => sqlExpressionFactory.AndAlso(l, r));
clonedSelectExpression._groupBy.Clear();
clonedSelectExpression.ApplyPredicate(correlationPredicate);
if (!(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27102", out var enabled) && enabled))
if (!(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27102", out var enabled27102) && enabled27102))
{
clonedSelectExpression._groupingCorrelationPredicate = clonedSelectExpression.Predicate;
}
if (!(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27163", out var enabled27163) && enabled27163))
{
_groupingParentSelectExpressionTableCount = _tables.Count;

}

if (!_identifier.All(e => _groupBy.Contains(e.Column)))
{
Expand Down Expand Up @@ -1495,6 +1501,7 @@ private void ApplySetOperation(SetOperationType setOperationType, SelectExpressi
Offset = Offset,
Limit = Limit,
_groupingParentSelectExpressionId = _groupingParentSelectExpressionId,
_groupingParentSelectExpressionTableCount = _groupingParentSelectExpressionTableCount,
_groupingCorrelationPredicate = _groupingCorrelationPredicate
};
Offset = null;
Expand All @@ -1504,6 +1511,7 @@ private void ApplySetOperation(SetOperationType setOperationType, SelectExpressi
Having = null;
_groupingCorrelationPredicate = null;
_groupingParentSelectExpressionId = null;
_groupingParentSelectExpressionTableCount = null;
_groupBy.Clear();
_orderings.Clear();
_tables.Clear();
Expand Down Expand Up @@ -2819,7 +2827,8 @@ private SqlRemappingVisitor PushdownIntoSubqueryInternal()
Having = Having,
Offset = Offset,
Limit = Limit,
_groupingParentSelectExpressionId = _groupingParentSelectExpressionId
_groupingParentSelectExpressionId = _groupingParentSelectExpressionId,
_groupingParentSelectExpressionTableCount = _groupingParentSelectExpressionTableCount
};
subquery._usedAliases = _usedAliases;
_tables.Clear();
Expand Down Expand Up @@ -3482,7 +3491,8 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
Tags = Tags,
_usedAliases = _usedAliases,
_groupingCorrelationPredicate = groupingCorrelationPredicate,
_groupingParentSelectExpressionId = _groupingParentSelectExpressionId
_groupingParentSelectExpressionId = _groupingParentSelectExpressionId,
_groupingParentSelectExpressionTableCount = _groupingParentSelectExpressionTableCount,
};

newSelectExpression._tptLeftJoinTables.AddRange(_tptLeftJoinTables);
Expand Down
103 changes: 103 additions & 0 deletions test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,5 +786,108 @@ protected class Table
public int Id { get; set; }
public int? Value { get; set; }
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Group_by_multiple_aggregate_joining_different_tables(bool async)
{
var contextFactory = await InitializeAsync<Context27163>();
using var context = contextFactory.CreateContext();

var query = context.Parents
.GroupBy(x => new { })
.Select(g => new
{
Test1 = g
.Select(x => x.Child1.Value1)
.Distinct()
.Count(),
Test2 = g
.Select(x => x.Child2.Value2)
.Distinct()
.Count()
});

var orders = async
? await query.ToListAsync()
: query.ToList();
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Group_by_multiple_aggregate_joining_different_tables_with_query_filter(bool async)
{
var contextFactory = await InitializeAsync<Context27163>();
using var context = contextFactory.CreateContext();

var query = context.Parents
.GroupBy(x => new { })
.Select(g => new
{
Test1 = g
.Select(x => x.ChildFilter1.Value1)
.Distinct()
.Count(),
Test2 = g
.Select(x => x.ChildFilter2.Value2)
.Distinct()
.Count()
});

var orders = async
? await query.ToListAsync()
: query.ToList();
}

protected class Context27163 : DbContext
{
public Context27163(DbContextOptions options)
: base(options)
{
}

public DbSet<Parent> Parents { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ChildFilter1>().HasQueryFilter(e => e.Filter1 == "Filter1");
modelBuilder.Entity<ChildFilter2>().HasQueryFilter(e => e.Filter2 == "Filter2");
}
}

public class Parent
{
public int Id { get; set; }
public Child1 Child1 { get; set; }
public Child2 Child2 { get; set; }
public ChildFilter1 ChildFilter1 { get; set; }
public ChildFilter2 ChildFilter2 { get; set; }
}

public class Child1
{
public int Id { get; set; }
public string Value1 { get; set; }
}

public class Child2
{
public int Id { get; set; }
public string Value2 { get; set; }
}

public class ChildFilter1
{
public int Id { get; set; }
public string Filter1 { get; set; }
public string Value1 { get; set; }
}

public class ChildFilter2
{
public int Id { get; set; }
public string Filter2 { get; set; }
public string Value2 { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.EntityFrameworkCore.Query
Expand Down Expand Up @@ -299,6 +300,18 @@ FROM [Level1] AS [l1]
) AS [t0] ON [l].[Id] = [t0].[Level1_Optional_Id]");
}

[ConditionalTheory(Skip = "Issue#26104")]
public override Task GroupBy_aggregate_where_required_relationship(bool async)
{
return base.GroupBy_aggregate_where_required_relationship(async);
}

[ConditionalTheory(Skip = "Issue#26104")]
public override Task GroupBy_aggregate_where_required_relationship_2(bool async)
{
return base.GroupBy_aggregate_where_required_relationship_2(async);
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
Expand Down
Loading