|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +use std::sync::Arc; |
| 18 | + |
| 19 | +use arrow_array::builder::BinaryBuilder; |
| 20 | +use datafusion_common::{error::Result, DataFusionError}; |
| 21 | +use datafusion_expr::ColumnarValue; |
| 22 | +use sedona_expr::{ |
| 23 | + item_crs::ItemCrsKernel, |
| 24 | + scalar_udf::{ScalarKernelRef, SedonaScalarKernel}, |
| 25 | +}; |
| 26 | +use sedona_geometry::wkb_factory::WKB_MIN_PROBABLE_BYTES; |
| 27 | +use sedona_schema::{ |
| 28 | + datatypes::{SedonaType, WKB_GEOMETRY}, |
| 29 | + matchers::ArgMatcher, |
| 30 | +}; |
| 31 | + |
| 32 | +use crate::executor::GeosExecutor; |
| 33 | +use crate::geos_to_wkb::write_geos_geometry; |
| 34 | + |
| 35 | +/// ST_Normalize() implementation using the geos crate |
| 36 | +pub fn st_normalize_impl() -> Vec<ScalarKernelRef> { |
| 37 | + ItemCrsKernel::wrap_impl(STNormalize {}) |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Debug)] |
| 41 | +struct STNormalize {} |
| 42 | + |
| 43 | +impl SedonaScalarKernel for STNormalize { |
| 44 | + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { |
| 45 | + let matcher = ArgMatcher::new(vec![ArgMatcher::is_geometry()], WKB_GEOMETRY); |
| 46 | + |
| 47 | + matcher.match_args(args) |
| 48 | + } |
| 49 | + |
| 50 | + fn invoke_batch( |
| 51 | + &self, |
| 52 | + arg_types: &[SedonaType], |
| 53 | + args: &[ColumnarValue], |
| 54 | + ) -> Result<ColumnarValue> { |
| 55 | + let executor = GeosExecutor::new(arg_types, args); |
| 56 | + let mut builder = BinaryBuilder::with_capacity( |
| 57 | + executor.num_iterations(), |
| 58 | + WKB_MIN_PROBABLE_BYTES * executor.num_iterations(), |
| 59 | + ); |
| 60 | + executor.execute_wkb_void(|maybe_wkb| { |
| 61 | + match maybe_wkb { |
| 62 | + Some(wkb) => { |
| 63 | + invoke_scalar(&wkb, &mut builder)?; |
| 64 | + builder.append_value([]); |
| 65 | + } |
| 66 | + _ => builder.append_null(), |
| 67 | + } |
| 68 | + |
| 69 | + Ok(()) |
| 70 | + })?; |
| 71 | + |
| 72 | + executor.finish(Arc::new(builder.finish())) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn invoke_scalar(geos_geom: &geos::Geometry, writer: &mut impl std::io::Write) -> Result<()> { |
| 77 | + let mut geometry = Clone::clone(geos_geom); |
| 78 | + geometry |
| 79 | + .normalize() |
| 80 | + .map_err(|e| DataFusionError::Execution(format!("Failed to normalize geometry: {e}")))?; |
| 81 | + |
| 82 | + write_geos_geometry(&geometry, writer)?; |
| 83 | + Ok(()) |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use datafusion_common::ScalarValue; |
| 89 | + use geos::{Geom, Geometry}; |
| 90 | + use rstest::rstest; |
| 91 | + use sedona_expr::scalar_udf::SedonaScalarUDF; |
| 92 | + use sedona_schema::datatypes::{WKB_GEOMETRY, WKB_GEOMETRY_ITEM_CRS, WKB_VIEW_GEOMETRY}; |
| 93 | + use sedona_testing::testers::ScalarUdfTester; |
| 94 | + |
| 95 | + use super::*; |
| 96 | + |
| 97 | + #[rstest] |
| 98 | + fn udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) { |
| 99 | + let udf = SedonaScalarUDF::from_impl("st_normalize", st_normalize_impl()); |
| 100 | + let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone()]); |
| 101 | + |
| 102 | + tester.assert_return_type(WKB_GEOMETRY); |
| 103 | + |
| 104 | + let input_wkt = "POLYGON((1 1, 1 0, 0 0, 0 1, 1 1))"; |
| 105 | + let expected_wkt = "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))"; |
| 106 | + |
| 107 | + let result = tester.invoke_scalar(input_wkt).unwrap(); |
| 108 | + tester.assert_scalar_result_equals(result, expected_wkt); |
| 109 | + |
| 110 | + let result = tester |
| 111 | + .invoke_scalar("MULTILINESTRING ((2 2, 1 1), (4 4, 3 3))") |
| 112 | + .unwrap(); |
| 113 | + tester.assert_scalar_result_equals(result, "MULTILINESTRING ((3 3, 4 4), (1 1, 2 2))"); |
| 114 | + |
| 115 | + let result = tester.invoke_scalar(ScalarValue::Null).unwrap(); |
| 116 | + assert!(result.is_null()); |
| 117 | + |
| 118 | + let batch_input = vec![ |
| 119 | + Some("POINT(2 1)"), |
| 120 | + None, |
| 121 | + Some("POLYGON((1 1, 1 0, 0 0, 0 1, 1 1))"), |
| 122 | + Some("MULTILINESTRING ((2 2, 1 1), (4 4, 3 3))"), |
| 123 | + ]; |
| 124 | + |
| 125 | + let batch_result = tester.invoke_wkb_array(batch_input).unwrap(); |
| 126 | + assert_eq!(batch_result.len(), 4); |
| 127 | + |
| 128 | + assert!(batch_result.is_null(1)); |
| 129 | + |
| 130 | + let expected = sedona_testing::create::create_array( |
| 131 | + &[ |
| 132 | + Some("POINT (2 1)"), |
| 133 | + None, |
| 134 | + Some("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))"), |
| 135 | + Some("MULTILINESTRING ((3 3, 4 4), (1 1, 2 2))"), |
| 136 | + ], |
| 137 | + &WKB_GEOMETRY, |
| 138 | + ); |
| 139 | + sedona_testing::compare::assert_array_equal(&batch_result, &expected); |
| 140 | + } |
| 141 | + |
| 142 | + #[rstest] |
| 143 | + fn udf_invoke_item_crs(#[values(WKB_GEOMETRY_ITEM_CRS.clone())] sedona_type: SedonaType) { |
| 144 | + let udf = SedonaScalarUDF::from_impl("st_normalize", st_normalize_impl()); |
| 145 | + let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type.clone()]); |
| 146 | + |
| 147 | + tester.assert_return_type(sedona_type); |
| 148 | + |
| 149 | + let result = tester |
| 150 | + .invoke_scalar("POLYGON((1 1, 1 0, 0 0, 0 1, 1 1))") |
| 151 | + .unwrap(); |
| 152 | + tester.assert_scalar_result_equals(result, "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))"); |
| 153 | + } |
| 154 | + |
| 155 | + #[rstest] |
| 156 | + fn udf_already_normalized(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) { |
| 157 | + let udf = SedonaScalarUDF::from_impl("st_normalize", st_normalize_impl()); |
| 158 | + let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type]); |
| 159 | + let already_normal = "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))"; |
| 160 | + let result = tester.invoke_scalar(already_normal).unwrap(); |
| 161 | + tester.assert_scalar_result_equals(result, already_normal); |
| 162 | + } |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn invoke_scalar_normalizes_via_geos_without_mutating_input() { |
| 166 | + let geom = Geometry::new_from_wkt("POLYGON((1 1, 1 0, 0 0, 0 1, 1 1))").unwrap(); |
| 167 | + let original_wkt = geom.to_wkt().unwrap(); |
| 168 | + |
| 169 | + let mut normalized_wkb = Vec::new(); |
| 170 | + invoke_scalar(&geom, &mut normalized_wkb).unwrap(); |
| 171 | + |
| 172 | + let normalized = Geometry::new_from_wkb(&normalized_wkb).unwrap(); |
| 173 | + assert_eq!( |
| 174 | + normalized.to_wkt().unwrap(), |
| 175 | + "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))" |
| 176 | + ); |
| 177 | + |
| 178 | + assert_eq!(geom.to_wkt().unwrap(), original_wkt); |
| 179 | + } |
| 180 | +} |
0 commit comments