Writing

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.

ST_Force4D(LINESTRING Z (0 1 3, 1 0 3, 2 0 3), 5, 7) Correct default Z = 5 3 3 3 point 1 point 2 point 3 ZM(0 1 3 7, 1 0 3 7, 2 0 3 7) Wrong: all replaced 5 5 5 point 1 point 2 point 3 ZM(0 1 5 7, 1 0 5 7, 2 0 5 7) Wrong: last replaced 3 3 5 point 1 point 2 point 3 ZM(0 1 3 7, 1 0 3 7, 2 0 5 7)
Same input, same defaults. The two wrong results were seen on Sedona 1.9.1.

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.

Sedona 1.9.1 main, with the fix XY + XYZ lines half each Sedona 1.9.1, run 1: 11,844 wrong rows Sedona 1.9.1, run 2: 6,534 wrong rows Sedona 1.9.1, run 3: 6,419 wrong rows 6,419–11,844 main with the fix, run 1: 0 wrong rows main with the fix, run 2: 0 wrong rows main with the fix, run 3: 0 wrong rows 0 XYZ + XYM lines half each Sedona 1.9.1, run 1: 19,252 wrong rows Sedona 1.9.1, run 2: 14,830 wrong rows Sedona 1.9.1, run 3: 14,963 wrong rows 14,830–19,252 main with the fix, run 1: 0 wrong rows main with the fix, run 2: 0 wrong rows main with the fix, run 3: 0 wrong rows 0 XYZ lines only control Sedona 1.9.1, run 1: 0 wrong rows Sedona 1.9.1, run 2: 0 wrong rows Sedona 1.9.1, run 3: 0 wrong rows 0 in all three runs main with the fix, run 1: 0 wrong rows main with the fix, run 2: 0 wrong rows main with the fix, run 3: 0 wrong rows 0 0 5,000 10,000 15,000 20,000 Wrong rows per million, one dot per run
Spark 3.5.9 in local mode with eight task threads, one million rows per run.
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] or local[*] with more than one thread, or spark.executor.cores above 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.

time → Thread A converts a 3D line static hasZ one copy, all threads Thread B converts a 2D line true false sets true point 1 true → 3 point 2 true → 3 sets false point 3 false → 5 Thread A returns ZM(0 1 3 7, 1 0 3 7, 2 0 5 7): its last elevation, 3, became 5.

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 NaN everywhere, 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.