Skip to content

Commit

Permalink
Move all pruning code to the pruner and make it an immutable visitor
Browse files Browse the repository at this point in the history
Fixes #31276
  • Loading branch information
roji committed Jan 14, 2024
1 parent 341241f commit a15c626
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 309 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,9 @@
<data name="SetOperationOverDifferentStructuralTypes" xml:space="preserve">
<value>Set operations over different entity or complex types are not supported ('{type1}' and '{type2}').</value>
</data>
<data name="SelectExpressionUpdateNotSupportedWhileMutable" xml:space="preserve">
<value>SelectExpression.Update() is not supported while the expression is in mutable state.</value>
</data>
<data name="SetOperationsNotAllowedAfterClientEvaluation" xml:space="preserve">
<value>Unable to translate set operation after client projection has been applied. Consider moving the set operation before the last 'Select' call.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,55 +77,15 @@ private Expression VisitCase(CaseExpression caseExpression)

private Expression VisitSelect(SelectExpression selectExpression)
{
var changed = false;
var projections = new List<ProjectionExpression>();
foreach (var item in selectExpression.Projection)
{
var updatedProjection = (ProjectionExpression)Visit(item);
projections.Add(updatedProjection);
changed |= updatedProjection != item;
}

var tables = new List<TableExpressionBase>();
foreach (var table in selectExpression.Tables)
{
var newTable = (TableExpressionBase)Visit(table);
changed |= newTable != table;
tables.Add(newTable);
}

var projections = this.VisitAndConvert(selectExpression.Projection);
var tables = this.VisitAndConvert(selectExpression.Tables);
var predicate = TryCompensateForBoolWithValueConverter((SqlExpression?)Visit(selectExpression.Predicate));
changed |= predicate != selectExpression.Predicate;

var groupBy = new List<SqlExpression>();
foreach (var groupingKey in selectExpression.GroupBy)
{
var newGroupingKey = (SqlExpression)Visit(groupingKey);
changed |= newGroupingKey != groupingKey;
groupBy.Add(newGroupingKey);
}

var groupBy = this.VisitAndConvert(selectExpression.GroupBy);
var having = TryCompensateForBoolWithValueConverter((SqlExpression?)Visit(selectExpression.Having));
changed |= having != selectExpression.Having;

var orderings = new List<OrderingExpression>();
foreach (var ordering in selectExpression.Orderings)
{
var orderingExpression = (SqlExpression)Visit(ordering.Expression);
changed |= orderingExpression != ordering.Expression;
orderings.Add(ordering.Update(orderingExpression));
}

var orderings = this.VisitAndConvert(selectExpression.Orderings);
var offset = (SqlExpression?)Visit(selectExpression.Offset);
changed |= offset != selectExpression.Offset;

var limit = (SqlExpression?)Visit(selectExpression.Limit);
changed |= limit != selectExpression.Limit;

return changed
? selectExpression.Update(
projections, tables, predicate, groupBy, having, orderings, limit, offset)
: selectExpression;
return selectExpression.Update(projections, tables, predicate, groupBy, having, orderings, limit, offset);
}

private Expression VisitInnerJoin(InnerJoinExpression innerJoinExpression)
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ protected override Expression VisitDelete(DeleteExpression deleteExpression)
&& selectExpression.Orderings.Count == 0
&& selectExpression.GroupBy.Count == 0
&& selectExpression.Tables.Count == 1
&& selectExpression.Tables[0] == deleteExpression.Table
&& selectExpression.Tables[0].Equals(deleteExpression.Table)
&& selectExpression.Projection.Count == 0)
{
_relationalCommandBuilder.Append("DELETE FROM ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ bool TranslateSetters(
translatedSetters = tempTranslatedSetters;

Check.DebugAssert(targetTableAlias is not null, "Target table alias should have a value");
var selectExpression = (SelectExpression)source.QueryExpression;
targetTable = selectExpression.Tables.First(t => t.GetRequiredAlias() == targetTableAlias);

return true;
Expand Down
12 changes: 7 additions & 5 deletions src/EFCore.Relational/Query/SqlExpressions/DeleteExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,22 @@ public override ExpressionType NodeType
protected override Expression VisitChildren(ExpressionVisitor visitor)
{
var selectExpression = (SelectExpression)visitor.Visit(SelectExpression);
var table = (TableExpression)visitor.Visit(Table);

return Update(selectExpression);
return Update(table, selectExpression);
}

/// <summary>
/// Creates a new expression that is like this one, but using the supplied children. If all of the children are the same, it will
/// return this expression.
/// </summary>
/// <param name="table">The <see cref="Table" /> property of the result.</param>
/// <param name="selectExpression">The <see cref="SelectExpression" /> property of the result.</param>
/// <returns>This expression if no children changed, or an expression with the updated children.</returns>
public DeleteExpression Update(SelectExpression selectExpression)
=> selectExpression != SelectExpression
? new DeleteExpression(Table, selectExpression, Tags)
: this;
public DeleteExpression Update(TableExpression table, SelectExpression selectExpression)
=> table == Table && selectExpression == SelectExpression
? this
: new DeleteExpression(table, selectExpression, Tags);

/// <inheritdoc />
public void Print(ExpressionPrinter expressionPrinter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ public TpcTableExpressionRemovingExpressionVisitor(SqlAliasManager sqlAliasManag
CreateColumnExpression(projection, setOperationAlias), projection.Alias));
}

generatedSelectExpression._mutable = false;
generatedSelectExpression.IsMutable = false;
result = generatedSelectExpression;
}

Expand Down
Loading

0 comments on commit a15c626

Please sign in to comment.