Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/sedonadb/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ docs/_build/

# Pyenv
.python-version

uv.lock
31 changes: 31 additions & 0 deletions python/sedonadb/tests/functions/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,37 @@ def test_st_isempty(eng, geom, expected):
eng.assert_query_result(f"SELECT ST_IsEmpty({geom_or_null(geom)})", expected)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
@pytest.mark.parametrize(
("geom", "expected"),
[
(None, None),
("LINESTRING(0 0, 1 1)", False),
("LINESTRING(0 0, 0 1, 1 1, 0 0)", True),
("MULTILINESTRING((0 0, 0 1, 1 1, 0 0),(0 0, 1 1))", False),
("POINT(0 0)", True),
("MULTIPOINT((0 0), (1 1))", True),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
("MULTIPOINT((0 0), (1 1))", True),
("MULTIPOINT((0 0), (1 1))", True),
("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", True),
("GEOMETRYCOLLECTION (LINESTRING(0 0, 0 1, 1 1, 0 0))", True),
("GEOMETRYCOLLECTION (LINESTRING(0 0, 0 1, 1 1, 0 0), LINESTRING(0 0, 1 1))", False),
("POINT EMPTY", False),
("LINESTRING EMPTY", False),
("POLYGON EMPTY", False),
("MULTIPOINT EMPTY", False),
("MULTILINESTRING EMPTY", False),
("MULTIPOLYGON EMPTY", False),
("GEOMETRYCOLLECTION EMPTY", False),
("GEOMETRYCOLLECTION (LINESTRING EMPTY)", False),

Since we're implementing more of this logic manually, we should test more comprehensively. Here are some more cases I suggest we add.

  • One non-empty geometry for each of the 7 types
  • GeometryCollection with a closed linestring and an open linestring (False)
  • Empty geometry for each of the 7 types
  • Non-empty GeometryCollection containing only empties

("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", True),
("GEOMETRYCOLLECTION (LINESTRING(0 0, 0 1, 1 1, 0 0))", True),
(
"GEOMETRYCOLLECTION (LINESTRING(0 0, 0 1, 1 1, 0 0), LINESTRING(0 0, 1 1))",
False,
),
("POINT EMPTY", False),
("LINESTRING EMPTY", False),
("POLYGON EMPTY", False),
("MULTIPOINT EMPTY", False),
("MULTILINESTRING EMPTY", False),
("MULTIPOLYGON EMPTY", False),
("GEOMETRYCOLLECTION EMPTY", False),
("GEOMETRYCOLLECTION (LINESTRING EMPTY)", False),
],
)
def test_st_isclosed(eng, geom, expected):
eng = eng.create_or_skip()
eng.assert_query_result(f"SELECT ST_IsClosed({geom_or_null(geom)})", expected)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
@pytest.mark.parametrize(
("geom", "expected"),
Expand Down
3 changes: 3 additions & 0 deletions rust/sedona-functions/benches/native-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ fn criterion_benchmark(c: &mut Criterion) {
benchmark::scalar(c, &f, "native", "st_isempty", Point);
benchmark::scalar(c, &f, "native", "st_isempty", LineString(10));

benchmark::scalar(c, &f, "native", "st_isclosed", Point);
benchmark::scalar(c, &f, "native", "st_isclosed", LineString(10));

benchmark::scalar(
c,
&f,
Expand Down
1 change: 1 addition & 0 deletions rust/sedona-functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod st_geomfromwkb;
mod st_geomfromwkt;
mod st_haszm;
pub mod st_intersection_aggr;
pub mod st_isclosed;
pub mod st_isempty;
mod st_length;
mod st_makeline;
Expand Down
1 change: 1 addition & 0 deletions rust/sedona-functions/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub fn default_function_set() -> FunctionSet {
crate::st_xyzm_minmax::st_zmax_udf,
crate::st_xyzm_minmax::st_mmin_udf,
crate::st_xyzm_minmax::st_mmax_udf,
crate::st_isclosed::st_isclosed_udf,
);

register_aggregate_udfs!(
Expand Down
182 changes: 182 additions & 0 deletions rust/sedona-functions/src/st_isclosed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// 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;
use datafusion_expr::{scalar_doc_sections::DOC_SECTION_OTHER, Documentation, Volatility};
use geo_traits::GeometryCollectionTrait;
use geo_traits::{
to_geo::{ToGeoLineString, ToGeoMultiLineString},
GeometryTrait,
};
use sedona_common::sedona_internal_err;
use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
use sedona_geometry::is_empty::is_geometry_empty;
use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
use wkb::reader::Wkb;

use crate::executor::WkbExecutor;

pub fn st_isclosed_udf() -> SedonaScalarUDF {
SedonaScalarUDF::new(
"st_isclosed",
vec![Arc::new(STIsClosed {})],
Volatility::Immutable,
Some(st_is_closed_doc()),
)
}

fn st_is_closed_doc() -> Documentation {
Documentation::builder(
DOC_SECTION_OTHER,
"Return true if the geometry is closed",
"ST_IsClosed (A: Geometry)",
)
.with_argument("geom", "geometry: Input geometry")
.with_sql_example("SELECT ST_IsClosed(ST_GeomFromWKT('LINESTRING(0 0, 1 1, 0 1, 0 0)'))")
.build()
}

#[derive(Debug)]
struct STIsClosed {}

impl SedonaScalarKernel for STIsClosed {
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: &[datafusion_expr::ColumnarValue],
) -> Result<datafusion_expr::ColumnarValue> {
let executor = WkbExecutor::new(arg_types, args);
let mut builder = BooleanBuilder::with_capacity(executor.num_iterations());

executor.execute_wkb_void(|maybe_item| {
match maybe_item {
Some(item) => {
builder.append_value(invoke_scalar(&item)?);
}
None => builder.append_null(),
}
Ok(())
})?;

executor.finish(Arc::new(builder.finish()))
}
}

fn invoke_scalar(item: &Wkb) -> Result<bool> {
is_geometry_closed(item)
}

fn is_geometry_closed(item: &Wkb) -> Result<bool> {
if is_geometry_empty(&item).map_err(|e| {
datafusion_common::error::DataFusionError::Execution(format!(
"Failed to check if geometry is empty: {e}"
))
})? {
return Ok(false);
}
match item.as_type() {
geo_traits::GeometryType::LineString(linestring) => {
Ok(linestring.to_line_string().is_closed())
}
geo_traits::GeometryType::MultiLineString(multilinestring) => {
Ok(multilinestring.to_multi_line_string().is_closed())
}
geo_traits::GeometryType::GeometryCollection(geometry_collection) => geometry_collection
.geometries()
.try_fold(true, |acc, item| {
is_geometry_closed(item).map(|is_closed| acc && is_closed)
}),
geo_traits::GeometryType::Point(_)
| geo_traits::GeometryType::MultiPoint(_)
| geo_traits::GeometryType::Polygon(_)
| geo_traits::GeometryType::MultiPolygon(_) => Ok(true),
_ => sedona_internal_err!("Invalid geometry type"),
}
}

#[cfg(test)]
mod tests {
use arrow_array::{create_array as arrow_array, ArrayRef};
use datafusion_expr::ScalarUDF;
use rstest::rstest;
use sedona_schema::datatypes::{WKB_GEOMETRY, WKB_VIEW_GEOMETRY};
use sedona_testing::{compare::assert_array_equal, testers::ScalarUdfTester};

use super::*;

#[test]
fn udf_metadata() {
let udf: ScalarUDF = st_isclosed_udf().into();
assert_eq!(udf.name(), "st_isclosed");
assert!(udf.documentation().is_some());
}

#[rstest]
fn udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) {
use datafusion_common::ScalarValue;

let tester = ScalarUdfTester::new(st_isclosed_udf().into(), vec![sedona_type.clone()]);

tester.assert_return_type(DataType::Boolean);

let result = tester
.invoke_wkb_scalar(Some("LINESTRING(0 0, 1 1, 0 1, 0 0)"))
.unwrap();
tester.assert_scalar_result_equals(result, ScalarValue::Boolean(Some(true)));

let result = tester.invoke_wkb_scalar(None).unwrap();
tester.assert_scalar_result_equals(result, ScalarValue::Null);

let input_wkt = vec![
None,
Some("LINESTRING(0 0, 1 1)"),
Some("LINESTRING(0 0, 0 1, 1 1, 0 0)"),
Some("MULTILINESTRING((0 0, 0 1, 1 1, 0 0),(0 0, 1 1))"),
Some("POINT(0 0)"),
Some("MULTIPOINT((0 0), (1 1))"),
Some("LINESTRING EMPTY"),
Some("POINT EMPTY"),
];
let expected: ArrayRef = arrow_array!(
Boolean,
[
None,
Some(false),
Some(true),
Some(false),
Some(true),
Some(true),
Some(false),
Some(false)
]
);
assert_array_equal(&tester.invoke_wkb_array(input_wkt).unwrap(), &expected);
}
}