paleolimbot commented on code in PR #73:
URL: https://github.com/apache/sedona-db/pull/73#discussion_r2345064736


##########
rust/sedona-geo/src/st_dwithin.rs:
##########
@@ -0,0 +1,166 @@
+// 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::BooleanBuilder;
+use arrow_schema::DataType;
+use datafusion_common::{error::Result, DataFusionError};
+use datafusion_expr::ColumnarValue;
+use geo::{Distance, Euclidean};
+use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
+use sedona_functions::executor::WkbExecutor;
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+use wkb::reader::Wkb;
+
+use crate::to_geo::item_to_geometry;
+
+/// ST_DWithin() implementation using [Euclidean] [Distance]
+pub fn st_dwithin_impl() -> ScalarKernelRef {
+    Arc::new(STDWithin {})
+}
+
+#[derive(Debug)]
+struct STDWithin {}
+
+impl SedonaScalarKernel for STDWithin {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![
+                ArgMatcher::is_geometry(),
+                ArgMatcher::is_geometry(),
+                ArgMatcher::is_numeric(),
+            ],
+            SedonaType::Arrow(DataType::Boolean),
+        );
+
+        matcher.match_args(args)
+    }
+
+    fn invoke_batch(
+        &self,
+        arg_types: &[SedonaType],
+        args: &[ColumnarValue],
+    ) -> Result<ColumnarValue> {
+        // Extract the constant scalar value before looping over the input 
geometries
+        let distance: Option<f64>;
+        let arg2 = args[2].cast_to(&DataType::Float64, None)?;
+        if let ColumnarValue::Scalar(scalar_arg) = &arg2 {
+            if scalar_arg.is_null() {
+                distance = None;
+            } else {
+                distance = Some(f64::try_from(scalar_arg.clone())?);
+            }
+        } else {
+            return Err(DataFusionError::Execution(format!(
+                "Invalid distance: {:?}",
+                args[2]
+            )));
+        }
+
+        let executor = WkbExecutor::new(arg_types, args);
+        let mut builder = 
BooleanBuilder::with_capacity(executor.num_iterations());
+        executor.execute_wkb_wkb_void(|maybe_wkb0, maybe_wkb1| {
+            match (maybe_wkb0, maybe_wkb1, distance) {
+                (Some(wkb0), Some(wkb1), Some(distance)) => {
+                    builder.append_value(invoke_scalar(wkb0, wkb1, distance)?);
+                }
+                _ => builder.append_null(),
+            }
+
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn invoke_scalar(wkb_a: &Wkb, wkb_b: &Wkb, distance_bound: f64) -> 
Result<bool> {
+    let geom_a = item_to_geometry(wkb_a)?;
+    let geom_b = item_to_geometry(wkb_b)?;

Review Comment:
   Just double-checking: do we need these conversions, or is the distance 
metric implemented for the generic case?
   
   Also, it should be more efficient to use the `GeoTypesExecutor`, which 
should ensure that scalar inputs on either the right or left are only converted 
once.



##########
rust/sedona-geo/src/st_dwithin.rs:
##########
@@ -0,0 +1,166 @@
+// 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::BooleanBuilder;
+use arrow_schema::DataType;
+use datafusion_common::{error::Result, DataFusionError};
+use datafusion_expr::ColumnarValue;
+use geo::{Distance, Euclidean};
+use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
+use sedona_functions::executor::WkbExecutor;
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+use wkb::reader::Wkb;
+
+use crate::to_geo::item_to_geometry;
+
+/// ST_DWithin() implementation using [Euclidean] [Distance]
+pub fn st_dwithin_impl() -> ScalarKernelRef {
+    Arc::new(STDWithin {})
+}
+
+#[derive(Debug)]
+struct STDWithin {}
+
+impl SedonaScalarKernel for STDWithin {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![
+                ArgMatcher::is_geometry(),
+                ArgMatcher::is_geometry(),
+                ArgMatcher::is_numeric(),
+            ],
+            SedonaType::Arrow(DataType::Boolean),
+        );
+
+        matcher.match_args(args)
+    }
+
+    fn invoke_batch(
+        &self,
+        arg_types: &[SedonaType],
+        args: &[ColumnarValue],
+    ) -> Result<ColumnarValue> {
+        // Extract the constant scalar value before looping over the input 
geometries
+        let distance: Option<f64>;
+        let arg2 = args[2].cast_to(&DataType::Float64, None)?;
+        if let ColumnarValue::Scalar(scalar_arg) = &arg2 {
+            if scalar_arg.is_null() {
+                distance = None;
+            } else {
+                distance = Some(f64::try_from(scalar_arg.clone())?);
+            }
+        } else {
+            return Err(DataFusionError::Execution(format!(
+                "Invalid distance: {:?}",
+                args[2]
+            )));
+        }

Review Comment:
   This will error when `distance` isn't a constant? I think you could probably 
handle this case with:
   
   ```rust
   let arg2_array = arg2.to_array(executor.num_iterations())?;
   let arg2_f64_array = as_float64_array(&arg2_array)?;
   let arg2_iter = arg2_f64_array.iter();
   
   // In the loop
   let distance = arg2_iter.next().unwrap()
   ```
   
   That is possibly slower for the scalar case and could be kept separate if it 
ends up mattering.



-- 
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]

Reply via email to