-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query: TPC prune unused concrete tables and columns (#27993)
- Add TpcTablesExpression which holds all union all tables so we can prune unused subqueries - Also allow pruning of unused column inside each subquery - Assign unique aliases in inner subqueries across whole SelectExpression - Apply predicate on discriminator column directly to subqueries by removing unnecessary subqueries Resolves #27967 Resolves #27957
- Loading branch information
Showing
15 changed files
with
1,630 additions
and
1,406 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
src/EFCore.Relational/Query/Internal/TpcTablesExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.Internal; | ||
|
||
/// <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 class TpcTablesExpression : TableExpressionBase | ||
{ | ||
/// <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 TpcTablesExpression( | ||
string? alias, IEntityType entityType, IReadOnlyList<SelectExpression> subSelectExpressions) | ||
: base(alias) | ||
{ | ||
EntityType = entityType; | ||
SelectExpressions = subSelectExpressions; | ||
} | ||
|
||
/// <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> | ||
private TpcTablesExpression( | ||
string? alias, | ||
IEntityType entityType, | ||
IReadOnlyList<SelectExpression> subSelectExpressions, | ||
IEnumerable<IAnnotation>? annotations) | ||
: base(alias, annotations) | ||
{ | ||
EntityType = entityType; | ||
SelectExpressions = subSelectExpressions; | ||
} | ||
|
||
/// <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> | ||
[NotNull] | ||
public override string? Alias | ||
{ | ||
get => base.Alias!; | ||
internal set => base.Alias = value; | ||
} | ||
|
||
/// <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 virtual IEntityType EntityType { get; } | ||
|
||
/// <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 virtual IReadOnlyList<SelectExpression> SelectExpressions { get; } | ||
|
||
/// <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 virtual TpcTablesExpression Prune(IReadOnlyList<string> discriminatorValues, IReadOnlyCollection<string>? referencedColumns) | ||
{ | ||
var subSelectExpressions = discriminatorValues.Count == 0 | ||
? new List<SelectExpression> { SelectExpressions[0] } | ||
: SelectExpressions.Where(se => | ||
discriminatorValues.Contains((string)((SqlConstantExpression)se.Projection[^1].Expression).Value!)).ToList(); | ||
|
||
Check.DebugAssert(subSelectExpressions.Count > 0, "TPC must have at least 1 table selected."); | ||
|
||
if (referencedColumns != null) | ||
{ | ||
foreach (var se in subSelectExpressions) | ||
{ | ||
se.Prune(referencedColumns); | ||
} | ||
} | ||
|
||
return new TpcTablesExpression(Alias, EntityType, subSelectExpressions, GetAnnotations()); | ||
} | ||
|
||
/// <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> | ||
// This is implementation detail hence visitors are not supposed to see inside unless they really need to. | ||
protected override Expression VisitChildren(ExpressionVisitor visitor) => this; | ||
|
||
/// <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> | ||
protected override void Print(ExpressionPrinter expressionPrinter) | ||
{ | ||
expressionPrinter.AppendLine("("); | ||
using (expressionPrinter.Indent()) | ||
{ | ||
expressionPrinter.VisitCollection(SelectExpressions, e => e.AppendLine().AppendLine("UNION ALL")); | ||
} | ||
expressionPrinter.AppendLine() | ||
.AppendLine(") AS " + Alias); | ||
PrintAnnotations(expressionPrinter); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object? obj) | ||
=> obj != null | ||
&& (ReferenceEquals(this, obj) | ||
|| obj is TpcTablesExpression tpcTablesExpression | ||
&& Equals(tpcTablesExpression)); | ||
|
||
private bool Equals(TpcTablesExpression tpcTablesExpression) | ||
{ | ||
if (!base.Equals(tpcTablesExpression) | ||
|| EntityType != tpcTablesExpression.EntityType) | ||
{ | ||
return false; | ||
} | ||
|
||
return SelectExpressions.SequenceEqual(tpcTablesExpression.SelectExpressions); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), EntityType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.