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

Reenable use of SelectClauseVisitor for subqueries #3414

Merged
merged 4 commits into from
Aug 22, 2023
Merged
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
13 changes: 13 additions & 0 deletions src/NHibernate.Test/Async/Linq/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,19 @@ where sheet.Users.Select(x => x.NullableEnum2 ?? value).Contains(value)
Assert.That(query.Count, Is.EqualTo(1));
}

[Test]
public async Task TimesheetsWithProjectionInSubqueryAsync()
{
if (Dialect is MsSqlCeDialect)
Assert.Ignore("Dialect is not supported");

var query = await ((from sheet in db.Timesheets
where sheet.Users.Select(x => new { Id = x.Id, Name = x.Name }).Any(x => x.Id == 1)
select sheet).ToListAsync());

Assert.That(query.Count, Is.EqualTo(2));
}

[Test]
public async Task ContainsSubqueryWithCoalesceStringEnumSelectAsync()
{
Expand Down
13 changes: 13 additions & 0 deletions src/NHibernate.Test/Linq/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,19 @@ where sheet.Users.Select(x => x.NullableEnum2 ?? value).Contains(value)
Assert.That(query.Count, Is.EqualTo(1));
}

[Test]
public void TimesheetsWithProjectionInSubquery()
{
if (Dialect is MsSqlCeDialect)
Assert.Ignore("Dialect is not supported");

var query = (from sheet in db.Timesheets
where sheet.Users.Select(x => new { Id = x.Id, Name = x.Name }).Any(x => x.Id == 1)
select sheet).ToList();

Assert.That(query.Count, Is.EqualTo(2));
}

[Test]
public void ContainsSubqueryWithCoalesceStringEnumSelect()
{
Expand Down
6 changes: 1 addition & 5 deletions src/NHibernate/Linq/Visitors/QueryModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,9 @@ public override void VisitSelectClause(SelectClause selectClause, QueryModel que

private HqlSelect GetSelectClause(Expression selectClause)
{
if (!_root)
return _hqlTree.TreeBuilder.Select(
HqlGeneratorExpressionVisitor.Visit(selectClause, VisitorParameters).AsExpression());

var visitor = new SelectClauseVisitor(typeof(object[]), VisitorParameters);

visitor.VisitSelector(selectClause);
visitor.VisitSelector(selectClause, !_root);

if (visitor.ProjectionExpression != null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Linq/Visitors/SelectClauseNominator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public SelectClauseHqlNominator(VisitorParameters parameters)
_parameters = parameters;
}

internal Expression Nominate(Expression expression)
internal Expression Nominate(Expression expression, bool isSubQuery = false)
{
HqlCandidates = new HashSet<Expression>();
ContainsUntranslatedMethodCalls = false;
_canBeCandidate = true;
_stateStack = new Stack<bool>();
_stateStack.Push(false);
_stateStack.Push(isSubQuery);

return Visit(expression);
}
Expand Down
6 changes: 4 additions & 2 deletions src/NHibernate/Linq/Visitors/SelectClauseVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public IEnumerable<HqlExpression> GetHqlNodes()
return _hqlTreeNodes;
}

public void VisitSelector(Expression expression)
public void VisitSelector(Expression expression) => VisitSelector(expression, false);
gliljas marked this conversation as resolved.
Show resolved Hide resolved
fredericDelaporte marked this conversation as resolved.
Show resolved Hide resolved

public void VisitSelector(Expression expression, bool isSubQuery)
{
var distinct = expression as NhDistinctExpression;
if (distinct != null)
Expand All @@ -44,7 +46,7 @@ public void VisitSelector(Expression expression)

// Find the sub trees that can be expressed purely in HQL
var nominator = new SelectClauseHqlNominator(_parameters);
expression = nominator.Nominate(expression);
expression = nominator.Nominate(expression, isSubQuery);
_hqlNodes = nominator.HqlCandidates;

// Linq2SQL ignores calls to local methods. Linq2EF seems to not support
Expand Down
Loading