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: Run shaper only when needed in SplitQueryingEnumerable #25557

Merged
1 commit merged into from
Aug 17, 2021
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
27 changes: 15 additions & 12 deletions src/EFCore.Relational/Query/Internal/SplitQueryingEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class SplitQueryingEnumerable<T> : IEnumerable<T>, IAsyncEnumerable<T>, I
private readonly RelationalQueryContext _relationalQueryContext;
private readonly RelationalCommandCache _relationalCommandCache;
private readonly Func<QueryContext, DbDataReader, ResultContext, SplitQueryResultCoordinator, T> _shaper;
private readonly Action<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator> _relatedDataLoaders;
private readonly Func<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator, Task> _relatedDataLoadersAsync;
private readonly Action<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator>? _relatedDataLoaders;
private readonly Func<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator, Task>? _relatedDataLoadersAsync;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
Expand All @@ -42,8 +42,8 @@ public SplitQueryingEnumerable(
RelationalQueryContext relationalQueryContext,
RelationalCommandCache relationalCommandCache,
Func<QueryContext, DbDataReader, ResultContext, SplitQueryResultCoordinator, T> shaper,
Action<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator> relatedDataLoaders,
Func<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator, Task> relatedDataLoadersAsync,
Action<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator>? relatedDataLoaders,
Func<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator, Task>? relatedDataLoadersAsync,
Type contextType,
bool standAloneStateManager,
bool detailedErrorsEnabled,
Expand Down Expand Up @@ -129,7 +129,7 @@ private sealed class Enumerator : IEnumerator<T>
private readonly RelationalQueryContext _relationalQueryContext;
private readonly RelationalCommandCache _relationalCommandCache;
private readonly Func<QueryContext, DbDataReader, ResultContext, SplitQueryResultCoordinator, T> _shaper;
private readonly Action<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator> _relatedDataLoaders;
private readonly Action<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator>? _relatedDataLoaders;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
Expand Down Expand Up @@ -187,10 +187,14 @@ public bool MoveNext()
if (hasNext)
{
_resultCoordinator!.ResultContext.Values = null;
_shaper(_relationalQueryContext, _dbDataReader!, _resultCoordinator.ResultContext, _resultCoordinator);
_relatedDataLoaders?.Invoke(_relationalQueryContext, _executionStrategy!, _resultCoordinator);
Current = _shaper(
_relationalQueryContext, _dbDataReader!, _resultCoordinator.ResultContext, _resultCoordinator);
if (_relatedDataLoaders != null)
smitpatel marked this conversation as resolved.
Show resolved Hide resolved
{
_relatedDataLoaders.Invoke(_relationalQueryContext, _executionStrategy!, _resultCoordinator);
Current = _shaper(
_relationalQueryContext, _dbDataReader!, _resultCoordinator.ResultContext, _resultCoordinator);
}
}
else
{
Expand Down Expand Up @@ -272,7 +276,7 @@ private sealed class AsyncEnumerator : IAsyncEnumerator<T>
private readonly RelationalQueryContext _relationalQueryContext;
private readonly RelationalCommandCache _relationalCommandCache;
private readonly Func<QueryContext, DbDataReader, ResultContext, SplitQueryResultCoordinator, T> _shaper;
private readonly Func<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator, Task> _relatedDataLoaders;
private readonly Func<QueryContext, IExecutionStrategy, SplitQueryResultCoordinator, Task>? _relatedDataLoaders;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
Expand Down Expand Up @@ -334,15 +338,14 @@ await _executionStrategy.ExecuteAsync(
if (hasNext)
{
_resultCoordinator!.ResultContext.Values = null;
_shaper(_relationalQueryContext, _dbDataReader!, _resultCoordinator.ResultContext, _resultCoordinator);
Current = _shaper(_relationalQueryContext, _dbDataReader!, _resultCoordinator.ResultContext, _resultCoordinator);
if (_relatedDataLoaders != null)
{
await _relatedDataLoaders(_relationalQueryContext, _executionStrategy!, _resultCoordinator)
.ConfigureAwait(false);
Current =
_shaper(_relationalQueryContext, _dbDataReader!, _resultCoordinator.ResultContext, _resultCoordinator);
}

Current =
_shaper(_relationalQueryContext, _dbDataReader!, _resultCoordinator.ResultContext, _resultCoordinator);
}
else
{
Expand Down
71 changes: 71 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
Expand Down Expand Up @@ -10158,6 +10159,76 @@ public class JsonResult

#endregion

#region Issue24569

[ConditionalTheory]
[InlineData(true)]
[InlineData(false)]
public virtual async Task NoTracking_split_query_creates_only_required_instances(bool async)
{
var contextFactory = await InitializeAsync<MyContext25400>(seed: c => c.Seed(),
onConfiguring: o => new SqlServerDbContextOptionsBuilder(o).UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));

using (var context = contextFactory.CreateContext())
{
Test24569.ConstructorCallCount = 0;

var query = context.Set<Test24569>().AsNoTracking().OrderBy(e => e.Id);
var test = async
? await query.FirstOrDefaultAsync()
: query.FirstOrDefault();

Assert.Equal(1, Test24569.ConstructorCallCount);

AssertSql(
@"SELECT TOP(1) [t].[Id], [t].[Value]
FROM [Tests] AS [t]
ORDER BY [t].[Id]");
}
}

protected class MyContext25400 : DbContext
{
public DbSet<Test24569> Tests { get; set; }

public MyContext25400(DbContextOptions options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Test24569>().HasKey(e => e.Id);
}

public void Seed()
{
Tests.Add(new Test24569(15));

SaveChanges();
}
}

protected class Test24569
{
public static int ConstructorCallCount = 0;

public Test24569()
{
++ConstructorCallCount;
}

public Test24569(int value)
{
Value = value;
}

public int Id { get; set; }
public int Value { get; set; }
}

#endregion

protected override string StoreName => "QueryBugsTest";
protected TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;
Expand Down