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


##########
python/sedonadb/tests/functions/test_functions.py:
##########
@@ -262,7 +262,30 @@ def test_st_buffer(eng, geom, dist, expected_area):
     eng.assert_query_result(
         f"SELECT ST_Area(ST_Buffer({geom_or_null(geom)}, 
{val_or_null(dist)}))",
         expected_area,
-        numeric_epsilon=1e-9,
+        # geos passes with 1e-9, but geo needs it as high as 1e-3
+        numeric_epsilon=1e-3,
+    )
+
+
[email protected]("eng", [SedonaDB, PostGIS])
[email protected](
+    ("geom", "dist", "expected"),
+    [
+        ("POINT EMPTY", 2.0, "POLYGON EMPTY"),

Review Comment:
   Given the distance doesn't matter here it might be easier for future us to 
mentally parse if the parameters were just `geom` and `expected` (or even just 
`geom` since the result is identical)



##########
rust/sedona-geo/src/st_buffer.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 std::sync::Arc;
+
+use arrow_array::builder::BinaryBuilder;
+use arrow_schema::DataType;
+use datafusion_common::{error::Result, exec_err, DataFusionError};
+use datafusion_expr::ColumnarValue;
+use geo::algorithm::buffer::{Buffer, BufferStyle};
+use geo_types::Polygon;
+use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
+use sedona_functions::executor::WkbExecutor;
+use sedona_geometry::is_empty::is_geometry_empty;
+use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES;
+use sedona_schema::{
+    datatypes::{SedonaType, WKB_GEOMETRY},
+    matchers::ArgMatcher,
+};
+use wkb::{
+    reader::Wkb,
+    writer::{write_geometry, WriteOptions},
+    Endianness,
+};
+
+use crate::to_geo::item_to_geometry;
+
+/// ST_Buffer() implementation using buffer calculation
+pub fn st_buffer_impl() -> ScalarKernelRef {
+    Arc::new(STBuffer {})
+}
+
+#[derive(Debug)]
+struct STBuffer {}
+
+impl SedonaScalarKernel for STBuffer {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![ArgMatcher::is_geometry(), ArgMatcher::is_numeric()],
+            WKB_GEOMETRY,
+        );
+
+        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 params: Option<BufferStyle<f64>>;
+        let arg1 = args[1].cast_to(&DataType::Float64, None)?;
+        if let ColumnarValue::Scalar(scalar_arg) = &arg1 {
+            if scalar_arg.is_null() {
+                params = None;
+            } else {
+                let distance = f64::try_from(scalar_arg.clone())?;
+                params = Some(BufferStyle::new(distance));
+            }
+        } else {
+            return exec_err!("Invalid distance: {:?}", args[1]);
+        }
+
+        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_wkb| {
+            match (maybe_wkb, params.clone()) {
+                (Some(wkb), Some(params)) => {
+                    invoke_scalar(&wkb, params, &mut builder)?;
+                    builder.append_value([]);
+                }
+                _ => builder.append_null(),
+            }
+
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn invoke_scalar(
+    wkb: &Wkb,
+    params: BufferStyle<f64>,
+    writer: &mut impl std::io::Write,
+) -> Result<()> {
+    // PostGIS returns POLYGON EMPTY for all empty geometries
+    let is_empty = is_geometry_empty(wkb).map_err(|e| 
DataFusionError::External(Box::new(e)))?;
+    if is_empty {

Review Comment:
   > Wondering if there's a better way we can handle empty points in 
item_geometry()
   
   We could have it return something like `enum ItemToGeometryResult { 
Unsupported(Wkb), Supported(Geometry))`? (No need to do that here unless you're 
excited about it).
   
   I suppose it might be more complicated because each algorithm might have 
different considerations.



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