Skip to content

Commit

Permalink
Add support for translating GROUP BY and OUTER APPLY; optimize transl…
Browse files Browse the repository at this point in the history
…ated

SQL

- Resolves dotnet#2341
- Resolves dotnet#5085
- Resolves dotnet#6618
- Resolves dotnet#6647
- Resolves dotnet#6782
- Resolves dotnet#7080
- Resolves dotnet#7220
- Resolves dotnet#7417
- Resolves dotnet#7497
- Resolves dotnet#7523
- Resolves dotnet#7525
  • Loading branch information
tuespetre committed Feb 7, 2017
1 parent f15099d commit 5652a69
Show file tree
Hide file tree
Showing 99 changed files with 8,808 additions and 4,146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public static IServiceCollection AddEntityFrameworkInMemoryDatabase([NotNull] th
.TryAddScoped<IMaterializerFactory, MaterializerFactory>()
.TryAddScoped<IQueryContextFactory, InMemoryQueryContextFactory>()
.TryAddScoped<IEntityQueryModelVisitorFactory, InMemoryQueryModelVisitorFactory>()
.TryAddScoped<IEntityQueryableExpressionVisitorFactory, InMemoryEntityQueryableExpressionVisitorFactory>();
.TryAddScoped<IEntityQueryableExpressionVisitorFactory, InMemoryEntityQueryableExpressionVisitorFactory>()
.TryAddScoped<IInMemoryResultOperatorHandler, InMemoryResultOperatorHandler>();

ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(serviceCollectionMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
<Compile Include="Query\Internal\InMemoryQueryContextFactory.cs" />
<Compile Include="Query\Internal\InMemoryQueryModelVisitor.cs" />
<Compile Include="Query\Internal\InMemoryQueryModelVisitorFactory.cs" />
<Compile Include="Query\Internal\IInMemoryResultOperatorHandler.cs" />
<Compile Include="Query\Internal\MaterializerFactory.cs" />
<Compile Include="Query\Internal\InMemoryResultOperatorHandler.cs" />
<Compile Include="Storage\Internal\InMemoryTransaction.cs" />
<Compile Include="Storage\Internal\InMemoryTransactionManager.cs" />
<Compile Include="Storage\Internal\IInMemoryDatabase.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 Remotion.Linq.Clauses;

namespace Microsoft.EntityFrameworkCore.Query.Internal
{
/// <summary>
/// An in-memory-specific handler for <see cref="ResultOperatorBase" /> instances.
/// </summary>
public interface IInMemoryResultOperatorHandler : IResultOperatorHandler
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
using Microsoft.EntityFrameworkCore.Extensions.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Query.Expressions.Internal;
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors;
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Remotion.Linq;
using Remotion.Linq.Clauses;

namespace Microsoft.EntityFrameworkCore.Query.Internal
{
Expand Down Expand Up @@ -93,6 +96,42 @@ var includeExpressionVisitor
Expression = includeExpressionVisitor.Visit(Expression);
}

public override Expression BindValueBufferReadExpression(
[NotNull] ValueBufferReadExpression valueBufferReadExpression,
int index)
{
var propertyIndex = valueBufferReadExpression.Property.GetIndex();

return base.BindValueBufferReadExpression(valueBufferReadExpression, propertyIndex);
}

protected override Expression CompileJoinClauseInnerKeySelectorExpression(
[NotNull] IQuerySource querySource,
[NotNull] Expression innerKeySelector,
[NotNull] ParameterExpression parameter,
[NotNull] QueryModel queryModel)
{
var compiled = base.CompileJoinClauseInnerKeySelectorExpression(querySource, innerKeySelector, parameter, queryModel);

if (parameter.Type == typeof(ValueBuffer))
{
var test = IsValueBufferEmpty(parameter);
var ifTrue = Expression.Default(innerKeySelector.Type);
var ifFalse = compiled;

return Expression.Condition(test, ifTrue, ifFalse);
}

return compiled;
}

private static Expression IsValueBufferEmpty(Expression valueBufferExpression)
{
return Expression.MakeMemberAccess(
valueBufferExpression,
typeof(ValueBuffer).GetRuntimeProperty(nameof(ValueBuffer.IsEmpty)));
}

private sealed class InMemoryIncludeExpressionVisitor : ExpressionVisitorBase
{
private readonly IncludeSpecification _includeSpecification;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public InMemoryQueryModelVisitorFactory(
[NotNull] IProjectionExpressionVisitorFactory projectionExpressionVisitorFactory,
[NotNull] IEntityQueryableExpressionVisitorFactory entityQueryableExpressionVisitorFactory,
[NotNull] IQueryAnnotationExtractor queryAnnotationExtractor,
[NotNull] IResultOperatorHandler resultOperatorHandler,
[NotNull] IInMemoryResultOperatorHandler resultOperatorHandler,
[NotNull] IEntityMaterializerSource entityMaterializerSource,
[NotNull] IExpressionPrinter expressionPrinter,
[NotNull] IMaterializerFactory materializerFactory)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Extensions.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Query.Expressions;
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors;
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal;
using Remotion.Linq;
using Remotion.Linq.Clauses;
using Remotion.Linq.Clauses.Expressions;
using Remotion.Linq.Clauses.ResultOperators;
using Remotion.Linq.Parsing;
using Microsoft.EntityFrameworkCore.Storage;
using Remotion.Linq.Clauses.ExpressionVisitors;

namespace Microsoft.EntityFrameworkCore.Query.Internal
{
using ResultHandler = Func<EntityQueryModelVisitor, ResultOperatorBase, QueryModel, Expression>;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class InMemoryResultOperatorHandler : IInMemoryResultOperatorHandler
{
private static readonly Dictionary<Type, ResultHandler> _handlers
= new Dictionary<Type, ResultHandler>
{
{ typeof(OfTypeResultOperator), (v, r, q) => HandleOfType(v, (OfTypeResultOperator)r) },
};

private readonly IModel _model;
private readonly IResultOperatorHandler _resultOperatorHandler;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public InMemoryResultOperatorHandler(
[NotNull] IModel model,
[NotNull] IResultOperatorHandler resultOperatorHandler)
{
_model = model;
_resultOperatorHandler = resultOperatorHandler;
}

/// <summary>
/// Handles the result operator.
/// </summary>
/// <param name="entityQueryModelVisitor"> The entity query model visitor. </param>
/// <param name="resultOperator"> The result operator. </param>
/// <param name="queryModel"> The query model. </param>
/// <returns>
/// An compiled query expression fragment representing the result operator.
/// </returns>
public virtual Expression HandleResultOperator(
EntityQueryModelVisitor entityQueryModelVisitor,
ResultOperatorBase resultOperator,
QueryModel queryModel)
{
ResultHandler handler;
if (!_handlers.TryGetValue(resultOperator.GetType(), out handler))
{
return _resultOperatorHandler.HandleResultOperator(
entityQueryModelVisitor,
resultOperator,
queryModel);
}

return handler(entityQueryModelVisitor, resultOperator, queryModel);
}

private static Expression HandleOfType(
EntityQueryModelVisitor entityQueryModelVisitor,
OfTypeResultOperator ofTypeResultOperator)
{
var entityType
= entityQueryModelVisitor.QueryCompilationContext.Model
.FindEntityType(ofTypeResultOperator.SearchedItemType);

if (entityType != null)
{
var currentExpression = entityQueryModelVisitor.Expression as MethodCallExpression;

if (currentExpression?.Method == InMemoryQueryModelVisitor.ProjectionQueryMethodInfo)
{
return currentExpression.Update(
null,
new []
{
currentExpression.Arguments[0],
Expression.Constant(entityType)
});
}
}

return Expression.Call(
entityQueryModelVisitor.LinqOperatorProvider.OfType
.MakeGenericMethod(ofTypeResultOperator.SearchedItemType),
entityQueryModelVisitor.Expression);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public virtual void Throws_when_where_subquery_correlated()
Assert.Equal(CoreStrings.WarningAsErrorTemplate(
$"{nameof(RelationalEventId)}.{nameof(RelationalEventId.QueryClientEvaluationWarning)}",
RelationalStrings.ClientEvalWarning(
"{from Customer c2 in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Customer]) where (([c1].CustomerID == [c2].CustomerID) AndAlso [c2].IsLondon) select [c2] => Any()}")),
"[c2].IsLondon")),
Assert.Throws<InvalidOperationException>(
() => context.Customers
.Where(c1 => context.Customers
Expand Down Expand Up @@ -179,12 +179,12 @@ public virtual void Throws_when_group_join()
{
Assert.Equal(CoreStrings.WarningAsErrorTemplate(
$"{nameof(RelationalEventId)}.{nameof(RelationalEventId.QueryClientEvaluationWarning)}",
RelationalStrings.ClientEvalWarning("join Int32 i in __p_0 on [e1].EmployeeID equals [i]")),
RelationalStrings.ClientEvalWarning("join Int32 i in __p_0 on [e1].EmployeeID equals [i] into IEnumerable`1 g")),
Assert.Throws<InvalidOperationException>(
() =>
(from e1 in context.Employees
join i in new[] { 1, 2, 3 } on e1.EmployeeID equals i into g
select e1)
select new { e1, g })
.ToList()).Message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,17 @@ public static bool IsSimpleExpression([NotNull] this Expression expression)
|| unwrappedExpression is ParameterExpression
|| unwrappedExpression.IsAliasWithColumnExpression();
}

public static Expression MaybeAnonymousSubquery([CanBeNull] this Expression expression)
{
var subquery = expression as SelectExpression;

if (subquery != null)
{
subquery.Alias = string.Empty;
}

return expression;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public static void TryAddDefaultRelationalServices([NotNull] ServiceCollectionMa
.TryAddScoped<IConditionalRemovingExpressionVisitorFactory, ConditionalRemovingExpressionVisitorFactory>()
.TryAddScoped<ICompositePredicateExpressionVisitorFactory, CompositePredicateExpressionVisitorFactory>()
.TryAddScoped<IIncludeExpressionVisitorFactory, IncludeExpressionVisitorFactory>()
.TryAddScoped<IQueryFlattenerFactory, QueryFlattenerFactory>()
.TryAddScoped<ISelectExpressionFactory, SelectExpressionFactory>()
.TryAddScoped<IExpressionPrinter, RelationalExpressionPrinter>()
.TryAddScoped<IRelationalResultOperatorHandler, RelationalResultOperatorHandler>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@
<Compile Include="Properties\InternalsVisibleTo.cs" />
<Compile Include="Query\AsyncQueryMethodProvider.cs" />
<Compile Include="Query\Expressions\AliasExpression.cs" />
<Compile Include="Query\Expressions\OuterPropertyExpression.cs" />
<Compile Include="Query\Expressions\ColumnExpression.cs" />
<Compile Include="Query\Expressions\CompositeExpression.cs" />
<Compile Include="Query\Expressions\PredicateJoinExpressionBase.cs" />
<Compile Include="Query\Expressions\LeftJoinLateralExpression.cs" />
<Compile Include="Query\Expressions\SqlFragmentExpression.cs" />
<Compile Include="Query\Expressions\CrossJoinExpression.cs" />
<Compile Include="Query\Expressions\DiscriminatorPredicateExpression.cs" />
Expand All @@ -194,7 +198,7 @@
<Compile Include="Query\Expressions\ISelectExpressionFactory.cs" />
<Compile Include="Query\Expressions\IsNullExpression.cs" />
<Compile Include="Query\Expressions\JoinExpressionBase.cs" />
<Compile Include="Query\Expressions\LateralJoinExpression.cs" />
<Compile Include="Query\Expressions\CrossJoinLateralExpression.cs" />
<Compile Include="Query\Expressions\LeftOuterJoinExpression.cs" />
<Compile Include="Query\Expressions\LikeExpression.cs" />
<Compile Include="Query\Expressions\NotNullableExpression.cs" />
Expand Down Expand Up @@ -226,32 +230,37 @@
<Compile Include="Query\ExpressionVisitors\Internal\BufferedOffsetEntityShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\CompositePredicateExpressionVisitor.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\CompositePredicateExpressionVisitorFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\CompositeShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\ConditionalRemovingExpressionVisitor.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\ConditionalRemovingExpressionVisitorFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\EntityShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\EqualityPredicateExpandingVisitor.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\EqualityPredicateInExpressionOptimizer.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\GroupingShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\GroupJoinOuterEqualityComparer.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\ICompositePredicateExpressionVisitorFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IConditionalRemovingExpressionVisitorFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IIncludeExpressionVisitorFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IMaterializerFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IncludeExpressionVisitor.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IncludeExpressionVisitorFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IQueryFlattenerFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IOffsettableShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IShaper`.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IsNullExpressionBuildingVisitor.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\MaterializerFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\OffsettableShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\PredicateReductionExpressionOptimizer.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\PredicateNegationExpressionOptimizer.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\QueryFlattener.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\QueryFlattenerFactory.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\ProjectionShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\RelationalNullsExpandingVisitor.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\RelationalNullsExpressionVisitorBase.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\RelationalNullsOptimizedExpandingVisitor.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\ResultTransformingExpressionVisitor.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\IShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\Shaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\UnbufferedEntityShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\UnbufferedOffsetEntityShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\GroupJoinValueBufferShaper.cs" />
<Compile Include="Query\ExpressionVisitors\Internal\ValueBufferShaper.cs" />
<Compile Include="Query\ExpressionVisitors\ISqlTranslatingExpressionVisitorFactory.cs" />
<Compile Include="Query\ExpressionVisitors\RelationalEntityQueryableExpressionVisitor.cs" />
Expand Down
Loading

0 comments on commit 5652a69

Please sign in to comment.