feat: GeoArrow export — GEOGRAPHY/GEOMETRY as geoarrow.wkb#100
feat: GeoArrow export — GEOGRAPHY/GEOMETRY as geoarrow.wkb#100jatorre wants to merge 1 commit intoadbc-drivers:mainfrom
Conversation
|
cc @sfc-gh-obielov — We've reported to Snowflake that the REST API This is why this PR needs an extra |
Detect GEOGRAPHY/GEOMETRY columns during query execution and tag them with geoarrow.wkb Arrow extension metadata, enabling DuckDB and other Arrow consumers to receive native geometry types with CRS information. How it works: 1. Set GEOGRAPHY/GEOMETRY_OUTPUT_FORMAT=WKB at connection time so geo columns arrive as binary WKB instead of GeoJSON strings 2. Before executing a query, extract the table name and run DESCRIBE TABLE to identify GEOGRAPHY/GEOMETRY columns (catalog metadata is unaffected by the WKB output format setting) 3. Tag identified columns with geoarrow.wkb extension metadata in the Arrow schema — GEOGRAPHY gets CRS "EPSG:4326", GEOMETRY gets no CRS 4. Data flows as binary WKB with zero conversion overhead Note: Snowflake's REST API reports geo columns as "binary" in rowtype metadata when WKB output format is set, losing the original type info. This is why we need the separate DESCRIBE TABLE query. We've reported this to Snowflake. Limitations (documented as TODOs): - GEOMETRY SRID: requires data inspection to determine, same cross-driver issue as adbc-drivers/redshift#2 and adbc-drivers/databricks#350 - Arbitrary queries: only table scans (SELECT ... FROM table) get geoarrow metadata. Complex queries with joins/subqueries don't trigger geo detection. The data is still correct WKB, just without the metadata. Tested end-to-end: DuckDB reads Snowflake GEOGRAPHY as native GEOMETRY with CRS EPSG:4326, and GeoParquet export preserves the type. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
12535b8 to
16d70ea
Compare
lidavidm
left a comment
There was a problem hiding this comment.
@zeroshade is there an upstream feature request/bug report we can reference for this?
| wkb := "WKB" | ||
| if _, ok := d.cfg.Params["GEOGRAPHY_OUTPUT_FORMAT"]; !ok { | ||
| d.cfg.Params["GEOGRAPHY_OUTPUT_FORMAT"] = &wkb | ||
| } | ||
| if _, ok := d.cfg.Params["GEOMETRY_OUTPUT_FORMAT"]; !ok { | ||
| d.cfg.Params["GEOMETRY_OUTPUT_FORMAT"] = &wkb | ||
| } |
There was a problem hiding this comment.
| wkb := "WKB" | |
| if _, ok := d.cfg.Params["GEOGRAPHY_OUTPUT_FORMAT"]; !ok { | |
| d.cfg.Params["GEOGRAPHY_OUTPUT_FORMAT"] = &wkb | |
| } | |
| if _, ok := d.cfg.Params["GEOMETRY_OUTPUT_FORMAT"]; !ok { | |
| d.cfg.Params["GEOMETRY_OUTPUT_FORMAT"] = &wkb | |
| } | |
| if _, ok := d.cfg.Params["GEOGRAPHY_OUTPUT_FORMAT"]; !ok { | |
| d.cfg.Params["GEOGRAPHY_OUTPUT_FORMAT"] = new("WKB") | |
| } | |
| if _, ok := d.cfg.Params["GEOMETRY_OUTPUT_FORMAT"]; !ok { | |
| d.cfg.Params["GEOMETRY_OUTPUT_FORMAT"] = new("WKB") | |
| } |
There was a problem hiding this comment.
Also, if a user explicitly sets this to something that is not WKB, we should avoid adding the GeoArrow type and metadata, right?
There was a problem hiding this comment.
That is an interesting point. Snowflake I believe is the only engine that allows you to configure how the output of geo data types are encoded.
My feeling is that if the query or table is returning a native geometry or geography type we should always encode as WKB and mark it as geoarrow. It is not that you can have gearrow encoded on different formats (well you can with WKB vs native arrow objects but that’s another thing). The goal is to preserve the type so I don’t think it makes sense to change that.
But if the user does st_astext(geom) then it becomes text and it should be retuned as that. So essentially behaving like everybody else. You use functions to specify output formats (as_text, as_wkb, as_geojson,as_gml…).
So since this is adbc I would say no.
There was a problem hiding this comment.
In other words. We ignore because we override whatever output format the user has specified.
| // try to extract the table name and run DESCRIBE TABLE to identify | ||
| // GEOGRAPHY/GEOMETRY columns (catalog metadata is unaffected by WKB output format). | ||
| // TODO: Support arbitrary queries — currently only table scans get geoarrow metadata. | ||
| geoCols := st.cnxn.detectGeoColumnsFromQuery(ctx, st.query) |
There was a problem hiding this comment.
We should make this opt-in. It would also be good to document it in snowflake.md, and add validation cases for geometry/geography.
There was a problem hiding this comment.
Yeah, I don’t know. It really feels this should be fixed upstream, and I agree this is too much… yeah affecting the entire driver for this issue is too much. Maybe this whole thing should not be fixed until snowflake fixes upstream. The workaround is tooo bad.
Summary
Adds geospatial export support: GEOGRAPHY/GEOMETRY columns are automatically tagged with
geoarrow.wkbArrow extension metadata, enabling DuckDB and other Arrow consumers to receive native geometry types.How it works
GEOGRAPHY_OUTPUT_FORMAT=WKBandGEOMETRY_OUTPUT_FORMAT=WKBat connection time so geo columns arrive as binary WKB (not GeoJSON strings)DESCRIBE TABLEto identify GEOGRAPHY/GEOMETRY columns. Catalog metadata correctly reports the original type regardless of output format.ARROW:extension:name = "geoarrow.wkb". GEOGRAPHY gets CRSEPSG:4326(always WGS84). GEOMETRY gets no CRS (see TODOs).identCol).Schema mapping
Why DESCRIBE TABLE?
Snowflake's REST API reports geo columns as
"type": "binary"inrowtyperesponse metadata when WKB output format is set, losing the original type information. BothrowtypeandDESCRIBE RESULTare affected. OnlyDESCRIBE TABLE(catalog metadata) preserves the original type. We've reported this limitation to Snowflake.TODOs
GEOMETRY SRID — Requires inspecting data to determine SRID, which needs first-batch buffering. Same cross-driver challenge as adbc-drivers/redshift#2 and adbc-drivers/databricks#350. Fixed CRS for GEOGRAPHY (always 4326), empty CRS for GEOMETRY.
Arbitrary queries — Currently only table scans (
SELECT ... FROM table) trigger geo detection. Complex queries (joins, subqueries, CTEs) won't get geoarrow metadata. The data is still correct WKB, just without the Arrow extension type annotation.Test results
End-to-end against real Snowflake:
GEOMETRY('EPSG:4326')✓Related
🤖 Generated with Claude Code