I spent some time playing with the SQL 2008 geometry data type. This post will cover some of the things I have learned about how it is implemented from playing with the feature. Some of these will impact the feature’s usability for some. Others may not care.
1. late-binding of the geometry data is an interesting choice. So one of the nice properties of a SQL query is that, for many things, the parse and bind of a query can find all sorts of compilation errors that would show up during development. You don’t have to run the query in order to see the simple mistakes, and that helps us all. This type doesn’t throw parsing errors until you try to execute the query. That’s not perfect – you have to go execute spatial queries to see whether you have typed in lots of complex data correctly or not. I can speak from experience that I tend to mess that stuff up.
create table g1 (col1 int identity, col2 geometry)
insert into g1(col2) values (Geometry::STGeomFromText('LINESTRING(0 0, 10 10, 21 2)',0));
insert into g1(col2) values (Geometry::STGeomFromText('LINESTRING(funky chicken)',0));
That third query compiles just fine – running it returns an error.
2. The geography type uses some form of the .net framework. (I’ll point out that I have not enabled the CLR in my server). Given that the ordpath type requires that the CLR be enabled, I’ll guess that this means that there is a special implementation of the geometry type.
Here’s the runtime errof from the previous query:
Msg 6522, Level 16, State 1, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate “geometry”:
System.FormatException: 24141: A number is expected at position 16 of the input. The input has funky.
at Microsoft.SqlServer.Types.OpenGisWktReader.ParseLineStringText(FigureAttributes attributes)
at Microsoft.SqlServer.Types.SqlGeometry.STGeomFromText(SqlChars geometryTaggedText, Int32 srid)
The statement has been terminated.
(I think I will be making t-shirts that say “The input has funky” on it… let me know if you want in :).
I am actually pretty happy with the error message – the only thing missing is which column caused the error. You’ll notice that “position 16″ is inside of this second string being parsed by the .NET routines. I personally do find stack traces unacceptable for general use, but the .net and java folks seem to have doubled down on this approach, so we all get to learn about their libraries even in SQL now…
This is just a seam in how it was implemented. Be aware of this when using the type.
3. the geometry type has been (arbitrarily) given an estimated row width of about 4000 bytes. well, this is what shows up in showplan. So I inserted a bunch of data into this table (1.4 million rows in about 148MB). The cost estimates don’t seem to use the 4000 byte number for the row width (as it would take a long time to read 5.6GB – more than 13 seconds or so, given that is what the costing says, so I am guessing that this is just reported in showplan the same way it is costed internally). I’m just using a heap (no indexes) for this experiment.
4. I’m waiting for maps.live.com to make it easy for me to geocode addresses and insert them into SQL. It has a feature to send it to some gps device, but I haven’t played with it beyond learning that it didn’t detect that I had blocked cookies by default ;).
5. “select col2.ToString() from g1″ is a way to see something useful from that data type. otherwise you get back binary goo…
That’s all for tonight.