joonaspessi commented on code in PR #243: URL: https://github.com/apache/sedona-db/pull/243#discussion_r2463837677
########## 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: Thanks for pointing this out, nice that the array input was relatively easy to handle with ColumnarValue. Implementation is now updated with this change. Btw. Does this introduce some room for performance/memory improvement? With this approach and using scalar value, we will copy it for all geometries, potentially quite many times. Thinking if these tracks should be separated, or might be just micro optimization... ```rust let executor = GeosExecutor::new(arg_types, args); let tolerance_arg = args[1].cast_to(&DataType::Float64, None)?; let mut builder = BinaryBuilder::with_capacity( executor.num_iterations(), WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), ); match tolerance_arg { ColumnarValue::Scalar(scalar_val) => { // SCALAR PATH: Extract scalar once, reuse for all geometries ... } ColumnarValue::Array(_) => { // ARRAY PATH: Use iterator for per-row tolerances ... } } ``` -- 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]
