Skip to content

Commit e2a8d63

Browse files
authored
[GH-1761] error when invalid ST_Subdivide maxVertices argument (#1926)
1 parent ce3c8dd commit e2a8d63

7 files changed

Lines changed: 32 additions & 1 deletion

File tree

common/src/main/java/org/apache/sedona/common/Functions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,9 @@ public static Geometry createMultiGeometryFromOneElement(Geometry geometry) {
18661866
}
18671867

18681868
public static Geometry[] subDivide(Geometry geometry, int maxVertices) {
1869+
if (maxVertices < 5) {
1870+
throw new IllegalArgumentException("ST_Subdivide needs 5 or more max vertices");
1871+
}
18691872
return GeometrySubDivider.subDivide(geometry, maxVertices);
18701873
}
18711874

common/src/test/java/org/apache/sedona/common/FunctionsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4420,4 +4420,12 @@ public void emptyPoint() throws ParseException {
44204420
assertFalse(Functions.hasM(emptyPoint));
44214421
assertFalse(Functions.hasZ(emptyPoint));
44224422
}
4423+
4424+
@Test
4425+
public void subdivideInvalidMaxVertices() {
4426+
LineString lineString = GEOMETRY_FACTORY.createLineString(coordArray(0, 0, 99, 99));
4427+
IllegalArgumentException e =
4428+
assertThrows(IllegalArgumentException.class, () -> Functions.subDivide(lineString, 4));
4429+
assertEquals("ST_Subdivide needs 5 or more max vertices", e.getMessage());
4430+
}
44234431
}

docs/api/flink/Function.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4041,6 +4041,8 @@ POINT(100 150)
40414041

40424042
Introduction: Returns list of geometries divided based of given maximum number of vertices.
40434043

4044+
A minimum of 5 vertices is required for maxVertices parameter to form a closed box.
4045+
40444046
Format: `ST_SubDivide(geom: Geometry, maxVertices: Integer)`
40454047

40464048
Since: `v1.5.0`

docs/api/snowflake/vector-data/Function.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,6 +3169,8 @@ Output: `POINT(100 150)`
31693169

31703170
Introduction: Returns a multi-geometry divided based of given maximum number of vertices.
31713171

3172+
A minimum of 5 vertices is required for maxVertices parameter to form a closed box.
3173+
31723174
Format: `ST_SubDivide(geom: geometry, maxVertices: int)`
31733175

31743176
SQL example:
@@ -3188,6 +3190,8 @@ MULTILINESTRING ((0 0, 5 5), (5 5, 10 10), (10 10, 21 21), (21 21, 60 60), (60 6
31883190

31893191
Introduction: It works the same as ST_SubDivide but returns new rows with geometries instead of a multi-geometry.
31903192

3193+
A minimum of 5 vertices is required for maxVertices parameter to form a closed box.
3194+
31913195
Format: `
31923196
SELECT SEDONA.ST_AsText(GEOM)
31933197
FROM table(SEDONA.ST_SubDivideExplode(geom: geometry, maxVertices: int))`

docs/api/sql/Function.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4253,6 +4253,8 @@ POINT(100 150)
42534253

42544254
Introduction: Returns list of geometries divided based of given maximum number of vertices.
42554255

4256+
A minimum of 5 vertices is required for maxVertices parameter to form a closed box.
4257+
42564258
Format: `ST_SubDivide(geom: Geometry, maxVertices: Integer)`
42574259

42584260
Since: `v1.1.0`
@@ -4309,6 +4311,8 @@ Output:
43094311

43104312
Introduction: It works the same as ST_SubDivide but returns new rows with geometries instead of list.
43114313

4314+
A minimum of 5 vertices is required for maxVertices parameter to form a closed box.
4315+
43124316
Format: `ST_SubDivideExplode(geom: Geometry, maxVertices: Integer)`
43134317

43144318
Since: `v1.1.0`

spark/common/src/test/scala/org/apache/sedona/sql/PreserveSRIDSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class PreserveSRIDSuite extends TestBaseScala with TableDrivenPropertyChecks {
7575
("ST_SetPoint(geom3, 1, ST_Point(0.5, 0.5))", 1000),
7676
("ST_ClosestPoint(geom1, geom2)", 1000),
7777
("ST_FlipCoordinates(geom1)", 1000),
78-
("ST_SubDivide(geom4, 4)", 1000),
78+
("ST_SubDivide(geom4, 5)", 1000),
7979
("ST_MakeEnvelope(0, 1, 2, 3, 1000)", 1000),
8080
("ST_MakeLine(geom3, geom3)", 1000),
8181
("ST_Points(geom1)", 1000),

spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3770,4 +3770,14 @@ class functionTestScala
37703770
"The Line has SRID 4326 and Point has SRID 3857. The Line and Point should be in the same SRID.")
37713771
}
37723772

3773+
it("should raise an error when using ST_SubDivide with not enough vertices") {
3774+
val invalidDf = sparkSession.sql("""
3775+
|SELECT ST_Subdivide(ST_GeomFromWKT('LINESTRING(0 0, 99 99)'), 4) as result
3776+
""".stripMargin)
3777+
3778+
val exception = intercept[Exception] {
3779+
invalidDf.collect()
3780+
}
3781+
exception.getMessage should include("ST_Subdivide needs 5 or more max vertices")
3782+
}
37733783
}

0 commit comments

Comments
 (0)