Skip to content

Commit

Permalink
Query: Change alias of the column for TPT Discriminator (#21787)
Browse files Browse the repository at this point in the history
Feedback of #21784
Part of #21509
  • Loading branch information
smitpatel authored Jul 26, 2020
1 parent 9266c75 commit 2a3ff06
Show file tree
Hide file tree
Showing 13 changed files with 601 additions and 592 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@ public static IEnumerable<ITableMappingBase> GetViewOrTableMappings([NotNull] th
(IEnumerable<ITableMappingBase>)(entityType[RelationalAnnotationNames.ViewMappings]
?? entityType[RelationalAnnotationNames.TableMappings])
?? Enumerable.Empty<ITableMappingBase>();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static IReadOnlyList<string> GetTptDiscriminatorValues([NotNull] this IEntityType entityType)
=> entityType.GetConcreteDerivedTypesInclusive().Select(et => et.ShortName()).ToList();
}
}
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Query/EntityProjectionExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Utilities;

Expand Down Expand Up @@ -135,8 +136,7 @@ public virtual EntityProjectionExpression UpdateEntityType([NotNull] IEntityType
var discriminatorExpression = DiscriminatorExpression;
if (DiscriminatorExpression is CaseExpression caseExpression)
{
var entityTypesToSelect = derivedType.GetDerivedTypesInclusive().Where(et => !et.IsAbstract())
.Select(et => et.ShortName()).ToHashSet();
var entityTypesToSelect = derivedType.GetTptDiscriminatorValues();
var whenClauses = caseExpression.WhenClauses
.Where(wc => entityTypesToSelect.Contains((string)((SqlConstantExpression)wc.Result).Value))
.ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,7 @@ protected override ShapedQueryExpression TranslateOfType(ShapedQueryExpression s
if (discriminatorProperty == null)
{
var selectExpression = (SelectExpression)source.QueryExpression;
var discriminatorValues = derivedType.GetConcreteDerivedTypesInclusive().Where(et => !et.IsAbstract())
.Select(et => et.ShortName()).ToList();
var discriminatorValues = derivedType.GetTptDiscriminatorValues();
var projectionBindingExpression = (ProjectionBindingExpression)entityShaperExpression.ValueBufferExpression;

var projectionMember = projectionBindingExpression.ProjectionMember;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,12 +780,11 @@ protected override Expression VisitTypeBinary(TypeBinaryExpression typeBinaryExp
var derivedType = entityType.GetDerivedTypes().SingleOrDefault(et => et.ClrType == typeBinaryExpression.TypeOperand);
if (derivedType != null)
{
var concreteEntityTypes = derivedType.GetConcreteDerivedTypesInclusive().ToList();
var discriminatorProperty = entityType.GetDiscriminatorProperty();
if (discriminatorProperty == null)
{
// TPT
var discriminatorValues = concreteEntityTypes.Select(et => et.ShortName()).ToList();
var discriminatorValues = derivedType.GetTptDiscriminatorValues();
if (entityReferenceExpression.SubqueryEntity != null)
{
var entityShaper = (EntityShaperExpression)entityReferenceExpression.SubqueryEntity.ShaperExpression;
Expand Down Expand Up @@ -839,6 +838,7 @@ SqlExpression GeneratePredicateTPT(EntityProjectionExpression entityProjectionEx
}
else
{
var concreteEntityTypes = derivedType.GetConcreteDerivedTypesInclusive().ToList();
var discriminatorColumn = BindProperty(entityReferenceExpression, discriminatorProperty);
if (discriminatorColumn != null)
{
Expand Down
10 changes: 6 additions & 4 deletions src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public sealed class SelectExpression : TableExpressionBase
{ ExpressionType.GreaterThanOrEqual, ExpressionType.LessThanOrEqual },
};

private const string DiscriminatorColumnAlias = "Discriminator";

private readonly IDictionary<EntityProjectionExpression, IDictionary<IProperty, int>> _entityProjectionCache
= new Dictionary<EntityProjectionExpression, IDictionary<IProperty, int>>();

Expand Down Expand Up @@ -328,7 +330,7 @@ public void ApplyProjection()

if (entityProjection.DiscriminatorExpression != null)
{
AddToProjection(entityProjection.DiscriminatorExpression);
AddToProjection(entityProjection.DiscriminatorExpression, DiscriminatorColumnAlias);
}

result[keyValuePair.Key] = Constant(map);
Expand Down Expand Up @@ -443,7 +445,7 @@ public IDictionary<IProperty, int> AddToProjection([NotNull] EntityProjectionExp

if (entityProjection.DiscriminatorExpression != null)
{
AddToProjection(entityProjection.DiscriminatorExpression);
AddToProjection(entityProjection.DiscriminatorExpression, DiscriminatorColumnAlias);
}

_entityProjectionCache[entityProjection] = dictionary;
Expand Down Expand Up @@ -933,7 +935,7 @@ void HandleEntityProjection(
discriminatorExpression = GenerateDiscriminatorExpression(
select1, projection1.DiscriminatorExpression,
select2, projection2.DiscriminatorExpression,
"Discriminator");
DiscriminatorColumnAlias);
}

_projectionMapping[projectionMember] = new EntityProjectionExpression(
Expand Down Expand Up @@ -1143,7 +1145,7 @@ EntityProjectionExpression LiftEntityProjectionFromSubquery(EntityProjectionExpr
ColumnExpression discriminatorExpression = null;
if (entityProjection.DiscriminatorExpression != null)
{
discriminatorExpression = subquery.GenerateOuterColumn(entityProjection.DiscriminatorExpression);
discriminatorExpression = subquery.GenerateOuterColumn(entityProjection.DiscriminatorExpression, DiscriminatorColumnAlias);
projectionMap[entityProjection.DiscriminatorExpression] = discriminatorExpression;
}

Expand Down
1 change: 0 additions & 1 deletion src/EFCore/Metadata/Internal/EntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void Can_use_of_type_animal()
@"SELECT [a].[Species], [a].[CountryId], [a].[Name], [b].[EagleId], [b].[IsFlightless], [e].[Group], [k].[FoundOn], CASE
WHEN [k].[Species] IS NOT NULL THEN N'Kiwi'
WHEN [e].[Species] IS NOT NULL THEN N'Eagle'
END
END AS [Discriminator]
FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Species] = [b].[Species]
LEFT JOIN [Eagle] AS [e] ON [a].[Species] = [e].[Species]
Expand All @@ -39,7 +39,7 @@ public override void Can_use_is_kiwi()
@"SELECT [a].[Species], [a].[CountryId], [a].[Name], [b].[EagleId], [b].[IsFlightless], [e].[Group], [k].[FoundOn], CASE
WHEN [k].[Species] IS NOT NULL THEN N'Kiwi'
WHEN [e].[Species] IS NOT NULL THEN N'Eagle'
END
END AS [Discriminator]
FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Species] = [b].[Species]
LEFT JOIN [Eagle] AS [e] ON [a].[Species] = [e].[Species]
Expand All @@ -55,7 +55,7 @@ public override void Can_use_is_kiwi_with_other_predicate()
@"SELECT [a].[Species], [a].[CountryId], [a].[Name], [b].[EagleId], [b].[IsFlightless], [e].[Group], [k].[FoundOn], CASE
WHEN [k].[Species] IS NOT NULL THEN N'Kiwi'
WHEN [e].[Species] IS NOT NULL THEN N'Eagle'
END
END AS [Discriminator]
FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Species] = [b].[Species]
LEFT JOIN [Eagle] AS [e] ON [a].[Species] = [e].[Species]
Expand Down Expand Up @@ -87,7 +87,7 @@ public override void Can_use_of_type_bird()
@"SELECT [a].[Species], [a].[CountryId], [a].[Name], [b].[EagleId], [b].[IsFlightless], [e].[Group], [k].[FoundOn], CASE
WHEN [k].[Species] IS NOT NULL THEN N'Kiwi'
WHEN [e].[Species] IS NOT NULL THEN N'Eagle'
END
END AS [Discriminator]
FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Species] = [b].[Species]
LEFT JOIN [Eagle] AS [e] ON [a].[Species] = [e].[Species]
Expand All @@ -104,7 +104,7 @@ public override void Can_use_of_type_bird_predicate()
@"SELECT [a].[Species], [a].[CountryId], [a].[Name], [b].[EagleId], [b].[IsFlightless], [e].[Group], [k].[FoundOn], CASE
WHEN [k].[Species] IS NOT NULL THEN N'Kiwi'
WHEN [e].[Species] IS NOT NULL THEN N'Eagle'
END
END AS [Discriminator]
FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Species] = [b].[Species]
LEFT JOIN [Eagle] AS [e] ON [a].[Species] = [e].[Species]
Expand Down Expand Up @@ -134,7 +134,7 @@ public override void Can_use_of_type_bird_first()
@"SELECT TOP(1) [a].[Species], [a].[CountryId], [a].[Name], [b].[EagleId], [b].[IsFlightless], [e].[Group], [k].[FoundOn], CASE
WHEN [k].[Species] IS NOT NULL THEN N'Kiwi'
WHEN [e].[Species] IS NOT NULL THEN N'Eagle'
END
END AS [Discriminator]
FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Species] = [b].[Species]
LEFT JOIN [Eagle] AS [e] ON [a].[Species] = [e].[Species]
Expand All @@ -150,7 +150,7 @@ public override void Can_use_of_type_kiwi()
AssertSql(
@"SELECT [a].[Species], [a].[CountryId], [a].[Name], [b].[EagleId], [b].[IsFlightless], [k].[FoundOn], CASE
WHEN [k].[Species] IS NOT NULL THEN N'Kiwi'
END
END AS [Discriminator]
FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Species] = [b].[Species]
LEFT JOIN [Eagle] AS [e] ON [a].[Species] = [e].[Species]
Expand Down
Loading

0 comments on commit 2a3ff06

Please sign in to comment.