Skip to content

Commit

Permalink
Use spools for subqueries in join conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed May 29, 2024
1 parent 9d91698 commit 13a5e3a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7749,7 +7749,8 @@ from account
var merge = AssertNode<MergeJoinNode>(loop1.RightSource);
Assert.AreEqual(1, merge.DefinedValues.Count);
Assert.AreEqual("Expr2.new_name", merge.DefinedValues["Expr3"]);
var fetch2 = AssertNode<FetchXmlScan>(merge.LeftSource);
var tableSpool = AssertNode<TableSpoolNode>(merge.LeftSource);
var fetch2 = AssertNode<FetchXmlScan>(tableSpool.Source);
AssertFetchXml(fetch2, @"
<fetch>
<entity name='contact'>
Expand Down
6 changes: 5 additions & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlan/SortNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private IDataExecutionPlanNodeInternal FoldSorts(NodeCompilationContext context)
}

// Allow folding sorts around filters and Compute Scalar (so long as sort is not on a calculated field)
// Can fold to the outer input of a nested loop join
// Can fold to the outer input of a nested loop join and sources of spools
var source = Source;
var fetchXml = Source as FetchXmlScan;

Expand All @@ -253,6 +253,10 @@ private IDataExecutionPlanNodeInternal FoldSorts(NodeCompilationContext context)
source = computeScalar.Source;
else if (source is NestedLoopNode nestedLoop)
source = nestedLoop.LeftSource;
else if (source is TableSpoolNode tableSpool)
source = tableSpool.Source;
else if (source is IndexSpoolNode indexSpool)
source = indexSpool.Source;
else
break;

Expand Down
2 changes: 1 addition & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4523,7 +4523,7 @@ private IDataExecutionPlanNodeInternal ConvertTableReference(TableReference refe
joinNode = new NestedLoopNode
{
LeftSource = foldable.LeftSource,
RightSource = foldable.RightSource,
RightSource = new TableSpoolNode { Source = foldable.RightSource },
JoinType = foldable.JoinType
};

Expand Down

0 comments on commit 13a5e3a

Please sign in to comment.