Where Did the Elevation Go? A Race in Apache Sedona's ST_Force4D
Give Apache Sedona's ST_Force4D a 3D line whose three points all sit at elevation 3, with 5 as the default for a missing elevation and 7 for a missing measure. It should keep the 3s and add the 7s. Sometimes it returned the line with every elevation set to 5.
There was no exception, just a valid geometry with the wrong numbers in it. It happens when Spark runs several tasks at once on geometries that differ in whether they have Z or M. In test runs on a column mixing 2D and 3D lines, about 1 in 100 rows came back wrong in one way or another. Every release from 1.6.1, where the function was added, through 1.9.1, the latest as of November 2026, has the bug. The fix is merged into Sedona's main branch but is not in a release yet.
What ST_Force4D promises
Sedona adds spatial types and functions to Spark and Flink. ST_Force4D gives every point of a geometry four values: X, Y, Z (usually elevation) and M (a measure, such as distance along a route). Values the geometry already has are kept, and missing ones are filled with the defaults you pass.
In WKT, LINESTRING Z (0 1 3, 1 0 3, 2 0 3) is a line with three points at elevation 3, and ST_Force4D with defaults 5 and 7 should return LINESTRING ZM(0 1 3 7, 1 0 3 7, 2 0 3 7). A 2D line, LINESTRING (0 1, 1 0, 2 0), should come back as LINESTRING ZM(0 1 5 7, 1 0 5 7, 2 0 5 7).
Wrong only when threads overlap
To see how often it goes wrong, the same Spark query ran over a million rows, three times per dataset, on Sedona 1.9.1 and on a build of Sedona's main branch with the fix. Each result was compared with what the same input gives on its own.
| Data, 1,000,000 rows per run | Wrong rows on 1.9.1 | 3D rows with a real Z replaced | Wrong rows with the fix |
|---|---|---|---|
| XY and XYZ lines, half each | 6,419 to 11,844 | 3,363 to 5,820 of 500,000 | 0 |
| XYZ and XYM lines, half each | 14,830 to 19,252 | 6,507 to 8,628 of 500,000 | 0 |
| XYZ lines only | 0 | 0 | 0 |
In a column mixing 2D and 3D lines, 0.6 to 1.2 percent of rows came back wrong. Mixing XYZ and XYM lines was worse, 1.5 to 1.9 percent, and there real M values were overwritten too. A column of 3D lines only had no wrong rows, and neither did any run with the fix.
Who is affected:
- Every API. Spark SQL, the Scala and Python DataFrame APIs, Flink SQL and direct Java calls all go through the same Java code, with or without the default arguments.
- Parallel tasks in one JVM. In Spark, that is an executor that runs more than one task at a time:
local[N]orlocal[*]with more than one thread, orspark.executor.coresabove 1. Only Spark was measured; Flink runs the same code. - Mixed inputs anywhere in the JVM. Two calls have to overlap on inputs that differ in whether they have Z or M. They do not have to come from the same column or the same query.
- Until a release has the fix, build Sedona from its main branch, or run affected jobs with one task per executor at a time.
Two flags for the whole JVM
Here is the class behind ST_Force4D before the fix, shortened:
public class GeometryForce4DTransformer extends GeometryTransformer {
private static boolean hasZ;
private static boolean hasM;
private final double mValue;
private final double zValue;
@Override
protected CoordinateSequence transformCoordinates(CoordinateSequence coords, Geometry parent) {
CoordinateXYZM[] newCoords = new CoordinateXYZM[coords.size()];
for (int i = 0; i < coords.size(); i++) {
Coordinate coordinate = coords.getCoordinate(i);
newCoords[i] =
new CoordinateXYZM(
coordinate.getX(),
coordinate.getY(),
hasZ ? coordinate.getZ() : zValue,
hasM ? coordinate.getM() : mValue);
}
return createCoordinateSequence(newCoords);
}
public static Geometry transform(Geometry geometry, double zValue, double mValue) {
if (geometry.getCoordinates().length == 0) return geometry;
hasZ = Functions.hasZ(geometry);
hasM = Functions.hasM(geometry);
if (hasZ && hasM) return geometry;
GeometryForce4DTransformer transformer = new GeometryForce4DTransformer(zValue, mValue);
return transformer.transform(geometry);
}
}
transform records whether the input has Z and M, then walks the geometry, and transformCoordinates reads both flags again at every point.
The word that matters is static. There is one hasZ and one hasM for the whole JVM, shared by every thread. In a column of 2D and 3D lines, a 3D line sets hasZ to true and a 2D line sets it to false. With eight tasks running, a line can read a value that another thread has just written.
The flag can change between two points of the same line, so a line can be partly right. Each wrong result follows from what a line read at its three points:
- A 3D line that reads false, false, false gets 5, 5, 5: every real elevation is replaced.
- A 3D line that reads true, true, false gets 3, 3, 5, as in the figure.
- A 2D line that reads true at its last point copies a Z it does not have, which is
NaN:LINESTRING ZM(0 1 5 7, 1 0 5 7, 2 0 NaN 7). - A 2D line that reads true at every point gets
NaNeverywhere, and the WKT output drops Z altogether:LINESTRING M(0 1 7, 1 0 7, 2 0 7).
With XYZ and XYM lines, both flags change, so real M values were replaced too. The early return, if (hasZ && hasM) return geometry;, can also see both flags true at once, one set by each kind of line, and skip the conversion.
One field per call
transform already created a new transformer for each geometry. The fix keeps the flags on that object, so each call has its own:
- private static boolean hasZ;
- private static boolean hasM;
+ private boolean hasZ;
+ private boolean hasM;
...
public static Geometry transform(Geometry geometry, double zValue, double mValue) {
if (geometry.getCoordinates().length == 0) return geometry;
- hasZ = Functions.hasZ(geometry);
- hasM = Functions.hasM(geometry);
- if (hasZ && hasM) return geometry;
-
GeometryForce4DTransformer transformer = new GeometryForce4DTransformer(zValue, mValue);
+ transformer.hasZ = Functions.hasZ(geometry);
+ transformer.hasM = Functions.hasM(geometry);
+ if (transformer.hasZ && transformer.hasM) return geometry;
+
return transformer.transform(geometry);
}
The pull request also adds a test in which eight threads convert an XYZ line and an XYM line 160,000 times in total, and every result must be correct. Before the fix, that workload produced 3,772 to 9,066 wrong results in each of five runs. With the fix, it produced none.
Review made it smaller
The first version of the pull request did more. It also fixed problems that show up on a single thread, such as existing M values being overwritten in collections that mix dimensions. Jia Yu, reviewing it, proposed the smaller fix above, compiled it, ran the new test against it, and asked to keep the pull request to the race. The narrowed fix, apache/sedona#3549, was merged on November 16, a day after the issue, #3548, was filed. The rest went into #3551 and #3552, merged on November 18.
The point
- A race on a shared flag does not throw. It writes valid geometries with wrong numbers, and the worst of them, a 3D line with every elevation replaced, looks exactly like the correct answer for a 2D line.
- A static field in code that runs on executors is shared by every task there. Keep state that belongs to one call on an object made for that call.
- A test for this kind of bug has to run threads at the same time on inputs that differ, and check every result.
Measured on Apache Sedona 1.9.1 and on Sedona's main branch at eb3bd6a75b0, with Spark 3.5.9 in local mode with eight task threads, one million rows and three runs per dataset. A test harness flagged ST_Force4D. The investigation, the code and the measurements were done with an AI coding assistant (Claude Code), and I reviewed the changes and filed the issue.