Skip to content

Commit

Permalink
Query: Block queries with AsSplitQuery when collection was not split
Browse files Browse the repository at this point in the history
Resolves #22067
  • Loading branch information
smitpatel committed Aug 20, 2020
1 parent e567b4d commit 715f994
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 54 deletions.
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 collection in projection, which was not 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 @@ -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);
}
}

0 comments on commit 715f994

Please sign in to comment.