Skip to content

[Spark] Support geometry and geography columns in the Spark connector - #2372

Open
timothyw553 wants to merge 4 commits into
unitycatalog:mainfrom
timothyw553:stack/geotypes-spark
Open

timothyw553 wants to merge 4 commits into
unitycatalog:mainfrom
timothyw553:stack/geotypes-spark

Conversation

@timothyw553

@timothyw553 timothyw553 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

🥞 Stacked PR

Use this link to review incremental changes.


PR Checklist

Description of changes

Adds GEOMETRY and GEOGRAPHY column support to the Spark connector on Spark 4.1+, behind a GeoTypes shim that is a no-op on Spark 4.0. When loading a table whose type_text names a geo type (top-level or nested), toStructField parses the column from type_json via DataType.fromJson because DataType.fromDDL rejects Delta forms such as geometry(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=true in 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.

@yili-db

yili-db commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

/review

@timothyw553 timothyw553 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
+          }
+      },

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)\"")

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated BaseTableReadWriteTest to use lowercase spherical to match the canonical geography algorithm casing.

@yili-db

yili-db commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

/review

@timothyw553 timothyw553 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ddlError

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attached the JSON fallback error as a suppressed exception on ddlError and updated UCProxySuite to verify it.

.asInstanceOf[StructType]
.fields
.head
.dataType

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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}]}""")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@timothyw553
timothyw553 requested a review from yili-db December 1, 2026 01:16
@yili-db

yili-db commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@yili-db

yili-db commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db

yili-db commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

/review

1 similar comment
@yili-db

yili-db commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

/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]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants