Skip to content

Commit

Permalink
Add Equals/Hash/Print support for set operations to SelectExpression
Browse files Browse the repository at this point in the history
Fixes #16262
  • Loading branch information
roji committed Jun 28, 2019
1 parent a2ce460 commit 86a31cd
Showing 1 changed file with 63 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1083,19 +1083,26 @@ private bool Equals(SelectExpression selectExpression)
return false;
}

if (SetOperationType != selectExpression.SetOperationType)
{
return false;
}

if (_projectionMapping.Count != selectExpression._projectionMapping.Count)
{
foreach (var projectionMapping in _projectionMapping)
return false;
}

foreach (var projectionMapping in _projectionMapping)
{
if (!selectExpression._projectionMapping.TryGetValue(projectionMapping.Key, out var projection))
{
if (!selectExpression._projectionMapping.TryGetValue(projectionMapping.Key, out var projection))
{
return false;
}
return false;
}

if (!projectionMapping.Value.Equals(projection))
{
return false;
}
if (!projectionMapping.Value.Equals(projection))
{
return false;
}
}

Expand Down Expand Up @@ -1167,6 +1174,9 @@ public override int GetHashCode()
{
var hash = new HashCode();
hash.Add(base.GetHashCode());

hash.Add(SetOperationType);

foreach (var projectionMapping in _projectionMapping)
{
hash.Add(projectionMapping.Key);
Expand Down Expand Up @@ -1206,47 +1216,59 @@ public override void Print(ExpressionPrinter expressionPrinter)
}

expressionPrinter.StringBuilder.AppendLine();
if (Alias != null)

if (IsSetOperation)
{
expressionPrinter.StringBuilder.AppendLine("(");
expressionPrinter.StringBuilder.IncrementIndent();
expressionPrinter.Visit(Tables[0]);
expressionPrinter.StringBuilder
.AppendLine()
.AppendLine(SetOperationType.ToString().ToUpperInvariant());
expressionPrinter.Visit(Tables[1]);
}
else
{
if (Alias != null)
{
expressionPrinter.StringBuilder.AppendLine("(");
expressionPrinter.StringBuilder.IncrementIndent();
}

expressionPrinter.StringBuilder.Append("SELECT ");
expressionPrinter.StringBuilder.Append("SELECT ");

if (IsDistinct)
{
expressionPrinter.StringBuilder.Append("DISTINCT ");
}
if (IsDistinct)
{
expressionPrinter.StringBuilder.Append("DISTINCT ");
}

if (Limit != null
&& Offset == null)
{
expressionPrinter.StringBuilder.Append("TOP(");
expressionPrinter.Visit(Limit);
expressionPrinter.StringBuilder.Append(") ");
}
if (Limit != null
&& Offset == null)
{
expressionPrinter.StringBuilder.Append("TOP(");
expressionPrinter.Visit(Limit);
expressionPrinter.StringBuilder.Append(") ");
}

if (Projection.Any())
{
expressionPrinter.VisitList(Projection);
}
else
{
expressionPrinter.StringBuilder.Append("1");
}
if (Projection.Any())
{
expressionPrinter.VisitList(Projection);
}
else
{
expressionPrinter.StringBuilder.Append("1");
}

if (Tables.Any())
{
expressionPrinter.StringBuilder.AppendLine().Append("FROM ");
if (Tables.Any())
{
expressionPrinter.StringBuilder.AppendLine().Append("FROM ");

expressionPrinter.VisitList(Tables, p => p.StringBuilder.AppendLine());
}
expressionPrinter.VisitList(Tables, p => p.StringBuilder.AppendLine());
}

if (Predicate != null)
{
expressionPrinter.StringBuilder.AppendLine().Append("WHERE ");
expressionPrinter.Visit(Predicate);
if (Predicate != null)
{
expressionPrinter.StringBuilder.AppendLine().Append("WHERE ");
expressionPrinter.Visit(Predicate);
}
}

if (Orderings.Any())
Expand Down

0 comments on commit 86a31cd

Please sign in to comment.