paleolimbot commented on code in PR #243: URL: https://github.com/apache/sedona-db/pull/243#discussion_r2463152774
########## c/sedona-geos/src/st_simplifypreservetopology.rs: ########## @@ -0,0 +1,203 @@ +// 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::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::error::Result; +use datafusion_common::DataFusionError; +use datafusion_expr::ColumnarValue; +use geos::Geom; +use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; + +use crate::executor::GeosExecutor; + +/// ST_SimplifyPreserveTopology() implementation using the geos crate +pub fn st_simplify_preserve_topology_impl() -> ScalarKernelRef { + Arc::new(STSimplifyPreserveTopology {}) +} + +#[derive(Debug)] +struct STSimplifyPreserveTopology {} + +impl SedonaScalarKernel for STSimplifyPreserveTopology { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ArgMatcher::is_geometry(), ArgMatcher::is_numeric()], + WKB_GEOMETRY, + ); + + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let tolerance: Option<f64>; + let arg1 = args[1].cast_to(&DataType::Float64, None)?; + if let ColumnarValue::Scalar(scalar_arg) = &arg1 { + if scalar_arg.is_null() { + tolerance = None; + } else { + tolerance = Some(f64::try_from(scalar_arg.clone())?); + } + } else { + return Err(DataFusionError::Execution(format!( + "Invalid tolerance: {:?}", + args[1] + ))); + } Review Comment: While you're here, I think it would be good to support this as an array input as well (it's roughly the same amount of code). The example I wrote up for `ST_Buffer()` was: ```rust let distance_value = args[1].cast_to(&DataType::Float64)?.to_array(executor.num_iterations()); let distance_array = if let ColumnarValue::Array(array_ref) = distance_value { as_float64_array(array_ref)? } else { return sedona_internal_err!("Unexpected scalar value"); }; let mut distance_iter = distance_array.iter(); // ... executor.execute_wkb_void(|wkb| { match (wkb, distance_iter.next().unwrap() { (Some(wkb_item), Some(distance_item)) => { invoke_scalar(...) } _ => { /* append null */ } } }) ``` -- 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]
