nhibernate / nhibernate-core

NHibernate Object Relational Mapper
https://nhibernate.info
GNU Lesser General Public License v2.1
2.11k stars 921 forks source link

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

Open gliljas opened 2 months ago

gliljas commented 2 months ago

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.

hazzik commented 1 month ago

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