Skip to content

[GH-3564] Report a geometry as invalid when the JTS validator throws - #3566

Merged
jiayuasu merged 2 commits into
apache:masterfrom
rohan4naik:fix/gh-3564-isvalid-validator-exception
Nov 20, 2026
Merged

jiayuasu merged 2 commits into
apache:masterfrom
rohan4naik:fix/gh-3564-isvalid-validator-exception

Conversation

@rohan4naik

Copy link
Copy Markdown
Contributor

Did you read the Contributor Guide?

Is this PR related to a ticket?

What changes were proposed in this PR?

Problem. ST_IsValid, ST_IsValidReason and ST_IsValidDetail fail the whole query when JTS IsValidOp throws while validating a geometry (e.g. IllegalArgumentException: Segment vertex does not intersect ring for the polygon in #3564).

Cause. Functions.isValid, isValidReason and isValidDetail in common call IsValidOp.isValid() unguarded, so a RuntimeException from the validator propagates to the engine.

Change, following the fix proposed in the issue:

  • common Functions: only the isValidOp.isValid() call is guarded, catching RuntimeException.
    • isValid returns false.
    • isValidReason returns the error as the reason, e.g. IllegalArgumentException: Segment vertex does not intersect ring.
    • isValidDetail returns (false, <reason>, null).
    • isValidReason and isValidDetail return null for a null geometry.
    • isValid now reuses the existing getIsValidOpObject helper instead of repeating the flag handling.
  • Spark ST_IsValidDetail: the expression dropped the reason whenever the location was null (it returned (valid, null, null)). It now keeps the reason, serializes the location only when present, and returns NULL when the common function returns null.
  • Docs: one sentence on the new behaviour on the ST_IsValid, ST_IsValidReason and ST_IsValidDetail pages (SQL, Flink, Snowflake).

Because the fix is in common, Flink and Snowflake pick it up without changes (the Snowflake ST_IsValidDetail UDTF already serializes a null location as null).

Alternative considered. Catching in each engine wrapper (Spark/Flink/Snowflake) would have needed the same code three times and left the common Java API throwing, so the guard is in common as the issue suggests. The guard is kept around the validator call only, so errors from anything else still surface.

Note for review. Since RuntimeException is caught, Functions.isValid(null) in the common Java API now returns false rather than throwing a NullPointerException (the method returns a primitive, so it cannot return null). The SQL functions are unaffected because the engines handle NULL input before calling it. Happy to add an explicit null check that keeps throwing if you prefer.

How was this patch tested?

  • FunctionsTest (common): new tests isValidWhenValidatorThrows, isValidReasonWhenValidatorThrows, isValidDetailWhenValidatorThrows and isValidReasonAndDetailOfNullGeometry, using the polygon from the issue. All four fail without the fix (three with the IllegalArgumentException, one with a NullPointerException) and pass with it. Full FunctionsTest passes locally (352 tests).
  • functionTestScala (spark/common, Spark 3.5 / Scala 2.12): two new tests covering the three SQL functions with the issue's polygon and with a NULL geometry. Full suite passes locally (246 tests).
  • mvn spotless:check passes on common and spark/common.

I did not run the Flink, Snowflake or Python test suites locally.

Did this PR include necessary documentation updates?

  • Yes, I have updated the documentation.

This change was AI-assisted (Claude Code); I reproduced the failure and ran the tests listed above locally.

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.
@rohan4naik
rohan4naik requested a review from jiayuasu as a code owner November 20, 2026 08:45
Copilot AI balanced review requested due to automatic review settings November 20, 2026 08:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

- 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.
@jiayuasu jiayuasu added this to the sedona-2.0.0 milestone Nov 20, 2026
@jiayuasu
jiayuasu merged commit 039e75f into apache:master Nov 20, 2026
49 of 57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ST_IsValid, ST_IsValidReason and ST_IsValidDetail fail the whole job when the JTS validator throws

3 participants