Skip to content
Open
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
1 change: 1 addition & 0 deletions c/sedona-geos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod st_issimple;
mod st_isvalid;
mod st_isvalidreason;
mod st_length;
mod st_numinteriorrings;
mod st_perimeter;
mod st_reverse;
mod st_simplifypreservetopology;
Expand Down
2 changes: 2 additions & 0 deletions c/sedona-geos/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
st_isvalid::st_is_valid_impl,
st_isvalidreason::st_is_valid_reason_impl,
st_length::st_length_impl,
st_numinteriorrings::st_num_interior_rings_impl,
st_perimeter::st_perimeter_impl,
st_reverse::st_reverse_impl,
st_simplifypreservetopology::st_simplify_preserve_topology_impl,
Expand Down Expand Up @@ -67,6 +68,7 @@ pub fn scalar_kernels() -> Vec<(&'static str, ScalarKernelRef)> {
("st_isvalidreason", st_is_valid_reason_impl()),
("st_length", st_length_impl()),
("st_overlaps", st_overlaps_impl()),
("st_numinteriorrings", st_num_interior_rings_impl()),
("st_perimeter", st_perimeter_impl()),
("st_reverse", st_reverse_impl()),
(
Expand Down
131 changes: 131 additions & 0 deletions c/sedona-geos/src/st_numinteriorrings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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::Int32Builder;
use arrow_schema::DataType;
use datafusion_common::{error::Result, DataFusionError};
use datafusion_expr::ColumnarValue;
use geos::Geom;
use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};

use crate::executor::GeosExecutor;

/// ST_NumInteriorRings() implementation using the geos crate
pub fn st_num_interior_rings_impl() -> ScalarKernelRef {
Arc::new(STNumInteriorRings {})
}

#[derive(Debug)]
struct STNumInteriorRings {}

impl SedonaScalarKernel for STNumInteriorRings {
fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
let matcher = ArgMatcher::new(
vec![ArgMatcher::is_geometry()],
SedonaType::Arrow(DataType::Int32),
);

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 = Int32Builder::with_capacity(executor.num_iterations());

// single-geometry executor path
executor.execute_wkb_void(|geom| {
match geom {
Some(g) => {
let n = invoke_scalar(g)?;
builder.append_value(n);
}
None => builder.append_null(),
}
Ok(())
})?;

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

fn invoke_scalar(geos_geom: &geos::Geometry) -> Result<i32> {
// geos::Geometry provides get_num_interior_rings() -> GResult<usize>
let count = geos_geom.get_num_interior_rings().map_err(|e| {
DataFusionError::Execution(format!("Failed to get num interior rings: {e}"))
})?;

// safe to cast to i32 for SQL integer return
Ok(count as i32)
}

#[cfg(test)]
mod tests {
use arrow_array::{create_array as arrow_array, ArrayRef};
use datafusion_common::ScalarValue;
use rstest::rstest;
use sedona_expr::scalar_udf::SedonaScalarUDF;
use sedona_schema::datatypes::{WKB_GEOMETRY, WKB_VIEW_GEOMETRY};
use sedona_testing::compare::assert_array_equal;
use sedona_testing::create::create_array;
use sedona_testing::testers::ScalarUdfTester;

use super::*;

#[rstest]
fn udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) {
let udf = SedonaScalarUDF::from_kernel("st_numinteriorrings", st_num_interior_rings_impl());
let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone()]);
tester.assert_return_type(DataType::Int32);

// scalar-scalar: polygon with two interior rings -> 2
let result = tester
.invoke_scalar_scalar(
"POLYGON((0 0,10 0,10 6,0 6,0 0),(1 1,2 1,2 5,1 5,1 1),(8 5,8 4,9 4,9 5,8 5))", // returns 2
"POLYGON((0 0,10 0,10 6,0 6,0 0))", // second arg ignored, only single-arg UDF tester will pass one value, but keep pattern consistent
)
.unwrap();

// Note: Above tester.invoke_scalar_scalar in your testing harness for single-arg UDFs
// may accept two parameters; if not, use invoke_scalar("WKT") variant available in the tester.
tester.assert_scalar_result_equals(result, 2_i32);

// Nulls -> Null
let result = tester
.invoke_scalar_scalar(ScalarValue::Null, ScalarValue::Null)
.unwrap();
assert!(result.is_null());

// array-array style: mix of polygon, non-polygon and null
let lhs = create_array(
&[
Some("POLYGON((0 0,10 0,10 6,0 6,0 0),(1 1,2 1,2 5,1 5,1 1))"), //returns 1
Some("POINT (5 5)"), //returns 0 only out circle is there
None,
],
&WKB_GEOMETRY,
);

let expected: ArrayRef = arrow_array!(Int32, [Some(1), Some(0), None]);
assert_array_equal(&tester.invoke_array_array(lhs).unwrap(), &expected);
}
}
75 changes: 75 additions & 0 deletions python/sedonadb/tests/functions/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2028,3 +2028,78 @@ def test_st_simplifypreservetopology(eng, geom, tolerance, expected):
def test_st_zmflag(eng, geom, expected):
eng = eng.create_or_skip()
eng.assert_query_result(f"SELECT ST_ZmFlag({geom_or_null(geom)})", expected)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
@pytest.mark.parametrize(
("geom", "expected"),
[
(None, None), # NULL -> NULL
("POINT (1 2)", None), # non-polygon -> NULL
("LINESTRING (0 0, 1 1, 2 2)", None), # non-polygon -> NULL
("POLYGON EMPTY", 0), # empty polygon has no interior rings
("POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0))", 0), # simple polygon, no holes
(
# polygon with one hole
"POLYGON (\
(0 0, 10 0, 10 10, 0 10, 0 0),\
(2 2, 8 2, 8 8, 2 8, 2 2))",
1,
),
(
# polygon with two holes
"POLYGON (\
(0 0, 10 0, 10 10, 0 10, 0 0),\
(2 2, 4 2, 4 4, 2 4, 2 2),\
(6 6, 8 6, 8 8, 6 8, 6 6))",
2,
),
(
# MultiPolygon (PostGIS: ST_NumInteriorRings returns NULL on multipolygons)
"MULTIPOLYGON (\
((0 0, 5 0, 5 5, 0 5, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1)),\
((10 10, 14 10, 14 14, 10 14, 10 10)))",
None,
),
(
# GeometryCollection -> NULL
"GEOMETRYCOLLECTION (\
POINT (1 2),\
POLYGON ((0 0, 3 0, 3 3, 0 3, 0 0)))",
None,
),
],
)
def test_st_numinteriorrings_basic(eng, geom, expected):
eng = eng.create_or_skip()
eng.assert_query_result(
f"SELECT ST_NumInteriorRings({geom_or_null(geom)})",
expected,
)


@pytest.mark.parametrize("eng", [SedonaDB, PostGIS])
def test_st_numinteriorrings_multipolygon_sum_via_dump(eng):
"""
Total number of interior rings in a MULTIPOLYGON, using ST_Dump then SUM.
Works the same on PostGIS and SedonaDB.
"""
eng = eng.create_or_skip()

# Three polygons: first has 1 hole,Second has 0 since its not a ring in first place, Third has 0 holes -> total = 1
mp = (
"MULTIPOLYGON ("
" ((0 0, 5 0, 5 5, 0 5, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1)),"
"((0 0, 5 0, 5 5, 0 5, 0 1), (1 1, 2 1, 2 2, 1 2, 1 2)),"
" ((10 10, 14 10, 14 14, 10 14, 10 10))"
")"
)

sql = f"""
WITH g AS (
SELECT ST_GeomFromText('{mp}', 0) AS geom
)
SELECT COALESCE(SUM(ST_NumInteriorRings((ST_Dump(geom)).geom)), 0)
FROM g
"""
eng.assert_query_result(sql, 1)
Loading