Skip to content

[GH-3124] Use SRID-prefixed ISO WKB for SQL spatial transport - #3528

Merged
jiayuasu merged 13 commits into
apache:masterfrom
jiayuasu:codex/oss-sql-wkb-serde
Nov 10, 2026
Merged

jiayuasu merged 13 commits into
apache:masterfrom
jiayuasu:codex/oss-sql-wkb-serde

Conversation

@jiayuasu

@jiayuasu jiayuasu commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Did you read the Contributor Guide?

Yes, I have read the Contributor Rules and Contributor Development Guide.

Is this PR related to a ticket?

Related to #3124. Builds on the public Python WKB compatibility helper from merged #3524. This PR is a prerequisite for #3391's Spark 4.2 native integration.

What changes were proposed in this PR?

Replace Spark SQL GeometryUDT and GeographyUDT payloads with a signed four-byte little-endian SRID followed by ISO WKB. JVM geometry uses public JTS IO, JVM geography uses the public S2 WKB writer and a WKB header adapter, and Python uses public Shapely WKB.

The SRID prefix is always present, including for SRID 0. For supported ISO WKB payloads, it supplies the decoded geometry's SRID, including collection members and their factories. Geography retains its existing S2 representation and lazy parsing behavior. Readers accept either ISO WKB payload byte order; the prefix is always little-endian. Geometry supports the full signed 32-bit SRID range; geography retains its nonnegative SRID requirement.

The implementation preserves nulls, supported XY/XYZ/XYM/XYZM layouts, dimensional empties, decoded-value ownership, and direct object evaluation during spatial joins. JVM polygon and multipart layout validation is retained; general collection members can have independent layouts. Public JTS/Shapely normalization and GEOS-dependent dimensional behavior are documented. Lazy JVM geography keeps its coordinate bytes and byte order while normalizing WKB headers; S2 computation and lazy/eager shape-index behavior are unchanged.

RDD/Kryo, spatial-index, Python RDD and Flink serialization keep their existing formats. The common non-SQL geography serializer keeps its big-endian prefix. Spark 4.2 native types and Spark's default serializer are handled separately in #3391.

Persisted ordinary-Parquet UDT columns require migration. Released compact geometry payloads and big-endian geography payloads must be exported using the version that wrote them, then reconstructed after upgrading. The guide uses portable WKB/EWKB columns and retains a separate geography SRID column so SRID 0 is not lost. The new reader does not guess the old format. GeoParquet and explicit WKB/EWKB interchange columns are unaffected by this UDT payload change.

Performance compared with master

The SQL migration is materially slower than compact geometry serialization; this is the cost of using public JTS/Shapely ISO WKB across the SQL boundary. Measurements compare master 0a61b7d6 (serializer sources identical to the frozen baseline build at 20efe7d4) with abb6442af579377b14b4646f4d675beb3bdd307c. The JVM geometry decode rows marked † include the subsequently removed component walk and are pre-cleanup measurements, not current-head timings. Geometry writers and the measured Python/geography paths are unchanged; no benchmark was rerun for this cleanup. Python master uses its built public C extension, verified active in every fork.

Geometry boundary/input Operation Master Measured PR Measured PR/master JVM allocation, master → measured PR
JVM point Encode 10.15 ns/op 80.52 ns/op 7.95× 72 → 392 B/op
JVM point Decode † 13.01 ns/op 61.92 ns/op 4.81× 176 → 496 B/op
JVM polygon (256 vertices) Encode 221.94 ns/op 6,195.83 ns/op 27.90× 4,176 → 25,048 B/op
JVM polygon (256 vertices) Decode † 632.72 ns/op 4,609.81 ns/op 7.29× 11,496 → 15,920 B/op
Python point Encode 0.17 µs/op 4.82 µs/op 28.21× —
Python point Decode 0.25 µs/op 2.41 µs/op 9.62× —
Python polygon (256 vertices) Encode 0.57 µs/op 10.84 µs/op 18.91× —
Python polygon (256 vertices) Decode 1.03 µs/op 5.08 µs/op 4.94× —

Geography is compared against master's existing big-endian-SRID+WKB SQL boundary. Both lazy WKB and already-computed S2 inputs are included; S2 construction is outside timing.

JVM geography input Operation Master Measured PR Measured PR/master Allocation, master → measured PR
Lazy point Encode 7.67 ns/op 17.84 ns/op 2.35× 48 → 136 B/op
Lazy point Decode 9.53 ns/op 9.15 ns/op 0.97× 80 → 80 B/op
Lazy polygon (256 vertices) Encode 209.39 ns/op 230.85 ns/op 1.10× 4,152 → 4,240 B/op
Lazy polygon (256 vertices) Decode 145.45 ns/op 149.53 ns/op 1.01× 4,184 → 4,184 B/op
Computed S2 point Encode 99.37 ns/op 108.17 ns/op 1.09× 296 → 384 B/op
Computed S2 point Decode 9.54 ns/op 9.21 ns/op 0.96× 80 → 80 B/op
Computed S2 polygon (256 vertices) Encode 13,050.01 ns/op 13,041.38 ns/op 1.00× 24,952 → 25,040 B/op
Computed S2 polygon (256 vertices) Decode 145.12 ns/op 149.92 ns/op 1.02× 4,184 → 4,184 B/op

Master's Python geography wrapper emits compact geometry bytes that are incompatible with its JVM geography boundary, so it is not used as a valid geography transport baseline. A separate six-pair Python geography-helper comparison against the compatible boundary in #3524 found point/polygon encoding costs of +28.1%/+10.3% and decoding costs of +11.0%/+6.2%; the measured before/after helper sources are byte-identical to #3524/current code, and this is not a master compact baseline.

Method: six paired fresh processes alternating master/measured-PR order; two-second prewarm, three warmup and five measured rounds; no samples dropped. XY inputs, SRID 4326. JVM: Corretto 17, Spark 4.1.1/Scala 2.13 dependencies, fixed 1 GiB heap, wall/CPU/allocation counters. Python: CPython 3.12.11, Shapely 2.1.2, GEOS 3.13.1, wall/CPU counters. Times are medians of per-fork medians; ratios are medians of paired ratios. All timed inputs round-trip; Python decoder ownership and JVM geography laziness were checked. These are local SQL codec microbenchmarks, not end-to-end Spark query, join or Spark 4.2 default native serializer measurements.

How was this patch tested?

Current-head validation at 99e48eef7022e1f95339c20c24eae36a857f19af:

  • Clean verification on Spark 3.5.8/Scala 2.12 and Spark 4.1.1/Scala 2.13 passed 35 Scala and 27 Java checks per profile, including the six merged S2 distance-join regressions. Coverage includes SQL geometry/geography serialization, direct evaluation, supported ISO byte orders, signed SRIDs, layouts, factories, ownership and malformed-input bounds.
  • Python passed six GeoArrow schema/round-trip checks on Spark 3.5 and 171 checks on Spark 4.1. The five Spark 4.1 skips comprise four Spark 3.5-only checks that passed on 3.5 and one check requiring the absent optional geoarrow.types extension.
  • Fresh shaded jars passed runtime class-origin and class-byte verification. Spotless, Scalastyle, normal commit hooks and git diff --check passed. The SRID-prefixed ISO WKB contract is unchanged; [GH-3124] Use native spatial types on Spark 4.2 #3391 records native Spark 4.2 integration validation.

The performance runs above retained all 36 processes and 960 measured samples, with master's Python C extension verified active in each baseline fork. Their exact measured revision is stated separately from the current validation head.

All applicable current-head CI passed: JVM, Python, wheels, PyFlink, documentation, R and pre-commit. The R setup's package-retrieval stall cleared on one fresh-run retry; no source changes were needed.

Did this PR include necessary documentation updates?

Yes. The SQL spatial serialization guide documents the little-endian SRID + ISO WKB contract, layout behavior, and migration from released compact geometry and big-endian geography UDT files. It distinguishes SQL UDT payloads from standalone WKB/EWKB, unchanged RDD/index formats, and Spark 4.2 native values. Scala installation instructions link to the guide.

@jiayuasu jiayuasu changed the title [GH-3124] Use public EWKB for SQL geometry transport [GH-3124] Use SRID-prefixed ISO WKB for SQL geometry transport Nov 9, 2026
@jiayuasu jiayuasu changed the title [GH-3124] Use SRID-prefixed ISO WKB for SQL geometry transport [GH-3124] Use SRID-prefixed ISO WKB for SQL spatial transport Nov 9, 2026
@jiayuasu
jiayuasu marked this pull request as ready for review November 10, 2026 07:22
@jiayuasu
jiayuasu merged commit 553fe1b into apache:master Nov 10, 2026
47 of 48 checks passed
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.

Migrate Sedona's GeometryUDT / Geography UDT to Spark 4.2 native Geometry and Geography data type

1 participant