Repository navigation
Description
Expected behavior
The SRID + ISO WKB payload that #3528 introduced for the geometry type costs about as much to write and read as the 1.x format did, so queries on Spark 3.5, 4.0 and 4.1 run about as fast as on 1.9.1.
Actual behavior
On master (288ce23), queries that move geometries through the UDT run 9-38% slower than on 1.9.1:
Spark 3.5.10, local[8], 500,000 33-vertex polygons and 2,000,000 points |
1.9.1 | master | master vs 1.9.1 |
|---|---|---|---|
sum(ST_Area(geom)) on a cached table |
0.234 s | 0.288 s | 1.28x |
sum(ST_Area(ST_Buffer(geom, 0.001))) |
0.956 s | 1.040 s | 1.09x |
ST_Translate, then write GeoParquet |
0.740 s | 0.994 s | 1.36x |
read that GeoParquet, sum(ST_Area) |
0.413 s | 0.560 s | 1.38x |
points-in-polygons ST_Intersects join, count(*) |
2.445 s | 2.716 s | 1.16x |
(Median over six fresh JVMs per version in rotating order, five timed runs each after a warmup. Results were identical across versions. I measured Spark 3.5; Spark 4.0 and 4.1 use the same serializer.)
The cost is in how GeometrySerializer produces and reads the bytes, not in the format. Each value is written through the patched JTS WKBWriter, which writes every double through a stream into a ByteArrayOutputStream and copies the result. PublicWkbHeaderAdapter.toIsoWkb then walks the bytes again (and copies them again for Z or M), and a ByteBuffer copies them once more to prefix the SRID. Reading copies the payload and creates a new WKBReader per value. #3528 measured the encoding cost against the 1.x format already: 7.95x for a point, 27.9x for a 256-vertex polygon.
Proposal
Keep the format and the JTS reader, and write and read the bytes directly. I have a branch that does this:
- Writing: a small codec writes exactly the bytes the patched
WKBWriterandtoIsoWkbproduce, once, into an array of the exact size. It follows the writer's rules: the Z/M union, multipart members sharing the parent's layout, collection members keeping their own, empties, and canonical NaN bits.validateLayoutstill runs first, so errors don't change. - Reading: it reads the little-endian ISO payloads it writes when nothing in them is empty. Everything else (big-endian, EWKB, empties, short lines, unclosed rings, malformed input) still goes to the existing
WKBReaderpath unchanged.
A test suite keeps the current implementation as a reference. It checks for the same bytes or the same exception on a seeded random corpus (every type and layout, empties, nested collections, plain/declared/packed JTS sequences, NaN payloads, signed SRIDs). It also checks for the same geometry or the same exception on the payloads and on big-endian, EWKB, truncated, extended and corrupted variants. Locally, a larger version of that comparison ran 8 million random geometries and about 65 million decodes with no differences.
With the branch, the same queries are back at 1.9.1 speed:
| 1.9.1 | master | branch | branch vs 1.9.1 | |
|---|---|---|---|---|
sum(ST_Area), cached |
0.234 s | 0.288 s | 0.247 s | 1.09x |
ST_Buffer, then area |
0.956 s | 1.040 s | 0.972 s | 1.01x |
ST_Translate, write GeoParquet |
0.740 s | 0.994 s | 0.700 s | 0.95x |
| read GeoParquet, area | 0.413 s | 0.560 s | 0.419 s | 1.02x |
| join, count | 2.445 s | 2.716 s | 2.431 s | 1.00x |
Per value (JMH, JDK 17), in ns:
| master | branch | ||
|---|---|---|---|
| encode point | 42 | 10 | 4.1x faster |
| encode polygon, 256 vertices | 6,544 | 907 | 7.2x |
| encode line, 1,024 vertices | 26,986 | 4,020 | 6.7x |
| decode point | 74 | 18 | 4.2x |
| decode polygon, 256 vertices | 6,782 | 1,310 | 5.2x |
| decode line, 1,024 vertices | 26,506 | 5,391 | 4.9x |
Encoding allocates only the output array, against about six times that size now.
Since #3528 chose to go through public JTS code on purpose, I'd like to check first: would you take a PR that adds this Sedona-owned codec for the same public format? If so, I'll open it against this issue. The GeoParquet writer and GeographySerializer use the same writer-plus-adapter pattern and could follow separately.
Steps to reproduce
# pyspark 3.5.10 with Sedona 1.9.1 (sedona-spark-shaded-3.5_2.12:1.9.1) or master, local[8], 4 GB driver,
# spark.sql.extensions=org.apache.sedona.sql.SedonaSqlExtensions, Kryo with SedonaKryoRegistrator.
polys = spark.sql("""
SELECT id, ST_Buffer(ST_Point(x, y), 0.03) AS geom
FROM (SELECT id, (id % 1000) * 0.1 AS x, CAST(id DIV 1000 AS DOUBLE) * 0.1 AS y FROM range(500000))""")
polys.cache().createOrReplaceTempView("polys_c"); polys.count()
spark.sql("SELECT sum(ST_Area(geom)) FROM polys_c").collect() # time this after a warmup runSedona version
2.0.0-SNAPSHOT (master 288ce23), compared with 1.9.1
Apache Spark version
3.5.10
API type
SQL
Scala version
2.12
JRE version
17
Python version
3.12
Environment
Standalone (local[8] in Docker)
Existing issues
- I searched the existing issues and did not find a duplicate.
Activity