-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integer schema columns converted to numeric (decimal) columns with OGRSQL dialect #3345
Comments
Could you show the output of ogrinfo -al -so {connection_string} on the MSSQL table you request ? |
This table has smallint, int, and numeric fields The MSSQL schema The ogrinfo
The PostGIS schema |
Any chance to verify that the result is the same with some newer GDAL? Version 2.3.3 is pretty old. And what happens if you use the native SQL (if there is a support for native MSSQL dialect, I do not know). |
Ok. I ran the I can't use the MSSQL dialect because of #1990 and how it infers the geometry incorrectly. Are those versions new enough or should I upgrade? |
Ok I updated gdal on the machine to 3.2.1 and will let you know what happens in the morning after it runs. |
@jratike80 I have the same experience with gdal v3.2.1. Would you or @rouault like any other information? |
bump |
But isn't that right in PostGIS? By https://www.postgresql.org/docs/13/datatype-numeric.html
and then
So the error happens here
In other words "10 becomes 10.0 because integer is interpreted as a decimal. |
Reading that it seems correct, but is it the best field type choice? Since pg has more specific integer types, I would prefer to use them. The doc also says that
which makes the occurrence of the error subject to the same interpretation of the specs. |
By this RFC https://trac.osgeo.org/gdal/wiki/rfc50_ogr_field_subtype PostGIS driver does support smallint (OFSTInt16) subtype. Perhaps it is the MSSQL driver https://gdal.org/drivers/vector/mssqlspatial.html that does not support smallint in reading. Smallint converts into Integer (5.0) but that cannot be changed back into smallint because Integer (5.0) it covers wider range than -32768 - +32768 . Fortunately CAST ASSMALLINT is supported so with some hand editing you can create the PostGIS table as you wish. Example:
My conclusions (maybe not correct, I am a GDAL user like you, not developer).
|
Just to clarify I am using the OGR SQL dialect. Casting would be an option if I wasn't dynamically loading ~400 tables. I did write code to query the types in mssql and alter the postgis types after loading the data with ogr but I think it would be ideal if the type inference matched. I will look into the c# postgresql driver to see why it treats a numeric 5,0 as a decimal. That is a good idea. |
Check first what data ogrinfo finds from your table. I get it like this:
Inttest is numeric(5,0) in PostGIS, for ogrinfo it is Integer (5.0), and the value is "2" without decimals. Do you get something different? |
I pasted ogr info in #3345 (comment) |
Not that part that contains feature data. Run the command again without
|
Ok sorry, I'm not familiar with that tools options
|
The value of symbol is 83 without decimals and you can concentrate on the GeoJSON writer because that seems to add decimals into integers. |
Yup, it's a two part issue for me. The database table is created with a schema that is different. Downstream, the database driver interprets the numeric's as decimals without looking at the precision. And finally the bug my users notice is that the serialized output is different. If I can fix these issues in either or both places, I think that would be a win for everyone. The database driver folks closed the issue as expected behavior and your argument is that the types are equivalent so my point of view seems to be the minority. |
Please clarify:
I don't quite understand what issues npgsql developer is awaiting npgsql/npgsql#3471 (comment) but probably they know their code. I agree that a) support of smallint subtype in GDAL MSSQL driver and b) better handling of different ways to define integer in the PostGIS schema with npgsql would be nice. Until that it seems that you must live with the rules that npgsql sets. It requires some manual work with checking the MSSQL schema and fine tuning the SQL.
|
I apologize for the confusion. I am explaining the layers of architecture and process with all the parts involved to expose this issue to my users. Since there are a few dependent parts I thought it would be good to layout.
Yes but please remember I'm using the OGR SQL driver NOT the MSSQL driver.
I am referencing the c# pgsql driver that is used to query postgresql for the data after it has been loaded with GDAL. Nothing to do with GDAL here, just explaining another layer in my chain of issues.
I have a c# web api that uses the npgsql driver to query data and return it to users serialized as json. That is the end of my chain. |
Hi,
OGR SQL https://gdal.org/user/ogr_sql_dialect.html is not a driver. It is query method for accessing data through the drivers. This is a list of drivers https://gdal.org/drivers/vector/index.html. It may be that in some circumstances the selected SQL dialect also has an effect on the results even they are reading data through the same driver. Limitations and oddities may sort of pile up. Have you confirmed that the selected SQL dialect plays role in your case? And why do you use OGR SQL dialect at all? |
I already answered this question above but i will repeat it. Because #1990 |
All right, thanks. This is a long thread already and I do this just for fun so my concentration may not be best possible. What prevents you from using OGR SQL with CAST...AS SMALLINT? |
I really appreciate everything you have done so far
I mentioned that I am mirroring a database with hundreds of tables with this script and the schemas change enough that it would be cumbersome to store a static copy of it somewhere. As a short cut I |
…real columns, and correctly roundtrip smallint/Int16 (fixes OSGeo#3345)
Expected behavior and actual behavior.
I expect to be able to translate a table from MSSQLServer to PostGIS with as close to the same schema as reasonably possible.
The actual behavior is that a column of type
smallint
in mssql translates to anumeric(5)
in postgis andint
translates to anumeric(10)
. Numeric field types imply decimal types. Postgresql has the equivalent types but I assume the dialect isn't able to infer from the sql query?One issue created by this is that when using this data in a service that serializes these types to json or other formats, the output is different.
10
becomes10.0
because numeric is interpreted as a decimal. However insignificant the.0
, typed deserializers will break on the int vs decimal property type.Steps to reproduce the problem.
Create a table with a
smallint
,int
, orbigint
and run code similar to this to convert from mssql to postgis.Operating system
Windows Server 2016 Standard
GDAL version and provenance
3.2.1 from conda
The text was updated successfully, but these errors were encountered: