-
Notifications
You must be signed in to change notification settings - Fork 14
fix(go): address issues in geo implementation #144
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
+136
−44
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -553,9 +553,12 @@ func (st *statement) executeIngest(ctx context.Context) (int64, error) { | |
| // Build the COPY query. If the schema has geo columns, this is a COPY | ||
| // transform that converts WKB/WKT → GEOGRAPHY/GEOMETRY inline during | ||
| // COPY INTO; otherwise it is the plain copy query. | ||
| copyQ, geoOverrides := st.buildCopyQuery(schema) | ||
| copyQ, geoOverrides, err := st.buildCopyQuery(schema) | ||
| if err != nil { | ||
| return -1, err | ||
| } | ||
|
|
||
| err := st.initIngest(ctx, geoOverrides) | ||
| err = st.initIngest(ctx, geoOverrides) | ||
| if err != nil { | ||
| return -1, err | ||
| } | ||
|
|
@@ -577,9 +580,9 @@ func (st *statement) executeIngest(ctx context.Context) (int64, error) { | |
| // Geo column detection covers both arrow.EXTENSION types and | ||
| // ARROW:extension:name field metadata so that data arriving over the C Data | ||
| // Interface (where extension types are not registered) is also recognized. | ||
| func (st *statement) buildCopyQuery(schema *arrow.Schema) (string, map[string]string) { | ||
| func (st *statement) buildCopyQuery(schema *arrow.Schema) (string, map[string]string, error) { | ||
| if schema == nil { | ||
| return copyQuery, nil | ||
| return copyQuery, nil, nil | ||
| } | ||
|
|
||
| // Detect geo columns: either a registered arrow.ExtensionType or the | ||
|
|
@@ -603,21 +606,21 @@ func (st *statement) buildCopyQuery(schema *arrow.Schema) (string, map[string]st | |
| } | ||
|
|
||
| switch extName { | ||
| case "geoarrow.wkb", "geoarrow.wkb_view", "geoarrow.wkt", "geoarrow.wkt_view": | ||
| case "geoarrow.wkb", "geoarrow.wkt": | ||
| geoCols = append(geoCols, geoCol{name: f.Name, extName: extName, extMeta: extMeta}) | ||
| } | ||
| } | ||
|
|
||
| if len(geoCols) == 0 { | ||
| return copyQuery, nil | ||
| return copyQuery, nil, nil | ||
| } | ||
|
|
||
| // Build a COPY transform with inline geo conversion. Each geo column's | ||
| // target type is resolved per-column so a non-4326 CRS can promote that | ||
| // column to GEOMETRY while sibling 4326 columns stay GEOGRAPHY. | ||
| geoOverrides := make(map[string]string, len(geoCols)) | ||
| var selectCols []string | ||
| for _, f := range schema.Fields() { | ||
| for fieldIndex, f := range schema.Fields() { | ||
| quoted := quoteIdentifier(f.Name) | ||
| parqRef := fmt.Sprintf("$1:%s", quoted) | ||
|
|
||
|
|
@@ -638,7 +641,11 @@ func (st *statement) buildCopyQuery(schema *arrow.Schema) (string, map[string]st | |
|
|
||
| // Geo column: apply conversion function. | ||
| isWKB := strings.Contains(gc.extName, "wkb") | ||
| geoType := st.ingestOptions.resolveGeoType(gc.extMeta) | ||
| geoType, err := st.ingestOptions.resolveGeoType(fieldIndex, f.Name, gc.extMeta) | ||
| if err != nil { | ||
| return "", nil, err | ||
| } | ||
|
|
||
| geoOverrides[gc.name] = geoType | ||
| var expr string | ||
| if geoType == "geography" { | ||
|
|
@@ -648,7 +655,7 @@ func (st *statement) buildCopyQuery(schema *arrow.Schema) (string, map[string]st | |
| expr = fmt.Sprintf("TRY_TO_GEOGRAPHY(%s::VARCHAR) AS %s", parqRef, quoted) | ||
| } | ||
| } else { | ||
| srid := extractSRIDFromMeta(gc.extMeta) | ||
| srid, _ := extractSRIDFromMeta(gc.extMeta) | ||
| if srid != 0 { | ||
| if isWKB { | ||
| expr = fmt.Sprintf("ST_SETSRID(TO_GEOMETRY(%s::BINARY), %d) AS %s", parqRef, srid, quoted) | ||
|
|
@@ -671,35 +678,45 @@ func (st *statement) buildCopyQuery(schema *arrow.Schema) (string, map[string]st | |
| strings.Join(selectCols, ", "), | ||
| bindStageName, | ||
| ) | ||
| return transformQ, geoOverrides | ||
| return transformQ, geoOverrides, nil | ||
| } | ||
|
|
||
| // resolveGeoType picks the Snowflake target type for a single geoarrow column. | ||
| // When the user has set ingest_geo_type explicitly, that value is honored for | ||
| // every column (current behavior). Otherwise the column's CRS metadata decides: | ||
| // any non-EPSG:4326 SRID promotes the column to GEOMETRY so the SRID survives | ||
| // the round trip; missing CRS, EPSG:4326, or unparsable CRS stays GEOGRAPHY. | ||
| func (opts *ingestOptions) resolveGeoType(extMeta string) string { | ||
| func (opts *ingestOptions) resolveGeoType(fieldIndex int, fieldName string, extMeta string) (string, error) { | ||
| if opts.geoTypeExplicit { | ||
| return opts.geoType | ||
| return opts.geoType, nil | ||
| } | ||
| srid := extractSRIDFromMeta(extMeta) | ||
| if srid != 0 && srid != 4326 { | ||
| return "geometry" | ||
| srid, edges := extractSRIDFromMeta(extMeta) | ||
| if srid == 4326 && edges == "spherical" { | ||
| return "geography", nil | ||
| } else if edges == "spherical" { | ||
| // Snowflake GEOGRAPHY is always SRID 4326, so if the user | ||
| // specified spherical edges but a different SRID, we should | ||
| // error/ask them to explicitly set the geo type | ||
| return "", adbc.Error{ | ||
| Msg: fmt.Sprintf("[snowflake] field #%d (%s) is a GeoArrow array with spherical edges but an SRID of %d; Snowflake GEOGRAPHY is always SRID 4326, so explicitly set %s to choose whether to ingest this as GEOGRAPHY or GEOMETRY", fieldIndex+1, quoteIdentifier(fieldName), srid, OptionStatementIngestGeoType), | ||
| Code: adbc.StatusInvalidData, | ||
| } | ||
| } | ||
| return "geography" | ||
| return "geometry", nil | ||
| } | ||
|
|
||
| // extractSRIDFromMeta extracts the SRID from geoarrow extension metadata string. | ||
| // The metadata is a JSON string that may contain a "crs" field. | ||
| // extractSRIDFromMeta extracts the SRID and edges from GeoArrow extension | ||
| // metadata string. The metadata is a JSON string that may contain a "crs" | ||
| // field. | ||
| // | ||
| // Supported formats: | ||
| // - PROJJSON: {"crs": {"id": {"authority": "EPSG", "code": 4326}}} | ||
| // - Simple string: "EPSG:4326" (as CRS value) | ||
| // | ||
| // Returns 0 if no SRID can be determined. | ||
| func extractSRIDFromMeta(metadata string) int { | ||
| func extractSRIDFromMeta(metadata string) (int, string) { | ||
| if metadata == "" { | ||
| return 0 | ||
| return 0, "" | ||
| } | ||
|
|
||
| type projID struct { | ||
|
|
@@ -709,38 +726,61 @@ func extractSRIDFromMeta(metadata string) int { | |
| type projCRS struct { | ||
| ID projID `json:"id"` | ||
| } | ||
|
|
||
| type projIDString struct { | ||
| Authority string `json:"authority"` | ||
| Code string `json:"code"` | ||
| } | ||
| type projCRSString struct { | ||
| ID projIDString `json:"id"` | ||
| } | ||
|
|
||
| type geoarrowMeta struct { | ||
| CRS json.RawMessage `json:"crs"` | ||
| CRS json.RawMessage `json:"crs"` | ||
| Edges string `json:"edges"` | ||
| } | ||
|
|
||
| var meta geoarrowMeta | ||
| if err := json.Unmarshal([]byte(metadata), &meta); err != nil { | ||
| return 0 | ||
| return 0, "" | ||
| } | ||
|
|
||
| if len(meta.CRS) == 0 { | ||
| return 0 | ||
| return 0, meta.Edges | ||
| } | ||
|
|
||
| // CRS can be a string like "EPSG:4326" or a PROJJSON object | ||
| var crsStr string | ||
| if err := json.Unmarshal(meta.CRS, &crsStr); err == nil { | ||
| if strings.HasPrefix(crsStr, "EPSG:") { | ||
| if code, err := strconv.Atoi(crsStr[5:]); err == nil { | ||
| return code | ||
| return code, meta.Edges | ||
| } | ||
| } else if crsStr == "OGC:CRS84" { | ||
| return 4326, meta.Edges | ||
| } | ||
| return 0 | ||
| return 0, meta.Edges | ||
| } | ||
|
|
||
| var crs projCRS | ||
| if err := json.Unmarshal(meta.CRS, &crs); err == nil { | ||
| if strings.EqualFold(crs.ID.Authority, "EPSG") && crs.ID.Code != 0 { | ||
| return crs.ID.Code | ||
| return crs.ID.Code, meta.Edges | ||
| } | ||
| } | ||
|
Comment on lines
766
to
770
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other one that will show up here is |
||
|
|
||
| var crsString projCRSString | ||
| if err := json.Unmarshal(meta.CRS, &crsString); err == nil { | ||
| if strings.EqualFold(crsString.ID.Authority, "EPSG") { | ||
| if code, err := strconv.Atoi(crsString.ID.Code); err == nil { | ||
| return code, meta.Edges | ||
| } | ||
| } else if strings.EqualFold(crsString.ID.Authority, "OGC") && strings.EqualFold(crsString.ID.Code, "CRS84") { | ||
| return 4326, meta.Edges | ||
| } | ||
| } | ||
|
|
||
| return 0 | ||
| return 0, meta.Edges | ||
| } | ||
|
|
||
| // ExecuteQuery executes the current query or prepared statement | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other one that will show up here that is identical to
"EPSG:4326"is"OGC:CRS84"(I wish this weren't true for what it's worth 😬 ). You can return an SRID of4326for this case.