Repository navigation
[Spark] Support geometry and geography columns in the Spark connector - #2372
timothyw553 wants to merge 4 commits into
Conversation
b59c6da to
c69c1f9
Compare
c69c1f9 to
abfd249
Compare
|
/review |
timothyw553
left a comment
There was a problem hiding this comment.
two comments, one on the type_json fallback order and one on a geography test expectation.
| DataType.fromDDL(col.getTypeText), | ||
| // Delta writes geo types as e.g. geometry(OGC:CRS84), which Spark's DDL parser rejects but | ||
| // its JSON parser reads. | ||
| GeoTypes.fromTypeJson(col.getTypeText, col.getTypeJson) |
There was a problem hiding this comment.
to my understanding the regex also matches non-geo columns like struct<geometry:int>, and trying type_json first can throw on legacy/empty type_json or mixed-array metadata (or let type_json override valid type_text). should we try DataType.fromDDL first and fall back to type_json only on ParseException, rethrowing the ddl error if json fails too, and drop fromTypeJson from the shims? could we also add tests for those fallback and error paths?
- GeoTypes.fromTypeJson(col.getTypeText, col.getTypeJson)
- .getOrElse(DataType.fromDDL(col.getTypeText)),
+ try {
+ DataType.fromDDL(col.getTypeText)
+ } catch {
+ case ddlError: ParseException =>
+ try {
+ DataType.fromJson(
+ s"""{"type":"struct","fields":[${col.getTypeJson}]}""")
+ .asInstanceOf[StructType].fields.head.dataType
+ } catch {
+ case NonFatal(_) => throw ddlError
+ }
+ },There was a problem hiding this comment.
Simplified toStructField to try DataType.fromDDL first, fall back to DataType.fromJson on ParseException (rethrowing the DDL error if that fails), and removed fromTypeJson from both GeoTypes shims.
| "ST_GeogFromWKB(X'0101000000000000000000F03F0000000000000040')", | ||
| POINT_WKB_HEX, | ||
| ColumnTypeName.GEOGRAPHY, | ||
| "geography(OGC:CRS84, SPHERICAL)", |
There was a problem hiding this comment.
doesn't the server return lowercase spherical for type_text here (like in DeltaManagedTableReadWriteTest)? since this is skipped on spark 4.1 it won't get caught until spark 4.2+.
- "geography(OGC:CRS84, SPHERICAL)",
+ "geography(OGC:CRS84, spherical)",
"\"geography(OGC:CRS84, SPHERICAL)\"")There was a problem hiding this comment.
Updated BaseTableReadWriteTest to use lowercase spherical to match the canonical geography algorithm casing.
abfd249 to
4d7fdb7
Compare
|
/review |
timothyw553
left a comment
There was a problem hiding this comment.
overall LGTM. left two small comments on the type_json fallback (preserving the json error and stripping nested column-mapping metadata).
| .head | ||
| .dataType | ||
| } catch { | ||
| case NonFatal(_) => throw ddlError |
There was a problem hiding this comment.
swallowing NonFatal(_) drops why the type_json fallback failed (for example ST_INVALID_ALGORITHM_VALUE for an unsupported geography algorithm), leaving only a DDL syntax error. could we attach it as suppressed so it's easier to debug?
- case NonFatal(_) => throw ddlError
+ case NonFatal(jsonError) =>
+ ddlError.addSuppressed(jsonError)
+ throw ddlErrorThere was a problem hiding this comment.
Attached the JSON fallback error as a suppressed exception on ddlError and updated UCProxySuite to verify it.
| .asInstanceOf[StructType] | ||
| .fields | ||
| .head | ||
| .dataType |
There was a problem hiding this comment.
to my understanding UCColumnConversions.metadataOf strips delta.columnMapping.* so spark doesn't copy physical layout keys into new tables on CTAS or CREATE TABLE LIKE. extracting dataType directly from type_json here keeps metadata on nested struct/array/map fields though. should we strip delta.columnMapping.* recursively (while keeping collations) and assert that in the nested test?
There was a problem hiding this comment.
Added UCColumnConversions.stripDeltaColumnMappingMetadata to recursively strip delta.columnMapping.* keys from nested struct/array/map fields while preserving collations and user metadata, and expanded the nested test in UCProxySuite to assert both.
yili-db
left a comment
There was a problem hiding this comment.
[AI Assisted] Two items. One is inline. The other is here: the legacy create path in UCSingleCatalog.scala (line 1338, outside this diff) sends catalogString, which gives geometry(4326). Delta Kernel rejects a CRS without an authority:code form, so Delta readers would fail on such a table. Could we send the Delta form, or limit geo types to the Delta API path?
| } catch { | ||
| case ddlError: ParseException if col.getTypeJson != null => | ||
| try { | ||
| DataType.fromJson(s"""{"type":"struct","fields":[${col.getTypeJson}]}""") |
There was a problem hiding this comment.
[AI Assisted] For a struct that contains a geo field, this fallback returns the nested fields from DataType.fromJson, which keeps their delta.columnMapping.* metadata. UCColumnConversions.metadataOf strips those keys only from the outer column. Could we strip them from nested fields too, with a test?
There was a problem hiding this comment.
Good catch — added UCColumnConversions.stripDeltaColumnMappingMetadata to recursively strip delta.columnMapping.* keys from nested struct/array/map fields while preserving collations and other metadata, and expanded the nested geo test in UCProxySuite to verify it.
4d7fdb7 to
90cfadc
Compare
|
/review |
yili-db
left a comment
There was a problem hiding this comment.
[AI Assisted] Approving. The nested column-mapping metadata item is fixed: the fallback strips delta.columnMapping.* from nested fields, and the nested test covers it. The legacy create path sending geometry(4326) stays a non-blocking follow-up.
90cfadc to
aa11229
Compare
|
/review |
aa11229 to
ad0fdde
Compare
ad0fdde to
68491ad
Compare
|
/review |
1 similar comment
|
/review |
Add GEOMETRY and GEOGRAPHY to ColumnTypeName in api/all.yaml (appended so existing ordinals stay unchanged). Widen the ordinal CHECK constraints on uc_functions.data_type and uc_function_params.type_name from 0..21 to 0..23 across H2, PostgreSQL, and MySQL: - V24 / V26 (SQL): add named CHECK (0..23) constraints on uc_functions and uc_function_params. - V25 / V27 (Java migrations): look up and drop the old unnamed 0..21 CHECK constraints by clause so upgrades work on both fresh and repaired databases. Update SchemaMigrator, MigrationStepRulesTest, docs/server/upgrades.md, and schema-effects for all three databases, and add SdkFunctionCRUDTest coverage for GEOMETRY and GEOGRAPHY function parameters and return types. Signed-off-by: Timothy Wang <[email protected]>
Resolve the geo type strings Delta Kernel parses (geometry[(crs)], geography[(crs[, algorithm])|(algorithm)]) to GEOMETRY and GEOGRAPHY, and keep the string as type_text so the CRS and algorithm survive. type_json already carries it. Without this a Delta table with a geo column is rejected at create time as an unsupported primitive. A geography algorithm Delta does not define is rejected with INVALID_ARGUMENT, as Kernel does; other malformed forms fall through to the existing unsupported-primitive error. Signed-off-by: Timothy Wang <[email protected]>
Iceberg 1.11 defines geometry(crs) and geography(crs, algorithm), so map them to GEOMETRY and GEOGRAPHY instead of rejecting the table, and drop the comment that said Iceberg has no such types. type_text and type_json carry the Delta form, geometry(<crs>) and geography(<crs>, <algorithm>). Iceberg accepts any CRS string, but Delta clients only parse <authority>:<code>, so another CRS is rejected up front rather than stored as a type they can't read. Signed-off-by: Timothy Wang <[email protected]>
Map Spark's geometry and geography types to GEOMETRY and GEOGRAPHY when creating a table, through a GeoTypes shim that is a no-op on Spark 4.0, which has neither type. Load a table whose geo column was stored in Delta's form, such as geometry(OGC:CRS84): Spark's DDL parser rejects that, but its JSON parser reads it, so such a column is read from type_json. Tests: unit tests for both mappings, a create-and-load test for managed Delta tables on Spark 4.1+, and geo columns in the type matrix on Spark 4.2+, where the Parquet writer can write them. Delta's geo preview flag is set in the test sessions only. Signed-off-by: Timothy Wang <[email protected]>
68491ad to
3588fc4
Compare
🥞 Stacked PR
Use this link to review incremental changes.
PR Checklist
docsis updated (in [CLI/UI/Docs] Support and document geometry and geography columns #2373)Description of changes
Adds
GEOMETRYandGEOGRAPHYcolumn support to the Spark connector on Spark 4.1+, behind aGeoTypesshim that is a no-op on Spark 4.0. When loading a table whosetype_textnames a geo type (top-level or nested),toStructFieldparses the column fromtype_jsonviaDataType.fromJsonbecauseDataType.fromDDLrejects Delta forms such asgeometry(OGC:CRS84).Tested with unit and Spark integration suites (create/load on Spark 4.1+, write/read on Spark 4.2+ with
spark.databricks.delta.geo.preview.enabled=truein test sessions only) across Spark 4.0–4.1 with Delta 4.4.1 and Spark 4.1–4.3 with a Delta master snapshot.