Skip to content

[GH-3174] Align ST_IsPolygonCW/ST_IsPolygonCCW with PostGIS polygon orientation semantics - #3292

Merged
jiayuasu merged 4 commits into
apache:masterfrom
minhpham1810:GH-3174-postgis-polygon-orientation-parity
Aug 30, 2026
Merged

jiayuasu merged 4 commits into
apache:masterfrom
minhpham1810:GH-3174-postgis-polygon-orientation-parity

Conversation

@minhpham1810

Copy link
Copy Markdown
Contributor

Did you read the Contributor Guide?

Is this PR related to a ticket?

  • Yes, and the PR name follows the format [GH-XXX] my subject.

Closes #3174.

What changes were proposed in this PR?

  • Reworked isPolygonCW/isPolygonCCW in common/Functions.java to recursively inspect polygonal components (Polygon, MultiPolygon, and nested GeometryCollection at any depth) and vacuously return true for inputs with no polygonal components (points, line strings, empty geometry collections), matching PostGIS semantics. Both predicates share one recursive helper (allPolygonalComponentsMatchOrientation) to avoid duplicating the traversal logic.
  • Changed the Flink ST_IsPolygonCW/ST_IsPolygonCCW wrappers from primitive boolean to nullable Boolean with an explicit null check, so a SQL NULL input now returns NULL instead of false.
  • Updated documentation for ST_IsPolygonCW/ST_IsPolygonCCW across the SQL, Flink, and Snowflake docs, and added a breaking-change entry to the Sedona 2.0.0 release notes.
  • Added test coverage for non-polygonal geometries, nested GEOMETRYCOLLECTION recursion, and NULL propagation across the common, Spark SQL, Flink, and Snowflake test suites.

No code changes were needed in the Snowflake or Spark UDF wrappers: Snowflake's Java UDF runtime already short-circuits NULL input before invoking the handler, and Spark's InferredExpression already short-circuits NULL geometry arguments before calling the underlying common function.

Since master is already 2.0.0-SNAPSHOT, this breaking change lands directly at the major-version boundary called out in the issue, without needing a compatibility flag.

How was this patch tested?

  • mvn -pl common -Dtest=FunctionsTest test (328 tests, including new coverage for nested GeometryCollection recursion and mixed orientation)
  • mvn -pl flink -Dtest=FunctionTest#testIsPolygonCW,testIsPolygonCCW,testIsPolygonOrientationForEmptyGeometries,testIsPolygonOrientationForNonPolygonalAndCollectionGeometries,testIsPolygonOrientationNullPropagation test
  • mvn -Dsnowflake=true -pl snowflake-tester -am -DskipTests package (compiles cleanly; tests require live Snowflake credentials so were not executed)
  • Spark: added coverage to functionTestScala.scala. Could not get a full spark/common reactor build in the local dev environment due to a pre-existing, unrelated protobuf-java/generated-sources mismatch in the OSM PBF datasource reader (package proto4 does not exist); no code outside the added Scala test was touched in the Spark module, and the fix relies on InferredExpression's existing null-short-circuiting, which is exercised by many other predicates in this codebase.

Did this PR include necessary documentation updates?

  • Yes, I have updated the documentation.

- Refactor isPolygonCW and isPolygonCCW methods to handle non-polygonal geometries and nested GeometryCollections, returning true when no polygonal components are present.
- Update documentation for ST_IsPolygonCW and ST_IsPolygonCCW to reflect new behavior.
- Add tests to verify correct handling of non-polygonal geometries and null inputs in both SQL and Flink contexts.
@minhpham1810
minhpham1810 requested a review from jiayuasu as a code owner August 27, 2026 18:12
mvn spotless:apply on common/Functions.java (javadoc line wrapping)
and the snowflake-tester test files (verifySqlSingleRes call wrapping),
which CI's spotless-maven-plugin check flagged on the previous commit.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
return true;
}
// No polygonal component to violate the orientation for remaining geometry types
return true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we keep the previous behavior for a direct Java null here? It looks like null misses both branches and falls through to true, so Functions.isPolygonCW(null) and isPolygonCCW(null) now both return true instead of the previous false. The SQL wrappers seem to handle null separately, but direct common callers can still reach this. Maybe add a null guard and a small common test?

@minhpham1810
minhpham1810 requested a review from jiayuasu August 29, 2026 12:59
@jiayuasu jiayuasu added this to the sedona-2.0.0 milestone Aug 30, 2026
@jiayuasu

Copy link
Copy Markdown
Member

Thank you!

@jiayuasu
jiayuasu merged commit 920aab1 into apache:master Aug 30, 2026
45 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.

Align polygon orientation predicates with PostGIS in Sedona 2.0.0

2 participants