Repository navigation
[GH-3146] Write row group statistics for geography columns in GeoParquet 2.0 files - #3582
Merged
jiayuasu merged 3 commits intoNov 25, 2026
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Did you read the Contributor Guide?
Is this PR related to a ticket?
[GH-XXX] my subject. Part of Update GeoParquet writer and reader to support GeoParquet 2.0.0 #3146. It follows [GH-3146] Store geography columns in the GEOGRAPHY logical type in GeoParquet 2.0 files #3578, which writesGEOGRAPHYcolumns.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_statisticsof 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
GEOMETRYcolumn and not for aGEOGRAPHYcolumn.GeospatialStatistics.newBuildersays "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
GeographyBoundscomputes 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:xminis above itsxmaxwhen 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.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:
WKBReadergoes by the sign of the turning angle, and the shape thatST_Intersectsworks 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.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:
prepareForWriteof the write support is called at the start of every row group. Sedona starts a new box there.finalizeWriteis 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 publicColumnChunkMetaData.get.The list is
ParquetFileWriter.blocks, a private field. Sedona reads it, andParquetRecordWriter.internalWriterandInternalParquetRecordWriter.parquetFileWriteron 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.enabledandparquet.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:
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
geometadata has nobboxfor a geography column, as before.How was this patch tested?
New
GeoParquetGeographyStatisticsSuitewrites files and reads the statistics of their row groups from the footer:parquet.column.statistics.enabled#<column_name>set tofalse, to values that are no booleans, and with the name of a nested field that a column at the root has tooST_Intersectsand the statistics have to agree on: a ring along a great circle in both directions, and a ring without its closing vertexNew
GeographyBoundsTestcovers 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.pyandtests/geopandas/test_io.pywith the shaded jar of this branch on Spark 4.2 and 4.1 (127 passed on each):spark/common, withGeographyWkbScannerTest,GeographyBoundsTestand the new suitespark/spark-4.2Checked 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?
docs/tutorial/files/geoparquet-sedona-spark.mdand its Chinese version describe the statistics. No option is added.