Skip to content

[GH-3146] Take the CRS of a GeometryUDT column from its first geometry - #3559

Merged
jiayuasu merged 5 commits into
apache:masterfrom
jiayuasu:feature/geoparquet-2.0-write-udt
Nov 21, 2026
Merged

jiayuasu merged 5 commits into
apache:masterfrom
jiayuasu:feature/geoparquet-2.0-write-udt

Conversation

@jiayuasu

@jiayuasu jiayuasu commented Nov 18, 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?

GeoParquet 2.0 states the CRS of a geometry column in the Parquet schema, which the Parquet writer needs before the first row. A native Spark geometry type (Spark 4.2) names its SRID, so the writer knows the CRS up front. A GeometryUDT column, the geometry type on Spark 4.1, has an SRID on each geometry only. After #3553 a 2.x write of such a column therefore fails unless geoparquet.crs or geoparquet.crs.<column_name> is set.

With this PR the writer takes the CRS from the data when no CRS option covers the column:

  • A new FirstGeometryOutputWriter holds the first rows of each file back until every such column has shown a non-null geometry. It then opens the Parquet file with the SRIDs of those geometries as the CRS of the columns and writes the held rows.
  • The write fails if a later geometry of the file has another SRID, because a 2.x column has a single CRS. The message names the column, both SRIDs and the CRS options.
  • A column whose first geometry has SRID 0, or that has no geometry in the file, is written with an unknown CRS (srid:0, and null in the geo metadata).
  • The rows held back are limited to 16 MB per file; geoparquet.srid.lookahead.bytes changes the limit. The key is not under geoparquet.crs., where every longer key names a column. A column that has shown no geometry by then is written with an unknown CRS, and a later geometry with an SRID fails the write.
  • The row that ends the wait, because it shows the last geometry or reaches the limit, is written as it is. Only the rows before it are kept as copies, so the rows held back stay under the limit however large that row is.
  • A row held back is kept as an UnsafeRow of its own. Spark's write paths hand over UnsafeRows, which are copied as before. A caller of the writer that hands over another kind of row, such as a GenericInternalRow that it uses again for the next row, gets the row converted first, so the copy shares no values with the caller and its size counts against the limit.
  • If a row held back fails when the file is opened at the end of the write, with a second SRID for example, the writer closes the Parquet file. Spark does not close a writer again after its close() has failed.
  • The keys of the GeoParquet write options are taken in any case, as Spark takes the keys of write options without regard to case: geoparquet.version, geoparquet.crs, geoparquet.crs.<column_name>, geoparquet.covering, geoparquet.covering.<column_name>, geoparquet.covering.mode and geoparquet.srid.lookahead.bytes. Before, a key in another case, GEOPARQUET.CRS for example, was ignored without a message. A 2.x write then failed for the missing CRS, and with this PR it would have taken the CRS from the data instead of the option. prepareWrite now sets each option of the write in the Hadoop configuration under the key that the writer reads, so an option of the write also replaces a value that the Hadoop configuration has for the same key in another case. This applies to 1.x writes too.
  • A key that ends in a column name is for the geometry column with that name. A key that names a geometry column in the case of the column goes to that column before anything else: geoparquet.covering.MODE is the covering of a column MODE as before, not the covering mode in upper case. If no geometry column has the name in that case and the key is no other option either, the key is for the column whose name differs in case only. With spark.sql.caseSensitive=true a DataFrame can have several of those, and the write then fails with a message that names the key and the columns.
  • A key that is set in the Hadoop configuration only, not as an option of the write, is read in its exact case as before, the column name in it included.

What stays the same:

  • geoparquet.crs and geoparquet.crs.<column_name> state the CRS as given, without a check against the geometries, and no rows are held back for a column they cover.
  • Native Spark geometry columns, which already name their SRID.
  • GeoParquet 1.x writes. Their CRS is in the geo metadata, which is written after the last row.

Not in this PR: the default version (still 1.1.0), geography columns, and reading on Spark 3.5/4.0.

How was this patch tested?

GeoParquetNativeTypesSuite has new tests for a 2.x write of GeometryUDT columns without a CRS option: the CRS comes from the first geometry after null rows, each of two geometry columns gets the CRS of its own first geometry, a second SRID in a column fails the write, a column without geometries is written with an unknown CRS, the writer stops waiting when the rows held back reach geoparquet.srid.lookahead.bytes, and geoparquet.crs is written as given without a check against the geometries. Further tests cover option keys in another case (geoparquet.crs.<column_name> for 2.x and 1.x, GEOPARQUET.CRS, GeoParquet.Version), two columns that differ in case only where the option of one must not reach the other, a key that fits both of them in a case that neither has, an option of the write against a value in the Hadoop configuration, geoparquet.covering.MODE for a geometry column MODE and the covering mode under a key in another case (both in geoparquetIOTests), a column named lookahead.bytes with its own CRS option, and the Parquet writer being released when a row held back fails at the end of the file. Three tests call the writer of a write task directly: the row that ends the wait reaches the Parquet writer as the same object, a GenericInternalRow that the caller changes after the write is read back with the values it had, and an 8 KiB GenericInternalRow reaches a limit of 4 KiB. The existing test that took the CRS from the SRID of a native column now runs for GeometryUDT columns as well.

The GeoParquet suites pass locally on Spark 4.1 / Scala 2.13, Spark 3.5 / Scala 2.12 and Spark 4.2 / Scala 2.13 (including the GeoParquet suites of the spark-4.2 module).

Did this PR include necessary documentation updates?

  • Yes, I have updated the documentation. The GeoParquet tutorial describes how the CRS is taken from the data, and the Parameter page has a new section that lists the GeoParquet write options, geoparquet.srid.lookahead.bytes among them, with the rules for the case of their keys.

@jiayuasu
jiayuasu force-pushed the feature/geoparquet-2.0-write-udt branch 4 times, most recently from 248ccb0 to 9ec89b5 Compare November 19, 2026 07:16
@jiayuasu
jiayuasu marked this pull request as ready for review November 19, 2026 07:16
…eometry

GeoParquet 2.0 states the CRS of a geometry column in the Parquet
schema, which the Parquet writer needs before the first row. A
GeometryUDT column, the geometry type on Spark 4.1 and older, has an
SRID on each geometry only, so a 2.x write of such a column required
geoparquet.crs.

A 2.x write now holds rows back until every such column has shown a
geometry, opens the file with those SRIDs as the CRS of the columns,
and fails if a later geometry of the file has another SRID. A column
with SRID 0, or without any geometry in the file, is written with an
unknown CRS.

The rows held back are limited to 16 MB per file, which
geoparquet.crs.lookahead.bytes changes. A column that has shown no
geometry by then is written with an unknown CRS as well.

geoparquet.crs and geoparquet.crs.<column> still state the CRS as
given, without a check against the geometries. GeoParquet 1.x writes
are unchanged.
@jiayuasu
jiayuasu force-pushed the feature/geoparquet-2.0-write-udt branch from 9ec89b5 to 37965e7 Compare November 20, 2026 16:58
…row held back fails, and write the row that ends the wait without a copy
… one column each, and hold back rows of any kind as copies of their own
…ritten to that column, before any other option that the key is in another case
@jiayuasu jiayuasu added this to the sedona-2.0.0 milestone Nov 21, 2026
@jiayuasu
jiayuasu merged commit 4b3f5e2 into apache:master Nov 21, 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