petern48 commented on code in PR #387: URL: https://github.com/apache/sedona-db/pull/387#discussion_r2579570109
########## c/sedona-geos/src/st_nrings.rs: ########## @@ -0,0 +1,202 @@ +// 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 crate::executor::GeosExecutor; +use arrow_array::builder::Int32Builder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{Geom, Geometry, GeometryTypes}; +use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel}; +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; + +pub fn st_nrings_impl() -> ScalarKernelRef { + Arc::new(STNRings {}) +} + +#[derive(Debug)] +struct STNRings {} + +impl SedonaScalarKernel for STNRings { + 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()); + executor.execute_wkb_void(|maybe_geom| { + match maybe_geom { + None => builder.append_null(), + Some(geom) => { + let res = invoke_scalar(&geom)?; + match res { + Some(n) => builder.append_value(n), + None => builder.append_null(), + } + } + } + Ok(()) + })?; + executor.finish(Arc::new(builder.finish())) + } +} + +fn invoke_scalar(geom: &Geometry) -> Result<Option<i32>> { + match geom.geometry_type() { + GeometryTypes::Polygon => { + if geom + .is_empty() + .map_err(|e| DataFusionError::Execution(format!("{e}")))? + { + return Ok(Some(0)); + } + let num_interior = geom + .get_num_interior_rings() + .map_err(|e| DataFusionError::Execution(format!("{e}")))?; + Ok(Some((num_interior + 1) as i32)) + } + GeometryTypes::MultiPolygon | GeometryTypes::GeometryCollection => { + if geom + .is_empty() + .map_err(|e| DataFusionError::Execution(format!("{e}")))? + { + return Ok(Some(0)); + } + let total = count_rings_recursive(geom)?; + Ok(Some(total)) + } + _ => Ok(Some(0)), + } +} + +fn count_rings_recursive<G: Geom>(geom: &G) -> Result<i32> { Review Comment: I'm seeing a lot of duplicated logic between this function, `count_rings_recursive`, and `invoke_scalar`. The function signatures are *basically* the same. I think it's very possible to reduce this to one function. `count_rings_recursive` seems to be the one we want here. 1) It doesn't return an Option<i32>. 2) It already has the recursive call we need here. How about we delete the original `invoke_scalar` above and rename this function to `invoke_scalar`. ########## c/sedona-geos/src/st_nrings.rs: ########## @@ -0,0 +1,202 @@ +// 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 crate::executor::GeosExecutor; +use arrow_array::builder::Int32Builder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{Geom, Geometry, GeometryTypes}; +use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel}; +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; + +pub fn st_nrings_impl() -> ScalarKernelRef { + Arc::new(STNRings {}) +} + +#[derive(Debug)] +struct STNRings {} + +impl SedonaScalarKernel for STNRings { + 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()); + executor.execute_wkb_void(|maybe_geom| { + match maybe_geom { + None => builder.append_null(), + Some(geom) => { + let res = invoke_scalar(&geom)?; + match res { + Some(n) => builder.append_value(n), + None => builder.append_null(), + } Review Comment: ```suggestion builder.append_value(invoke_scalar(&geom)?; ``` Looking at your code below, there aren't any cases that return `None`, so we should go ahead and avoid returning an Option here, so we don't have to handle `None`. In other words, return `Result<i32>` instead of `Result<Option<i32>>`. This will happen naturally if you follow the suggestion I made below about using the `count_rings_recursive` function. ########## python/sedonadb/tests/functions/test_functions.py: ########## @@ -2774,3 +2774,40 @@ def test_st_numpoints(eng, geom, expected): f"SELECT ST_NumPoints({geom_or_null(geom)})", expected, ) + + [email protected]("eng", [SedonaDB, PostGIS]) [email protected]( + ("geom", "expected"), + [ + (None, None), + ("POINT (1 2)", 0), + ("LINESTRING (0 0, 1 1, 2 2)", 0), + ("MULTIPOINT ((0 0), (1 1))", 0), + ("MULTILINESTRING ((0 0, 1 1), (2 2, 3 3))", 0), + ("POLYGON EMPTY", 0), + ("MULTIPOLYGON EMPTY", 0), Review Comment: ```suggestion ("POINT EMPTY", 0), ("MULTIPOINT EMPTY", 0), ("LINESTRING EMPTY", 0), ("MULTILINESTRING EMPTY", 0), ("POLYGON EMPTY", 0), ("MULTIPOLYGON EMPTY", 0), ("GEOMETRYCOLLECTION EMPTY", 0), ``` -- 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]
