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

Query: Block queries with AsSplitQuery when collection was not split #22144

Merged
merged 1 commit into from
Aug 20, 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
10 changes: 9 additions & 1 deletion src/EFCore.Relational/Properties/RelationalStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 30 additions & 27 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -765,4 +765,7 @@
<data name="VisitChildrenMustBeOverridden" xml:space="preserve">
<value>VisitChildren must be overridden in class deriving from SqlExpression.</value>
</data>
<data name="UnableToSplitCollectionProjectionInSplitQuery" xml:space="preserve">
<value>The query has been configured to use '{splitQueryEnumValue}' and contains a collection in the 'Select' call, which could not be split into separate query. Please remove '{splitQueryMethodName}' if applied or add '{singleQueryMethodName}' to the query.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
Expand Down Expand Up @@ -68,36 +69,26 @@ protected override Expression VisitExtension(Expression extensionExpression)
selectExpression.PushdownIntoSubquery();
}

if (_splitQuery)
{
var splitCollectionShaperExpression = (RelationalSplitCollectionShaperExpression)selectExpression.ApplyCollectionJoin(
projectionBindingExpression.Index.Value,
collectionId,
collectionShaperExpression.InnerShaper,
collectionShaperExpression.Navigation,
collectionShaperExpression.ElementType,
_splitQuery);
var innerShaper = Visit(collectionShaperExpression.InnerShaper);

var innerShaper = Visit(splitCollectionShaperExpression.InnerShaper);
var collectionJoin = selectExpression.ApplyCollectionJoin(
projectionBindingExpression.Index.Value,
collectionId,
innerShaper,
collectionShaperExpression.Navigation,
collectionShaperExpression.ElementType,
_splitQuery);

return splitCollectionShaperExpression.Update(
splitCollectionShaperExpression.ParentIdentifier,
splitCollectionShaperExpression.ChildIdentifier,
splitCollectionShaperExpression.SelectExpression,
innerShaper);
}
else
if (_splitQuery
&& collectionJoin == null)
{
var innerShaper = Visit(collectionShaperExpression.InnerShaper);

return selectExpression.ApplyCollectionJoin(
projectionBindingExpression.Index.Value,
collectionId,
innerShaper,
collectionShaperExpression.Navigation,
collectionShaperExpression.ElementType,
_splitQuery);
throw new InvalidOperationException(RelationalStrings.UnableToSplitCollectionProjectionInSplitQuery(
$"{nameof(QuerySplittingBehavior)}.{QuerySplittingBehavior.SplitQuery}",
nameof(RelationalQueryableExtensions.AsSplitQuery),
nameof(RelationalQueryableExtensions.AsSingleQuery)));
}

return collectionJoin;
}

return extensionExpression is ShapedQueryExpression shapedQueryExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,13 @@ public Expression ApplyCollectionJoin(

if (splitQuery)
{
var containsReferenceToOuter = new SelectExpressionCorrelationFindingExpressionVisitor(this)
.ContainsOuterReference(innerSelectExpression);
if (containsReferenceToOuter)
{
return null;
}

var parentIdentifier = GetIdentifierAccessor(_identifier).Item1;
innerSelectExpression.ApplyProjection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.TestModels.ComplexNavigationsModel;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
Expand Down Expand Up @@ -482,6 +483,51 @@ public virtual async Task Filtered_include_calling_methods_directly_on_parameter
.AsSplitQuery()))).Message;
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Projecting_collection_with_FirstOrDefault_split_throws(bool async)
{
Assert.Equal(
RelationalStrings.UnableToSplitCollectionProjectionInSplitQuery(
"QuerySplittingBehavior.SplitQuery", "AsSplitQuery", "AsSingleQuery"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => AssertFirstOrDefault(
async,
ss => ss.Set<Level1>()
.AsSplitQuery()
.Select(e => new
{
e.Id,
Level2s = e.OneToMany_Optional1.ToList()
}),
predicate: l => l.Id == 1,
asserter: (e, a) =>
{
Assert.Equal(e.Id, a.Id);
AssertCollection(e.Level2s, a.Level2s);
}))).Message);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Projecting_collection_with_FirstOrDefault_without_split_works(bool async)
{
return AssertFirstOrDefault(
async,
ss => ss.Set<Level1>()
.Select(e => new
{
e.Id,
Level2s = e.OneToMany_Optional1.ToList()
}),
predicate: l => l.Id == 1,
asserter: (e, a) =>
{
Assert.Equal(e.Id, a.Id);
AssertCollection(e.Level2s, a.Level2s);
});
}

protected virtual bool CanExecuteQueryString => false;

protected override QueryAsserter CreateQueryAsserter(TFixture fixture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5087,7 +5087,7 @@ FROM [LevelTwo] AS [l0]
) AS [t]
WHERE 0 < [t].[row]
) AS [t0] ON [l].[Id] = [t0].[OneToMany_Optional_Inverse2Id]
ORDER BY [l].[Id], [t0].[OneToMany_Optional_Inverse2Id], [t0].[Name], [t0].[Id]",
ORDER BY [l].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse2Id], [t0].[Name]",
//
@"SELECT [t2].[Id], [t2].[Level2_Optional_Id], [t2].[Level2_Required_Id], [t2].[Name], [t2].[OneToMany_Optional_Inverse3Id], [t2].[OneToMany_Optional_Self_Inverse3Id], [t2].[OneToMany_Required_Inverse3Id], [t2].[OneToMany_Required_Self_Inverse3Id], [t2].[OneToOne_Optional_PK_Inverse3Id], [t2].[OneToOne_Optional_Self3Id], [l].[Id], [t0].[Id]
FROM [LevelOne] AS [l]
Expand All @@ -5107,7 +5107,7 @@ FROM [LevelThree] AS [l1]
) AS [t1]
WHERE 0 < [t1].[row]
) AS [t2] ON [t0].[Id] = [t2].[OneToMany_Optional_Inverse3Id]
ORDER BY [l].[Id], [t0].[OneToMany_Optional_Inverse2Id], [t0].[Name], [t0].[Id], [t2].[OneToMany_Optional_Inverse3Id], [t2].[Name] DESC");
ORDER BY [l].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse2Id], [t0].[Name], [t2].[OneToMany_Optional_Inverse3Id], [t2].[Name] DESC");
}

public override async Task Filtered_include_basic_OrderBy_Take_split(bool async)
Expand Down Expand Up @@ -5326,7 +5326,7 @@ FROM [LevelTwo] AS [l0]
) AS [t]
WHERE [t].[row] <= 3
) AS [t0] ON [l].[Id] = [t0].[OneToMany_Optional_Inverse2Id]
ORDER BY [l].[Id], [t0].[OneToMany_Optional_Inverse2Id], [t0].[Name], [t0].[Id]",
ORDER BY [l].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse2Id], [t0].[Name]",
//
@"SELECT [t2].[Id], [t2].[Level2_Optional_Id], [t2].[Level2_Required_Id], [t2].[Name], [t2].[OneToMany_Optional_Inverse3Id], [t2].[OneToMany_Optional_Self_Inverse3Id], [t2].[OneToMany_Required_Inverse3Id], [t2].[OneToMany_Required_Self_Inverse3Id], [t2].[OneToOne_Optional_PK_Inverse3Id], [t2].[OneToOne_Optional_Self3Id], [l].[Id], [t0].[Id]
FROM [LevelOne] AS [l]
Expand All @@ -5348,7 +5348,7 @@ FROM [LevelThree] AS [l1]
) AS [t1]
WHERE 1 < [t1].[row]
) AS [t2] ON [t0].[Id] = [t2].[OneToMany_Required_Inverse3Id]
ORDER BY [l].[Id], [t0].[OneToMany_Optional_Inverse2Id], [t0].[Name], [t0].[Id], [t2].[OneToMany_Required_Inverse3Id], [t2].[Name] DESC");
ORDER BY [l].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse2Id], [t0].[Name], [t2].[OneToMany_Required_Inverse3Id], [t2].[Name] DESC");
}

public override async Task Filtered_include_same_filter_set_on_same_navigation_twice_split(bool async)
Expand Down Expand Up @@ -5567,7 +5567,7 @@ FROM [LevelThree] AS [l1]
) AS [t]
WHERE [t].[row] <= 1
) AS [t0] ON [l0].[Id] = [t0].[OneToMany_Optional_Inverse3Id]
ORDER BY [l].[Id], [l0].[Id], [t0].[OneToMany_Optional_Inverse3Id], [t0].[Id]",
ORDER BY [l].[Id], [l0].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse3Id]",
//
@"SELECT [l2].[Id], [l2].[Level3_Optional_Id], [l2].[Level3_Required_Id], [l2].[Name], [l2].[OneToMany_Optional_Inverse4Id], [l2].[OneToMany_Optional_Self_Inverse4Id], [l2].[OneToMany_Required_Inverse4Id], [l2].[OneToMany_Required_Self_Inverse4Id], [l2].[OneToOne_Optional_PK_Inverse4Id], [l2].[OneToOne_Optional_Self4Id], [l].[Id], [l0].[Id], [t0].[Id]
FROM [LevelOne] AS [l]
Expand All @@ -5582,7 +5582,7 @@ FROM [LevelThree] AS [l1]
WHERE [t].[row] <= 1
) AS [t0] ON [l0].[Id] = [t0].[OneToMany_Optional_Inverse3Id]
INNER JOIN [LevelFour] AS [l2] ON [t0].[Id] = [l2].[OneToMany_Optional_Inverse4Id]
ORDER BY [l].[Id], [l0].[Id], [t0].[OneToMany_Optional_Inverse3Id], [t0].[Id]",
ORDER BY [l].[Id], [l0].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse3Id]",
//
@"SELECT [l2].[Id], [l2].[Level3_Optional_Id], [l2].[Level3_Required_Id], [l2].[Name], [l2].[OneToMany_Optional_Inverse4Id], [l2].[OneToMany_Optional_Self_Inverse4Id], [l2].[OneToMany_Required_Inverse4Id], [l2].[OneToMany_Required_Self_Inverse4Id], [l2].[OneToOne_Optional_PK_Inverse4Id], [l2].[OneToOne_Optional_Self4Id], [l].[Id], [l0].[Id], [t0].[Id]
FROM [LevelOne] AS [l]
Expand All @@ -5597,7 +5597,7 @@ FROM [LevelThree] AS [l1]
WHERE [t].[row] <= 1
) AS [t0] ON [l0].[Id] = [t0].[OneToMany_Optional_Inverse3Id]
INNER JOIN [LevelFour] AS [l2] ON [t0].[Id] = [l2].[OneToMany_Required_Inverse4Id]
ORDER BY [l].[Id], [l0].[Id], [t0].[OneToMany_Optional_Inverse3Id], [t0].[Id]");
ORDER BY [l].[Id], [l0].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse3Id]");
}

public override async Task Filtered_include_complex_three_level_with_middle_having_filter2_split(bool async)
Expand Down Expand Up @@ -5626,7 +5626,7 @@ FROM [LevelThree] AS [l1]
) AS [t]
WHERE [t].[row] <= 1
) AS [t0] ON [l0].[Id] = [t0].[OneToMany_Optional_Inverse3Id]
ORDER BY [l].[Id], [l0].[Id], [t0].[OneToMany_Optional_Inverse3Id], [t0].[Id]",
ORDER BY [l].[Id], [l0].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse3Id]",
//
@"SELECT [l2].[Id], [l2].[Level3_Optional_Id], [l2].[Level3_Required_Id], [l2].[Name], [l2].[OneToMany_Optional_Inverse4Id], [l2].[OneToMany_Optional_Self_Inverse4Id], [l2].[OneToMany_Required_Inverse4Id], [l2].[OneToMany_Required_Self_Inverse4Id], [l2].[OneToOne_Optional_PK_Inverse4Id], [l2].[OneToOne_Optional_Self4Id], [l].[Id], [l0].[Id], [t0].[Id]
FROM [LevelOne] AS [l]
Expand All @@ -5641,7 +5641,7 @@ FROM [LevelThree] AS [l1]
WHERE [t].[row] <= 1
) AS [t0] ON [l0].[Id] = [t0].[OneToMany_Optional_Inverse3Id]
INNER JOIN [LevelFour] AS [l2] ON [t0].[Id] = [l2].[OneToMany_Optional_Inverse4Id]
ORDER BY [l].[Id], [l0].[Id], [t0].[OneToMany_Optional_Inverse3Id], [t0].[Id]",
ORDER BY [l].[Id], [l0].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse3Id]",
//
@"SELECT [l2].[Id], [l2].[Level3_Optional_Id], [l2].[Level3_Required_Id], [l2].[Name], [l2].[OneToMany_Optional_Inverse4Id], [l2].[OneToMany_Optional_Self_Inverse4Id], [l2].[OneToMany_Required_Inverse4Id], [l2].[OneToMany_Required_Self_Inverse4Id], [l2].[OneToOne_Optional_PK_Inverse4Id], [l2].[OneToOne_Optional_Self4Id], [l].[Id], [l0].[Id], [t0].[Id]
FROM [LevelOne] AS [l]
Expand All @@ -5656,7 +5656,7 @@ FROM [LevelThree] AS [l1]
WHERE [t].[row] <= 1
) AS [t0] ON [l0].[Id] = [t0].[OneToMany_Optional_Inverse3Id]
INNER JOIN [LevelFour] AS [l2] ON [t0].[Id] = [l2].[OneToMany_Required_Inverse4Id]
ORDER BY [l].[Id], [l0].[Id], [t0].[OneToMany_Optional_Inverse3Id], [t0].[Id]");
ORDER BY [l].[Id], [l0].[Id], [t0].[Id], [t0].[OneToMany_Optional_Inverse3Id]");
}

public override void Filtered_include_variable_used_inside_filter_split()
Expand Down Expand Up @@ -5833,6 +5833,21 @@ ELSE [l1].[Name]
END IS NULL");
}

public override async Task Projecting_collection_with_FirstOrDefault_without_split_works(bool async)
{
await base.Projecting_collection_with_FirstOrDefault_without_split_works(async);

AssertSql(
@"SELECT [t].[Id], [l0].[Id], [l0].[Date], [l0].[Level1_Optional_Id], [l0].[Level1_Required_Id], [l0].[Name], [l0].[OneToMany_Optional_Inverse2Id], [l0].[OneToMany_Optional_Self_Inverse2Id], [l0].[OneToMany_Required_Inverse2Id], [l0].[OneToMany_Required_Self_Inverse2Id], [l0].[OneToOne_Optional_PK_Inverse2Id], [l0].[OneToOne_Optional_Self2Id]
FROM (
SELECT TOP(1) [l].[Id]
FROM [LevelOne] AS [l]
WHERE [l].[Id] = 1
) AS [t]
LEFT JOIN [LevelTwo] AS [l0] ON [t].[Id] = [l0].[OneToMany_Optional_Inverse2Id]
ORDER BY [t].[Id], [l0].[Id]");
}

private void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ FROM [JoinOneToTwo] AS [j]
) AS [t]
WHERE (1 < [t].[row]) AND ([t].[row] <= 3)
) AS [t0] ON [e].[Id] = [t0].[OneId]
ORDER BY [e].[Id], [t0].[OneId], [t0].[Id], [t0].[TwoId]",
ORDER BY [e].[Id], [t0].[OneId], [t0].[TwoId], [t0].[Id]",
//
@"SELECT [t1].[Id], [t1].[CollectionInverseId], [t1].[Name], [t1].[ReferenceInverseId], [e].[Id], [t0].[OneId], [t0].[TwoId], [t0].[Id]
FROM [EntityOnes] AS [e]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ FROM [JoinOneToTwo] AS [j]
) AS [t]
WHERE (1 < [t].[row]) AND ([t].[row] <= 3)
) AS [t0] ON [e].[Id] = [t0].[OneId]
ORDER BY [e].[Id], [t0].[OneId], [t0].[Id], [t0].[TwoId]",
ORDER BY [e].[Id], [t0].[OneId], [t0].[TwoId], [t0].[Id]",
//
@"SELECT [t1].[ThreeId], [t1].[TwoId], [t1].[Id], [t1].[CollectionInverseId], [t1].[Name], [t1].[ReferenceInverseId], [e].[Id], [t0].[OneId], [t0].[TwoId], [t0].[Id]
FROM [EntityOnes] AS [e]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ FROM [JoinOneToTwo] AS [j]
) AS [t]
WHERE (1 < [t].[row]) AND ([t].[row] <= 3)
) AS [t0] ON [e].[Id] = [t0].[OneId]
ORDER BY [e].[Id], [t0].[OneId], [t0].[Id], [t0].[TwoId]",
ORDER BY [e].[Id], [t0].[OneId], [t0].[TwoId], [t0].[Id]",
//
@"SELECT [t1].[Id], [t1].[CollectionInverseId], [t1].[Name], [t1].[ReferenceInverseId], [e].[Id], [t0].[OneId], [t0].[TwoId], [t0].[Id]
FROM [EntityOnes] AS [e]
Expand Down
Loading