Skip to content

Commit

Permalink
Implement GROUP BY
Browse files Browse the repository at this point in the history
  • Loading branch information
tuespetre committed Mar 8, 2017
1 parent a882135 commit d14681e
Show file tree
Hide file tree
Showing 33 changed files with 4,074 additions and 1,327 deletions.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Storage;
using Remotion.Linq.Clauses.ResultOperators;

namespace Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal
{
/// <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 GroupingShaper : Shaper, IShaper<IGrouping<ValueBuffer, ValueBuffer>>
{
/// <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 GroupingShaper([NotNull] GroupResultOperator groupResultOperator)
: base(groupResultOperator)
{
}

/// <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 override Type Type => typeof(IGrouping<ValueBuffer, ValueBuffer>);

/// <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 virtual IGrouping<ValueBuffer, ValueBuffer> Shape(QueryContext queryContext, ValueBuffer valueBuffer)
{
return new ValueBufferGrouping(valueBuffer);
}

/// <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 override Shaper WithOffset(int offset) => this;

/// <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 static Expression CreateValueBufferAccessExpression(
[NotNull] Expression valueBufferGroupingExpression)
{
return Expression.MakeMemberAccess(
Expression.Convert(
valueBufferGroupingExpression,
typeof(ValueBufferGrouping)),
_valueBufferGroupingValueBufferProperty);
}

private static readonly PropertyInfo _valueBufferGroupingValueBufferProperty
= typeof(ValueBufferGrouping).GetTypeInfo().GetDeclaredProperty("ValueBuffer");

private struct ValueBufferGrouping : IGrouping<ValueBuffer, ValueBuffer>
{
public ValueBufferGrouping(ValueBuffer valueBuffer)
{
ValueBuffer = valueBuffer;
}

public ValueBuffer ValueBuffer { get; }

public ValueBuffer Key => ValueBuffer;

public IEnumerator<ValueBuffer> GetEnumerator() => Enumerable.Repeat(ValueBuffer, 1).GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class RelationalEntityQueryableExpressionVisitor : EntityQueryableExpress
private readonly IMaterializerFactory _materializerFactory;
private readonly IShaperCommandContextFactory _shaperCommandContextFactory;
private readonly IRelationalAnnotationProvider _relationalAnnotationProvider;
private readonly ISqlTranslatingExpressionVisitorFactory _sqlTranslatingExpressionVisitorFactory;
private readonly IQuerySource _querySource;

/// <summary>
Expand All @@ -53,6 +54,7 @@ public RelationalEntityQueryableExpressionVisitor(
_materializerFactory = dependencies.MaterializerFactory;
_shaperCommandContextFactory = dependencies.ShaperCommandContextFactory;
_relationalAnnotationProvider = dependencies.RelationalAnnotationProvider;
_sqlTranslatingExpressionVisitorFactory = dependencies.SqlTranslatingExpressionVisitorFactory;
_querySource = querySource;
}

Expand Down Expand Up @@ -92,6 +94,11 @@ protected override Expression VisitMember(MemberExpression node)
{
Check.NotNull(node, nameof(node));

if (TryHandleGroupingKeyExpression(node))
{
return node;
}

QueryModelVisitor
.BindMemberExpression(
node,
Expand All @@ -105,6 +112,46 @@ protected override Expression VisitMember(MemberExpression node)
return base.VisitMember(node);
}

private bool TryHandleGroupingKeyExpression(MemberExpression node)
{
if (node.Expression == null || !node.Expression.Type.IsGrouping() || node.Member.Name != "Key")
{
return false;
}

var groupedQuerySource = node.Expression.TryGetQuerySource();

if (groupedQuerySource == null)
{
return false;
}

var targetSelectExpression = QueryModelVisitor.TryGetQuery(groupedQuerySource);

if (targetSelectExpression == null)
{
return false;
}

var sqlTranslator = _sqlTranslatingExpressionVisitorFactory.Create(QueryModelVisitor);
var sqlExpression = sqlTranslator.Visit(node);

if (sqlExpression is CompositeExpression compositeExpression)
{
foreach (var expression in compositeExpression.Flatten())
{
targetSelectExpression.AddToProjection(
targetSelectExpression.RetargetProjectionExpression(
expression,
groupedQuerySource));
}

return true;
}

return false;
}

/// <summary>
/// Visit a method call expression.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,28 @@ public sealed class RelationalEntityQueryableExpressionVisitorDependencies
/// <param name="materializerFactory"> The materializer factory. </param>
/// <param name="shaperCommandContextFactory"> The shaper command context factory. </param>
/// <param name="relationalAnnotationProvider"> The relational annotation provider. </param>
/// <param name="sqlTranslatingExpressionVisitorFactory"> The SQL translating expression visitor factory. </param>
public RelationalEntityQueryableExpressionVisitorDependencies(
[NotNull] IModel model,
[NotNull] ISelectExpressionFactory selectExpressionFactory,
[NotNull] IMaterializerFactory materializerFactory,
[NotNull] IShaperCommandContextFactory shaperCommandContextFactory,
[NotNull] IRelationalAnnotationProvider relationalAnnotationProvider)
[NotNull] IRelationalAnnotationProvider relationalAnnotationProvider,
[NotNull] ISqlTranslatingExpressionVisitorFactory sqlTranslatingExpressionVisitorFactory)
{
Check.NotNull(model, nameof(model));
Check.NotNull(selectExpressionFactory, nameof(selectExpressionFactory));
Check.NotNull(materializerFactory, nameof(materializerFactory));
Check.NotNull(shaperCommandContextFactory, nameof(shaperCommandContextFactory));
Check.NotNull(relationalAnnotationProvider, nameof(relationalAnnotationProvider));
Check.NotNull(sqlTranslatingExpressionVisitorFactory, nameof(sqlTranslatingExpressionVisitorFactory));

Model = model;
SelectExpressionFactory = selectExpressionFactory;
MaterializerFactory = materializerFactory;
ShaperCommandContextFactory = shaperCommandContextFactory;
RelationalAnnotationProvider = relationalAnnotationProvider;
SqlTranslatingExpressionVisitorFactory = sqlTranslatingExpressionVisitorFactory;
}

/// <summary>
Expand Down Expand Up @@ -93,6 +97,11 @@ public RelationalEntityQueryableExpressionVisitorDependencies(
/// </summary>
public IRelationalAnnotationProvider RelationalAnnotationProvider { get; }

/// <summary>
/// The SQL translating expression visitor factory.
/// </summary>
public ISqlTranslatingExpressionVisitorFactory SqlTranslatingExpressionVisitorFactory { get; }

/// <summary>
/// Clones this dependency parameter object with one service replaced.
/// </summary>
Expand All @@ -104,7 +113,8 @@ public RelationalEntityQueryableExpressionVisitorDependencies With([NotNull] IMo
SelectExpressionFactory,
MaterializerFactory,
ShaperCommandContextFactory,
RelationalAnnotationProvider);
RelationalAnnotationProvider,
SqlTranslatingExpressionVisitorFactory);

/// <summary>
/// Clones this dependency parameter object with one service replaced.
Expand All @@ -117,7 +127,8 @@ public RelationalEntityQueryableExpressionVisitorDependencies With([NotNull] ISe
selectExpressionFactory,
MaterializerFactory,
ShaperCommandContextFactory,
RelationalAnnotationProvider);
RelationalAnnotationProvider,
SqlTranslatingExpressionVisitorFactory);

/// <summary>
/// Clones this dependency parameter object with one service replaced.
Expand All @@ -130,7 +141,8 @@ public RelationalEntityQueryableExpressionVisitorDependencies With([NotNull] IMa
SelectExpressionFactory,
materializerFactory,
ShaperCommandContextFactory,
RelationalAnnotationProvider);
RelationalAnnotationProvider,
SqlTranslatingExpressionVisitorFactory);

/// <summary>
/// Clones this dependency parameter object with one service replaced.
Expand All @@ -143,7 +155,8 @@ public RelationalEntityQueryableExpressionVisitorDependencies With([NotNull] ISh
SelectExpressionFactory,
MaterializerFactory,
shaperCommandContextFactory,
RelationalAnnotationProvider);
RelationalAnnotationProvider,
SqlTranslatingExpressionVisitorFactory);

/// <summary>
/// Clones this dependency parameter object with one service replaced.
Expand All @@ -156,6 +169,21 @@ public RelationalEntityQueryableExpressionVisitorDependencies With([NotNull] IRe
SelectExpressionFactory,
MaterializerFactory,
ShaperCommandContextFactory,
relationalAnnotationProvider);
relationalAnnotationProvider,
SqlTranslatingExpressionVisitorFactory);

/// <summary>
/// Clones this dependency parameter object with one service replaced.
/// </summary>
/// <param name="sqlTranslatingExpressionVisitorFactory"> A replacement for the current dependency of this type. </param>
/// <returns> A new parameter object with the given service replaced. </returns>
public RelationalEntityQueryableExpressionVisitorDependencies With([NotNull] ISqlTranslatingExpressionVisitorFactory sqlTranslatingExpressionVisitorFactory)
=> new RelationalEntityQueryableExpressionVisitorDependencies(
Model,
SelectExpressionFactory,
MaterializerFactory,
ShaperCommandContextFactory,
RelationalAnnotationProvider,
sqlTranslatingExpressionVisitorFactory);
}
}
Loading

0 comments on commit d14681e

Please sign in to comment.