-
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: Add RowNumberExpression support to convert lateral joins to joins
This enables support for non-scalar single result on Sqlite. Resolves #10001 for Sqlite Resolves #17292
- Loading branch information
Showing
10 changed files
with
273 additions
and
107 deletions.
There are no files selected for viewing
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
96 changes: 96 additions & 0 deletions
96
src/EFCore.Relational/Query/SqlExpressions/RowNumberExpression.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,96 @@ | ||
// 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.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions | ||
{ | ||
public class RowNumberExpression : SqlExpression | ||
{ | ||
public RowNumberExpression(IReadOnlyList<SqlExpression> partitions, IReadOnlyList<OrderingExpression> orderings, RelationalTypeMapping typeMapping) | ||
: base(typeof(long), typeMapping) | ||
{ | ||
Check.NotEmpty(orderings, nameof(orderings)); | ||
|
||
Partitions = partitions; | ||
Orderings = orderings; | ||
} | ||
|
||
public virtual IReadOnlyList<SqlExpression> Partitions { get; } | ||
public virtual IReadOnlyList<OrderingExpression> Orderings { get; } | ||
|
||
protected override Expression VisitChildren(ExpressionVisitor visitor) | ||
{ | ||
var changed = false; | ||
var partitions = new List<SqlExpression>(); | ||
foreach (var partition in Partitions) | ||
{ | ||
var newPartition = (SqlExpression)visitor.Visit(partition); | ||
changed |= newPartition != partition; | ||
partitions.Add(newPartition); | ||
} | ||
|
||
var orderings = new List<OrderingExpression>(); | ||
foreach (var ordering in Orderings) | ||
{ | ||
var newOrdering = (OrderingExpression)visitor.Visit(ordering); | ||
changed |= newOrdering != ordering; | ||
orderings.Add(newOrdering); | ||
} | ||
|
||
return changed | ||
? new RowNumberExpression(partitions, orderings, TypeMapping) | ||
: this; | ||
} | ||
|
||
public virtual RowNumberExpression Update(IReadOnlyList<SqlExpression> partitions, IReadOnlyList<OrderingExpression> orderings) | ||
{ | ||
return (Partitions == null ? partitions == null : Partitions.SequenceEqual(partitions)) | ||
&& Orderings.SequenceEqual(orderings) | ||
? this | ||
: new RowNumberExpression(partitions, orderings, TypeMapping); | ||
} | ||
|
||
public override void Print(ExpressionPrinter expressionPrinter) | ||
{ | ||
expressionPrinter.Append("ROW_NUMBER() OVER(PARTITION BY "); | ||
expressionPrinter.VisitList(Partitions); | ||
expressionPrinter.Append(" ORDER BY "); | ||
expressionPrinter.VisitList(Orderings); | ||
expressionPrinter.Append(")"); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
=> obj != null | ||
&& (ReferenceEquals(this, obj) | ||
|| obj is RowNumberExpression rowNumberExpression | ||
&& Equals(rowNumberExpression)); | ||
|
||
private bool Equals(RowNumberExpression rowNumberExpression) | ||
=> base.Equals(rowNumberExpression) | ||
&& (Partitions == null ? rowNumberExpression.Partitions == null : Partitions.SequenceEqual(rowNumberExpression.Partitions)) | ||
&& Orderings.SequenceEqual(rowNumberExpression.Orderings); | ||
|
||
public override int GetHashCode() | ||
{ | ||
var hash = new HashCode(); | ||
hash.Add(base.GetHashCode()); | ||
foreach (var partition in Partitions) | ||
{ | ||
hash.Add(partition); | ||
} | ||
|
||
foreach (var ordering in Orderings) | ||
{ | ||
hash.Add(ordering); | ||
} | ||
|
||
return hash.ToHashCode(); | ||
} | ||
} | ||
} |
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.