Skip to content

Commit e56b626

Browse files
Needs to duplicate the tree, because query plan compilation alters it.
1 parent a406639 commit e56b626

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/NHibernate/Linq/NhLinqExpression.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public IASTNode Translate(ISessionFactoryImplementor sessionFactory, bool filter
7373
// Query has already been translated. Arguments do not really matter, because queries are anyway tied
7474
// to a single session factory and cannot switch from being a filter query (query on a mapped collection)
7575
// or not.
76-
return ExpressionToHqlTranslationResults.Statement.AstNode;
76+
return DuplicateTree(ExpressionToHqlTranslationResults.Statement.AstNode);
7777
}
7878

7979
var requiredHqlParameters = new List<NamedParameterDescriptor>();
@@ -96,7 +96,8 @@ public IASTNode Translate(ISessionFactoryImplementor sessionFactory, bool filter
9696
.Except(requiredHqlParameters.Select(p => p.Name))
9797
.Any();
9898

99-
return ExpressionToHqlTranslationResults.Statement.AstNode;
99+
// The ast node may be altered by caller, duplicate it for preserving the original one.
100+
return DuplicateTree(ExpressionToHqlTranslationResults.Statement.AstNode);
100101
}
101102

102103
internal void CopyExpressionTranslation(NhLinqExpression other)
@@ -106,5 +107,15 @@ internal void CopyExpressionTranslation(NhLinqExpression other)
106107
// Type could have been overridden by translation.
107108
Type = other.Type;
108109
}
110+
111+
private static IASTNode DuplicateTree(IASTNode ast)
112+
{
113+
var thisNode = ast.DupNode();
114+
foreach (var child in ast)
115+
{
116+
thisNode.AddChild(DuplicateTree(child));
117+
}
118+
return thisNode;
119+
}
109120
}
110121
}

0 commit comments

Comments
 (0)