Skip to content

[GH-3146] Write row group statistics for geography columns in GeoParquet 2.0 files - #3582

Merged
jiayuasu merged 3 commits into
apache:masterfrom
jiayuasu:feature/geoparquet-2.0-geography-statistics
Nov 25, 2026
Merged

jiayuasu merged 3 commits into
apache:masterfrom
jiayuasu:feature/geoparquet-2.0-geography-statistics

Conversation

@jiayuasu

@jiayuasu jiayuasu commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Did you read the Contributor Guide?

Is this PR related to a ticket?

What changes were proposed in this PR?

The row groups of a geography column of a GeoParquet 2 file get geospatial statistics: a bounding box and the geospatial types, in the geospatial_statistics of the Parquet format. Before this PR such a column had none, on Spark 4.1 and 4.2 alike.

parquet-java collects these statistics for a GEOMETRY column and not for a GEOGRAPHY column. GeospatialStatistics.newBuilder says "For Geography type, we have not implemented the algorithm yet", in 1.16.0 and in 1.17.0. It also drops statistics that a writer hands to it for such a column: the statistics object of the column chunk has no box and no types, and merging into it is a no-op. So Sedona computes the statistics and puts them into the file itself.

The bounding box

GeographyBounds computes the box of the geographies of a row group in degrees of longitude and latitude, as the Parquet format defines it for edges on the sphere:

  • It has the edges of a geography and not only its vertices. The edge between two points on the 50th parallel that are 90 degrees apart reaches 59.3 degrees north, and so does the box.
  • It reaches a pole, with all longitudes, for a polygon around that pole.
  • Its xmin is above its xmax when it crosses the antimeridian. A point on the antimeridian is in the box as longitude 180 and as -180, and so are points on both of them in one row group.
  • It has no bounds for Z and M.

A point is added from its coordinates, without an object for it, and a row group of points has the box of their coordinates as they are written. A line and the first ring of a polygon give their vertices to the edge bounder of S2 (S2EdgeUtil.RectBounder). Where S2 has computed a bound, the box is 1e-14 radians larger on every side, which covers the rounding between degrees and radians.

The inside of a ring is its smaller side, as in Sedona's reading of a polygon into S2. With edges over less than half of all longitudes that side has no pole and lies within the bounds of the edges. Only a ring that spans half of all longitudes or more becomes a loop of S2, which tells which side is the inside and whether a pole is in it.

Three cases get their own rule:

  • A ring that halves the sphere, such as one along a great circle, has two sides of one size. Sedona does not read its inside the same way everywhere: WKBReader goes by the sign of the turning angle, and the shape that ST_Intersects works on goes by S2's rule for a loop that is a hemisphere. The box of such a ring is the whole sphere, which has both sides.
  • A ring whose last vertex is not its first still has the edge between the two. Sedona's own writer of the WKB of a geography leaves the last vertex of a ring out.
  • A coordinate that is not a number is left out by itself, as the Parquet format has it: POINT (75 NaN) gives its longitude to the box. Without a longitude or without a latitude in a row group there is no box.

A geography that cannot be read leaves its row group without a box, and the types are still written.

Putting the statistics into the file

parquet-java has no public way to give a column chunk its geospatial statistics. Sedona uses the order in which the Parquet writer works:

  1. prepareForWrite of the write support is called at the start of every row group. Sedona starts a new box there.
  2. finalizeWrite is called after the last row group is flushed and before the footer is written. At that point Sedona replaces the row groups in the list that the footer is written from by copies whose geography column chunks have the statistics. A copy is made with the public ColumnChunkMetaData.get.

The list is ParquetFileWriter.blocks, a private field. Sedona reads it, and ParquetRecordWriter.internalWriter and InternalParquetRecordWriter.parquetFileWriter on the way to it, by reflection, once per file. The three fields have these names in parquet-java 1.16.0 and 1.17.0, the versions of Spark 4.1 and 4.2. If one of them is not there, or the row groups do not have the row counts that Sedona counted, Sedona logs one warning and the file is written as before, without these statistics.

To have the Parquet writer of a file at hand, a GeoParquet 2 write with a geography column at the root of the schema now goes through FirstGeometryOutputWriter, which opens the file at its first row when it has no SRID to look up.

The Parquet options parquet.column.statistics.enabled and parquet.column.statistics.enabled#<column_name> turn the statistics off, as they do for the statistics of parquet-java, and are read as parquet-java reads them: the name after # is the path of a column with a dot between its fields, so it is the key of a column at the root only for a name without a dot, and a value that is no boolean leaves the statistics on. An encrypted file gets none.

Cost

Measured in the write support alone, without the encoding of the Parquet library, on Spark 4.1:

value without statistics with statistics
point 44 ns 60 ns
line of 2 vertices 54 ns 214 ns
polygon of 16 vertices 99 ns 777 ns
polygon of 256 vertices 1.2 µs 10.2 µs
line of 4096 vertices 14.1 µs 140.1 µs

That is 30 to 50 ns for a vertex of a line or a polygon. A point costs the same within the noise of the measurement.

Not in this PR

  • Sedona's reader does not use the statistics of row groups yet, for geometry columns or for geography columns.
  • The geo metadata has no bbox for a geography column, as before.

How was this patch tested?

New GeoParquetGeographyStatisticsSuite writes files and reads the statistics of their row groups from the footer:

  • points, in their own coordinates; a line whose edge goes north of its end points; a polygon around the pole; a line and points across the antimeridian; a point on the antimeridian; points on longitude 180 and on -180 in one row group with a line elsewhere
  • collections and multi geographies; a Z coordinate; empty geographies; a row group of nulls
  • several row groups in one file, each with the bounds of its own rows
  • random lines and polygons, whose vertices are all in the box
  • a geometry column next to the geography column keeps the statistics of parquet-java
  • parquet.column.statistics.enabled#<column_name> set to false, to values that are no booleans, and with the name of a nested field that a column at the root has too
  • polygons whose inside ST_Intersects and the statistics have to agree on: a ring along a great circle in both directions, and a ring without its closing vertex
  • a writer whose row groups cannot be reached

New GeographyBoundsTest covers the box by itself, and compares it with the bounds that Sedona's reading into S2 gives, for 300 random lines and 600 random polygons of up to 80 degrees in radius, in both directions of the ring. It also covers rings that halve the sphere, rings without their closing vertex, and coordinates that are not numbers, and checks for 2000 random sets of points and lines, with points on the antimeridian and at the poles, that every point and vertex is in the box.

Run locally with the other GeoParquet suites, and the Python tests tests/sql/test_geoparquet.py, tests/sql/test_geography.py, tests/sql/test_structured_adapter.py and tests/geopandas/test_io.py with the shaded jar of this branch on Spark 4.2 and 4.1 (127 passed on each):

Spark 4.2 Spark 4.1 Spark 3.5
GeoParquet suites of spark/common, with GeographyWkbScannerTest, GeographyBoundsTest and the new suite 325 313 171
suites of spark/spark-4.2 37

Checked by hand with PySpark on Spark 4.1 and 4.2: PyArrow 26 shows the statistics of each row group in geo_statistics.

Did this PR include necessary documentation updates?

  • Yes, I have updated the documentation. The section on writing GeoParquet 2.0 in docs/tutorial/files/geoparquet-sedona-spark.md and its Chinese version describe the statistics. No option is added.

…eoParquet 2.0 files

The Parquet library collects geospatial statistics for a GEOMETRY column and
has no algorithm for a GEOGRAPHY column, and it drops statistics that a
writer gives it for one. Sedona computes the bounding box and the types of
the geographies of each row group itself, with the edges on the sphere, and
puts them into the row groups before the footer of the file is written.
… predicates match

A ring that halves the sphere gets the box of the whole sphere: Sedona does
not read the inside of such a ring the same way everywhere. A ring without
its closing vertex keeps the edge from its last vertex to its first. A
coordinate that is not a number is left out by itself. The switch of the
statistics of a column is read as the Parquet library reads it. A wide ring
becomes a loop of S2, and no reader holds on to the WKB of a geography.
… the box of a geography row group

A row group with a point on longitude 180, a point on longitude -180 and no
other point had the longitudes of its points from 180 eastwards to -180. S2
reads the interval from pi to -pi as one without a longitude, so the box of
the row group was the box of its lines and polygons alone, and the two points
were outside of it. The interval is now the antimeridian.
@jiayuasu jiayuasu added this to the sedona-2.0.0 milestone Nov 25, 2026
@jiayuasu
jiayuasu marked this pull request as ready for review November 25, 2026 21:51
@jiayuasu
jiayuasu merged commit 1288854 into apache:master Nov 25, 2026
43 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.

1 participant