Decimal value with more than 5 decimal places gets truncated when doing a conditional select with linq

Description

My table has two decimal values with precision 15 and scale 9.

In my query i want to select which value to select based on a boolean (UsePreviousRate).

If PreviousRate is 12345.123456789 i expect the property Rate in RateDto to be the same but the value is rounded to 5 decimals (12345.12346)

Generated SQL looks like

The same query with queryover and conditional projection returns expected value

Generated sql

Environment

MSSQL

Attachments

1

Activity

Frédéric Delaporte November 27, 2017 at 1:54 PM

Issue moved here.

Frédéric Delaporte October 2, 2017 at 5:25 PM
Edited

Still failing with a variation:

Yields:

And that is a double bug:

  • An optimization for SQL Server query plan cache set decimal parameters precision and scale to their "max" values in SqlClientDriver if they do not specify those themselves. And max scale is only 5. Any way, max scale is non-sens, since it is indeed equal to precision, in which case their are no more digits before the dot. It is more a default scale. And such a default scale is quite a nasty thing. It causes the value to be truncated (and not rounded) to 5 digits after the dot, when the query parsing was not able to infer its precision/scale from compared entity property (due to the expression). This issue has been isolated in NH-4087.

  • An undue cast to the NHibernate default decimal (decimal(19, 5)) is done on the expression, causing a round. So this undue cast may compensate the first bug when truncation and rounding are equivalent... But fixing the first bug will leave us with the second anyway.

Tests currently pushed here, I may PR that later.

Frédéric Delaporte October 2, 2017 at 4:36 PM

Binaries removed from test case zip.

Erik jansson October 2, 2017 at 8:34 AM
Edited

This bug has been fixed in NH 4.1.1.4000.

Generated SQL

Fixed

Details

Assignee

Reporter

Labels

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created May 6, 2015 at 10:10 AM
Updated December 7, 2017 at 10:59 AM
Resolved December 7, 2017 at 10:58 AM
Who's Looking?