petern48 commented on code in PR #231:
URL: https://github.com/apache/sedona-db/pull/231#discussion_r2443387214
##########
python/sedonadb/tests/functions/test_functions.py:
##########
@@ -604,6 +604,52 @@ def test_st_isclosed(eng, geom, expected):
eng.assert_query_result(f"SELECT ST_IsClosed({geom_or_null(geom)})",
expected)
[email protected]("eng", [SedonaDB, PostGIS])
[email protected](
+ ("geom", "expected"),
+ [
+ (None, None),
+ # Valid rings
+ ("LINESTRING(0 0, 0 1, 1 1, 1 0, 0 0)", True),
+ ("LINESTRING(0 0, 1 0, 1 1, 0 0)", True),
+ ("LINESTRING(0 0, 2 2, 1 2, 0 0)", True),
+ # Closed but self-intersecting - bowtie shape
+ ("LINESTRING(0 0, 0 1, 1 0, 1 1, 0 0)", False),
+ # Not closed
+ ("LINESTRING(0 0, 1 1)", False),
+ ("LINESTRING(2 0, 2 2, 3 3)", False),
+ ("LINESTRING(0 0, 2 2)", False),
+ # Empty LineString
+ ("LINESTRING EMPTY", False),
Review Comment:
```suggestion
("LINESTRING EMPTY", False),
# Collections of linestrings that would have been considered rings
("MULTILINESTRING((0 0, 0 1, 1 1, 1 0, 0 0))", False),
("GEOMETRYCOLLECTION(LINESTRING(0 0, 0 1, 1 1, 1 0, 0 0))", False),
```
Let's add these two cases. Returning `False` is the natural behavior for the
`geos`'s `is_ring()` method, which agrees with DuckDB
##########
c/sedona-geos/src/st_isring.rs:
##########
@@ -0,0 +1,178 @@
+// 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.
+
+use std::sync::Arc;
+
+use arrow_array::builder::BooleanBuilder;
+use arrow_schema::DataType;
+use datafusion_common::{error::Result, DataFusionError};
+use datafusion_expr::ColumnarValue;
+use geos::{Geom, GeometryTypes};
+use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+
+use crate::executor::GeosExecutor;
+
+/// ST_IsRing() implementation using the geos crate
+pub fn st_is_ring_impl() -> ScalarKernelRef {
+ Arc::new(STIsRing {})
+}
+
+#[derive(Debug)]
+struct STIsRing {}
+
+impl SedonaScalarKernel for STIsRing {
+ fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+ let matcher = ArgMatcher::new(
+ vec![ArgMatcher::is_geometry()],
+ SedonaType::Arrow(DataType::Boolean),
+ );
+
+ matcher.match_args(args)
+ }
+
+ fn invoke_batch(
+ &self,
+ arg_types: &[SedonaType],
+ args: &[ColumnarValue],
+ ) -> Result<ColumnarValue> {
+ let executor = GeosExecutor::new(arg_types, args);
+ let mut builder =
BooleanBuilder::with_capacity(executor.num_iterations());
+
+ executor.execute_wkb_void(|maybe_wkb| {
+ match maybe_wkb {
+ Some(wkb) => {
+ builder.append_value(invoke_scalar(&wkb)?);
+ }
+ _ => builder.append_null(),
+ }
+ Ok(())
+ })?;
+
+ executor.finish(Arc::new(builder.finish()))
+ }
+}
+
+fn invoke_scalar(geos_geom: &geos::Geometry) -> Result<bool> {
+ // Check if geometry is a LineString
+ let geom_type = geos_geom.geometry_type();
+
+ // ST_IsRing only applies to LineStrings - return false for other types
+ // This matches DuckDB spatial extension and Apache Sedona behavior
+ if geom_type != GeometryTypes::LineString {
+ return Ok(false);
+ }
+
+ // Check if the LineString is closed
+ let is_closed = geos_geom.is_closed().map_err(|e| {
+ DataFusionError::Execution(format!("Failed to check if geometry is
closed: {e}"))
+ })?;
+
+ // Check if the LineString is simple (no self-intersections)
+ let is_simple = geos_geom.is_simple().map_err(|e| {
+ DataFusionError::Execution(format!("Failed to check if geometry is
simple: {e}"))
+ })?;
+
+ Ok(is_closed && is_simple)
Review Comment:
```suggestion
Ok(geos_geom.is_ring().map_err(|e| {
DataFusionError::Execution(format!("Failed to check if geometry is a
ring: {e}"))
})?)
```
We can actually simplify this code to simply call `geos`'s `.is_ring()`
method
[here](https://github.com/georust/geos/blob/47afbad2483e489911ddb456417808340e9342c3/src/geometry.rs#L252-L264).
Seems to pass everything when I tried this locally.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]