jesspav commented on code in PR #67: URL: https://github.com/apache/sedona-db/pull/67#discussion_r2344526332
########## rust/sedona-functions/src/st_flipcoordinates.rs: ########## @@ -0,0 +1,283 @@ +// 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, vec}; + +use crate::executor::WkbExecutor; +use arrow_array::builder::BinaryBuilder; +use datafusion_common::error::{DataFusionError, Result}; +use datafusion_expr::{ + scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, Volatility, +}; +use geo_traits::{ + CoordTrait, Dimensions, GeometryCollectionTrait, GeometryTrait, GeometryType, LineStringTrait, + MultiLineStringTrait, MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait, +}; +use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF}; +use sedona_geometry::{ + error::SedonaGeometryError, + wkb_factory::{ + write_wkb_coord, write_wkb_empty_point, write_wkb_geometrycollection_header, + write_wkb_linestring_header, write_wkb_multilinestring_header, write_wkb_multipoint_header, + write_wkb_multipolygon_header, write_wkb_point_header, write_wkb_polygon_header, + write_wkb_polygon_ring_header, WKB_MIN_PROBABLE_BYTES, + }, +}; + +use sedona_schema::{ + datatypes::{Edges, SedonaType, WKB_GEOMETRY}, + matchers::ArgMatcher, +}; +use wkb::reader::Wkb; + +/// ST_FlipCoordinates() scalar UDF implementation +/// +/// An implementation of flip coordinates +pub fn st_flipcoordinates_udf() -> SedonaScalarUDF { + SedonaScalarUDF::new( + "st_flipcoordinates", + vec![Arc::new(STFlipCoordinates {})], + Volatility::Immutable, + Some(st_flipcoordinates_doc()), + ) +} + +fn st_flipcoordinates_doc() -> Documentation { + Documentation::builder( + DOC_SECTION_OTHER, + "Returns a version of the given geometry with X and Y axis flipped.", + "ST_FlipCoordinates(A:geometry)", + ) + .with_argument("geom", "geometry: Input geometry") + .with_sql_example("SELECT ST_FlipCoordinates(df.geometry)") + .build() +} + +#[derive(Debug)] +struct STFlipCoordinates {} + +impl SedonaScalarKernel for STFlipCoordinates { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY); + + if matcher.match_args(args).is_ok() { + match &args[0] { + SedonaType::Wkb(_, crs) | SedonaType::WkbView(_, crs) => { + Ok(Some(SedonaType::Wkb(Edges::Planar, crs.clone()))) + } + _ => Ok(Some(WKB_GEOMETRY)), + } + } else { + Ok(None) + } + } + + 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(), + ); + + executor.execute_wkb_void(|maybe_item| { + match maybe_item { + Some(item) => { + invoke_scalar(&item, &mut builder)?; + builder.append_value([]); + } + None => builder.append_null(), + } + Ok(()) + })?; + + executor.finish(Arc::new(builder.finish())) + } +} + +fn invoke_scalar(wkb: &Wkb, writer: &mut impl std::io::Write) -> Result<()> { + swap_yx(wkb, writer).map_err(|e| DataFusionError::External(e.into())) +} + +fn swap_yx( + geom: impl GeometryTrait<T = f64>, + writer: &mut impl std::io::Write, +) -> Result<(), SedonaGeometryError> { + let dims = geom.dim(); + + match geom.as_type() { Review Comment: 10% regression on first run: ``` native-st_flipcoordinates-Array(Point) time: [2.4731 ms 2.4816 ms 2.4903 ms] change: [+8.6002% +9.1592% +9.7010%] (p = 0.00 < 0.05) Performance has regressed. Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) high mild native-st_flipcoordinates-Array(LineString(10)) time: [11.046 ms 11.166 ms 11.290 ms] change: [+8.5195% +10.982% +13.499%] (p = 0.00 < 0.05) Performance has regressed. Found 4 outliers among 100 measurements (4.00%) 4 (4.00%) high mild ``` A faster on the second run: ``` native-st_flipcoordinates-Array(Point) time: [2.4643 ms 2.4716 ms 2.4791 ms] change: [-0.8520% -0.4027% +0.0647%] (p = 0.09 > 0.05) No change in performance detected. Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) high mild native-st_flipcoordinates-Array(LineString(10)) time: [10.530 ms 10.607 ms 10.687 ms] change: [-6.2650% -5.0094% -3.7191%] (p = 0.00 < 0.05) Performance has improved. Found 12 outliers among 100 measurements (12.00%) ``` I'll re-run the other version. -- 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]
