Copilot commented on code in PR #267: URL: https://github.com/apache/sedona-db/pull/267#discussion_r2482677382
########## rust/sedona-functions/src/st_translate.rs: ########## @@ -0,0 +1,223 @@ +// 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 arrow_array::builder::BinaryBuilder; +use arrow_schema::DataType; +use datafusion_common::{cast::as_float64_array, error::Result, DataFusionError}; +use datafusion_expr::{ + scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, Volatility, +}; + +use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}; +use sedona_geometry::{ + error::SedonaGeometryError, + transform::{transform, CrsTransform}, + wkb_factory::WKB_MIN_PROBABLE_BYTES, +}; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::{iter::zip, sync::Arc}; + +use crate::executor::WkbExecutor; + +/// ST_Translate() scalar UDF +pub fn st_translate_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_translate", + vec![Arc::new(STTranslate)], + Volatility::Immutable, + Some(st_translate_doc()), + ) +} + +fn st_translate_doc() -> Documentation { + Documentation::builder( + DOC_SECTION_OTHER, + "Update coordinates of geom by a fixed offset", + "ST_Translate (geom: Geometry, deltax: numeric, deltay: numeric)", + ) + .with_argument("geom", "geometry: Input geometry") + .with_argument("deltax", "numeric: X value difference") + .with_argument("deltay", "numeric: Y value difference") + .with_sql_example("SELECT ST_Translate(ST_GeomFromWKT('LINESTRING(0 1, 2 3, 4 5)'), 2.0, 3.0)") + .build() +} + +#[derive(Debug)] +struct STTranslate; + +impl SedonaScalarKernel for STTranslate { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new( + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), + ArgMatcher::is_numeric(), + ], + WKB_GEOMETRY, + ); + + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = WkbExecutor::new(arg_types, args); + let mut builder = BinaryBuilder::with_capacity( + executor.num_iterations(), + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), + ); + + let deltax = args[1] + .cast_to(&DataType::Float64, None)? + .to_array(executor.num_iterations())?; + let deltay = args[2] + .cast_to(&DataType::Float64, None)? + .to_array(executor.num_iterations())?; + let deltax_array = as_float64_array(&deltax)?; + let deltay_array = as_float64_array(&deltay)?; + let mut delta_iter = zip(deltax_array, deltay_array); + + executor.execute_wkb_void(|maybe_wkb| { + let (deltax, deltay) = delta_iter.next().unwrap(); + match (maybe_wkb, deltax, deltay) { + (Some(wkb), Some(deltax), Some(deltay)) => { + let trans = Translate { deltax, deltay }; + transform(wkb, &trans, &mut builder) + .map_err(|e| DataFusionError::External(Box::new(e)))?; + builder.append_value([]); Review Comment: The builder is appending an empty byte slice after transforming the geometry. The transformed WKB bytes should be appended instead. The `transform` function at line 104 likely writes to the builder, so this line should either be removed or the builder should append the actual transformed bytes. ```suggestion ``` -- 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]
