-
Notifications
You must be signed in to change notification settings - Fork 36
feat(sql): Implement ST_Azimuth()
#183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a8dd476
Implement ST_Azimuth()
yutannihilation 515ac93
Merge branch 'main' into feat/st-azimuth
yutannihilation 351885d
Tweak
yutannihilation 833ad30
Update rust/sedona-functions/src/st_azimuth.rs
yutannihilation aecaf6c
Move implementation to sedona-functions
yutannihilation cf305bb
Add benchmark
yutannihilation 3de7f97
Return NULL for the same points case
yutannihilation a6e05d6
Add tests
yutannihilation 055dda5
Add Python tests for ST_Azimuth
yutannihilation a85e2c6
Update rust/sedona-functions/src/st_azimuth.rs
yutannihilation 330f3b2
Merge branch 'feat/st-azimuth' of https://github.com/yutannihilation/…
yutannihilation 8fbff66
Add imports
yutannihilation 9904cbe
Reject empty points
yutannihilation 95bfd01
Use degrees() in document
yutannihilation File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| // 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, exec_err}; | ||
| 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 degrees(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, raise an error. | ||
| _ => { | ||
| exec_err!("ST_Azimuth expects both arguments to be non-empty POINT geometries") | ||
| } | ||
| } | ||
| } | ||
| _ => exec_err!("ST_Azimuth expects both arguments to be non-empty POINT geometries"), | ||
| } | ||
| } | ||
|
|
||
| fn calc_azimuth(start_x: f64, start_y: f64, end_x: f64, end_y: f64) -> Option<f64> { | ||
| let dx = end_x - start_x; | ||
| let dy = end_y - start_y; | ||
|
|
||
| if dx == 0.0 && dy == 0.0 { | ||
| return None; | ||
| } | ||
|
|
||
| let mut angle = dx.atan2(dy); | ||
| if angle < 0.0 { | ||
| angle += 2.0 * std::f64::consts::PI; | ||
| } | ||
|
|
||
| Some(angle) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use datafusion_common::scalar::ScalarValue; | ||
| use datafusion_expr::ScalarUDF; | ||
| use rstest::rstest; | ||
| use sedona_schema::datatypes::{WKB_GEOMETRY, WKB_VIEW_GEOMETRY}; | ||
| use sedona_testing::create::create_scalar; | ||
| use sedona_testing::testers::ScalarUdfTester; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn udf_metadata() { | ||
| let udf: ScalarUDF = st_azimuth_udf().into(); | ||
| assert_eq!(udf.name(), "st_azimuth"); | ||
| assert!(udf.documentation().is_some()); | ||
| } | ||
|
|
||
| #[rstest] | ||
| fn udf( | ||
| #[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] start_type: SedonaType, | ||
| #[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] end_type: SedonaType, | ||
| ) { | ||
| let tester = ScalarUdfTester::new( | ||
| st_azimuth_udf().into(), | ||
| vec![start_type.clone(), end_type.clone()], | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| tester.return_type().unwrap(), | ||
| SedonaType::Arrow(DataType::Float64) | ||
| ); | ||
|
|
||
| let start = create_scalar(Some("POINT (0 0)"), &start_type); | ||
| let north = create_scalar(Some("POINT (0 1)"), &end_type); | ||
| let east = create_scalar(Some("POINT (1 0)"), &end_type); | ||
| let south = create_scalar(Some("POINT (0 -1)"), &end_type); | ||
| let west = create_scalar(Some("POINT (-1 0)"), &end_type); | ||
| let same = create_scalar(Some("POINT (0 0)"), &end_type); | ||
| let empty = create_scalar(Some("POINT EMPTY"), &end_type); | ||
|
|
||
| let result = tester | ||
| .invoke_scalar_scalar(start.clone(), north.clone()) | ||
| .unwrap(); | ||
| assert!(matches!( | ||
| result, | ||
| ScalarValue::Float64(Some(val)) if (val - 0.0).abs() < 1e-12 | ||
| )); | ||
|
|
||
| let result = tester | ||
| .invoke_scalar_scalar(start.clone(), east.clone()) | ||
| .unwrap(); | ||
| assert!(matches!( | ||
| result, | ||
| ScalarValue::Float64(Some(val)) if (val - std::f64::consts::FRAC_PI_2).abs() < 1e-12 | ||
| )); | ||
|
|
||
| let result = tester | ||
| .invoke_scalar_scalar(start.clone(), south.clone()) | ||
| .unwrap(); | ||
| assert!(matches!( | ||
| result, | ||
| ScalarValue::Float64(Some(val)) if (val - std::f64::consts::PI).abs() < 1e-12 | ||
| )); | ||
|
|
||
| let result = tester | ||
| .invoke_scalar_scalar(start.clone(), west.clone()) | ||
| .unwrap(); | ||
| assert!(matches!( | ||
| result, | ||
| ScalarValue::Float64(Some(val)) if (val - (3.0 * std::f64::consts::FRAC_PI_2)).abs() < 1e-12 | ||
| )); | ||
|
|
||
| // If two points are the same, return NULL | ||
| let result = tester | ||
| .invoke_scalar_scalar(start.clone(), same.clone()) | ||
| .unwrap(); | ||
| assert!(result.is_null()); | ||
|
|
||
| // If either one of the points is empty, return NULL | ||
| let result = tester.invoke_scalar_scalar(start.clone(), empty.clone()); | ||
| assert!( | ||
| result.is_err() | ||
| && result | ||
| .unwrap_err() | ||
| .to_string() | ||
| .contains("ST_Azimuth expects both arguments to be non-empty POINT geometries") | ||
| ); | ||
|
|
||
| // If either one of the points is NULL, return NULL | ||
| let result = tester | ||
| .invoke_scalar_scalar(ScalarValue::Null, north.clone()) | ||
| .unwrap(); | ||
| assert!(result.is_null()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this! We have a few cases where we're not sure exactly what this kind of thing should do (luckily people typing SQL NULLs for this kind of thing is pretty rare 🙂 )