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

Fix Linq .Second bug for PostgreSQL et al. #3528

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gliljas
Copy link
Member

@gliljas gliljas commented Apr 19, 2024

Fix for #3525

The actual fix was adding a new HQL function "secondtruncated", which is used if it is available, otherwise it falls back to the old "second". Another option would be to fix the "second" function in the dialects where it doesn't work, but that could be considered a more breaking change. As mentioned in the issue, Hibernate has defined "second" to actually include fractional seconds, and have adjusted the dialects where the corresponding SQL function truncates.

The most changes were in DateTimeTests.cs. I really wanted to use that test class since it already tests exactly this (but not seconds), but to get fractional seconds into the Northwind dataset was a bit icky, so I opted to create specific test data.

I used the approach of comparing the result from the LINQ query with the result from the same query run in memory. Of course that doesn't work in scenarios with null, case sensitivity etc., but here it works just fine.

The MySQL test fails on Github, for some reason. I can't make it fail locally.

src/NHibernate.Test/Linq/DateTimeTests.cs Show resolved Hide resolved
@@ -33,8 +34,13 @@ public DateTimePropertiesHqlGenerator()

public override HqlTreeNode BuildHql(MemberInfo member, Expression expression, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.MethodCall(member.Name.ToLowerInvariant(),
var functionName = member.Name.ToLowerInvariant();
if (functionName == "second" && (visitor.SessionFactory as ISessionFactoryImplementor)?.Dialect.Functions.ContainsKey("secondtruncated") == true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead of checking if the function is supported define secondtruncated in the base dialect?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to, but the only meaningful way to do that, I think, would be to create a new kind of ISQLFunction, which aliases another function.

RegisterFunction("secondtruncated ", new SQLFunctionAlias(this, "second"));

@hazzik
Copy link
Member

hazzik commented May 30, 2024

Mysql seems to be failing because it needs to use floor instead of cast. I had this issue with my local postgres.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants