petern48 commented on code in PR #288: URL: https://github.com/apache/sedona-db/pull/288#discussion_r2507384212
########## c/sedona-geos/src/st_reverse.rs: ########## @@ -0,0 +1,124 @@ +// 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 datafusion_common::{error::Result, 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_Reverse() implementation using the geos crate +pub fn st_reverse_impl() -> ScalarKernelRef { + Arc::new(STReverse {}) +} + +#[derive(Debug)] +struct STReverse {} + +impl SedonaScalarKernel for STReverse { + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { + let matcher = ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY); + + matcher.match_args(args) + } + + fn invoke_batch( + &self, + arg_types: &[SedonaType], + args: &[ColumnarValue], + ) -> Result<ColumnarValue> { + let executor = GeosExecutor::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_wkb| { + match maybe_wkb { + Some(wkb) => { + invoke_scalar(&wkb, &mut builder)?; + builder.append_value([]); + } + _ => builder.append_null(), + } + + Ok(()) + })?; + + executor.finish(Arc::new(builder.finish())) + } +} + +fn invoke_scalar(geos_geom: &geos::Geometry, writer: &mut impl std::io::Write) -> Result<()> { + let geometry = geos_geom + .get_centroid() + .map_err(|e| DataFusionError::Execution(format!("Failed to calculate centroid: {e}")))?; + Review Comment: Alright, **here's the main change of the entire PR**. `invoke_scalar` here is called above to perform the actual operation on each of the inputs rows. It still uses `get_centroid`. I need to figure out how to perform the reverse operation. Because of VS Code's great Rust extension, I can `Ctrl + click` the `get_centroid()` function and my IDE will automatically open the library file this function is located in, which is [here](https://github.com/georust/geos/blob/47afbad2483e489911ddb456417808340e9342c3/src/geometry.rs#L905). If your IDE doesn't do that you can use that link above to go to the file. Now I `Ctrl + f` and search for a reverse function. I find it [here](https://github.com/georust/geos/blob/47afbad2483e489911ddb456417808340e9342c3/src/geometry.rs#L2829). Hooray! there's an easy method that exists that will do all the work for me. In the next commit, I change the `.get_centroid()` call to `.reverse()`. Notice: STCentroid's function wasn't called `.centroid()`, it was called `.get_centroid()`. Keep that in mind when you're searching for the function: it might not be named exactly what you'd guess. It will likely at least be in `snake_case` rather than `CamelCase`. **NOTE**: If you don't find a convenient function (which is entirely possible because `geos` Rust might not have been implemented yet), you should probably STOP and try a different function. Unless you're particularly interested in that function, maybe try asking for help. -- 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]
