Skip to content

NumericHandler Ignores Scale in Returned Value #1921

Closed

Description

@StillLearnin

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.

Activity

  1. roji commented on May 9, 2018

    @roji
    Member

    @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.

  2. Logerfo commented on May 9, 2018

    @Logerfo

    @roji

    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).

  3. StillLearnin commented on May 9, 2018

    @StillLearnin
    ContributorAuthor

    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.

  4. roji commented on May 9, 2018

    @roji
    Member

    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:

    var precision = ((typeModifier - 4) >> 16) & 65535;
    var scale = (typeModifier - 4) & 65535;

    You can then apply the scale.

  5. StillLearnin commented on May 9, 2018

    @StillLearnin
    ContributorAuthor

    @roji sure, I'll work on it (including tests).

  6. YohDeadfall commented on May 9, 2018

    @YohDeadfall
    Contributor

    It makes sense for data visualization as @StillLearnin already said, @roji, so I would like to see a PR (:

  7. StillLearnin commented on May 10, 2018

    @StillLearnin
    ContributorAuthor

    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) produces 1.235
    SELECT '1.2344'::numeric(10,3) produces 1.234
    SELECT '1.2'::numeric(10,3) produces 1.200

  8. StillLearnin commented on May 10, 2018

    @StillLearnin
    ContributorAuthor

    Found a case that doesn't match though.
    SELECT '1'::numeric(10,2) psql returns 1.00 but Npgsql returns 1

    So there is more work to do.

  9. StillLearnin commented on May 10, 2018

    @StillLearnin
    ContributorAuthor

    Hmm... and .net uses banker's rounding by default but psql doesn't

    Command psql .net Math.Round
    SELECT '1.5'::numeric(10,0); 2 2
    SELECT '2.5'::numeric(10,0); 3 2
  10. roji commented on May 11, 2018

    @roji
    Member

    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.

  11. StillLearnin commented on May 15, 2018

    @StillLearnin
    ContributorAuthor

    Fixed by #1925

  12. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions