yutannihilation commented on code in PR #504: URL: https://github.com/apache/sedona-db/pull/504#discussion_r2688548371
########## rust/sedona-functions/src/st_affine.rs: ########## @@ -0,0 +1,450 @@ +// 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, Array}; +use arrow_schema::DataType; +use datafusion_common::error::Result; +use datafusion_expr::{ + scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, Volatility, +}; +use geo_traits::GeometryTrait; +use sedona_expr::{ + item_crs::ItemCrsKernel, + scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}, +}; +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; +use sedona_schema::{ + datatypes::{SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use std::sync::Arc; + +use crate::{executor::WkbExecutor, st_affine_helpers}; + +/// ST_Affine() scalar UDF +/// +/// Native implementation for affine transformation +pub fn st_affine_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_affine", + ItemCrsKernel::wrap_impl(vec![ + Arc::new(STAffine { is_3d: true }), + Arc::new(STAffine { is_3d: false }), + ]), + Volatility::Immutable, + Some(st_affine_doc()), + ) +} + +fn st_affine_doc() -> Documentation { + Documentation::builder( + DOC_SECTION_OTHER, + "Apply an affine transformation to the given geometry.", + "ST_Affine (geom: Geometry, a: Double, b: Double, c: Double, d: Double, e: Double, f: Double, g: Double, h: Double, i: Double, xOff: Double, yOff: Double, zOff: Double)", + ) + .with_argument("geom", "geometry: Input geometry") + .with_argument("a", "a component of the affine matrix") + .with_argument("b", "a component of the affine matrix") + .with_argument("c", "a component of the affine matrix") + .with_argument("d", "a component of the affine matrix") + .with_argument("e", "a component of the affine matrix") + .with_argument("f", "a component of the affine matrix") + .with_argument("g", "a component of the affine matrix") + .with_argument("h", "a component of the affine matrix") + .with_argument("i", "a component of the affine matrix") + .with_argument("xOff", "X offset") + .with_argument("yOff", "Y offset") + .with_argument("zOff", "Z offset") + .with_sql_example("SELECT ST_Affine(ST_GeomFromText('POLYGON Z ((1 0 1, 1 1 1, 2 2 2, 1 0 1))'), 1, 2, 4, 1, 1, 2, 3, 2, 5, 4, 8, 3)") + .build() +} + +#[derive(Debug)] +struct STAffine { + is_3d: bool, +} + +impl SedonaScalarKernel for STAffine { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let arg_matchers = if self.is_3d { + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), // a + ArgMatcher::is_numeric(), // b + ArgMatcher::is_numeric(), // c + ArgMatcher::is_numeric(), // d + ArgMatcher::is_numeric(), // e + ArgMatcher::is_numeric(), // f + ArgMatcher::is_numeric(), // g + ArgMatcher::is_numeric(), // h + ArgMatcher::is_numeric(), // i + ArgMatcher::is_numeric(), // xOff + ArgMatcher::is_numeric(), // yOff + ArgMatcher::is_numeric(), // zOff + ] + } else { + vec![ + ArgMatcher::is_geometry(), + ArgMatcher::is_numeric(), // a + ArgMatcher::is_numeric(), // b + ArgMatcher::is_numeric(), // d + ArgMatcher::is_numeric(), // e + ArgMatcher::is_numeric(), // xOff + ArgMatcher::is_numeric(), // yOff + ] + }; + + let matcher = ArgMatcher::new(arg_matchers, 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 array_args = args[1..] + .iter() + .map(|arg| { + arg.cast_to(&DataType::Float64, None)? + .to_array(executor.num_iterations()) + }) + .collect::<Result<Vec<Arc<dyn Array>>>>()?; Review Comment: I see, thanks for the details. Then, I'll leave this for now. -- 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]
