|
| 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 | + |
| 18 | +use std::sync::Arc; |
| 19 | + |
| 20 | +use arrow_array::builder::BooleanBuilder; |
| 21 | +use arrow_schema::DataType; |
| 22 | +use datafusion_common::{error::Result, DataFusionError}; |
| 23 | +use datafusion_expr::ColumnarValue; |
| 24 | +use geos::{Geom, GeometryTypes}; |
| 25 | +use sedona_expr::scalar_udf::{ScalarKernelRef, SedonaScalarKernel}; |
| 26 | +use sedona_schema::{datatypes::SedonaType, matchers::ArgMatcher}; |
| 27 | + |
| 28 | +use crate::executor::GeosExecutor; |
| 29 | + |
| 30 | +/// ST_IsRing() implementation using the geos crate |
| 31 | +pub fn st_is_ring_impl() -> ScalarKernelRef { |
| 32 | + Arc::new(STIsRing {}) |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug)] |
| 36 | +struct STIsRing {} |
| 37 | + |
| 38 | +impl SedonaScalarKernel for STIsRing { |
| 39 | + fn return_type(&self, args: &[SedonaType]) -> Result<Option<SedonaType>> { |
| 40 | + let matcher = ArgMatcher::new( |
| 41 | + vec![ArgMatcher::is_geometry()], |
| 42 | + SedonaType::Arrow(DataType::Boolean), |
| 43 | + ); |
| 44 | + |
| 45 | + matcher.match_args(args) |
| 46 | + } |
| 47 | + |
| 48 | + fn invoke_batch( |
| 49 | + &self, |
| 50 | + arg_types: &[SedonaType], |
| 51 | + args: &[ColumnarValue], |
| 52 | + ) -> Result<ColumnarValue> { |
| 53 | + let executor = GeosExecutor::new(arg_types, args); |
| 54 | + let mut builder = BooleanBuilder::with_capacity(executor.num_iterations()); |
| 55 | + |
| 56 | + executor.execute_wkb_void(|maybe_wkb| { |
| 57 | + match maybe_wkb { |
| 58 | + Some(wkb) => { |
| 59 | + builder.append_value(invoke_scalar(&wkb)?); |
| 60 | + } |
| 61 | + _ => builder.append_null(), |
| 62 | + } |
| 63 | + Ok(()) |
| 64 | + })?; |
| 65 | + |
| 66 | + executor.finish(Arc::new(builder.finish())) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fn invoke_scalar(geos_geom: &geos::Geometry) -> Result<bool> { |
| 71 | + // Check if geometry is empty - (PostGIS compatibility) |
| 72 | + let is_empty = geos_geom.is_empty().map_err(|e| { |
| 73 | + DataFusionError::Execution(format!("Failed to check if geometry is a ring: {e}")) |
| 74 | + })?; |
| 75 | + |
| 76 | + if is_empty { |
| 77 | + return Ok(false); |
| 78 | + } |
| 79 | + |
| 80 | + // Check if geometry is a LineString - (PostGIS compatibility) |
| 81 | + if geos_geom.geometry_type() != GeometryTypes::LineString { |
| 82 | + return Err(DataFusionError::Execution( |
| 83 | + "ST_IsRing() should only be called on a linear feature".to_string(), |
| 84 | + )); |
| 85 | + } |
| 86 | + |
| 87 | + geos_geom.is_ring().map_err(|e| { |
| 88 | + DataFusionError::Execution(format!("Failed to check if geometry is a ring: {e}")) |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use arrow_array::{create_array as arrow_array, ArrayRef}; |
| 95 | + use rstest::rstest; |
| 96 | + use sedona_expr::scalar_udf::SedonaScalarUDF; |
| 97 | + use sedona_schema::datatypes::{WKB_GEOMETRY, WKB_VIEW_GEOMETRY}; |
| 98 | + use sedona_testing::compare::assert_array_equal; |
| 99 | + use sedona_testing::testers::ScalarUdfTester; |
| 100 | + |
| 101 | + use super::*; |
| 102 | + |
| 103 | + #[rstest] |
| 104 | + fn udf(#[values(WKB_GEOMETRY, WKB_VIEW_GEOMETRY)] sedona_type: SedonaType) { |
| 105 | + let udf = SedonaScalarUDF::from_kernel("st_isring", st_is_ring_impl()); |
| 106 | + let tester = ScalarUdfTester::new(udf.into(), vec![sedona_type]); |
| 107 | + tester.assert_return_type(DataType::Boolean); |
| 108 | + |
| 109 | + // Valid ring (closed + simple) - square |
| 110 | + let result = tester |
| 111 | + .invoke_scalar("LINESTRING(0 0, 0 1, 1 1, 1 0, 0 0)") |
| 112 | + .unwrap(); |
| 113 | + tester.assert_scalar_result_equals(result, true); |
| 114 | + |
| 115 | + // Valid ring (closed + simple) - triangle |
| 116 | + let result = tester |
| 117 | + .invoke_scalar("LINESTRING(0 0, 1 0, 1 1, 0 0)") |
| 118 | + .unwrap(); |
| 119 | + tester.assert_scalar_result_equals(result, true); |
| 120 | + |
| 121 | + // Non-LineString types should throw errors (PostGIS compatibility) |
| 122 | + |
| 123 | + // Point (not a linestring) - should error |
| 124 | + let result = tester.invoke_scalar("POINT(21 52)"); |
| 125 | + assert!(result.is_err()); |
| 126 | + assert!(result |
| 127 | + .unwrap_err() |
| 128 | + .to_string() |
| 129 | + .contains("should only be called on a linear feature")); |
| 130 | + |
| 131 | + // Polygon (not a linestring) - should error |
| 132 | + let result = tester.invoke_scalar("POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))"); |
| 133 | + assert!(result.is_err()); |
| 134 | + assert!(result |
| 135 | + .unwrap_err() |
| 136 | + .to_string() |
| 137 | + .contains("should only be called on a linear feature")); |
| 138 | + |
| 139 | + // MultiLineString (collection) - should error |
| 140 | + let result = tester.invoke_scalar("MULTILINESTRING((0 0, 0 1, 1 1, 1 0, 0 0))"); |
| 141 | + assert!(result.is_err()); |
| 142 | + assert!(result |
| 143 | + .unwrap_err() |
| 144 | + .to_string() |
| 145 | + .contains("should only be called on a linear feature")); |
| 146 | + |
| 147 | + // GeometryCollection - should error |
| 148 | + let result = |
| 149 | + tester.invoke_scalar("GEOMETRYCOLLECTION(LINESTRING(0 0, 0 1, 1 1, 1 0, 0 0))"); |
| 150 | + assert!(result.is_err()); |
| 151 | + assert!(result |
| 152 | + .unwrap_err() |
| 153 | + .to_string() |
| 154 | + .contains("should only be called on a linear feature")); |
| 155 | + |
| 156 | + let input_wkt = vec![ |
| 157 | + Some("LINESTRING(0 0, 0 1, 1 1, 1 0, 0 0)"), // Valid ring => true |
| 158 | + Some("LINESTRING(0 0, 0 1, 1 0, 1 1, 0 0)"), // Self-intersecting => false |
| 159 | + Some("LINESTRING(0 0, 2 2)"), // Not closed => false |
| 160 | + Some("LINESTRING EMPTY"), // Empty => false |
| 161 | + Some("POINT EMPTY"), // Empty => false |
| 162 | + None, // NULL => null |
| 163 | + ]; |
| 164 | + |
| 165 | + let expected: ArrayRef = arrow_array!( |
| 166 | + Boolean, |
| 167 | + [ |
| 168 | + Some(true), |
| 169 | + Some(false), |
| 170 | + Some(false), |
| 171 | + Some(false), |
| 172 | + Some(false), |
| 173 | + None |
| 174 | + ] |
| 175 | + ); |
| 176 | + |
| 177 | + assert_array_equal(&tester.invoke_wkb_array(input_wkt).unwrap(), &expected); |
| 178 | + } |
| 179 | +} |
0 commit comments