question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Numeric types with a 0 precision are converted to decimal types instead of int

See original GitHub issue

I am mirroring a mysql spatial database in postgis and querying the postgis data with this driver via a web api. The mirroring process takes mssql int types (small, normal, big) and inserts them into postgresql as numeric(5,0), numeric(10,0), etc which is equivalent to integer types. The npgsql driver returns these values as decimals instead of integer types.

Steps to reproduce

schema

  1. create a table with a schema containing numeric values with no precision e.g. numeric(5,0)
  2. query the table and view the returned type e.g. 5.0
  3. alter the field to the equivalent integer type e.g. smallint
  4. query the table and view the returned type 5

The issue

The type will be decimal and int when both should be int. This causes problems when serializing to json as the numeric(5,0) is interpreted as a decimal when sent with an empty decimal space compared to the integer serialization without a decimal.

Further technical details

Npgsql version: 4.1.5 PostgreSQL version: 11.6 Operating system: Google Cloud SQL

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Jan 21, 2021

I don’t really think it’s useful to discuss whether it makes sense to use numeric(x, 0) - it’s certainly possible and may be useful in some (admittedly exotic) cases, where numbers beyond the range of bigint are being represented. May I ask if that’s the reason why numeric(x, 0) is being used? I’m asking because it’s likely that the performance of operations on numeric(x, 0) be inferior to the same operations on bigint.

Apart from that, regarding how numeric(x, 0) should be mapped: simply put, numeric(x,0) is still numeric; just as PostgreSQL maintains that distinction, so does Npgsql when mapping PG types to CLR types.

1reaction
YohDeadfallcommented, Jan 20, 2021

Okay, what will happen if I slightly modify a query some one wrote? For example, will do some math instead of reading a value. The answer is that it might brake a lot of code in the client application because instead of int there will be decimal.

The driver isn’t responsible for taking decisions on how to reinterpret data, but yours as a developer of a system which just uses the driver. The responsibility of the driver is to communicate with the server, receive and send data as is. It’s the single responsibility principle.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 8.1. Numeric Types
Numeric types consist of two-, four-, and eight-byte integers, four- and eight-byte floating-point numbers, and selectable-precision decimals.
Read more >
decimal and numeric (Transact-SQL) - SQL Server
Numeric data types that have fixed precision and scale. Decimal and numeric are synonyms and can be used interchangeably.
Read more >
Why decimal.Decimal doesn't support arithmetic operations ...
Decimal instead of built-in float. I know it doesn't work with float-type values and it returns TypeError: unsupported operand type(s) for +: ' ......
Read more >
Floating-point numeric types - C# reference
Learn about the built-in C# floating-point types: float, double, and decimal.
Read more >
Numeric Data Types
If data is converted to another data type with lower precision, ... NUMBER(10,1), dec DECIMAL(20,2), numeric NUMERIC(30,3), int INT, integer INTEGER ); DESC ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found