Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make previously HQL:ed expressions automatic candidates in SELECT #3638

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/NHibernate/Linq/Visitors/QueryModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public static ExpressionToHqlTranslationResults GenerateHqlQuery(QueryModel quer
}

private readonly IntermediateHqlTree _hqlTree;
private readonly HashSet<Expression> _hqlCandidates;
private readonly NhLinqExpressionReturnType? _rootReturnType;
private static readonly ResultOperatorMap ResultOperatorMap;
private bool _serverSide = true;
Expand Down Expand Up @@ -165,6 +166,7 @@ private QueryModelVisitor(VisitorParameters visitorParameters, bool root, QueryM
_root = root;
_rootReturnType = root ? rootReturnType : null;
_hqlTree = new IntermediateHqlTree(root, _queryMode);
_hqlCandidates = new HashSet<Expression>();
}

private void Visit()
Expand Down Expand Up @@ -476,7 +478,7 @@ public override void VisitSelectClause(SelectClause selectClause, QueryModel que

private HqlSelect GetSelectClause(Expression selectClause)
{
var visitor = new SelectClauseVisitor(typeof(object[]), VisitorParameters);
var visitor = new SelectClauseVisitor(typeof(object[]), VisitorParameters, _hqlCandidates);

visitor.VisitSelector(selectClause, !_root);

Expand Down Expand Up @@ -537,7 +539,7 @@ private void VisitDeleteClause(Expression expression)
return;

// We only need to check there is no unexpected select, for avoiding silently ignoring them.
var visitor = new SelectClauseVisitor(typeof(object[]), VisitorParameters);
var visitor = new SelectClauseVisitor(typeof(object[]), VisitorParameters, new HashSet<Expression>());
visitor.VisitSelector(expression);

if (visitor.ProjectionExpression != null)
Expand Down Expand Up @@ -566,6 +568,7 @@ public override void VisitOrderByClause(OrderByClause orderByClause, QueryModel
: (HqlDirectionStatement)_hqlTree.TreeBuilder.Descending();

_hqlTree.AddOrderByClause(orderBy, direction);
_hqlCandidates.Add(clause.Expression);
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/NHibernate/Linq/Visitors/SelectClauseVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ public class SelectClauseVisitor : RelinqExpressionVisitor
private HashSet<Expression> _hqlNodes;
private readonly ParameterExpression _inputParameter;
private readonly VisitorParameters _parameters;
private readonly HashSet<Expression> _hqlCandidates;
private int _iColumn;
private List<HqlExpression> _hqlTreeNodes = new List<HqlExpression>();
private readonly HqlGeneratorExpressionVisitor _hqlVisitor;

public SelectClauseVisitor(System.Type inputType, VisitorParameters parameters)
// Since v5.6
[Obsolete("Use overload providing expressions to be executed with HQL.")]
public SelectClauseVisitor(System.Type inputType, VisitorParameters parameters) :
this(inputType, parameters, new HashSet<Expression>())
{ }

public SelectClauseVisitor(System.Type inputType, VisitorParameters parameters, HashSet<Expression> hqlCandidates)
{
_inputParameter = Expression.Parameter(inputType, "input");
_parameters = parameters;
_hqlCandidates = hqlCandidates;
_hqlVisitor = new HqlGeneratorExpressionVisitor(_parameters);
}
gliljas marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -69,9 +77,9 @@ public void VisitSelector(Expression expression, bool isSubQuery)

if (distinct != null)
{
var treeNodes = new List<HqlTreeNode>(_hqlTreeNodes.Count + 1) {_hqlTreeBuilder.Distinct()};
var treeNodes = new List<HqlTreeNode>(_hqlTreeNodes.Count + 1) { _hqlTreeBuilder.Distinct() };
treeNodes.AddRange(_hqlTreeNodes);
_hqlTreeNodes = new List<HqlExpression>(1) {_hqlTreeBuilder.ExpressionSubTreeHolder(treeNodes)};
_hqlTreeNodes = new List<HqlExpression>(1) { _hqlTreeBuilder.ExpressionSubTreeHolder(treeNodes) };
}
}

Expand All @@ -82,7 +90,7 @@ public override Expression Visit(Expression expression)
return null;
}

if (_hqlNodes.Contains(expression))
if (_hqlNodes.Contains(expression) || _hqlCandidates.Contains(expression))
{
// Pure HQL evaluation
_hqlTreeNodes.Add(_hqlVisitor.Visit(expression).AsExpression());
Expand Down
Loading