Skip to content

Commit

Permalink
Fixed column renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Sep 27, 2023
1 parent 165577d commit 203ac33
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ private void ConvertStatement(TSqlStatement statement, ExecutionPlanOptimizer op

if (cteValidator.RecursiveQueries.Count > 0)
{
anchorQuery = (AliasNode)anchorQuery.Clone();
var ctePlan = anchorQuery.Source;
var anchorSchema = anchorQuery.GetSchema(_nodeContext);

// Add a ComputeScalar node to add the initial recursion depth (0)
var recursionDepthField = _nodeContext.GetExpressionName();
Expand Down Expand Up @@ -241,6 +243,8 @@ private void ConvertStatement(TSqlStatement statement, ExecutionPlanOptimizer op
};

recurseConcat.ColumnSet.Add(concatCol);

col.SourceColumn = col.OutputColumn;
}

recurseConcat.ColumnSet.Add(new ConcatenateColumn
Expand All @@ -259,7 +263,8 @@ private void ConvertStatement(TSqlStatement statement, ExecutionPlanOptimizer op
// Pull the same records into the recursive loop
var recurseTableSpool = new TableSpoolNode
{
Producer = recurseIndexStack
Producer = recurseIndexStack,
SpoolType = SpoolType.Lazy
};

// Increment the depth
Expand Down Expand Up @@ -287,8 +292,6 @@ private void ConvertStatement(TSqlStatement statement, ExecutionPlanOptimizer op
};

// Capture all CTE fields in the outer references
var anchorSchema = anchorQuery.GetSchema(_nodeContext);

foreach (var col in anchorSchema.Schema)
recurseLoop.OuterReferences[col.Key.SplitMultiPartIdentifier().Last()] = "@" + _nodeContext.GetExpressionName();

Expand Down Expand Up @@ -373,6 +376,7 @@ private void ConvertStatement(TSqlStatement statement, ExecutionPlanOptimizer op
recurseConcat.ColumnSet.Last().SourceColumns.Add(incrementedDepthField);

anchorQuery.Source = recurseIndexStack;
_cteSubplans[cte.ExpressionName.Value] = anchorQuery;
}
}
}
Expand Down

0 comments on commit 203ac33

Please sign in to comment.