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/3.1] Query: Simplify SQL generated for optional owned/weak entities when table splitting #19871

Merged
merged 2 commits into from
Feb 14, 2020
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
43 changes: 36 additions & 7 deletions src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ private void AddConditions(

if (sharingTypes.Count > 0)
{
bool discriminatorAdded = AddDiscriminatorCondition(selectExpression, entityType);
var discriminatorAdded = AddDiscriminatorCondition(selectExpression, entityType);

var linkingFks = entityType.GetRootType().FindForeignKeys(entityType.FindPrimaryKey().Properties)
.Where(
Expand All @@ -508,14 +508,43 @@ private void AddConditions(

if (!skipJoins)
{
AddInnerJoin(selectExpression, linkingFks[0], sharingTypes, skipInnerJoins: false);

foreach (var otherFk in linkingFks.Skip(1))
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue18299", out var isEnabled) && isEnabled)
{
var otherSelectExpression = new SelectExpression(entityType);
AddInnerJoin(selectExpression, linkingFks[0], sharingTypes, skipInnerJoins: false);

foreach (var otherFk in linkingFks.Skip(1))
{
var otherSelectExpression = new SelectExpression(entityType);

AddInnerJoin(otherSelectExpression, otherFk, sharingTypes, skipInnerJoins: false);
selectExpression.ApplyUnion(otherSelectExpression, distinct: true);
AddInnerJoin(otherSelectExpression, otherFk, sharingTypes, skipInnerJoins: false);
selectExpression.ApplyUnion(otherSelectExpression, distinct: true);
}
}
else
{
var first = true;

foreach (var foreignKey in linkingFks)
{
if (!(entityType.FindOwnership() == foreignKey
&& foreignKey.PrincipalEntityType.BaseType == null))
{
var otherSelectExpression = first
? selectExpression
: new SelectExpression(entityType);

AddInnerJoin(otherSelectExpression, foreignKey, sharingTypes, skipInnerJoins: false);

if (first)
{
first = false;
}
else
{
selectExpression.ApplyUnion(otherSelectExpression, distinct: true);
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,28 @@ public override void ConcurrencyCheckAttribute_throws_if_value_in_database_chang
@"SELECT TOP(1) [s].[UniqueNo], [s].[MaxLengthProperty], [s].[Name], [s].[RowVersion], [t].[UniqueNo], [t].[AdditionalDetails_Name], [t0].[UniqueNo], [t0].[Details_Name]
FROM [Sample] AS [s]
LEFT JOIN (
SELECT [s0].[UniqueNo], [s0].[AdditionalDetails_Name], [s1].[UniqueNo] AS [UniqueNo0]
SELECT [s0].[UniqueNo], [s0].[AdditionalDetails_Name]
FROM [Sample] AS [s0]
INNER JOIN [Sample] AS [s1] ON [s0].[UniqueNo] = [s1].[UniqueNo]
WHERE [s0].[AdditionalDetails_Name] IS NOT NULL
) AS [t] ON [s].[UniqueNo] = [t].[UniqueNo]
LEFT JOIN (
SELECT [s2].[UniqueNo], [s2].[Details_Name], [s3].[UniqueNo] AS [UniqueNo0]
FROM [Sample] AS [s2]
INNER JOIN [Sample] AS [s3] ON [s2].[UniqueNo] = [s3].[UniqueNo]
WHERE [s2].[Details_Name] IS NOT NULL
SELECT [s1].[UniqueNo], [s1].[Details_Name]
FROM [Sample] AS [s1]
WHERE [s1].[Details_Name] IS NOT NULL
) AS [t0] ON [s].[UniqueNo] = [t0].[UniqueNo]
WHERE [s].[UniqueNo] = 1",
//
@"SELECT TOP(1) [s].[UniqueNo], [s].[MaxLengthProperty], [s].[Name], [s].[RowVersion], [t].[UniqueNo], [t].[AdditionalDetails_Name], [t0].[UniqueNo], [t0].[Details_Name]
FROM [Sample] AS [s]
LEFT JOIN (
SELECT [s0].[UniqueNo], [s0].[AdditionalDetails_Name], [s1].[UniqueNo] AS [UniqueNo0]
SELECT [s0].[UniqueNo], [s0].[AdditionalDetails_Name]
FROM [Sample] AS [s0]
INNER JOIN [Sample] AS [s1] ON [s0].[UniqueNo] = [s1].[UniqueNo]
WHERE [s0].[AdditionalDetails_Name] IS NOT NULL
) AS [t] ON [s].[UniqueNo] = [t].[UniqueNo]
LEFT JOIN (
SELECT [s2].[UniqueNo], [s2].[Details_Name], [s3].[UniqueNo] AS [UniqueNo0]
FROM [Sample] AS [s2]
INNER JOIN [Sample] AS [s3] ON [s2].[UniqueNo] = [s3].[UniqueNo]
WHERE [s2].[Details_Name] IS NOT NULL
SELECT [s1].[UniqueNo], [s1].[Details_Name]
FROM [Sample] AS [s1]
WHERE [s1].[Details_Name] IS NOT NULL
) AS [t0] ON [s].[UniqueNo] = [t0].[UniqueNo]
WHERE [s].[UniqueNo] = 1",
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ public override void Property_entry_original_value_is_set()
@"SELECT TOP(1) [e].[Id], [e].[EngineSupplierId], [e].[Name], [t].[Id], [t].[StorageLocation_Latitude], [t].[StorageLocation_Longitude]
FROM [Engines] AS [e]
LEFT JOIN (
SELECT [e0].[Id], [e0].[StorageLocation_Latitude], [e0].[StorageLocation_Longitude], [e1].[Id] AS [Id0]
SELECT [e0].[Id], [e0].[StorageLocation_Latitude], [e0].[StorageLocation_Longitude]
FROM [Engines] AS [e0]
INNER JOIN [Engines] AS [e1] ON [e0].[Id] = [e1].[Id]
WHERE [e0].[StorageLocation_Longitude] IS NOT NULL AND [e0].[StorageLocation_Latitude] IS NOT NULL
) AS [t] ON [e].[Id] = [t].[Id]
ORDER BY [e].[Id]",
Expand Down
Loading