petern48 commented on code in PR #219: URL: https://github.com/apache/sedona-db/pull/219#discussion_r2436263188
########## rust/sedona-functions/src/st_isclosed.rs: ########## @@ -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::{ + to_geo::{ToGeoGeometryCollection, 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<G: GeometryTrait<T = f64>>(item: G) -> Result<bool> { Review Comment: ```suggestion fn is_geometry_closed(item: &Wkb) -> Result<bool> { ``` I think this should fix the trait recursion problem. ########## rust/sedona-functions/src/st_isclosed.rs: ########## @@ -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::{ + to_geo::{ToGeoGeometryCollection, 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<G: GeometryTrait<T = f64>>(item: G) -> 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 + .to_geometry_collection() + .iter() Review Comment: ```suggestion .geometries() ``` optional nit: If you want, you can add use `geo_traits::GeometryCollectionTrait` to use this code instead. This change would've fixed one of the arms on its own, but the `to_line_string()` and `to_multi_line_string()` cases would still cause issues, so we need the above fix. -- 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]
