Numeric types with a 0 precision are converted to decimal types instead of int
See original GitHub issueI 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
- create a table with a schema containing numeric values with no precision e.g.
numeric(5,0)
- query the table and view the returned type e.g.
5.0
- alter the field to the equivalent integer type e.g.
smallint
- 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:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 whynumeric(x, 0)
is being used? I’m asking because it’s likely that the performance of operations onnumeric(x, 0)
be inferior to the same operations onbigint
.Apart from that, regarding how
numeric(x, 0)
should be mapped: simply put,numeric(x,0)
is stillnumeric
; just as PostgreSQL maintains that distinction, so does Npgsql when mapping PG types to CLR types.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 bedecimal
.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.