ST_Force4D returns wrong Z/M values under concurrent execution (static state in GeometryForce4DTransformer)
Expected behavior
ST_Force4D keeps the Z and M values a geometry already has and fills only the missing ones, regardless of what other threads are doing:
LINESTRING Z(0 1 3, 1 0 3, 2 0 3)with z=5, m=7 →LINESTRING ZM(0 1 3 7, 1 0 3 7, 2 0 3 7)LINESTRING M(0 1 4, 1 0 4, 2 0 4)with z=5, m=7 →LINESTRING ZM(0 1 5 4, 1 0 5 4, 2 0 5 4)
Actual behavior
GeometryForce4DTransformer stores hasZ and hasM in private static fields that are set in transform() and read later in transformCoordinates(). When two threads process geometries with different dimensions, one overwrites the other's flags. Results are silently wrong, with no exception. Observed outputs include:
LINESTRING ZM(0 1 5 NaN, 1 0 5 NaN, 2 0 3 7)LINESTRING ZM(0 1 5 4, 1 0 NaN 7, 2 0 NaN 7)LINESTRING Z(0 1 5, 1 0 5, 2 0 5)(not 4D at all)
With 8 threads alternating between the two inputs above, 33,689 of 1,600,000 calls returned a wrong result. This affects any Spark executor running more than one task, when a column mixes XYZ and XYM geometries.
The same whole-geometry flags also give wrong results single-threaded for collections with mixed dimensions:
ST_Force4D(GEOMETRYCOLLECTION (POINT Z(1 2 3), POINT M(4 5 6), POINT ZM(7 8 9 10), POINT (11 12)), 20, 30)
actual: GEOMETRYCOLLECTION ZM(POINT ZM(1 2 3 30), POINT ZM(4 5 NaN 30), POINT ZM(7 8 9 30), POINT ZM(11 12 NaN 30))
expected: GEOMETRYCOLLECTION ZM(POINT ZM(1 2 3 30), POINT ZM(4 5 20 6), POINT ZM(7 8 9 10), POINT ZM(11 12 20 30))
The existing M values 6 and 10 are lost, and Z becomes NaN instead of the supplied default.
Related finding in the sibling class: ST_Force3DM returns XYZM input unchanged instead of dropping Z (POINT ZM(1 2 3 4) stays POINT ZM(1 2 3 4), expected POINT M(1 2 4)), and on a mixed-dimension collection it overwrites existing M values with the default. Both come from the same whole-geometry hasM check, so I would fix them together.
Steps to reproduce
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.sedona.common.Constructors;
import org.apache.sedona.common.Functions;
import org.locationtech.jts.geom.Geometry;
public class Force4DRace {
public static void main(String[] args) throws Exception {
Geometry lineZ = Constructors.geomFromWKT("LINESTRING Z(0 1 3, 1 0 3, 2 0 3)", 0);
Geometry lineM = Constructors.geomFromWKT("LINESTRING M(0 1 4, 1 0 4, 2 0 4)", 0);
String expectedZ = "LINESTRING ZM(0 1 3 7, 1 0 3 7, 2 0 3 7)";
String expectedM = "LINESTRING ZM(0 1 5 4, 1 0 5 4, 2 0 5 4)";
ExecutorService pool = Executors.newFixedThreadPool(8);
List<Future<Integer>> futures = new ArrayList<>();
for (int t = 0; t < 8; t++) {
boolean useZ = t % 2 == 0;
futures.add(
pool.submit(
() -> {
int mismatches = 0;
for (int i = 0; i < 20000; i++) {
String actual = Functions.asWKT(Functions.force4D(useZ ? lineZ : lineM, 5, 7));
if (!actual.equals(useZ ? expectedZ : expectedM)) mismatches++;
}
return mismatches;
}));
}
int mismatches = 0;
for (Future<Integer> f : futures) mismatches += f.get();
pool.shutdown();
System.out.println(mismatches); // expected 0, prints thousands
}
}
On my machine this printed 6,692 mismatches out of 160,000 calls.
Sedona version
master (2.0.0-SNAPSHOT, commit 27027a98b51)
Apache Spark version
Not Spark-specific. The bug is in sedona-common, so it affects the Spark, Flink and Snowflake wrappers of ST_Force4D.
Apache Flink version
Not Flink-specific (see above).
API type
Java (sedona-common)
Scala version
Not applicable (reproduced directly against sedona-common).
JRE version
17
Python version
Not applicable.
Environment
Reproduced on Windows 11 with OpenJDK 17, standalone JVM (no cluster).
Scope update: following review on #3549, this issue now tracks only the ST_Force4D concurrency bug (the static hasZ/hasM fields), and #3549 fixes exactly that.
The single-threaded problems also described above (mixed-dimension geometry collections, and ST_Force3DM leaving XYZM input unchanged) have been moved to #3551 and will be fixed in a separate PR.