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


##########
rust/sedona-geo/src/st_azimuth.rs:
##########
@@ -0,0 +1,174 @@
+// 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::Float64Builder;
+use arrow_schema::DataType;
+use datafusion_common::error::Result;
+use datafusion_expr::ColumnarValue;
+use geo_traits::{CoordTrait, GeometryTrait, GeometryType, PointTrait};
+use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel};
+use sedona_functions::executor::WkbExecutor;
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+use wkb::reader::Wkb;
+
+/// ST_Azimuth() implementation
+pub fn st_azimuth_impl() -> ScalarKernelRef {
+    Arc::new(STAzimuth {})
+}
+
+#[derive(Debug)]
+struct STAzimuth {}
+
+impl SedonaScalarKernel for STAzimuth {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![ArgMatcher::is_geometry(), ArgMatcher::is_geometry()],
+            SedonaType::Arrow(DataType::Float64),
+        );
+
+        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 = 
Float64Builder::with_capacity(executor.num_iterations());
+        executor.execute_wkb_wkb_void(|maybe_start, maybe_end| {
+            match (maybe_start, maybe_end) {
+                (Some(start), Some(end)) => match invoke_scalar(start, end)? {
+                    Some(angle) => builder.append_value(angle),
+                    None => builder.append_null(),
+                },
+                _ => builder.append_null(),
+            }
+
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn invoke_scalar(start: &Wkb, end: &Wkb) -> Result<Option<f64>> {
+    match (start.as_type(), end.as_type()) {
+        (GeometryType::Point(start_point), GeometryType::Point(end_point)) => {
+            match (start_point.coord(), end_point.coord()) {
+                // If both geometries are non-empty points, calculate the angle
+                (Some(start_coord), Some(end_coord)) => Ok(calc_azimuth(
+                    start_coord.x(),
+                    start_coord.y(),
+                    end_coord.x(),
+                    end_coord.y(),
+                )),
+                // If either of the points is empty, the result is NULL
+                _ => Ok(None),
+            }
+        }
+        _ => Err(datafusion_common::error::DataFusionError::Execution(
+            "ST_Azimuth expects both arguments to be POINT geometries".into(),
+        )),
+    }
+}
+
+// Note: When the two points are completely coincident, PostGIS's ST_Azimuth()
+//       returns NULL. However, this returns 0.0.

Review Comment:
   To date, SedonaDB tests against PostGIS for feature parity and we file bugs 
with Sedona when we notice something is inconsistent.



##########
rust/sedona-functions/src/st_azimuth.rs:
##########
@@ -0,0 +1,221 @@
+// 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::Float64Builder;
+use arrow_schema::DataType;
+use datafusion_common::error::Result;
+use datafusion_expr::{
+    scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, 
Volatility,
+};
+use geo_traits::{CoordTrait, GeometryTrait, GeometryType, PointTrait};
+use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+use std::sync::Arc;
+use wkb::reader::Wkb;
+
+use crate::executor::WkbExecutor;
+
+/// ST_Azimuth() scalar UDF
+///
+/// Stub function for azimuth calculation between two points.
+pub fn st_azimuth_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "st_azimuth",
+        vec![Arc::new(STAzimuth {})],
+        Volatility::Immutable,
+        Some(st_azimuth_doc()),
+    )
+}
+
+fn st_azimuth_doc() -> Documentation {
+    Documentation::builder(
+        DOC_SECTION_OTHER,
+        "Returns the azimuth (a clockwise angle measured from north) in 
radians from geomA to geomB",
+        "ST_Azimuth (A: Geometry, B: Geometry)",
+    )
+    .with_argument("geomA", "geometry: Start point geometry")
+    .with_argument("geomB", "geometry: End point geometry")
+    .with_sql_example(
+        "SELECT ST_Azimuth(ST_Point(0, 0), ST_Point(1, 1))",
+    )
+    .build()
+}
+
+#[derive(Debug)]
+struct STAzimuth {}
+
+impl SedonaScalarKernel for STAzimuth {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![ArgMatcher::is_geometry(), ArgMatcher::is_geometry()],
+            SedonaType::Arrow(DataType::Float64),
+        );
+
+        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 = 
Float64Builder::with_capacity(executor.num_iterations());
+        executor.execute_wkb_wkb_void(|maybe_start, maybe_end| {
+            match (maybe_start, maybe_end) {
+                (Some(start), Some(end)) => match invoke_scalar(start, end)? {
+                    Some(angle) => builder.append_value(angle),
+                    None => builder.append_null(),
+                },
+                _ => builder.append_null(),
+            }
+
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn invoke_scalar(start: &Wkb, end: &Wkb) -> Result<Option<f64>> {
+    match (start.as_type(), end.as_type()) {
+        (GeometryType::Point(start_point), GeometryType::Point(end_point)) => {
+            match (start_point.coord(), end_point.coord()) {
+                // If both geometries are non-empty points, calculate the angle
+                (Some(start_coord), Some(end_coord)) => Ok(calc_azimuth(
+                    start_coord.x(),
+                    start_coord.y(),
+                    end_coord.x(),
+                    end_coord.y(),
+                )),
+                // If either of the points is empty, the result is NULL
+                _ => Ok(None),

Review Comment:
   Just checking: does PostGIS allow a MULITPOINT with a single child here?



##########
rust/sedona-functions/src/st_azimuth.rs:
##########
@@ -0,0 +1,221 @@
+// 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::Float64Builder;
+use arrow_schema::DataType;
+use datafusion_common::error::Result;
+use datafusion_expr::{
+    scalar_doc_sections::DOC_SECTION_OTHER, ColumnarValue, Documentation, 
Volatility,
+};
+use geo_traits::{CoordTrait, GeometryTrait, GeometryType, PointTrait};
+use sedona_expr::scalar_udf::{SedonaScalarKernel, SedonaScalarUDF};
+use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher};
+use std::sync::Arc;
+use wkb::reader::Wkb;
+
+use crate::executor::WkbExecutor;
+
+/// ST_Azimuth() scalar UDF
+///
+/// Stub function for azimuth calculation between two points.
+pub fn st_azimuth_udf() -> SedonaScalarUDF {
+    SedonaScalarUDF::new(
+        "st_azimuth",
+        vec![Arc::new(STAzimuth {})],
+        Volatility::Immutable,
+        Some(st_azimuth_doc()),
+    )
+}
+
+fn st_azimuth_doc() -> Documentation {
+    Documentation::builder(
+        DOC_SECTION_OTHER,
+        "Returns the azimuth (a clockwise angle measured from north) in 
radians from geomA to geomB",
+        "ST_Azimuth (A: Geometry, B: Geometry)",
+    )
+    .with_argument("geomA", "geometry: Start point geometry")
+    .with_argument("geomB", "geometry: End point geometry")
+    .with_sql_example(
+        "SELECT ST_Azimuth(ST_Point(0, 0), ST_Point(1, 1))",
+    )
+    .build()
+}
+
+#[derive(Debug)]
+struct STAzimuth {}
+
+impl SedonaScalarKernel for STAzimuth {
+    fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> {
+        let matcher = ArgMatcher::new(
+            vec![ArgMatcher::is_geometry(), ArgMatcher::is_geometry()],
+            SedonaType::Arrow(DataType::Float64),
+        );
+
+        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 = 
Float64Builder::with_capacity(executor.num_iterations());
+        executor.execute_wkb_wkb_void(|maybe_start, maybe_end| {
+            match (maybe_start, maybe_end) {
+                (Some(start), Some(end)) => match invoke_scalar(start, end)? {
+                    Some(angle) => builder.append_value(angle),
+                    None => builder.append_null(),
+                },
+                _ => builder.append_null(),
+            }
+
+            Ok(())
+        })?;
+
+        executor.finish(Arc::new(builder.finish()))
+    }
+}
+
+fn invoke_scalar(start: &Wkb, end: &Wkb) -> Result<Option<f64>> {
+    match (start.as_type(), end.as_type()) {
+        (GeometryType::Point(start_point), GeometryType::Point(end_point)) => {
+            match (start_point.coord(), end_point.coord()) {
+                // If both geometries are non-empty points, calculate the angle
+                (Some(start_coord), Some(end_coord)) => Ok(calc_azimuth(
+                    start_coord.x(),
+                    start_coord.y(),
+                    end_coord.x(),
+                    end_coord.y(),
+                )),
+                // If either of the points is empty, the result is NULL
+                _ => Ok(None),
+            }
+        }
+        _ => Err(datafusion_common::error::DataFusionError::Execution(
+            "ST_Azimuth expects both arguments to be POINT geometries".into(),
+        )),

Review Comment:
   DataFusion has a helper for this one!
   
   ```suggestion
           _ => exec_err!("ST_Azimuth expects both arguments to be POINT 
geometries"),
   ```



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