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: Combine QueryingEnumerable for regular and non composed FromSq… #16029

Merged
merged 1 commit into from
Jun 11, 2019
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
39 changes: 35 additions & 4 deletions src/EFCore.Relational/Query/Pipeline/AsyncQueryingEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
Expand All @@ -19,7 +20,7 @@ private class AsyncQueryingEnumerable<T> : IAsyncEnumerable<T>
{
private readonly RelationalQueryContext _relationalQueryContext;
private readonly SelectExpression _selectExpression;
private readonly Func<QueryContext, DbDataReader, ResultCoordinator, Task<T>> _shaper;
private readonly Func<QueryContext, DbDataReader, int[], ResultCoordinator, Task<T>> _shaper;
private readonly IQuerySqlGeneratorFactory _querySqlGeneratorFactory;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _logger;
Expand All @@ -32,7 +33,7 @@ public AsyncQueryingEnumerable(
ISqlExpressionFactory sqlExpressionFactory,
IParameterNameGeneratorFactory parameterNameGeneratorFactory,
SelectExpression selectExpression,
Func<QueryContext, DbDataReader, ResultCoordinator, Task<T>> shaper,
Func<QueryContext, DbDataReader, int[], ResultCoordinator, Task<T>> shaper,
Type contextType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
Expand All @@ -52,10 +53,11 @@ public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToke
private sealed class AsyncEnumerator : IAsyncEnumerator<T>
{
private RelationalDataReader _dataReader;
private int[] _indexMap;
private ResultCoordinator _resultCoordinator;
private readonly RelationalQueryContext _relationalQueryContext;
private readonly SelectExpression _selectExpression;
private readonly Func<QueryContext, DbDataReader, ResultCoordinator, Task<T>> _shaper;
private readonly Func<QueryContext, DbDataReader, int[], ResultCoordinator, Task<T>> _shaper;
private readonly IQuerySqlGeneratorFactory _querySqlGeneratorFactory;
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _logger;
Expand Down Expand Up @@ -104,6 +106,35 @@ public async ValueTask<bool> MoveNextAsync()
_relationalQueryContext.CommandLogger,
_cancellationToken);

if (selectExpression.IsNonComposedFromSql())
{
var projection = _selectExpression.Projection.ToList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possible optimization would be to have a non-allocating fast mode that just compares the column names and if they are already in the expected order doesn't create an index array otherwise it falls back to the remapping logic.
But we should measure whether this would be worth it. The most affected scenario would be queries that only return a single row.

var readerColumns = Enumerable.Range(0, _dataReader.DbDataReader.FieldCount)
smitpatel marked this conversation as resolved.
Show resolved Hide resolved
.ToDictionary(i => _dataReader.DbDataReader.GetName(i), i => i, StringComparer.OrdinalIgnoreCase);

_indexMap = new int[projection.Count];
for (var i = 0; i < projection.Count; i++)
{
if (projection[i].Expression is ColumnExpression columnExpression)
{
var columnName = columnExpression.Name;
if (columnName != null)
{
if (!readerColumns.TryGetValue(columnName, out var ordinal))
{
throw new InvalidOperationException(RelationalStrings.FromSqlMissingColumn(columnName));
}

_indexMap[i] = ordinal;
}
}
}
}
else
{
_indexMap = null;
}

_resultCoordinator = new ResultCoordinator();
}
catch (Exception)
Expand All @@ -121,7 +152,7 @@ public async ValueTask<bool> MoveNextAsync()

Current
= hasNext
? await _shaper(_relationalQueryContext, _dataReader.DbDataReader, _resultCoordinator)
? await _shaper(_relationalQueryContext, _dataReader.DbDataReader, _indexMap, _resultCoordinator)
: default;

return hasNext;
Expand Down

This file was deleted.

Loading