From 6a94fb317512d4a9a1b0e41f8edbe12477bc136f Mon Sep 17 00:00:00 2001 From: roha4naik Date: Fri, 20 Nov 2026 14:14:21 +0530 Subject: [PATCH 1/2] [GH-3564] Report a geometry as invalid when the JTS validator throws ST_IsValid, ST_IsValidReason and ST_IsValidDetail no longer fail the query when IsValidOp throws while validating a geometry. ST_IsValid returns false, ST_IsValidReason returns the error as the reason and ST_IsValidDetail returns (false, reason, NULL). ST_IsValidReason and ST_IsValidDetail return NULL for a NULL geometry. --- .../org/apache/sedona/common/Functions.java | 49 +++++++++++++++---- .../apache/sedona/common/FunctionsTest.java | 40 +++++++++++++++ .../flink/Geometry-Validation/ST_IsValid.md | 2 + .../Geometry-Validation/ST_IsValidReason.md | 2 + .../Geometry-Validation/ST_IsValid.md | 2 + .../Geometry-Validation/ST_IsValidDetail.md | 2 + .../Geometry-Validation/ST_IsValidReason.md | 2 + .../api/sql/Geometry-Validation/ST_IsValid.md | 2 + .../Geometry-Validation/ST_IsValidDetail.md | 2 + .../Geometry-Validation/ST_IsValidReason.md | 2 + .../sedona_sql/expressions/Functions.scala | 12 +++-- .../apache/sedona/sql/functionTestScala.scala | 42 ++++++++++++++++ 12 files changed, 144 insertions(+), 15 deletions(-) diff --git a/common/src/main/java/org/apache/sedona/common/Functions.java b/common/src/main/java/org/apache/sedona/common/Functions.java index b2e47b0b3d7..adbd2c2a0eb 100644 --- a/common/src/main/java/org/apache/sedona/common/Functions.java +++ b/common/src/main/java/org/apache/sedona/common/Functions.java @@ -1160,16 +1160,14 @@ public static boolean isValid(Geometry geometry) { } public static boolean isValid(Geometry geom, int flag) { - IsValidOp isValidOp = new IsValidOp(geom); + IsValidOp isValidOp = getIsValidOpObject(geom, flag); - // Set the validity model based on flags - if (flag == ESRI_VALIDITY) { - isValidOp.setSelfTouchingRingFormingHoleValid(true); - } else { - isValidOp.setSelfTouchingRingFormingHoleValid(false); + try { + return isValidOp.isValid(); + } catch (RuntimeException e) { + // A geometry the validator cannot process is not valid + return false; } - - return isValidOp.isValid(); } public static boolean isValidTrajectory(Geometry geom) { @@ -2955,9 +2953,19 @@ public static ValidDetail isValidDetail(Geometry geom) { } public static ValidDetail isValidDetail(Geometry geom, int flag) { + if (geom == null) { + return null; + } IsValidOp isValidOp = getIsValidOpObject(geom, flag); - if (isValidOp.isValid()) { + boolean valid; + try { + valid = isValidOp.isValid(); + } catch (RuntimeException e) { + return new ValidDetail(false, validatorFailureReason(e), null); + } + + if (valid) { return new ValidDetail(true, null, null); } else { TopologyValidationError error = isValidOp.getValidationError(); @@ -2972,9 +2980,19 @@ public static String isValidReason(Geometry geom) { } public static String isValidReason(Geometry geom, int flag) { + if (geom == null) { + return null; + } IsValidOp isValidOp = getIsValidOpObject(geom, flag); - if (isValidOp.isValid()) { + boolean valid; + try { + valid = isValidOp.isValid(); + } catch (RuntimeException e) { + return validatorFailureReason(e); + } + + if (valid) { return "Valid Geometry"; } else { TopologyValidationError error = isValidOp.getValidationError(); @@ -2982,6 +3000,17 @@ public static String isValidReason(Geometry geom, int flag) { } } + /** + * Describes a failure of the JTS validator itself, e.g. "IllegalArgumentException: Segment vertex + * does not intersect ring". Used as the invalidity reason of a geometry the validator cannot + * process. + */ + private static String validatorFailureReason(RuntimeException e) { + String message = e.getMessage(); + String name = e.getClass().getSimpleName(); + return message == null ? name : name + ": " + message; + } + /** * This method takes a GeometryCollection and returns another GeometryCollection containing the * polygons formed by the linework of a set of geometries. diff --git a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java index bfa3a445021..9240ad1c5f8 100644 --- a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java +++ b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java @@ -6247,6 +6247,46 @@ public void isValidReason() { assertEquals("Self-intersection at or near point (10.0, 20.0, NaN)", invalidReasonESRI); } + // JTS 1.20.0 throws "Segment vertex does not intersect ring" when validating this polygon. + private static final String VALIDATOR_FAILURE_WKT = + "POLYGON ((-1.7e308 -10, 1.7e308 -10, 1.7e308 0, 1.7e308 10, -1.7e308 10, -1.7e308 -10)," + + " (-1e308 0, -5e307 5, -5e307 -5, -1e308 0))"; + private static final String VALIDATOR_FAILURE_REASON = + "IllegalArgumentException: Segment vertex does not intersect ring"; + + @Test + public void isValidWhenValidatorThrows() throws ParseException { + Geometry geom = Constructors.geomFromEWKT(VALIDATOR_FAILURE_WKT); + assertFalse(Functions.isValid(geom)); + assertFalse(Functions.isValid(geom, 0)); + assertFalse(Functions.isValid(geom, 1)); + } + + @Test + public void isValidReasonWhenValidatorThrows() throws ParseException { + Geometry geom = Constructors.geomFromEWKT(VALIDATOR_FAILURE_WKT); + assertEquals(VALIDATOR_FAILURE_REASON, Functions.isValidReason(geom)); + assertEquals(VALIDATOR_FAILURE_REASON, Functions.isValidReason(geom, 0)); + assertEquals(VALIDATOR_FAILURE_REASON, Functions.isValidReason(geom, 1)); + } + + @Test + public void isValidDetailWhenValidatorThrows() throws ParseException { + Geometry geom = Constructors.geomFromEWKT(VALIDATOR_FAILURE_WKT); + ValidDetail expected = new ValidDetail(false, VALIDATOR_FAILURE_REASON, null); + assertTrue(expected.equals(Functions.isValidDetail(geom))); + assertTrue(expected.equals(Functions.isValidDetail(geom, 0))); + assertTrue(expected.equals(Functions.isValidDetail(geom, 1))); + } + + @Test + public void isValidReasonAndDetailOfNullGeometry() { + assertNull(Functions.isValidReason(null)); + assertNull(Functions.isValidReason(null, 1)); + assertNull(Functions.isValidDetail(null)); + assertNull(Functions.isValidDetail(null, 1)); + } + @Test public void points() throws ParseException { Geometry polygon = Constructors.geomFromEWKT("POLYGON ((0 0, 1 1, 5 1, 5 0, 1 0, 0 0))"); diff --git a/docs/api/flink/Geometry-Validation/ST_IsValid.md b/docs/api/flink/Geometry-Validation/ST_IsValid.md index 16ed54813d8..c623fc2678f 100644 --- a/docs/api/flink/Geometry-Validation/ST_IsValid.md +++ b/docs/api/flink/Geometry-Validation/ST_IsValid.md @@ -24,6 +24,8 @@ Introduction: Test if a geometry is well-formed. The function can be invoked wit - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. +If the validator cannot process a geometry and fails with an error, the geometry is reported as not valid (`false`) instead of failing the query. + ![ST_IsValid](../../../image/ST_IsValid/ST_IsValid.svg "ST_IsValid") Formats: diff --git a/docs/api/flink/Geometry-Validation/ST_IsValidReason.md b/docs/api/flink/Geometry-Validation/ST_IsValidReason.md index 8ec17d1989a..fe3ced591bd 100644 --- a/docs/api/flink/Geometry-Validation/ST_IsValidReason.md +++ b/docs/api/flink/Geometry-Validation/ST_IsValidReason.md @@ -24,6 +24,8 @@ Introduction: Returns text stating if the geometry is valid. If not, it provides - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. +If the validator cannot process a geometry and fails with an error, the error is returned as the reason, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, instead of failing the query. + ![ST_IsValidReason](../../../image/ST_IsValidReason/ST_IsValidReason.svg "ST_IsValidReason") Formats: diff --git a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValid.md b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValid.md index 2d89353eaba..ad6c808475c 100644 --- a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValid.md +++ b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValid.md @@ -24,6 +24,8 @@ Introduction: Test if a geometry is well-formed. The function can be invoked wit - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. +If the validator cannot process a geometry and fails with an error, the geometry is reported as not valid (`false`) instead of failing the query. + ![ST_IsValid](../../../../image/ST_IsValid/ST_IsValid.svg "ST_IsValid") Formats: diff --git a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidDetail.md b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidDetail.md index 08756d4de9c..b7d112fd61c 100644 --- a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidDetail.md +++ b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidDetail.md @@ -28,6 +28,8 @@ The flags parameter is a bitfield with the following options: - 0: Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. +If the validator cannot process a geometry and fails with an error, `valid` is `false`, `reason` is the error, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, and `location` is `NULL`, instead of failing the query. + ![ST_IsValidDetail](../../../../image/ST_IsValidDetail/ST_IsValidDetail.svg "ST_IsValidDetail") Format: diff --git a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidReason.md b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidReason.md index a06c21db0ce..be45a59312a 100644 --- a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidReason.md +++ b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidReason.md @@ -24,6 +24,8 @@ Introduction: Returns text stating if the geometry is valid. If not, it provides - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. +If the validator cannot process a geometry and fails with an error, the error is returned as the reason, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, instead of failing the query. + ![ST_IsValidReason](../../../../image/ST_IsValidReason/ST_IsValidReason.svg "ST_IsValidReason") Formats: diff --git a/docs/api/sql/Geometry-Validation/ST_IsValid.md b/docs/api/sql/Geometry-Validation/ST_IsValid.md index 16ed54813d8..c623fc2678f 100644 --- a/docs/api/sql/Geometry-Validation/ST_IsValid.md +++ b/docs/api/sql/Geometry-Validation/ST_IsValid.md @@ -24,6 +24,8 @@ Introduction: Test if a geometry is well-formed. The function can be invoked wit - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. +If the validator cannot process a geometry and fails with an error, the geometry is reported as not valid (`false`) instead of failing the query. + ![ST_IsValid](../../../image/ST_IsValid/ST_IsValid.svg "ST_IsValid") Formats: diff --git a/docs/api/sql/Geometry-Validation/ST_IsValidDetail.md b/docs/api/sql/Geometry-Validation/ST_IsValidDetail.md index 2bde64bb0ef..f8730654fea 100644 --- a/docs/api/sql/Geometry-Validation/ST_IsValidDetail.md +++ b/docs/api/sql/Geometry-Validation/ST_IsValidDetail.md @@ -28,6 +28,8 @@ The flags parameter is a bitfield with the following options: - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. +If the validator cannot process a geometry and fails with an error, `valid` is `false`, `reason` is the error, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, and `location` is `NULL`, instead of failing the query. + ![ST_IsValidDetail](../../../image/ST_IsValidDetail/ST_IsValidDetail.svg "ST_IsValidDetail") Formats: diff --git a/docs/api/sql/Geometry-Validation/ST_IsValidReason.md b/docs/api/sql/Geometry-Validation/ST_IsValidReason.md index 8ec17d1989a..fe3ced591bd 100644 --- a/docs/api/sql/Geometry-Validation/ST_IsValidReason.md +++ b/docs/api/sql/Geometry-Validation/ST_IsValidReason.md @@ -24,6 +24,8 @@ Introduction: Returns text stating if the geometry is valid. If not, it provides - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. +If the validator cannot process a geometry and fails with an error, the error is returned as the reason, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, instead of failing the query. + ![ST_IsValidReason](../../../image/ST_IsValidReason/ST_IsValidReason.svg "ST_IsValidReason") Formats: diff --git a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala index e0968c6e213..583f5929a8a 100644 --- a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala +++ b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala @@ -527,12 +527,14 @@ private[apache] case class ST_IsValidDetail(children: Seq[Expression]) throw new IllegalArgumentException(s"Invalid number of arguments: $nArgs") } - try { - if (validDetail.location == null) { - return InternalRow.fromSeq(Seq(validDetail.valid, null, null)) - } + if (validDetail == null) { + return null + } - val serLocation = GeometrySerializer.serialize(validDetail.location) + try { + val serLocation = + if (validDetail.location == null) null + else GeometrySerializer.serialize(validDetail.location) InternalRow.fromSeq( Seq(validDetail.valid, UTF8String.fromString(validDetail.reason), serLocation)) } catch { diff --git a/spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala b/spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala index 332f1e30346..68e23edeaaa 100644 --- a/spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala +++ b/spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala @@ -4040,6 +4040,48 @@ class functionTestScala } } + it( + "ST_IsValid, ST_IsValidReason and ST_IsValidDetail should not fail when the JTS validator throws") { + // JTS 1.20.0 throws "Segment vertex does not intersect ring" when validating this polygon + val wkt = + "POLYGON ((-1.7e308 -10, 1.7e308 -10, 1.7e308 0, 1.7e308 10, -1.7e308 10, -1.7e308 -10), " + + "(-1e308 0, -5e307 5, -5e307 -5, -1e308 0))" + val reason = "IllegalArgumentException: Segment vertex does not intersect ring" + val row = sparkSession + .sql(s"SELECT ST_GeomFromWKT('$wkt') AS geom") + .selectExpr( + "ST_IsValid(geom)", + "ST_IsValid(geom, 1)", + "ST_IsValidReason(geom)", + "ST_IsValidReason(geom, 1)", + "ST_IsValidDetail(geom)", + "ST_IsValidDetail(geom, 1)") + .first() + + assert(!row.getBoolean(0)) + assert(!row.getBoolean(1)) + assert(row.getString(2) == reason) + assert(row.getString(3) == reason) + assert(row.getAs[Row](4) == Row(false, reason, null)) + assert(row.getAs[Row](5) == Row(false, reason, null)) + } + + it("ST_IsValidReason and ST_IsValidDetail should return null for a null geometry") { + val row = sparkSession + .sql("SELECT ST_GeomFromWKT(CAST(NULL AS STRING)) AS geom") + .selectExpr( + "ST_IsValidReason(geom)", + "ST_IsValidReason(geom, 1)", + "ST_IsValidDetail(geom)", + "ST_IsValidDetail(geom, 1)") + .first() + + assert(row.isNullAt(0)) + assert(row.isNullAt(1)) + assert(row.isNullAt(2)) + assert(row.isNullAt(3)) + } + it("ST_IsValidReason should provide reasons for invalid geometries") { val testData = Seq( (5330, "POLYGON ((0 0, 3 3, 0 3, 3 0, 0 0))"), From c8ca42d9fb1be0107f32d49cfcc094e63e75951f Mon Sep 17 00:00:00 2001 From: Jia Yu Date: Fri, 20 Nov 2026 01:09:30 -0800 Subject: [PATCH 2/2] [GH-3564] Log the swallowed validator error and broaden the tests - Log the exception at debug level before ST_IsValid reports the geometry as invalid. - Return NULL from Spark ST_IsValidDetail for a NULL geometry before calling the validator. - Assert in FunctionsTest that the fixture still makes the JTS validator throw, and cover ordinary valid, invalid, empty and flag-dependent results. - Evaluate the Spark tests on repartitioned multi-row input so they run in executor tasks. - Add a Snowflake ST_IsValidDetail UDTF test. - Describe in the docs what the functions return when the validity checks cannot complete. --- .../org/apache/sedona/common/Functions.java | 65 ++++++----- .../apache/sedona/common/FunctionsTest.java | 95 +++++++++------- .../flink/Geometry-Validation/ST_IsValid.md | 2 +- .../Geometry-Validation/ST_IsValidReason.md | 2 +- .../Geometry-Validation/ST_IsValid.md | 2 +- .../Geometry-Validation/ST_IsValidDetail.md | 2 +- .../Geometry-Validation/ST_IsValidReason.md | 2 +- .../api/sql/Geometry-Validation/ST_IsValid.md | 2 +- .../Geometry-Validation/ST_IsValidDetail.md | 2 +- .../Geometry-Validation/ST_IsValidReason.md | 2 +- .../snowsql/udtfs/ST_IsValidDetailTest.java | 64 +++++++++++ .../sedona_sql/expressions/Functions.scala | 8 +- .../apache/sedona/sql/functionTestScala.scala | 107 +++++++++++------- 13 files changed, 230 insertions(+), 125 deletions(-) create mode 100644 snowflake/src/test/java/org/apache/sedona/snowflake/snowsql/udtfs/ST_IsValidDetailTest.java diff --git a/common/src/main/java/org/apache/sedona/common/Functions.java b/common/src/main/java/org/apache/sedona/common/Functions.java index adbd2c2a0eb..9cb66269881 100644 --- a/common/src/main/java/org/apache/sedona/common/Functions.java +++ b/common/src/main/java/org/apache/sedona/common/Functions.java @@ -1160,12 +1160,23 @@ public static boolean isValid(Geometry geometry) { } public static boolean isValid(Geometry geom, int flag) { - IsValidOp isValidOp = getIsValidOpObject(geom, flag); + IsValidOp isValidOp = new IsValidOp(geom); + + // Set the validity model based on flags + if (flag == ESRI_VALIDITY) { + isValidOp.setSelfTouchingRingFormingHoleValid(true); + } else { + isValidOp.setSelfTouchingRingFormingHoleValid(false); + } try { return isValidOp.isValid(); } catch (RuntimeException e) { - // A geometry the validator cannot process is not valid + // JTS throws on some inputs instead of reporting them invalid, e.g. coordinates near + // Double.MAX_VALUE that overflow its orientation test. A geometry that cannot be validated + // is not valid, and one bad row must not fail the query. isValidReason and isValidDetail + // report the error. + log.debug("Geometry validation failed, reporting the geometry as invalid", e); return false; } } @@ -2958,21 +2969,18 @@ public static ValidDetail isValidDetail(Geometry geom, int flag) { } IsValidOp isValidOp = getIsValidOpObject(geom, flag); - boolean valid; try { - valid = isValidOp.isValid(); + if (isValidOp.isValid()) { + return new ValidDetail(true, null, null); + } } catch (RuntimeException e) { - return new ValidDetail(false, validatorFailureReason(e), null); - } - - if (valid) { - return new ValidDetail(true, null, null); - } else { - TopologyValidationError error = isValidOp.getValidationError(); - String reason = error.toString(); - Geometry location = geom.getFactory().createPoint(error.getCoordinate()); - return new ValidDetail(false, reason, location); + // The validator threw before it found a location + return new ValidDetail(false, validationFailureReason(e), null); } + TopologyValidationError error = isValidOp.getValidationError(); + String reason = error.toString(); + Geometry location = geom.getFactory().createPoint(error.getCoordinate()); + return new ValidDetail(false, reason, location); } public static String isValidReason(Geometry geom) { @@ -2985,30 +2993,25 @@ public static String isValidReason(Geometry geom, int flag) { } IsValidOp isValidOp = getIsValidOpObject(geom, flag); - boolean valid; try { - valid = isValidOp.isValid(); + if (isValidOp.isValid()) { + return "Valid Geometry"; + } } catch (RuntimeException e) { - return validatorFailureReason(e); - } - - if (valid) { - return "Valid Geometry"; - } else { - TopologyValidationError error = isValidOp.getValidationError(); - return error.toString(); + return validationFailureReason(e); } + TopologyValidationError error = isValidOp.getValidationError(); + return error.toString(); } /** - * Describes a failure of the JTS validator itself, e.g. "IllegalArgumentException: Segment vertex - * does not intersect ring". Used as the invalidity reason of a geometry the validator cannot - * process. + * The reason reported for a geometry the validator throws on (see {@link #isValid(Geometry, + * int)}), in the form GEOS reports it, e.g. "IllegalArgumentException: Segment vertex does not + * intersect ring". */ - private static String validatorFailureReason(RuntimeException e) { - String message = e.getMessage(); - String name = e.getClass().getSimpleName(); - return message == null ? name : name + ": " + message; + private static String validationFailureReason(RuntimeException e) { + String type = e.getClass().getSimpleName(); + return e.getMessage() == null ? type : type + ": " + e.getMessage(); } /** diff --git a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java index 9240ad1c5f8..85507e2e918 100644 --- a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java +++ b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java @@ -5929,6 +5929,61 @@ public void testAddMeasurePreservesCoordinateLayout() throws ParseException { } } + @Test + public void isValidValidatorErrorReturnsFalse() throws ParseException { + // Near Double.MAX_VALUE the orientation determinant overflows to NaN, so JTS places the + // hole's first vertex on the shell boundary but then cannot find the shell segment it lies on. + Geometry geom = + geomFromWKT( + "POLYGON ((-1.7e308 -10, 1.7e308 -10, 1.7e308 0, 1.7e308 10, -1.7e308 10, -1.7e308 -10)," + + " (-1e308 0, -5e307 5, -5e307 -5, -1e308 0))", + 0); + IllegalArgumentException validatorError = + assertThrows( + "fixture no longer makes the JTS validator throw", + IllegalArgumentException.class, + () -> new org.locationtech.jts.operation.valid.IsValidOp(geom).isValid()); + assertEquals("Segment vertex does not intersect ring", validatorError.getMessage()); + + assertFalse(Functions.isValid(geom)); + assertFalse(Functions.isValid(geom, 0)); + assertFalse(Functions.isValid(geom, 1)); + + // The reason carries the validator's error, in the form GEOS reports it + String reason = "IllegalArgumentException: Segment vertex does not intersect ring"; + assertEquals(reason, Functions.isValidReason(geom)); + assertEquals(reason, Functions.isValidReason(geom, 0)); + assertEquals(reason, Functions.isValidReason(geom, 1)); + ValidDetail expectedDetail = new ValidDetail(false, reason, null); + assertTrue(expectedDetail.equals(Functions.isValidDetail(geom))); + assertTrue(expectedDetail.equals(Functions.isValidDetail(geom, 0))); + assertTrue(expectedDetail.equals(Functions.isValidDetail(geom, 1))); + } + + @Test + public void isValidReasonAndDetailOfNullAreNull() { + assertNull(Functions.isValidReason(null)); + assertNull(Functions.isValidReason(null, 1)); + assertNull(Functions.isValidDetail(null)); + assertNull(Functions.isValidDetail(null, 1)); + } + + @Test + public void isValidKeepsOrdinaryResults() throws ParseException { + assertTrue(Functions.isValid(geomFromWKT("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", 0))); + assertFalse(Functions.isValid(geomFromWKT("POLYGON ((0 0, 1 1, 0 1, 1 0, 0 0))", 0))); + assertTrue(Functions.isValid(geomFromWKT("POLYGON EMPTY", 0))); + assertTrue(Functions.isValid(geomFromWKT("GEOMETRYCOLLECTION EMPTY", 0))); + assertFalse(Functions.isValid(geomFromWKT("POINT (NaN NaN)", 0))); + + // The shell touches itself at (5 10) and encloses an inverted hole: invalid under OGC, + // valid under the Esri model. + Geometry invertedHole = + geomFromWKT("POLYGON ((0 0, 10 0, 10 10, 5 10, 7 5, 3 5, 5 10, 0 10, 0 0))", 0); + assertFalse(Functions.isValid(invertedHole, 0)); + assertTrue(Functions.isValid(invertedHole, 1)); + } + @Test public void testAddMeasureWithDefaultJtsCoordinates() { // JTS's default XY coordinates carry a NaN Z slot, not an actual Z dimension. @@ -6247,46 +6302,6 @@ public void isValidReason() { assertEquals("Self-intersection at or near point (10.0, 20.0, NaN)", invalidReasonESRI); } - // JTS 1.20.0 throws "Segment vertex does not intersect ring" when validating this polygon. - private static final String VALIDATOR_FAILURE_WKT = - "POLYGON ((-1.7e308 -10, 1.7e308 -10, 1.7e308 0, 1.7e308 10, -1.7e308 10, -1.7e308 -10)," - + " (-1e308 0, -5e307 5, -5e307 -5, -1e308 0))"; - private static final String VALIDATOR_FAILURE_REASON = - "IllegalArgumentException: Segment vertex does not intersect ring"; - - @Test - public void isValidWhenValidatorThrows() throws ParseException { - Geometry geom = Constructors.geomFromEWKT(VALIDATOR_FAILURE_WKT); - assertFalse(Functions.isValid(geom)); - assertFalse(Functions.isValid(geom, 0)); - assertFalse(Functions.isValid(geom, 1)); - } - - @Test - public void isValidReasonWhenValidatorThrows() throws ParseException { - Geometry geom = Constructors.geomFromEWKT(VALIDATOR_FAILURE_WKT); - assertEquals(VALIDATOR_FAILURE_REASON, Functions.isValidReason(geom)); - assertEquals(VALIDATOR_FAILURE_REASON, Functions.isValidReason(geom, 0)); - assertEquals(VALIDATOR_FAILURE_REASON, Functions.isValidReason(geom, 1)); - } - - @Test - public void isValidDetailWhenValidatorThrows() throws ParseException { - Geometry geom = Constructors.geomFromEWKT(VALIDATOR_FAILURE_WKT); - ValidDetail expected = new ValidDetail(false, VALIDATOR_FAILURE_REASON, null); - assertTrue(expected.equals(Functions.isValidDetail(geom))); - assertTrue(expected.equals(Functions.isValidDetail(geom, 0))); - assertTrue(expected.equals(Functions.isValidDetail(geom, 1))); - } - - @Test - public void isValidReasonAndDetailOfNullGeometry() { - assertNull(Functions.isValidReason(null)); - assertNull(Functions.isValidReason(null, 1)); - assertNull(Functions.isValidDetail(null)); - assertNull(Functions.isValidDetail(null, 1)); - } - @Test public void points() throws ParseException { Geometry polygon = Constructors.geomFromEWKT("POLYGON ((0 0, 1 1, 5 1, 5 0, 1 0, 0 0))"); diff --git a/docs/api/flink/Geometry-Validation/ST_IsValid.md b/docs/api/flink/Geometry-Validation/ST_IsValid.md index c623fc2678f..73b98c002a7 100644 --- a/docs/api/flink/Geometry-Validation/ST_IsValid.md +++ b/docs/api/flink/Geometry-Validation/ST_IsValid.md @@ -24,7 +24,7 @@ Introduction: Test if a geometry is well-formed. The function can be invoked wit - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. -If the validator cannot process a geometry and fails with an error, the geometry is reported as not valid (`false`) instead of failing the query. +If the validity checks cannot complete for a geometry, for example because coordinates close to the largest double value overflow them, the function returns `false` for that geometry instead of failing the query. [ST_IsValidReason](ST_IsValidReason.md) returns the error that stopped the checks. ![ST_IsValid](../../../image/ST_IsValid/ST_IsValid.svg "ST_IsValid") diff --git a/docs/api/flink/Geometry-Validation/ST_IsValidReason.md b/docs/api/flink/Geometry-Validation/ST_IsValidReason.md index fe3ced591bd..144dbfffd4a 100644 --- a/docs/api/flink/Geometry-Validation/ST_IsValidReason.md +++ b/docs/api/flink/Geometry-Validation/ST_IsValidReason.md @@ -24,7 +24,7 @@ Introduction: Returns text stating if the geometry is valid. If not, it provides - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. -If the validator cannot process a geometry and fails with an error, the error is returned as the reason, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, instead of failing the query. +If the validity checks cannot complete for a geometry, for example because coordinates close to the largest double value overflow them, the function returns the error that stopped them, such as `IllegalArgumentException: Segment vertex does not intersect ring`, instead of failing the query. ![ST_IsValidReason](../../../image/ST_IsValidReason/ST_IsValidReason.svg "ST_IsValidReason") diff --git a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValid.md b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValid.md index ad6c808475c..ca6d9db6419 100644 --- a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValid.md +++ b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValid.md @@ -24,7 +24,7 @@ Introduction: Test if a geometry is well-formed. The function can be invoked wit - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. -If the validator cannot process a geometry and fails with an error, the geometry is reported as not valid (`false`) instead of failing the query. +If the validity checks cannot complete for a geometry, for example because coordinates close to the largest double value overflow them, the function returns `false` for that geometry instead of failing the query. [ST_IsValidReason](ST_IsValidReason.md) returns the error that stopped the checks. ![ST_IsValid](../../../../image/ST_IsValid/ST_IsValid.svg "ST_IsValid") diff --git a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidDetail.md b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidDetail.md index b7d112fd61c..36898159165 100644 --- a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidDetail.md +++ b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidDetail.md @@ -28,7 +28,7 @@ The flags parameter is a bitfield with the following options: - 0: Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. -If the validator cannot process a geometry and fails with an error, `valid` is `false`, `reason` is the error, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, and `location` is `NULL`, instead of failing the query. +If the validity checks cannot complete for a geometry, for example because coordinates close to the largest double value overflow them, the function returns `false` as `valid`, the error that stopped them as `reason`, such as `IllegalArgumentException: Segment vertex does not intersect ring`, and a null `location`, instead of failing the query. ![ST_IsValidDetail](../../../../image/ST_IsValidDetail/ST_IsValidDetail.svg "ST_IsValidDetail") diff --git a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidReason.md b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidReason.md index be45a59312a..121fd26837f 100644 --- a/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidReason.md +++ b/docs/api/snowflake/vector-data/Geometry-Validation/ST_IsValidReason.md @@ -24,7 +24,7 @@ Introduction: Returns text stating if the geometry is valid. If not, it provides - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. -If the validator cannot process a geometry and fails with an error, the error is returned as the reason, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, instead of failing the query. +If the validity checks cannot complete for a geometry, for example because coordinates close to the largest double value overflow them, the function returns the error that stopped them, such as `IllegalArgumentException: Segment vertex does not intersect ring`, instead of failing the query. ![ST_IsValidReason](../../../../image/ST_IsValidReason/ST_IsValidReason.svg "ST_IsValidReason") diff --git a/docs/api/sql/Geometry-Validation/ST_IsValid.md b/docs/api/sql/Geometry-Validation/ST_IsValid.md index c623fc2678f..73b98c002a7 100644 --- a/docs/api/sql/Geometry-Validation/ST_IsValid.md +++ b/docs/api/sql/Geometry-Validation/ST_IsValid.md @@ -24,7 +24,7 @@ Introduction: Test if a geometry is well-formed. The function can be invoked wit - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. -If the validator cannot process a geometry and fails with an error, the geometry is reported as not valid (`false`) instead of failing the query. +If the validity checks cannot complete for a geometry, for example because coordinates close to the largest double value overflow them, the function returns `false` for that geometry instead of failing the query. [ST_IsValidReason](ST_IsValidReason.md) returns the error that stopped the checks. ![ST_IsValid](../../../image/ST_IsValid/ST_IsValid.svg "ST_IsValid") diff --git a/docs/api/sql/Geometry-Validation/ST_IsValidDetail.md b/docs/api/sql/Geometry-Validation/ST_IsValidDetail.md index f8730654fea..779c47d0415 100644 --- a/docs/api/sql/Geometry-Validation/ST_IsValidDetail.md +++ b/docs/api/sql/Geometry-Validation/ST_IsValidDetail.md @@ -28,7 +28,7 @@ The flags parameter is a bitfield with the following options: - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. -If the validator cannot process a geometry and fails with an error, `valid` is `false`, `reason` is the error, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, and `location` is `NULL`, instead of failing the query. +If the validity checks cannot complete for a geometry, for example because coordinates close to the largest double value overflow them, the function returns `false` as `valid`, the error that stopped them as `reason`, such as `IllegalArgumentException: Segment vertex does not intersect ring`, and a null `location`, instead of failing the query. ![ST_IsValidDetail](../../../image/ST_IsValidDetail/ST_IsValidDetail.svg "ST_IsValidDetail") diff --git a/docs/api/sql/Geometry-Validation/ST_IsValidReason.md b/docs/api/sql/Geometry-Validation/ST_IsValidReason.md index fe3ced591bd..144dbfffd4a 100644 --- a/docs/api/sql/Geometry-Validation/ST_IsValidReason.md +++ b/docs/api/sql/Geometry-Validation/ST_IsValidReason.md @@ -24,7 +24,7 @@ Introduction: Returns text stating if the geometry is valid. If not, it provides - 0 (default): Use usual OGC SFS (Simple Features Specification) validity semantics. - 1: "ESRI flag", Accepts certain self-touching rings as valid, which are considered invalid under OGC standards. -If the validator cannot process a geometry and fails with an error, the error is returned as the reason, e.g. `IllegalArgumentException: Segment vertex does not intersect ring`, instead of failing the query. +If the validity checks cannot complete for a geometry, for example because coordinates close to the largest double value overflow them, the function returns the error that stopped them, such as `IllegalArgumentException: Segment vertex does not intersect ring`, instead of failing the query. ![ST_IsValidReason](../../../image/ST_IsValidReason/ST_IsValidReason.svg "ST_IsValidReason") diff --git a/snowflake/src/test/java/org/apache/sedona/snowflake/snowsql/udtfs/ST_IsValidDetailTest.java b/snowflake/src/test/java/org/apache/sedona/snowflake/snowsql/udtfs/ST_IsValidDetailTest.java new file mode 100644 index 00000000000..55b142b34a1 --- /dev/null +++ b/snowflake/src/test/java/org/apache/sedona/snowflake/snowsql/udtfs/ST_IsValidDetailTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sedona.snowflake.snowsql.udtfs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; +import java.util.stream.Collectors; +import org.apache.sedona.common.Constructors; +import org.apache.sedona.snowflake.snowsql.GeometrySerde; +import org.junit.Test; +import org.locationtech.jts.io.ParseException; + +public class ST_IsValidDetailTest { + + private static ST_IsValidDetail.OutputRow process(String wkt, int flag) throws ParseException { + byte[] geometry = GeometrySerde.serialize(Constructors.geomFromWKT(wkt, 0)); + List rows = + new ST_IsValidDetail().process(geometry, flag).collect(Collectors.toList()); + assertEquals(1, rows.size()); + return rows.get(0); + } + + @Test + public void validatorErrorIsReportedWithoutLocation() throws ParseException { + // Near Double.MAX_VALUE the JTS validator throws instead of reporting the polygon invalid + String wkt = + "POLYGON ((-1.7e308 -10, 1.7e308 -10, 1.7e308 0, 1.7e308 10, -1.7e308 10, -1.7e308 -10)," + + " (-1e308 0, -5e307 5, -5e307 -5, -1e308 0))"; + for (int flag : new int[] {0, 1}) { + ST_IsValidDetail.OutputRow row = process(wkt, flag); + assertFalse(row.valid); + assertEquals("IllegalArgumentException: Segment vertex does not intersect ring", row.reason); + assertNull(row.location); + } + } + + @Test + public void validGeometryHasNoReasonOrLocation() throws ParseException { + ST_IsValidDetail.OutputRow row = process("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", 0); + assertTrue(row.valid); + assertNull(row.reason); + assertNull(row.location); + } +} diff --git a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala index 583f5929a8a..7ccfe1570f8 100644 --- a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala +++ b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala @@ -517,6 +517,9 @@ private[apache] case class ST_IsValidDetail(children: Seq[Expression]) override def eval(input: InternalRow): Any = { val geometry = children.head.toGeometry(input) + if (geometry == null) { + return null + } var validDetail: ValidDetail = null if (nArgs == 1) { validDetail = Functions.isValidDetail(geometry) @@ -527,11 +530,8 @@ private[apache] case class ST_IsValidDetail(children: Seq[Expression]) throw new IllegalArgumentException(s"Invalid number of arguments: $nArgs") } - if (validDetail == null) { - return null - } - try { + // A geometry the validator throws on is invalid with a reason but no location val serLocation = if (validDetail.location == null) null else GeometrySerializer.serialize(validDetail.location) diff --git a/spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala b/spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala index 68e23edeaaa..80a8bdc62bf 100644 --- a/spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala +++ b/spark/common/src/test/scala/org/apache/sedona/sql/functionTestScala.scala @@ -652,6 +652,71 @@ class functionTestScala assert(testtable.take(1).head.get(0) == null) } + it("ST_IsValid returns false for a geometry the validator throws on") { + // Row 3 makes JTS IsValidOp throw: near Double.MAX_VALUE its orientation test overflows. + val geoms = sparkSession + .sql(""" + |SELECT id, ST_GeomFromWKT(wkt) AS geom FROM VALUES + | (1, 'POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))'), + | (2, 'POLYGON ((0 0, 1 1, 0 1, 1 0, 0 0))'), + | (3, 'POLYGON ((-1.7e308 -10, 1.7e308 -10, 1.7e308 0, 1.7e308 10, -1.7e308 10, -1.7e308 -10), (-1e308 0, -5e307 5, -5e307 -5, -1e308 0))'), + | (4, NULL), + | (5, 'POLYGON EMPTY'), + | (6, 'POLYGON ((0 0, 10 0, 10 10, 5 10, 7 5, 3 5, 5 10, 0 10, 0 0))') + | AS t(id, wkt) + |""".stripMargin) + .repartition(3, col("id")) + val actual = geoms + .selectExpr("id", "ST_IsValid(geom)", "ST_IsValid(geom, 0)", "ST_IsValid(geom, 1)") + .collect() + .map(row => row.getInt(0) -> (1 to 3).map(i => Option(row.get(i)))) + .toMap + val expected = Map( + 1 -> Seq(Some(true), Some(true), Some(true)), + 2 -> Seq(Some(false), Some(false), Some(false)), + 3 -> Seq(Some(false), Some(false), Some(false)), + 4 -> Seq(None, None, None), + 5 -> Seq(Some(true), Some(true), Some(true)), + 6 -> Seq(Some(false), Some(false), Some(true))) + assertEquals(expected, actual) + } + + it("ST_IsValidReason and ST_IsValidDetail report the error the validator throws") { + val geoms = sparkSession + .sql(""" + |SELECT id, ST_GeomFromWKT(wkt) AS geom FROM VALUES + | (1, 'POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))'), + | (2, 'POLYGON ((-1.7e308 -10, 1.7e308 -10, 1.7e308 0, 1.7e308 10, -1.7e308 10, -1.7e308 -10), (-1e308 0, -5e307 5, -5e307 -5, -1e308 0))'), + | (3, NULL) + | AS t(id, wkt) + |""".stripMargin) + .repartition(3, col("id")) + val actual = geoms + .selectExpr( + "id", + "ST_IsValidReason(geom)", + "ST_IsValidReason(geom, 1)", + "ST_IsValidDetail(geom)", + "ST_IsValidDetail(geom, 1)") + .collect() + .map(row => row.getInt(0) -> (1 to 4).map(i => Option(row.get(i)))) + .toMap + val reason = "IllegalArgumentException: Segment vertex does not intersect ring" + val expected = Map( + 1 -> Seq( + Some("Valid Geometry"), + Some("Valid Geometry"), + Some(Row(true, null, null)), + Some(Row(true, null, null))), + 2 -> Seq( + Some(reason), + Some(reason), + Some(Row(false, reason, null)), + Some(Row(false, reason, null))), + 3 -> Seq(None, None, None, None)) + assertEquals(expected, actual) + } + it("Passed ST_ReducePrecision") { var testtable = sparkSession.sql(""" |SELECT ST_ReducePrecision(ST_GeomFromWKT('Point(0.1234567890123456789 0.1234567890123456789)'), 8) @@ -4040,48 +4105,6 @@ class functionTestScala } } - it( - "ST_IsValid, ST_IsValidReason and ST_IsValidDetail should not fail when the JTS validator throws") { - // JTS 1.20.0 throws "Segment vertex does not intersect ring" when validating this polygon - val wkt = - "POLYGON ((-1.7e308 -10, 1.7e308 -10, 1.7e308 0, 1.7e308 10, -1.7e308 10, -1.7e308 -10), " + - "(-1e308 0, -5e307 5, -5e307 -5, -1e308 0))" - val reason = "IllegalArgumentException: Segment vertex does not intersect ring" - val row = sparkSession - .sql(s"SELECT ST_GeomFromWKT('$wkt') AS geom") - .selectExpr( - "ST_IsValid(geom)", - "ST_IsValid(geom, 1)", - "ST_IsValidReason(geom)", - "ST_IsValidReason(geom, 1)", - "ST_IsValidDetail(geom)", - "ST_IsValidDetail(geom, 1)") - .first() - - assert(!row.getBoolean(0)) - assert(!row.getBoolean(1)) - assert(row.getString(2) == reason) - assert(row.getString(3) == reason) - assert(row.getAs[Row](4) == Row(false, reason, null)) - assert(row.getAs[Row](5) == Row(false, reason, null)) - } - - it("ST_IsValidReason and ST_IsValidDetail should return null for a null geometry") { - val row = sparkSession - .sql("SELECT ST_GeomFromWKT(CAST(NULL AS STRING)) AS geom") - .selectExpr( - "ST_IsValidReason(geom)", - "ST_IsValidReason(geom, 1)", - "ST_IsValidDetail(geom)", - "ST_IsValidDetail(geom, 1)") - .first() - - assert(row.isNullAt(0)) - assert(row.isNullAt(1)) - assert(row.isNullAt(2)) - assert(row.isNullAt(3)) - } - it("ST_IsValidReason should provide reasons for invalid geometries") { val testData = Seq( (5330, "POLYGON ((0 0, 3 3, 0 3, 3 0, 0 0))"),