Skip to content

Keep guessed parameter type in hql #2875

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

Merged
merged 1 commit into from
Aug 7, 2021
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
10 changes: 10 additions & 0 deletions src/NHibernate.Test/Async/Linq/LinqQuerySamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,5 +1385,15 @@ public void ReplaceFunctionWithNullArgumentAsync()
}, Throws.Nothing, "Expected REPLACE(FirstName, LastName, NULL) to be supported");
Assert.That(results, Is.Not.Null);
}

[Test(Description = "GH-2860")]
public async Task StringFormatWithTrimAsync()
{
var q =
from e in db.Employees
select new {Name = $"{e.FirstName} {e.LastName}".Trim(), Phone = e.Address.PhoneNumber};
var items = await (q.ToListAsync());
Assert.AreEqual(9, items.Count);
}
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/Linq/LinqQuerySamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,16 @@ public void ReplaceFunctionWithNullArgument()
}, Throws.Nothing, "Expected REPLACE(FirstName, LastName, NULL) to be supported");
Assert.That(results, Is.Not.Null);
}

[Test(Description = "GH-2860")]
public void StringFormatWithTrim()
{
var q =
from e in db.Employees
select new {Name = $"{e.FirstName} {e.LastName}".Trim(), Phone = e.Address.PhoneNumber};
var items = q.ToList();
Assert.AreEqual(9, items.Count);
}
}

public class ParentChildBatch<T, TKey, TSub>
Expand Down
5 changes: 4 additions & 1 deletion src/NHibernate/Hql/Ast/ANTLR/HqlSqlWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,10 @@ IASTNode GenerateNamedParameter(IASTNode delimiterNode, IASTNode nameNode)
{
// Add the parameter type information so that we are able to calculate functions return types
// when the parameter is used as an argument.
parameter.ExpectedType = namedParameter.Type;
if (namedParameter.IsGuessedType)
parameter.GuessedType = namedParameter.Type;
else
parameter.ExpectedType = namedParameter.Type;
}

_parameters.Add(paramSpec);
Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/ParameterNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public IType ExpectedType
}
}

internal IType GuessedType { get; set; }

public override SqlString RenderText(ISessionFactoryImplementor sessionFactory)
{
int count;
Expand Down
9 changes: 7 additions & 2 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/SelectClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,14 @@ public void InitializeExplicitSelectClause(FromClause fromClause)
else
{
IType type = expr.DataType;
if (type == null && !(expr is ParameterNode))
if (type == null)
{
throw new QueryException("No data type for node: " + expr.GetType().Name + " " + new ASTPrinter().ShowAsString((IASTNode)expr, ""));
if (expr is ParameterNode param)
{
type = param.GuessedType;
}
else
throw new QueryException("No data type for node: " + expr.GetType().Name + " " + new ASTPrinter().ShowAsString((IASTNode)expr, ""));
}
//sqlResultTypeList.add( type );

Expand Down
9 changes: 6 additions & 3 deletions src/NHibernate/Linq/Visitors/ParameterTypeLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ internal static void SetParameterTypes(
continue;
}

namedParameter.Type = GetParameterType(sessionFactory, constantExpressions, visitor, namedParameter);
namedParameter.Type = GetParameterType(sessionFactory, constantExpressions, visitor, namedParameter, out var tryProcessInHql);
namedParameter.IsGuessedType = tryProcessInHql;
}
}

Expand Down Expand Up @@ -145,8 +146,10 @@ private static IType GetParameterType(
ISessionFactoryImplementor sessionFactory,
HashSet<ConstantExpression> constantExpressions,
ConstantTypeLocatorVisitor visitor,
NamedParameter namedParameter)
NamedParameter namedParameter,
out bool tryProcessInHql)
{
tryProcessInHql = false;
// All constant expressions have the same type/value
var constantExpression = constantExpressions.First();
var constantType = constantExpression.Type.UnwrapIfNullable();
Expand All @@ -158,7 +161,7 @@ private static IType GetParameterType(

if (visitor.NotGuessableConstants.Contains(constantExpression) && constantExpression.Value != null)
{
return null;
tryProcessInHql = true;
}

// No related MemberExpressions was found, guess the type by value or its type when null.
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/Param/NamedParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal NamedParameter(string name, object value, IType type, bool isCollection
public string Name { get; private set; }
public object Value { get; internal set; }
public IType Type { get; internal set; }
internal bool IsGuessedType { get; set; }

public virtual bool IsCollection { get; }

Expand Down