petern48 commented on code in PR #399: URL: https://github.com/apache/sedona-db/pull/399#discussion_r2581786060
########## c/sedona-geos/src/st_exteriorring.rs: ########## @@ -0,0 +1,162 @@ +// 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::GenericBinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{GResult, Geom, GeometryTypes::Polygon}; +use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel}; +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; + +use crate::executor::GeosExecutor; + +pub fn st_exterior_ring_impl() -> ScalarKernelRef { + Arc::new(STExteriorRing {}) +} + +#[derive(Debug)] +struct STExteriorRing {} + +impl SedonaScalarKernel for STExteriorRing { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ArgMatcher::is_geometry()], + SedonaType::Arrow(DataType::Binary), + ); + 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 = GenericBinaryBuilder::<i32>::with_capacity( + executor.num_iterations(), + executor.num_iterations() * 100, + ); Review Comment: You'll also want to udpate this too. ########## c/sedona-geos/src/st_exteriorring.rs: ########## @@ -0,0 +1,162 @@ +// 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::GenericBinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{GResult, Geom, GeometryTypes::Polygon}; +use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel}; +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; + +use crate::executor::GeosExecutor; + +pub fn st_exterior_ring_impl() -> ScalarKernelRef { + Arc::new(STExteriorRing {}) +} + +#[derive(Debug)] +struct STExteriorRing {} + +impl SedonaScalarKernel for STExteriorRing { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ArgMatcher::is_geometry()], + SedonaType::Arrow(DataType::Binary), Review Comment: While yes, we represent geometries as binary datatypes, we have a special geometry datatype touse for convenience. See the reference file. ########## c/sedona-geos/src/st_exteriorring.rs: ########## @@ -0,0 +1,162 @@ +// 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::GenericBinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{error::Result, DataFusionError}; +use datafusion_expr::ColumnarValue; +use geos::{GResult, Geom, GeometryTypes::Polygon}; +use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel}; +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; + +use crate::executor::GeosExecutor; + +pub fn st_exterior_ring_impl() -> ScalarKernelRef { + Arc::new(STExteriorRing {}) +} + +#[derive(Debug)] +struct STExteriorRing {} + +impl SedonaScalarKernel for STExteriorRing { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ArgMatcher::is_geometry()], + SedonaType::Arrow(DataType::Binary), + ); + 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 = GenericBinaryBuilder::<i32>::with_capacity( + executor.num_iterations(), + executor.num_iterations() * 100, + ); + + executor.execute_wkb_void(|maybe_wkb| { + match maybe_wkb { + Some(wkb) => { + let result_wkb = invoke_scalar(&wkb).map_err(|e| { + DataFusionError::Execution(format!("Failed to get exterior ring: {e}")) + })?; + + match result_wkb { + Some(val) => builder.append_value(val), + None => builder.append_null(), + } + } + _ => builder.append_null(), + } + Ok(()) + })?; + + executor.finish(Arc::new(builder.finish())) + } +} + +fn invoke_scalar(geos_geom: &geos::Geometry) -> GResult<Option<Vec<u8>>> { + match geos_geom.geometry_type() { + Polygon => { + let ring = geos_geom.get_exterior_ring()?; + Ok(Some(ring.to_wkb()?.into())) + } + _ => Ok(None), + } +} + +#[cfg(test)] +mod tests { + use arrow_array::{builder::GenericBinaryBuilder, ArrayRef}; + use datafusion_common::ScalarValue; + use geos::Geom; + use rstest::rstest; + use sedona_expr::scalar_udf::SedonaScalarUDF; + use sedona_schema::datatypes::{WKB_GEOMETRY, WKB_VIEW_GEOMETRY}; + use sedona_testing::testers::ScalarUdfTester; + + use super::*; + + fn wkt_to_wkb(wkt: &str) -> Vec<u8> { + geos::Geometry::new_from_wkt(wkt) + .expect("Invalid WKT in test expectation") + .to_wkb() + .expect("Failed to convert to WKB") + .into() + } + + fn build_expected_geometry_array(wkts: Vec<Option<&str>>) -> ArrayRef { Review Comment: If you reference the test code in the reference file I mentioned above, you'll probably find some useful methods that you can use to avoid trying to do this manual logic yourself. -- 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]
