You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NumericHandler Ignores Scale in Returned Value #1921
From @roji in a previous discussion in #1915 about whether or not numeric(1000,2) columns should pad returned decimals to 2 places:
I'm not sure I agree that the scale defined on the numeric(precision, scale) datatype should have any impact on how many zeros are tacked on to the decimal value read. The way I understand it, the scale in the datatype definition defines the maximum scale saved in the database, much like the length in varchar(x) defines the maximum length. It doesn't mean that all strings have to be of length X. In other words, I think the value returned should be 1.2 and not 1.20
At first I agreed with this until I experimented with the output of several commands in psql
SELECT '1.2'::numeric(10,2); produces 1.20 while Npgsql produces 1.2 SELECT '1.235'::numeric(10,2); produces 1.24 while Npgsql produces -1
I think that to fix this would require fixing #1917.
@StillLearnin thanks for the added info and comments - we should have looked a bit further...
Obviously producing -1 in any case is a clear bug which has to be fixed.
However, I'm still not 100% convinced about the value of producing 1.20. It's true that we should try to align to standard PostgreSQL behavior whenever it makes sense (psql in this case), but I have a hard to see the value of 1.20 over 1.2 for C# developers. Since there's no actual difference in value here, it's only a question of user display, and it could be argued that the configured database scale shouldn't be carried over into the C# numeric being read.
I'm honestly a bit on the fence on this one, are there any other opinions or arguments out there (@YohDeadfall or others)?
Regarding the dependency on #1917, see #1917 (comment). If we do decide to produce 1.20 rather than 1.2, then #1919 (I have a fix in progress) will provide logic for parsing numeric's type modifier, which NumericHandler will be able to use in order to get the precision and scale.
I have a hard to see the value of 1.20 over 1.2 for C# developers.
I often use casting in a sql query to determine how many decimal places the column will have in a DataGridView. Because of this bug, a workaround is needed. I can apply a custom formatter to the DataGridViewColumn or convert the value to text in the query (and convert it again to use the value, if it's needed).
In most of the programming projects I'm involved in, the developer is also the DBA. In these cases, the developer sets the scale because that is the way he/she wants it to be everywhere. It's extra work to have converters or display formats everywhere (including reports). This is especially disconcerting when you were used to using EF with Microsoft SQL Server and there it "just works".
On the other hand, there are things that Postgresql does better than Microsoft SQL Server and how much less does a license for Postgresql and a copy of Npgsql cost than a license for MsSQL? So I'm not "really" complaining. Just would be glad to "improve" this connector if we can agree on what the "improvement" should be.
Thanks for your inputs... @StillLearnin of course the idea is to make Npgsql as usable as possible, and as you say to agree on the best behavior.
As people here seem pretty clear on the desired behavior being 1.20, and since psql behaves in this way, I guess that's the way to go (@YohDeadfall any opinion here?). @StillLearnin, as you've submitted #1915 are you interested in making this fix as well?
To implement, all you need to do is look at the FieldDescription parameter of the Read method, and if it's null, extract the TypeModifier. This is a simple int that can be processed as follows:
Thank for all the work on this. Keep in mind that we don't have to provide 100% identical behavior to psql on this, as it's mostly a display issue - if you end up doing some very convoluted or which hurts perf, it's better to drop that approach. There's definitely value in mimicking psql and always provided the defined number of digits, but not at all cost...
Activity
roji commentedon May 9, 2018
@StillLearnin thanks for the added info and comments - we should have looked a bit further...
Obviously producing -1 in any case is a clear bug which has to be fixed.
However, I'm still not 100% convinced about the value of producing
1.20. It's true that we should try to align to standard PostgreSQL behavior whenever it makes sense (psql in this case), but I have a hard to see the value of 1.20 over 1.2 for C# developers. Since there's no actual difference in value here, it's only a question of user display, and it could be argued that the configured database scale shouldn't be carried over into the C# numeric being read.I'm honestly a bit on the fence on this one, are there any other opinions or arguments out there (@YohDeadfall or others)?
Regarding the dependency on #1917, see #1917 (comment). If we do decide to produce
1.20rather than1.2, then #1919 (I have a fix in progress) will provide logic for parsing numeric's type modifier, which NumericHandler will be able to use in order to get the precision and scale.Logerfo commentedon May 9, 2018
@roji
I often use casting in a sql query to determine how many decimal places the column will have in a DataGridView. Because of this bug, a workaround is needed. I can apply a custom formatter to the DataGridViewColumn or convert the value to text in the query (and convert it again to use the value, if it's needed).
StillLearnin commentedon May 9, 2018
In most of the programming projects I'm involved in, the developer is also the DBA. In these cases, the developer sets the scale because that is the way he/she wants it to be everywhere. It's extra work to have converters or display formats everywhere (including reports). This is especially disconcerting when you were used to using EF with Microsoft SQL Server and there it "just works".
On the other hand, there are things that Postgresql does better than Microsoft SQL Server and how much less does a license for Postgresql and a copy of Npgsql cost than a license for MsSQL? So I'm not "really" complaining. Just would be glad to "improve" this connector if we can agree on what the "improvement" should be.
roji commentedon May 9, 2018
Thanks for your inputs... @StillLearnin of course the idea is to make Npgsql as usable as possible, and as you say to agree on the best behavior.
As people here seem pretty clear on the desired behavior being 1.20, and since psql behaves in this way, I guess that's the way to go (@YohDeadfall any opinion here?). @StillLearnin, as you've submitted #1915 are you interested in making this fix as well?
To implement, all you need to do is look at the FieldDescription parameter of the Read method, and if it's null, extract the TypeModifier. This is a simple int that can be processed as follows:
You can then apply the scale.
StillLearnin commentedon May 9, 2018
@roji sure, I'll work on it (including tests).
YohDeadfall commentedon May 9, 2018
It makes sense for data visualization as @StillLearnin already said, @roji, so I would like to see a PR (:
StillLearnin commentedon May 10, 2018
I don't know why, but after bringing my fork up to date with this repository, I couldn't reproduce the issues listed above.☹️
So I just added tests to make sure they don't crop up again.
SELECT '1.2345'::numeric(10,3)produces1.235SELECT '1.2344'::numeric(10,3)produces1.234SELECT '1.2'::numeric(10,3)produces1.200StillLearnin commentedon May 10, 2018
Found a case that doesn't match though.
SELECT '1'::numeric(10,2)psql returns1.00but Npgsql returns1So there is more work to do.
StillLearnin commentedon May 10, 2018
Hmm... and .net uses banker's rounding by default but psql doesn't
SELECT '1.5'::numeric(10,0);SELECT '2.5'::numeric(10,0);roji commentedon May 11, 2018
Thank for all the work on this. Keep in mind that we don't have to provide 100% identical behavior to psql on this, as it's mostly a display issue - if you end up doing some very convoluted or which hurts perf, it's better to drop that approach. There's definitely value in mimicking psql and always provided the defined number of digits, but not at all cost...
Will wait until you submit a PR to review.
StillLearnin commentedon May 15, 2018
Fixed by #1925