-
Notifications
You must be signed in to change notification settings - Fork 934
Reduce cast usage for COUNT aggregate and add support for Mssql count_big #2061
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
Conversation
Rebased and renamed to nhibernate-core/src/NHibernate.Test/QueryTest/CountFixture.cs Lines 23 to 24 in d4206e2
because count effective return type in Sql Server is an int . So the idea is that ISQLFunction.ReturnType should return the same type no matter what database is used, which is basically what it does today, except for some functions which IMO should be corrected:
The intention of |
@@ -286,7 +286,8 @@ protected virtual void RegisterKeywords() | |||
|
|||
protected virtual void RegisterFunctions() | |||
{ | |||
RegisterFunction("count", new CountBigQueryFunction()); | |||
RegisterFunction("count", new CountQueryFunction()); | |||
RegisterFunction("count_big", new CountBigQueryFunction()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this still causing the breaking change you were writing about?
For example the following test would fail in Sql Server:
nhibernate-core/src/NHibernate.Test/QueryTest/CountFixture.cs
Lines 23 to 24 in d4206e2
object count = s.CreateQuery("select count(*) from Simple").UniqueResult(); Assert.IsTrue(count is Int64);
becausecount
effective return type in Sql Server is anint
.
This changes the registration of HQL count
for SQL-Server to map to SQL-Server count
instead of count_big
, which returns an int32 and fails if the count is bigger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this still causing the breaking change you were writing about?
For HQL nothing is changed, the new GetEffectiveReturnType
method is used only for Linq queries. CountQueryFunction.ReturnType
still returns Int64
, which is what HQL uses to determine the type, so hql queries using count
will always be casted to Int64
.
@@ -150,7 +150,7 @@ selectExpr | |||
; | |||
|
|||
count | |||
: ^(COUNT { Out("count("); } ( distinctOrAll ) ? countExpr { Out(")"); } ) | |||
: ^(c=COUNT { OutAggregateFunctionName(c); Out("("); } ( distinctOrAll ) ? countExpr { Out(")"); } ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this invalidate my previous comment? I mean, was this causing the SQL-Server HQL count
to count_big
mapping to be ignored by this piece of code, causing NHibernate to anyway use SQL-Server int32 count
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, was this causing the SQL-Server HQL count to count_big mapping to be ignored by this piece of code, causing NHibernate to anyway use SQL-Server int32 count?
Correct, count
was hardcoded, which was the reason why changing from count
to count_big
in CountBigQueryFunction
didn't worked.
…_big (nhibernate#2061) Co-authored-by: Alexander Zaytsev <[email protected]>
/// <summary> | ||
/// The function name or <see langword="null"/> when multiple functions/operators/statements are used. | ||
/// </summary> | ||
string FunctionName { get; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be renamed to Name
to reduce need of obsoleting things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not, renamed in #2359.
@@ -19,6 +20,19 @@ public AggregateNode(IToken token) | |||
{ | |||
} | |||
|
|||
public string FunctionName |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be an overgeneralization and should be refactored into a node-specific code unless you see that it can be extended for functions other than count
and count_big
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be extended for string concatenation, which is supported by some databases (Sql server, MySql, Postgresql and Oracle), where different function names are used (LISTAGG
, GROUP_CONCAT
, STRING_AGG
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maca88 can you provide some sort of prototype of string_agg? I'm dying to use this with linq provider
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To my knowledge it is not possible to implement it without modifying NHibernate source code. It is on my todo list for the next release, so unfortunately you will have to wait.
This is a continuation of #2036, which extends the cast reduce usage for count aggregate function. In addition, this PR introduces
count_big
sql function that is used when usingLongCount
andMsSql2000Dialect
overrides it to use the specific Sql Servercount_big
function (by defaultcount
is used).