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

LINQ subqueries wrongly altered by SelectClauseVisitor #3271

Merged
merged 5 commits into from
Mar 31, 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
11 changes: 11 additions & 0 deletions src/NHibernate.Test/Async/Linq/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,17 @@ where sheet.Users.Contains(user)
Assert.That(query.Count, Is.EqualTo(2));
}

[Test]
public async Task TimesheetsWithEnumerableContainsOnSelectAsync()
{
var value = (EnumStoredAsInt32) 1000;
var query = await ((from sheet in db.Timesheets
where sheet.Users.Select(x => x.NullableEnum2 ?? value).Contains(value)
select sheet).ToListAsync());

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

[Test]
public async Task SearchOnObjectTypeWithExtensionMethodAsync()
{
Expand Down
11 changes: 11 additions & 0 deletions src/NHibernate.Test/Linq/WhereTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,17 @@ where sheet.Users.Contains(user)
Assert.That(query.Count, Is.EqualTo(2));
}

[Test]
public void TimesheetsWithEnumerableContainsOnSelect()
{
var value = (EnumStoredAsInt32) 1000;
var query = (from sheet in db.Timesheets
where sheet.Users.Select(x => x.NullableEnum2 ?? value).Contains(value)
select sheet).ToList();

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

[Test]
public void SearchOnObjectTypeWithExtensionMethod()
{
Expand Down
21 changes: 17 additions & 4 deletions src/NHibernate/Linq/Visitors/QueryModelVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public static ExpressionToHqlTranslationResults GenerateHqlQuery(QueryModel quer
private readonly NhLinqExpressionReturnType? _rootReturnType;
private static readonly ResultOperatorMap ResultOperatorMap;
private bool _serverSide = true;
private readonly bool _root;

public VisitorParameters VisitorParameters { get; }

Expand Down Expand Up @@ -161,6 +162,7 @@ private QueryModelVisitor(VisitorParameters visitorParameters, bool root, QueryM
_queryMode = root ? visitorParameters.RootQueryMode : QueryMode.Select;
VisitorParameters = visitorParameters;
Model = queryModel;
_root = root;
_rootReturnType = root ? rootReturnType : null;
_hqlTree = new IntermediateHqlTree(root, _queryMode);
}
Expand Down Expand Up @@ -467,19 +469,27 @@ public override void VisitSelectClause(SelectClause selectClause, QueryModel que
}

//This is a standard select query
_hqlTree.AddSelectClause(GetSelectClause(selectClause.Selector));

base.VisitSelectClause(selectClause, queryModel);
}

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.Selector);
visitor.VisitSelector(selectClause);

if (visitor.ProjectionExpression != null)
{
_hqlTree.AddItemTransformer(visitor.ProjectionExpression);
}

_hqlTree.AddSelectClause(_hqlTree.TreeBuilder.Select(visitor.GetHqlNodes()));

base.VisitSelectClause(selectClause, queryModel);
return _hqlTree.TreeBuilder.Select(visitor.GetHqlNodes());
}

private void VisitInsertClause(Expression expression)
Expand Down Expand Up @@ -527,6 +537,9 @@ private void VisitUpdateClause(Expression expression)

private void VisitDeleteClause(Expression expression)
{
if (!_root)
return;

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